Skip to contentSkip to conversation

t0001: check syntax of sample hooks

PATCH In review Jonathan Nieder 1 participant 2 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 t0001: check syntax of sample hooks

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.

---
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(-)
t/t0001-init.sh +18 -0
index 7c0a698..8421b86 100755
@@ -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 &&
templates/hooks--pre-rebase.sample +3 -0
index 053f111..6182192 100755
@@ -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
Signature · 1 line
1.7.2.rc2.532.g65d9b.dirty
#1 lore raw
Quoted 5 lines · Jonathan Nieder

+++ b/t/t0001-init.sh

t/t0001-init.sh +0 -0
@@ -141,6 +141,24 @@ test_expect_success 'reinit' '
 	test_cmp again/empty again/err2
 '
 

t/t0001-init.sh test_expect_success 'reinit' ' · line 153

Quoted 10 lines
t/t0001-init.sh +10 -0
+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