-
Notifications
You must be signed in to change notification settings - Fork 3
/
pre-commit
executable file
·50 lines (42 loc) · 1.32 KB
/
pre-commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env bash
#
# Pre-commit hook.
#
# It then finds all scripts that are in tasks and will execute them
# prior to allowing the user to continue with a commit.
#
UPSTREAM=master
ROOT="$(pwd)"
# Ensure that the upstream is a valid branch.
git rev-parse --verify $UPSTREAM > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "[1]: Branch ($UPSTREAM) is not a valid upstream."
exit 0
fi
# Get the current branch.
CUR=`git rev-parse --abbrev-ref HEAD 2> /dev/null`
# Test to see if we're on the upstream branch.
if [ $UPSTREAM == $CUR ]; then
echo "Unable to run against the upstream branch, swap to a feature!"
exit 0 # exit with 0 allows git commit to continue.
fi
# Diff filter is used to show only changed and added files.
DIFF=`git diff --cached --name-only --diff-filter=ACM $UPSTREAM`
# Ensure that the DIFF is not larger than Bash max variable size.
if [ ${#DIFF} -ge `getconf ARG_MAX` ]; then
echo '[2]: Diff exceeds ARG_MAX.'
exit 0
fi
# Locate the tasks that we should run before a commit.
# ------------------------------------------------------
TASKS=`find $ROOT/.git/hooks/tasks -iname "*.sh"`
for task in $TASKS
do
sh $task $UPSTREAM $DIFF
# Capture the exit status of the command, the git commit will be aborted if
# we return a non-zero status.
STATUS=$?
if [ $STATUS -ne 0 ]; then
exit $STATUS
fi;
done