Skip to content

Commit 27c57ce

Browse files
committed
Merge pull request #41 from loadsys/f/git-checkout-hook
Adds a script to auto-run composer on git checkout.
2 parents 853e7a5 + 976e009 commit 27c57ce

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"find-symlinks-to-in",
4141
"folder-delete-items-older-than-days",
4242
"git-currentbranch",
43+
"git-hook-workingcopychange",
4344
"git-localchanges",
4445
"git-remotechanges",
4546
"git-submodules",

git-hook-workingcopychange

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Ref:https://gist.github.com/jubianchi
4+
# Symlink this file at: `.git/hooks/post-checkout` and make it executable.
5+
# You can install it system wide too, see http://stackoverflow.com/a/2293578/685587
6+
7+
#---------------------------------------------------------------------
8+
usage ()
9+
{
10+
cat <<EOT
11+
12+
${0##*/}
13+
A git hook script intended to be used when your working copy
14+
changes (checkout or merge). Checks for a composer.lock file.
15+
If present, checks if the checkout includes changes to the
16+
lock file. If changes are detected, executes
17+
\`composer install\` to update dependencies to match the new
18+
working copy.
19+
20+
Symlink this file as \`.git/hooks/post-checkout\` in your
21+
project to have it auto-execute at the appropriate times. By
22+
default, the script will only prompt you when the associated
23+
commands should be executed.
24+
25+
Usage:
26+
bin/${0##*/}
27+
28+
Environment Variables:
29+
GIT_HOOK_POSTCHECKOUT_FORCE_EXECUTE
30+
When set to a non-empty string, will cause the script to
31+
auto-execute the associated commands instead of just
32+
prompting about them.
33+
34+
35+
EOT
36+
37+
exit 0
38+
}
39+
if [ "$1" = '-h' ]; then
40+
usage
41+
fi
42+
43+
44+
# Bail out early if we're in the middle of a merge or rebase.
45+
GIT_DIR=$(git rev-parse --git-dir)
46+
GIT_DIR_MERGE="$GIT_DIR"/rebase-merge
47+
GIT_DIR_APPLY="$GIT_DIR"/rebase-apply
48+
if [[ (-d "$GIT_DIR_MERGE" && -f "$GIT_DIR_MERGE/interactive") || -d "$GIT_DIR_APPLY" ]]; then
49+
exit 0
50+
fi
51+
52+
53+
# Add pairs of files to check for differences, and the commands to run
54+
# if diffs are found. MUST be in the same order! (first file must
55+
# pair with the first command!)
56+
FILE_LIST=("composer.lock")
57+
CMD_LIST=("composer install --dev --no-interaction --ignore-platform-reqs")
58+
59+
60+
# Loop over the configured files.
61+
for ((i=0; i < ${#FILE_LIST[@]}; ++i)); do
62+
CHECK_FILE="${FILE_LIST[i]}"
63+
CHECK_CMD="${CMD_LIST[i]}"
64+
DIFF=$(git diff --stat HEAD@{1}..HEAD@{0} -- "$CHECK_FILE")
65+
if [[ -f "$CHECK_FILE" && -n "$DIFF" ]]; then
66+
if [ "${GIT_HOOK_POSTCHECKOUT_FORCE_EXECUTE:-""}" ]; then
67+
echo "\`$CHECK_FILE\` has changed. Executing hook command."
68+
$CHECK_CMD
69+
else
70+
echo "\`$CHECK_FILE\` has changed. You should execute the following command:"
71+
echo ""
72+
echo " $CHECK_CMD"
73+
echo ""
74+
fi
75+
fi
76+
done

0 commit comments

Comments
 (0)