@matheusbsbs / matheus-bagliano / commits / 1abbe475ff
Fork 0

post-checkout hook, tests, and docs

Updated post-checkout hook to take a flag specifying whether the checkout is a branch checkout or a file checkout (from the index). Signed-off-by: Josh England <jjengla@sandia.gov> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Josh England committed Sep 26, 2007 at 15:31 UTC 1abbe475ff17349839f72a024cf665b8ec86473f
3 files changed +100
Documentation/hooks.txt
+14
@@ -87,6 +87,20 @@ parameter, and is invoked after a commit is made.
87 This hook is meant primarily for notification, and cannot affect
88 the outcome of `git-commit`.
89
90 +post-checkout
91 +-----------
92 +
93 +This hook is invoked when a `git-checkout` is run after having updated the
94 +worktree. The hook is given three parameters: the ref of the previous HEAD,
95 +the ref of the new HEAD (which may or may not have changed), and a flag
96 +indicating whether the checkout was a branch checkout (changing branches,
97 +flag=1) or a file checkout (retrieving a file from the index, flag=0).
98 +This hook cannot affect the outcome of `git-checkout`.
99 +
100 +This hook can be used to perform repository validity checks, auto-display
101 +differences from the previous HEAD if different, or set working dir metadata
102 +properties.
103 +
104 post-merge
105 -----------
106
git-checkout.sh
+12
@@ -137,6 +137,13 @@ Did you intend to checkout '$@' which can not be resolved as commit?"
137 git ls-files --error-unmatch -- "$@" >/dev/null || exit
138 git ls-files -- "$@" |
139 git checkout-index -f -u --stdin
140 +
141 + # Run a post-checkout hook -- the HEAD does not change so the
142 + # current HEAD is passed in for both args
143 + if test -x "$GIT_DIR"/hooks/post-checkout; then
144 + "$GIT_DIR"/hooks/post-checkout $old $old 0
145 + fi
146 +
147 exit $?
148 else
149 # Make sure we did not fall back on $arg^{tree} codepath
@@ -284,3 +291,8 @@ if [ "$?" -eq 0 ]; then
291 else
292 exit 1
293 fi
294 +
295 +# Run a post-checkout hook
296 +if test -x "$GIT_DIR"/hooks/post-checkout; then
297 + "$GIT_DIR"/hooks/post-checkout $old $new 1
298 +fi
t/t5403-post-checkout-hook.sh new
+74
@@ -0,0 +1,74 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2006 Josh England
4 +#
5 +
6 +test_description='Test the post-checkout hook.'
7 +. ./test-lib.sh
8 +
9 +test_expect_success setup '
10 + echo Data for commit0. >a &&
11 + echo Data for commit0. >b &&
12 + git update-index --add a &&
13 + git update-index --add b &&
14 + tree0=$(git write-tree) &&
15 + commit0=$(echo setup | git commit-tree $tree0) &&
16 + git update-ref refs/heads/master $commit0 &&
17 + git-clone ./. clone1 &&
18 + git-clone ./. clone2 &&
19 + GIT_DIR=clone2/.git git branch -a new2 &&
20 + echo Data for commit1. >clone2/b &&
21 + GIT_DIR=clone2/.git git add clone2/b &&
22 + GIT_DIR=clone2/.git git commit -m new2
23 +'
24 +
25 +for clone in 1 2; do
26 + cat >clone${clone}/.git/hooks/post-checkout <<'EOF'
27 +#!/bin/sh
28 +echo $@ > $GIT_DIR/post-checkout.args
29 +EOF
30 + chmod u+x clone${clone}/.git/hooks/post-checkout
31 +done
32 +
33 +test_expect_success 'post-checkout runs as expected ' '
34 + GIT_DIR=clone1/.git git checkout master &&
35 + test -e clone1/.git/post-checkout.args
36 +'
37 +
38 +test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' '
39 + old=$(awk "{print \$1}" clone1/.git/post-checkout.args) &&
40 + new=$(awk "{print \$2}" clone1/.git/post-checkout.args) &&
41 + flag=$(awk "{print \$3}" clone1/.git/post-checkout.args) &&
42 + test $old = $new -a $flag == 1
43 +'
44 +
45 +test_expect_success 'post-checkout runs as expected ' '
46 + GIT_DIR=clone1/.git git checkout master &&
47 + test -e clone1/.git/post-checkout.args
48 +'
49 +
50 +test_expect_success 'post-checkout args are correct with git checkout -b ' '
51 + GIT_DIR=clone1/.git git checkout -b new1 &&
52 + old=$(awk "{print \$1}" clone1/.git/post-checkout.args) &&
53 + new=$(awk "{print \$2}" clone1/.git/post-checkout.args) &&
54 + flag=$(awk "{print \$3}" clone1/.git/post-checkout.args) &&
55 + test $old = $new -a $flag == 1
56 +'
57 +
58 +test_expect_success 'post-checkout receives the right args with HEAD changed ' '
59 + GIT_DIR=clone2/.git git checkout new2 &&
60 + old=$(awk "{print \$1}" clone2/.git/post-checkout.args) &&
61 + new=$(awk "{print \$2}" clone2/.git/post-checkout.args) &&
62 + flag=$(awk "{print \$3}" clone2/.git/post-checkout.args) &&
63 + test $old != $new -a $flag == 1
64 +'
65 +
66 +test_expect_success 'post-checkout receives the right args when not switching branches ' '
67 + GIT_DIR=clone2/.git git checkout master b &&
68 + old=$(awk "{print \$1}" clone2/.git/post-checkout.args) &&
69 + new=$(awk "{print \$2}" clone2/.git/post-checkout.args) &&
70 + flag=$(awk "{print \$3}" clone2/.git/post-checkout.args) &&
71 + test $old == $new -a $flag == 0
72 +'
73 +
74 +test_done