{"thread":{"id":"10035","subject":"[PATCH] post-checkout hook, tests, and docs","startedAt":"2007-09-26T21:31:01Z","lastAt":"2007-10-04T16:47:55Z","messageCount":3,"participants":["Josh England","Alex Riesen","René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"54120","messageId":"1190842261-9750-1-git-send-email-jjengla@sandia.gov","threadId":"10035","inReplyTo":null,"subject":"[PATCH] post-checkout hook, tests, and docs","fromName":"Josh England","fromEmail":"jjengla@sandia.gov","sentAt":"2007-09-26T21:31:01Z","receivedAt":"2007-09-26T21:31:01Z","isPatch":true,"sender":{"key":"jjengla@sandia.gov","avatar":null},"body":"Updated post-checkout hook to take a flag specifying whether the checkout is\na branch checkout or a file checkout (from the index).\n\nSigned-off-by: Josh England <jjengla@sandia.gov>\n---\n Documentation/hooks.txt       |   14 ++++++++\n git-checkout.sh               |   12 +++++++\n t/t5403-post-checkout-hook.sh |   74 +++++++++++++++++++++++++++++++++++++++++\n 3 files changed, 100 insertions(+), 0 deletions(-)\n create mode 100755 t/t5403-post-checkout-hook.sh\n\ndiff --git a/Documentation/hooks.txt b/Documentation/hooks.txt\nindex 58b9547..f110162 100644\n--- a/Documentation/hooks.txt\n+++ b/Documentation/hooks.txt\n@@ -87,6 +87,20 @@ parameter, and is invoked after a commit is made.\n This hook is meant primarily for notification, and cannot affect\n the outcome of `git-commit`.\n \n+post-checkout\n+-----------\n+\n+This hook is invoked when a `git-checkout` is run after having updated the\n+worktree.  The hook is given three parameters: the ref of the previous HEAD,\n+the ref of the new HEAD (which may or may not have changed), and a flag\n+indicating whether the checkout was a branch checkout (changing branches,\n+flag=1) or a file checkout (retrieving a file from the index, flag=0).\n+This hook cannot affect the outcome of `git-checkout`.\n+\n+This hook can be used to perform repository validity checks, auto-display\n+differences from the previous HEAD if different, or set working dir metadata\n+properties.\n+\n post-merge\n -----------\n \ndiff --git a/git-checkout.sh b/git-checkout.sh\nindex 17f4392..8993920 100755\n--- a/git-checkout.sh\n+++ b/git-checkout.sh\n@@ -137,6 +137,13 @@ Did you intend to checkout '$@' which can not be resolved as commit?\"\n \tgit ls-files --error-unmatch -- \"$@\" >/dev/null || exit\n \tgit ls-files -- \"$@\" |\n \tgit checkout-index -f -u --stdin\n+\n+        # Run a post-checkout hook -- the HEAD does not change so the\n+        # current HEAD is passed in for both args\n+\tif test -x \"$GIT_DIR\"/hooks/post-checkout; then\n+\t    \"$GIT_DIR\"/hooks/post-checkout $old $old 0\n+\tfi\n+\n \texit $?\n else\n \t# Make sure we did not fall back on $arg^{tree} codepath\n@@ -284,3 +291,8 @@ if [ \"$?\" -eq 0 ]; then\n else\n \texit 1\n fi\n+\n+# Run a post-checkout hook\n+if test -x \"$GIT_DIR\"/hooks/post-checkout; then\n+        \"$GIT_DIR\"/hooks/post-checkout $old $new 1\n+fi\ndiff --git a/t/t5403-post-checkout-hook.sh b/t/t5403-post-checkout-hook.sh\nnew file mode 100755\nindex 0000000..e656aa1\n--- /dev/null\n+++ b/t/t5403-post-checkout-hook.sh\n@@ -0,0 +1,74 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2006 Josh England\n+#\n+\n+test_description='Test the post-checkout hook.'\n+. ./test-lib.sh\n+\n+test_expect_success setup '\n+\t echo Data for commit0. >a &&\n+\t echo Data for commit0. >b &&\n+\t git update-index --add a &&\n+\t git update-index --add b &&\n+\t tree0=$(git write-tree) &&\n+\t commit0=$(echo setup | git commit-tree $tree0) &&\n+        git update-ref refs/heads/master $commit0 &&\n+\t git-clone ./. clone1 &&\n+\t git-clone ./. clone2 &&\n+        GIT_DIR=clone2/.git git branch -a new2 &&\n+        echo Data for commit1. >clone2/b &&\n+\t GIT_DIR=clone2/.git git add clone2/b &&\n+\t GIT_DIR=clone2/.git git commit -m new2\n+'\n+\n+for clone in 1 2; do\n+    cat >clone${clone}/.git/hooks/post-checkout <<'EOF'\n+#!/bin/sh\n+echo $@ > $GIT_DIR/post-checkout.args\n+EOF\n+    chmod u+x clone${clone}/.git/hooks/post-checkout\n+done\n+\n+test_expect_success 'post-checkout runs as expected ' '\n+        GIT_DIR=clone1/.git git checkout master &&\n+        test -e clone1/.git/post-checkout.args\n+'\n+\n+test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' '\n+        old=$(awk \"{print \\$1}\" clone1/.git/post-checkout.args) &&\n+        new=$(awk \"{print \\$2}\" clone1/.git/post-checkout.args) &&\n+        flag=$(awk \"{print \\$3}\" clone1/.git/post-checkout.args) &&\n+        test $old = $new -a $flag == 1\n+'\n+\n+test_expect_success 'post-checkout runs as expected ' '\n+        GIT_DIR=clone1/.git git checkout master &&\n+        test -e clone1/.git/post-checkout.args\n+'\n+\n+test_expect_success 'post-checkout args are correct with git checkout -b ' '\n+        GIT_DIR=clone1/.git git checkout -b new1 &&\n+        old=$(awk \"{print \\$1}\" clone1/.git/post-checkout.args) &&\n+        new=$(awk \"{print \\$2}\" clone1/.git/post-checkout.args) &&\n+        flag=$(awk \"{print \\$3}\" clone1/.git/post-checkout.args) &&\n+        test $old = $new -a $flag == 1\n+'\n+\n+test_expect_success 'post-checkout receives the right args with HEAD changed ' '\n+        GIT_DIR=clone2/.git git checkout new2 &&\n+        old=$(awk \"{print \\$1}\" clone2/.git/post-checkout.args) &&\n+        new=$(awk \"{print \\$2}\" clone2/.git/post-checkout.args) &&\n+        flag=$(awk \"{print \\$3}\" clone2/.git/post-checkout.args) &&\n+        test $old != $new -a $flag == 1\n+'\n+\n+test_expect_success 'post-checkout receives the right args when not switching branches ' '\n+        GIT_DIR=clone2/.git git checkout master b &&\n+        old=$(awk \"{print \\$1}\" clone2/.git/post-checkout.args) &&\n+        new=$(awk \"{print \\$2}\" clone2/.git/post-checkout.args) &&\n+        flag=$(awk \"{print \\$3}\" clone2/.git/post-checkout.args) &&\n+        test $old == $new -a $flag == 0\n+'\n+\n+test_done\n-- \n1.5.3.2.90.g6069e-dirty\n"},{"id":"54822","messageId":"20071004133253.GA30393@steel.home","threadId":"10035","inReplyTo":"1190842261-9750-1-git-send-email-jjengla@sandia.gov","subject":"[PATCH] fix t5403-post-checkout-hook.sh: built-in test in dash does not have \"==\"","fromName":"Alex Riesen","fromEmail":"raa.lkml@gmail.com","sentAt":"2007-10-04T13:32:53Z","receivedAt":"2007-10-04T13:32:53Z","isPatch":true,"sender":{"key":"raa.lkml@gmail.com","avatar":"https://avatars.githubusercontent.com/u/324101?v=4"},"body":"Signed-off-by: Alex Riesen <raa.lkml@gmail.com>\n---\nJosh England, Wed, Sep 26, 2007 23:31:01 +0200:\n>  t/t5403-post-checkout-hook.sh |   74 +++++++++++++++++++++++++++++++++++++++++\n\non Ubuntu, /bin/sh is dash.\n\n t/t5403-post-checkout-hook.sh |    8 ++++----\n 1 files changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/t/t5403-post-checkout-hook.sh b/t/t5403-post-checkout-hook.sh\nindex 487abf3..823239a 100755\n--- a/t/t5403-post-checkout-hook.sh\n+++ b/t/t5403-post-checkout-hook.sh\n@@ -39,7 +39,7 @@ test_expect_success 'post-checkout receives the right arguments with HEAD unchan\n         old=$(awk \"{print \\$1}\" clone1/.git/post-checkout.args) &&\n         new=$(awk \"{print \\$2}\" clone1/.git/post-checkout.args) &&\n         flag=$(awk \"{print \\$3}\" clone1/.git/post-checkout.args) &&\n-        test $old = $new -a $flag == 1\n+        test $old = $new -a $flag = 1\n '\n \n test_expect_success 'post-checkout runs as expected ' '\n@@ -52,7 +52,7 @@ test_expect_success 'post-checkout args are correct with git checkout -b ' '\n         old=$(awk \"{print \\$1}\" clone1/.git/post-checkout.args) &&\n         new=$(awk \"{print \\$2}\" clone1/.git/post-checkout.args) &&\n         flag=$(awk \"{print \\$3}\" clone1/.git/post-checkout.args) &&\n-        test $old = $new -a $flag == 1\n+        test $old = $new -a $flag = 1\n '\n \n test_expect_success 'post-checkout receives the right args with HEAD changed ' '\n@@ -60,7 +60,7 @@ test_expect_success 'post-checkout receives the right args with HEAD changed ' '\n         old=$(awk \"{print \\$1}\" clone2/.git/post-checkout.args) &&\n         new=$(awk \"{print \\$2}\" clone2/.git/post-checkout.args) &&\n         flag=$(awk \"{print \\$3}\" clone2/.git/post-checkout.args) &&\n-        test $old != $new -a $flag == 1\n+        test $old != $new -a $flag = 1\n '\n \n test_expect_success 'post-checkout receives the right args when not switching branches ' '\n@@ -68,7 +68,7 @@ test_expect_success 'post-checkout receives the right args when not switching br\n         old=$(awk \"{print \\$1}\" clone2/.git/post-checkout.args) &&\n         new=$(awk \"{print \\$2}\" clone2/.git/post-checkout.args) &&\n         flag=$(awk \"{print \\$3}\" clone2/.git/post-checkout.args) &&\n-        test $old == $new -a $flag == 0\n+        test $old = $new -a $flag = 0\n '\n \n test_done\n-- \n1.5.3.3.147.g2054\n"},{"id":"54855","messageId":"4705193B.30400@lsrfire.ath.cx","threadId":"10035","inReplyTo":"20071004133253.GA30393@steel.home","subject":"Re: [PATCH] fix t5403-post-checkout-hook.sh: built-in test in dash does not have \"==\"","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2007-10-04T16:47:55Z","receivedAt":"2007-10-04T16:47:55Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Alex Riesen schrieb:\n> Signed-off-by: Alex Riesen <raa.lkml@gmail.com>\n> ---\n> Josh England, Wed, Sep 26, 2007 23:31:01 +0200:\n>>  t/t5403-post-checkout-hook.sh |   74 +++++++++++++++++++++++++++++++++++++++++\n> \n> on Ubuntu, /bin/sh is dash.\n\nAh, so YOU are the other Ubuntu user around here, Alex! ;-)  I've just\nsent out the exact same patch.  Sorry for the duplication, please ignore me.\n\nRené\n"}]}