Skip to content

Commit

Permalink
Implement sanity check with pre-push hook
Browse files Browse the repository at this point in the history
All participants were asked to fork from this repository, but some of
them might create their repositories without forking, that caused
problems for public reviewing. Thus, this patch introduced a simple way
to validate current workspace by probing the existence of specific
"magic" commit.

In addition, when someone attempts to push master branch to remote, this
hook would ensure that current workspace is buildable.
  • Loading branch information
jserv committed Jan 24, 2020
1 parent a3e0eae commit 8172192
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions scripts/install-git-hooks
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ chmod +x .git/hooks/pre-commit
ln -sf ../../scripts/commit-msg.hook .git/hooks/commit-msg || exit 1
chmod +x .git/hooks/commit-msg

ln -sf ../../scripts/pre-push.hook .git/hooks/pre-push || exit 1
chmod +x .git/hooks/pre-push

touch .git/hooks/applied || exit 1

echo
Expand Down
51 changes: 51 additions & 0 deletions scripts/pre-push.hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
RED='\033[0;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Validate repository
# commit d9805e67e74ea3a05956d92447bbe67214154c06
# Author: Jim Huang <[email protected]>
# Date: Wed Jan 22 14:13:06 2020 +0000
#
# Bump copyright year
commit=`git rev-list -n 1 --grep '^Bump copyright' 0f146d62ecba1f0c098165273204d35cfa9d07ec...HEAD`
if [ x"$commit" != x"d9805e67e74ea3a05956d92447bbe67214154c06" ] ; then
echo -e "${RED}ERROR${NC}: This repository is insane."
echo -e "Make sure you did fork from https://github.com/sysprog21/lab0-c recently."
echo ""
exit 1
fi

# Show hints
echo -e "${YELLOW}Hint${NC}: You might want to know why is Git is always ${GREEN}asking for my password${NC}."
echo -e " https://help.github.com/en/github/using-git/why-is-git-always-asking-for-my-password"
echo ""

# only run this if you are pushing to master
if [[ $current_branch = $protected_branch ]] ; then
echo -e "${YELLOW}Running pre push to master check...${NC}"

echo -e "${YELLOW}Trying to build tests project...${NC}"

# build the project
make

# $? is a shell variable which stores the return code from what we just ran
rc=$?
if [[ $rc != 0 ]] ; then
echo -e "${RED}Failed to build the project, please fix this and push again${NC}"
echo ""
exit $rc
fi

# Everything went OK so we can exit with a zero
echo -e "${GREEN}Pre push check passed!${NC}"
echo ""
fi

exit 0

0 comments on commit 8172192

Please sign in to comment.