# [PATCH v2] t0001: check syntax of sample hooks

2 messages from 2010-07-11 to 2010-07-11. Participants: Jonathan Nieder.
Thread: https://gitlist.dev/t/24361

## Jonathan Nieder, 2010-07-11 00:45

Subject: [PATCH v2] t0001: check syntax of sample hooks
Message-ID: <20100711004518.GA5658@burratino>
URL: https://gitlist.dev/e/20100711004518.GA5658%40burratino

```
Make sure that the sample hooks do not use any shell features
that the shell used to execute them does not support.

The documentation at the end of the sample pre-rebase script will
never be executed, but "sh -n" does not know that.  Convert it to
a HERE document to avoid spurious failures.

If the shell specified in a hook’s #! line has a space in it, the
new test will break it into words, not even taking quoting into
account.  So

 #!"c:/Program Files/bin/bash.exe"

specifies a shell named ‘"c:/Program’ with argument ‘Files/bin/bash.exe"’.
On Unix, that is exactly what exec() would do; on Windows, it is close
to what the exec() compatibility layer does (actually, the
‘Files/bin/bash.exe"’ argument is thrown away by parse_interpreter()).
To honor quoting during tests would be to set unreasonable
expectations for runtime.

The user may want to avoid specifying a path to the shell at all, by
building with

 make 'SHELL_PATH=/usr/bin/env bash'

The rules used in tests and at runtime on Unix allow this.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
As before[1], the real motivation is the change to appease sh -n for
pre-rebase, since I occasionally run a test like this locally.
Patch is against master.

Thoughts?

[1] http://thread.gmane.org/gmane.comp.version-control.git/145281

 t/t0001-init.sh                    |   18 ++++++++++++++++++
 templates/hooks--pre-rebase.sample |    3 +++
 2 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 7c0a698..8421b86 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -141,6 +141,24 @@ test_expect_success 'reinit' '
 	test_cmp again/empty again/err2
 '
 
+test_expect_success 'sample hooks use acceptable syntax' '
+	rm -fr hook-syntax &&
+	git init hook-syntax &&
+	(
+		cd hook-syntax &&
+		test -d .git/hooks &&
+		fail=f &&
+		for i in .git/hooks/*.sample
+		do
+			read shebang <"$i" &&
+			shell=${shebang#"#!"} &&
+			$shell -n $i ||
+			fail=t
+		done &&
+		test "$fail" = f
+	)
+'
+
 test_expect_success 'init with --template' '
 	mkdir template-source &&
 	echo content >template-source/file &&
diff --git a/templates/hooks--pre-rebase.sample b/templates/hooks--pre-rebase.sample
index 053f111..6182192 100755
--- a/templates/hooks--pre-rebase.sample
+++ b/templates/hooks--pre-rebase.sample
@@ -91,6 +91,7 @@ fi
 exit 0
 
 ################################################################
+: <<\EOF
 
 This sample hook safeguards topic branches that have been
 published from being rewound.
@@ -167,3 +168,5 @@ To compute (2):
 	git rev-list master..topic
 
 	if this is empty, it is fully merged to "master".
+
+EOF
-- 
1.7.2.rc2.532.g65d9b.dirty

```

## Jonathan Nieder, 2010-07-11 00:53

Subject: Re: [PATCH v2] t0001: check syntax of sample hooks
Message-ID: <20100711005327.GA5744@burratino>
URL: https://gitlist.dev/e/20100711005327.GA5744%40burratino
In-Reply-To: <20100711004518.GA5658@burratino>

```
Jonathan Nieder wrote:

> +++ b/t/t0001-init.sh
> @@ -141,6 +141,24 @@ test_expect_success 'reinit' '
>  	test_cmp again/empty again/err2
>  '
>  
> +test_expect_success 'sample hooks use acceptable syntax' '
> +	rm -fr hook-syntax &&
> +	git init hook-syntax &&
> +	(
> +		cd hook-syntax &&
> +		test -d .git/hooks &&
> +		fail=f &&
> +		for i in .git/hooks/*.sample
> +		do
> +			read shebang <"$i" &&

This should use ‘read -r’, for the sake of #!c:\Users\Xyzzy\bin\bash.exe

*sigh*
Jonathan

```
