-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-hook-workingcopychange
executable file
·76 lines (62 loc) · 2.27 KB
/
git-hook-workingcopychange
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env bash
#
# Ref:https://gist.github.com/jubianchi
# Symlink this file at: `.git/hooks/post-checkout` and make it executable.
# You can install it system wide too, see http://stackoverflow.com/a/2293578/685587
#---------------------------------------------------------------------
usage ()
{
cat <<EOT
${0##*/}
A git hook script intended to be used when your working copy
changes (checkout or merge). Checks for a composer.lock file.
If present, checks if the checkout includes changes to the
lock file. If changes are detected, executes
\`composer install\` to update dependencies to match the new
working copy.
Symlink this file as \`.git/hooks/post-checkout\` in your
project to have it auto-execute at the appropriate times. By
default, the script will only prompt you when the associated
commands should be executed.
Usage:
bin/${0##*/}
Environment Variables:
GIT_HOOK_POSTCHECKOUT_FORCE_EXECUTE
When set to a non-empty string, will cause the script to
auto-execute the associated commands instead of just
prompting about them.
EOT
exit ${1:-0} # Exit with code 0 unless an arg is passed to the method.
}
if [ "$1" = '-h' ]; then
usage
fi
# Bail out early if we're in the middle of a merge or rebase.
GIT_DIR=$(git rev-parse --git-dir)
GIT_DIR_MERGE="$GIT_DIR"/rebase-merge
GIT_DIR_APPLY="$GIT_DIR"/rebase-apply
if [[ (-d "$GIT_DIR_MERGE" && -f "$GIT_DIR_MERGE/interactive") || -d "$GIT_DIR_APPLY" ]]; then
exit 0
fi
# Add pairs of files to check for differences, and the commands to run
# if diffs are found. MUST be in the same order! (first file must
# pair with the first command!)
FILE_LIST=("composer.lock")
CMD_LIST=("composer install --dev --no-interaction --ignore-platform-reqs")
# Loop over the configured files.
for ((i=0; i < ${#FILE_LIST[@]}; ++i)); do
CHECK_FILE="${FILE_LIST[i]}"
CHECK_CMD="${CMD_LIST[i]}"
DIFF=$(git diff --stat HEAD@{1}..HEAD@{0} -- "$CHECK_FILE")
if [[ -f "$CHECK_FILE" && -n "$DIFF" ]]; then
if [ "${GIT_HOOK_POSTCHECKOUT_FORCE_EXECUTE:-""}" ]; then
echo "\`$CHECK_FILE\` has changed. Executing hook command."
$CHECK_CMD
else
echo "\`$CHECK_FILE\` has changed. You should execute the following command:"
echo ""
echo " $CHECK_CMD"
echo ""
fi
fi
done