# [PATCH] Documentation for post-checkout and post-merge hooks

6 messages from 2007-09-25 to 2007-09-26. Participants: Josh England, Junio C Hamano.
Thread: https://gitlist.dev/t/10021

## Josh England, 2007-09-25 22:49

Subject: [PATCH] post-checkout hooks and related tests
Message-ID: <1190760563-32453-1-git-send-email-jjengla@sandia.gov>
URL: https://gitlist.dev/e/1190760563-32453-1-git-send-email-jjengla%40sandia.gov

```
Signed-off-by: Josh England <jjengla@sandia.gov>
---
 git-checkout.sh               |   12 +++++++
 t/t5403-post-checkout-hook.sh |   70 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+), 0 deletions(-)
 create mode 100755 t/t5403-post-checkout-hook.sh

diff --git a/git-checkout.sh b/git-checkout.sh
index 17f4392..78355eb 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -137,6 +137,13 @@ Did you intend to checkout '$@' which can not be resolved as commit?"
 	git ls-files --error-unmatch -- "$@" >/dev/null || exit
 	git ls-files -- "$@" |
 	git checkout-index -f -u --stdin
+
+        # Run a post-checkout hook -- the HEAD does not change so the
+	# current HEAD is passed in for both args
+	if test -x "$GIT_DIR"/hooks/post-checkout; then
+	    "$GIT_DIR"/hooks/post-checkout $old $old
+	fi
+
 	exit $?
 else
 	# Make sure we did not fall back on $arg^{tree} codepath
@@ -284,3 +291,8 @@ if [ "$?" -eq 0 ]; then
 else
 	exit 1
 fi
+
+# Run a post-checkout hook
+if test -x "$GIT_DIR"/hooks/post-checkout; then
+        "$GIT_DIR"/hooks/post-checkout $old $new
+fi
diff --git a/t/t5403-post-checkout-hook.sh b/t/t5403-post-checkout-hook.sh
new file mode 100755
index 0000000..663f8d7
--- /dev/null
+++ b/t/t5403-post-checkout-hook.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Josh England
+#
+
+test_description='Test the post-checkout hook.'
+. ./test-lib.sh
+
+test_expect_success setup '
+	echo Data for commit0. >a &&
+	echo Data for commit0. >b &&
+	git update-index --add a &&
+	git update-index --add b &&
+	tree0=$(git write-tree) &&
+	commit0=$(echo setup | git commit-tree $tree0) &&
+        git update-ref refs/heads/master $commit0 &&
+	git-clone ./. clone1 &&
+	git-clone ./. clone2 &&
+        GIT_DIR=clone2/.git git branch -a new2 &&
+       	echo Data for commit1. >clone2/b &&
+	GIT_DIR=clone2/.git git add clone2/b &&
+	GIT_DIR=clone2/.git git commit -m new2
+'
+
+for clone in 1 2; do 
+    cat >clone${clone}/.git/hooks/post-checkout <<'EOF'
+#!/bin/sh
+echo $@ > $GIT_DIR/post-checkout.args
+EOF
+    chmod u+x clone${clone}/.git/hooks/post-checkout
+done
+
+test_expect_success 'post-checkout runs as expected ' '
+        GIT_DIR=clone1/.git git checkout master &&
+ 	test -e clone1/.git/post-checkout.args
+'
+
+test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' '
+         old=$(awk "{print \$1}" clone1/.git/post-checkout.args) &&
+         new=$(awk "{print \$2}" clone1/.git/post-checkout.args) &&
+         test $old = $new
+'
+
+test_expect_success 'post-checkout runs as expected ' '
+        GIT_DIR=clone1/.git git checkout master &&
+ 	test -e clone1/.git/post-checkout.args
+'
+
+test_expect_success 'post-checkout args are correct with git checkout -b ' '
+        GIT_DIR=clone1/.git git checkout -b new1 &&
+        old=$(awk "{print \$1}" clone1/.git/post-checkout.args) &&
+        new=$(awk "{print \$2}" clone1/.git/post-checkout.args) &&
+        test $old = $new
+'
+
+test_expect_success 'post-checkout receives the right args with HEAD changed ' '
+        GIT_DIR=clone2/.git git checkout new2 &&
+        old=$(awk "{print \$1}" clone2/.git/post-checkout.args) &&
+        new=$(awk "{print \$2}" clone2/.git/post-checkout.args) &&
+        test $old != $new
+'
+
+test_expect_success 'post-checkout receives the right args when not switching branches ' '
+        GIT_DIR=clone2/.git git checkout master b &&
+        old=$(awk "{print \$1}" clone2/.git/post-checkout.args) &&
+        new=$(awk "{print \$2}" clone2/.git/post-checkout.args) &&
+        test $old == $new
+'
+
+test_done
-- 
1.5.3.2.89.g296e

```

## Josh England, 2007-09-25 22:49

Subject: [PATCH] post-merge hook and related tests
Message-ID: <1190760563-32453-2-git-send-email-jjengla@sandia.gov>
URL: https://gitlist.dev/e/1190760563-32453-2-git-send-email-jjengla%40sandia.gov
In-Reply-To: <1190760563-32453-1-git-send-email-jjengla@sandia.gov>

```
Signed-off-by: Josh England <jjengla@sandia.gov>
---
 git-merge.sh               |   13 ++++++++++
 t/t5402-post-merge-hook.sh |   56 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 0 deletions(-)
 create mode 100644 t/t5402-post-merge-hook.sh

diff --git a/git-merge.sh b/git-merge.sh
index 3a01db0..66e48b3 100755
--- a/git-merge.sh
+++ b/git-merge.sh
@@ -97,6 +97,19 @@ finish () {
 		fi
 		;;
 	esac
+
+	# Run a post-merge hook
+        if test -x "$GIT_DIR"/hooks/post-merge
+        then
+	    case "$squash" in
+	    t)
+                "$GIT_DIR"/hooks/post-merge 1
+		;;
+	    '')
+                "$GIT_DIR"/hooks/post-merge 0
+		;;
+	    esac
+        fi
 }
 
 merge_name () {
diff --git a/t/t5402-post-merge-hook.sh b/t/t5402-post-merge-hook.sh
new file mode 100644
index 0000000..2a7a097
--- /dev/null
+++ b/t/t5402-post-merge-hook.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Josh England
+#
+
+test_description='Test the post-merge hook.'
+. ./test-lib.sh
+
+test_expect_success setup '
+	echo Data for commit0. >a &&
+	git update-index --add a &&
+	tree0=$(git write-tree) &&
+	commit0=$(echo setup | git commit-tree $tree0) &&
+	echo Changed data for commit1. >a &&
+	git update-index a &&
+	tree1=$(git write-tree) &&
+	commit1=$(echo modify | git commit-tree $tree1 -p $commit0) &&
+        git update-ref refs/heads/master $commit0 &&     
+	git-clone ./. clone1 &&
+	GIT_DIR=clone1/.git git update-index --add a &&
+	git-clone ./. clone2 &&
+	GIT_DIR=clone2/.git git update-index --add a
+'
+
+for clone in 1 2; do 
+    cat >clone${clone}/.git/hooks/post-merge <<'EOF'
+#!/bin/sh
+echo $@ >> $GIT_DIR/post-merge.args
+EOF
+    chmod u+x clone${clone}/.git/hooks/post-merge
+done
+
+test_expect_failure 'post-merge does not run for up-to-date ' '
+        GIT_DIR=clone1/.git git merge $commit0 &&
+ 	test -e clone1/.git/post-merge.args
+'
+
+test_expect_success 'post-merge runs as expected ' '
+        GIT_DIR=clone1/.git git merge $commit1 &&
+ 	test -e clone1/.git/post-merge.args
+'
+
+test_expect_success 'post-merge from normal merge receives the right argument ' '
+        grep 0 clone1/.git/post-merge.args
+'
+
+test_expect_success 'post-merge from squash merge runs as expected ' '
+        GIT_DIR=clone2/.git git merge --squash $commit1 &&
+ 	test -e clone2/.git/post-merge.args
+'
+
+test_expect_success 'post-merge from squash merge receives the right argument ' '
+        grep 1 clone2/.git/post-merge.args
+'
+
+test_done
-- 
1.5.3.2.89.g296e

```

## Josh England, 2007-09-25 22:49

Subject: [PATCH] Documentation for post-checkout and post-merge hooks
Message-ID: <1190760563-32453-3-git-send-email-jjengla@sandia.gov>
URL: https://gitlist.dev/e/1190760563-32453-3-git-send-email-jjengla%40sandia.gov
In-Reply-To: <1190760563-32453-2-git-send-email-jjengla@sandia.gov>

```
Signed-off-by: Josh England <jjengla@sandia.gov>
---
 Documentation/hooks.txt |   25 +++++++++++++++++++++++++
 1 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/Documentation/hooks.txt b/Documentation/hooks.txt
index c39edc5..37a3e5a 100644
--- a/Documentation/hooks.txt
+++ b/Documentation/hooks.txt
@@ -87,6 +87,31 @@ parameter, and is invoked after a commit is made.
 This hook is meant primarily for notification, and cannot affect
 the outcome of `git-commit`.
 
+post-checkout
+-----------
+
+This hook is invoked when a `git-checkout` is run after having updated the
+worktree.  The hook is given two parameters: the ref of the previous HEAD and
+the ref of the new HEAD, which may or may not have changed.  This hook cannot
+affect the outcome of `git-checkout`.
+
+This hook can be used to perform repository validity checks, auto-display
+differences from the previous HEAD if different, or set working dir metadata
+properties.
+
+post-merge
+-----------
+
+This hook is invoked by `git-merge`, which happens when a `git pull`
+is done on a local repository.  The hook takes a single parameter, a status
+flag specifying whether or not the merge being done was a squash merge.
+This hook cannot affect the outcome of `git-merge`.
+
+This hook can be used in conjunction with a corresponding pre-commit hook to
+save and restore any form of metadata associated with the working tree
+(eg: permissions/ownership, ACLS, etc).  See contrib/hooks/setgitperms.pl
+for an example of how to do this.
+
 [[pre-receive]]
 pre-receive
 -----------
-- 
1.5.3.2.89.g296e

```

## Junio C Hamano, 2007-09-25 23:17

Subject: Re: [PATCH] post-checkout hooks and related tests
Message-ID: <7vve9y2v39.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vve9y2v39.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1190760563-32453-1-git-send-email-jjengla@sandia.gov>

```
"Josh England" <jjengla@sandia.gov> writes:

> diff --git a/git-checkout.sh b/git-checkout.sh
> index 17f4392..78355eb 100755
> --- a/git-checkout.sh
> +++ b/git-checkout.sh
> @@ -137,6 +137,13 @@ Did you intend to checkout '$@' which can not be resolved as commit?"
>  	git ls-files --error-unmatch -- "$@" >/dev/null || exit
>  	git ls-files -- "$@" |
>  	git checkout-index -f -u --stdin
> +
> +        # Run a post-checkout hook -- the HEAD does not change so the
> +	# current HEAD is passed in for both args
> +	if test -x "$GIT_DIR"/hooks/post-checkout; then

It is usually a good idea to view your patch in your MUA before
sending them out.

You will spot HT vs SP indentation inconsistencies right away.

HEAD did not change but don't you want to differenciate if the
checkout was from the index or from the HEAD?  If not why not?

```

## Junio C Hamano, 2007-09-25 23:21

Subject: Re: [PATCH] post-merge hook and related tests
Message-ID: <7vr6km2uwi.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vr6km2uwi.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1190760563-32453-2-git-send-email-jjengla@sandia.gov>

```
"Josh England" <jjengla@sandia.gov> writes:

> Signed-off-by: Josh England <jjengla@sandia.gov>
> ---
>  git-merge.sh               |   13 ++++++++++
>  t/t5402-post-merge-hook.sh |   56 ++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 69 insertions(+), 0 deletions(-)
>  create mode 100644 t/t5402-post-merge-hook.sh

I think this is part of the 'master' for at least a few days, so
is a half of the Documentation patch.

```

## Josh England, 2007-09-26 17:16

Subject: Re: [PATCH] post-checkout hooks and related tests
Message-ID: <1190827010.6078.123.camel@beauty>
URL: https://gitlist.dev/e/1190827010.6078.123.camel%40beauty
In-Reply-To: <7vve9y2v39.fsf@gitster.siamese.dyndns.org>

```
On Tue, 2007-09-25 at 16:17 -0700, Junio C Hamano wrote:
> "Josh England" <jjengla@sandia.gov> writes:
> 
> > diff --git a/git-checkout.sh b/git-checkout.sh
> > index 17f4392..78355eb 100755
> > --- a/git-checkout.sh
> > +++ b/git-checkout.sh
> > @@ -137,6 +137,13 @@ Did you intend to checkout '$@' which can not be resolved as commit?"
> >  	git ls-files --error-unmatch -- "$@" >/dev/null || exit
> >  	git ls-files -- "$@" |
> >  	git checkout-index -f -u --stdin
> > +
> > +        # Run a post-checkout hook -- the HEAD does not change so the
> > +	# current HEAD is passed in for both args
> > +	if test -x "$GIT_DIR"/hooks/post-checkout; then
> 
> It is usually a good idea to view your patch in your MUA before
> sending them out.
>
> You will spot HT vs SP indentation inconsistencies right away.

Dang.  It looked ok in emacs.  :(

> HEAD did not change but don't you want to differenciate if the
> checkout was from the index or from the HEAD?  If not why not?

Hmmmm.  It wouldn't hurt to add another arg though I guess in case
someone might use it.  I'm just trying to figure out how to word this
nicely in the Documentation.

-JE

```
