# [PATCH] cogito: Honor either post-commit script name; fail if both are executable

3 messages from 2006-10-20 to 2006-10-27. Participants: Jim Meyering, Petr Baudis.
Thread: https://gitlist.dev/t/5971

## Jim Meyering, 2006-10-20 16:15

Subject: [PATCH] cogito: Honor either post-commit script name; fail if both are executable
Message-ID: <874ptzhsjs.fsf@rho.meyering.net>
URL: https://gitlist.dev/e/874ptzhsjs.fsf%40rho.meyering.net

```
Hi,

I promised this patch some time ago, made the changes,
and then never sent them.  This is slightly different
from the current implementation in that it fails when both
scripts are executable.  Also, it factors out the script names and
adds tests.

Signed-off-by: Jim Meyering <jim@meyering.net>
---
 cg-commit        |   43 ++++++++++++++++++++++++++++++-------------
 t/t9800-hooks.sh |   37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 13 deletions(-)

diff --git a/cg-commit b/cg-commit
index 01a4eb7..ee4769e 100755
--- a/cg-commit
+++ b/cg-commit
@@ -148,11 +148,11 @@ #	If the file exists it will be used as
 #	the commit message. The template file makes it possible to
 #	automatically add `Signed-off-by` line to the log message.
 #
-# $GIT_DIR/hooks/commit-post::
+# $GIT_DIR/hooks/post-commit:: (legacy: commit-post)
 #	If the file exists and is executable it will be executed upon
 #	completion of the commit. The script is passed two arguments.
 #	The first argument is the commit ID and the second is the
-#	branchname. A sample `commit-post` script might look like:
+#	branchname. A sample `post-commit` script might look like:
 #
 #	#!/bin/sh
 #	id=$1
@@ -680,18 +680,35 @@ elif [ "$newhead" ]; then
 		branchname="$(cat "$_git/branch-name")"
 	fi
 	[ -z "$branchname" ] && [ "$_git_head" != "master" ] && branchname="$_git_head"
-	if [ -x "$_git/hooks/commit-post" -o -x "$_git/hooks/post-commit" ] && [ ! "$no_hooks" ]; then
-		if [ "$(git-repo-config --bool cogito.hooks.commit.post.allmerged)" = "true" ]; then
-			# We just hope that for the initial commit, the user didn't
-			# manage to install the hook yet.
-			for merged in $(git-rev-list $newhead ^$oldhead | tac); do
-				[ -x "$_git/hooks/commit-post" ] && "$_git/hooks/commit-post" "$merged" "$branchname"
-				[ -x "$_git/hooks/post-commit" ] && "$_git/hooks/post-commit" "$merged" "$branchname"
-			done
-		else
-			[ -x "$_git/hooks/commit-post" ] && "$_git/hooks/commit-post" "$newhead" "$branchname"
-			[ -x "$_git/hooks/post-commit" ] && "$_git/hooks/post-commit" "$newhead" "$branchname"
+
+	if [ "$no_hooks" ]; then
+		exit 0
+	fi
+
+	# Decide which spelling to use for the post-commit hook script name.
+	post_commit="$_git/hooks/post-commit"
+	old_name="$_git/hooks/commit-post"
+	if [ -x "$post_commit" ]; then
+		if [ -x "$old_name" ]; then
+			die "both $post_commit and $old_name are executable."
 		fi
+		# This is the expected case: use $post_commit
+	else
+		if [ ! -x "$old_name" ]; then
+			# neither is executable: do nothing
+			exit 0
+		fi
+		post_commit=$old_name
+	fi
+
+	if [ "$(git-repo-config --bool cogito.hooks.commit.post.allmerged)" = "true" ]; then
+		# We just hope that for the initial commit, the user didn't
+		# manage to install the hook yet.
+		for merged in $(git-rev-list $newhead ^$oldhead | tac); do
+			"$post_commit" "$merged" "$branchname"
+		done
+	else
+		"$post_commit" "$newhead" "$branchname"
 	fi
 
 	exit 0
diff --git a/t/t9800-hooks.sh b/t/t9800-hooks.sh
new file mode 100755
index 0000000..a54e35f
--- /dev/null
+++ b/t/t9800-hooks.sh
@@ -0,0 +1,37 @@
+#!/usr/bin/env bash
+#
+# Copyright (c) 2006 Jim Meyering
+#
+test_description="Test the commit hooks."
+
+. ./test-lib.sh
+rm -rf .git
+
+echo x > f
+test_expect_success 'initialize repo' \
+	'(cg-init -m"Initial commit")'
+
+commit_post=.git/hooks/commit-post
+post_commit=.git/hooks/post-commit
+cat > $post_commit <<\EOF
+#!/bin/sh
+echo $0
+exit 0
+EOF
+cp $post_commit $commit_post
+
+chmod a+x $post_commit
+test_expect_success 'test the post-commit name' \
+	'(echo 1 >> f; test "`cg-commit -m x|grep git/hooks`" = $post_commit)'
+
+chmod a-x $post_commit
+chmod a+x $commit_post
+
+test_expect_success 'test the legacy commit-post name' \
+	'(echo 2 >> f; test "`cg-commit -m x|grep git/hooks`" = $commit_post)'
+
+chmod a+x $post_commit
+test_expect_failure 'fail when both are executable' \
+	'(echo 3 >> f; cg-commit -m x)'
+
+test_done
-- 
1.4.3.ge193

```

## Petr Baudis, 2006-10-27 02:05

Subject: Re: [PATCH] cogito: Honor either post-commit script name; fail if both are executable
Message-ID: <20061027020549.GY20017@pasky.or.cz>
URL: https://gitlist.dev/e/20061027020549.GY20017%40pasky.or.cz
In-Reply-To: <874ptzhsjs.fsf@rho.meyering.net>

```
  Hi!

Dear diary, on Fri, Oct 20, 2006 at 06:15:51PM CEST, I got a letter
where Jim Meyering <jim@meyering.net> said that...
> I promised this patch some time ago, made the changes,
> and then never sent them.  This is slightly different
> from the current implementation in that it fails when both
> scripts are executable.  Also, it factors out the script names and
> adds tests.
> 
> Signed-off-by: Jim Meyering <jim@meyering.net>

  I'm not sure I like this (of course, I always like additional tests,
though). The problem is that this loses a smooth upgrade path, things
suddenly break and you can't commit without having to think about fixing
your environment. We should always give users enough time for that with
deprecation warnings. So if we want to get rid of commit-post, we should
rather start printing deprecation warnings if commit-post exists, and in
few months cut commit-post off.

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
#!/bin/perl -sp0777i<X+d*lMLa^*lN%0]dsXx++lMlN/dsM0<j]dsj
$/=unpack('H*',$_);$_=`echo 16dio\U$k"SK$/SM$n\EsN0p[lN*1

```

## Jim Meyering, 2006-10-27 07:41

Subject: Re: [PATCH] cogito: Honor either post-commit script name; fail if both are executable
Message-ID: <87mz7ip5mq.fsf@rho.meyering.net>
URL: https://gitlist.dev/e/87mz7ip5mq.fsf%40rho.meyering.net
In-Reply-To: <20061027020549.GY20017@pasky.or.cz>

```
Petr Baudis <pasky@suse.cz> wrote:
> Dear diary, on Fri, Oct 20, 2006 at 06:15:51PM CEST, I got a letter
> where Jim Meyering <jim@meyering.net> said that...
>> I promised this patch some time ago, made the changes,
>> and then never sent them.  This is slightly different
>> from the current implementation in that it fails when both
>> scripts are executable.  Also, it factors out the script names and
>> adds tests.
>>
>> Signed-off-by: Jim Meyering <jim@meyering.net>
>
>   I'm not sure I like this (of course, I always like additional tests,
> though). The problem is that this loses a smooth upgrade path, things
> suddenly break and you can't commit without having to think about fixing
> your environment.

How would such a tiny change affect the upgrade path?
Failing when both scripts are executable feels more like doing the
(confused) user a favor than complicating their upgrade experience :-)

In case it wasn't clear, the only change induced by that patch is to
make cg-commit fail if *both* commit-post and post-commit scripts
exist and are executable.  I'll bet that is unusual enough not to matter.

> deprecation warnings. So if we want to get rid of commit-post, we should
> rather start printing deprecation warnings if commit-post exists, and in
> few months cut commit-post off.

If you want to retire "commit-post", that's mostly independent.
IMHO, the things to do:
  -- announce and document the name change (leaving mention of the old
       name at least temporarily in the documentation)
  -- later, start warning if the old name is used

```
