threads / patch / 19170

patchtemplates: make example pre-commit hook verify submodule commits exist

Subject: [PATCH] templates: make example pre-commit hook verify submodule commits exist

## tl;dr

2 messages between May 4, 2009 and May 4, 2009. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Sam Vilain· May 4, 2009, 03:32 UTC · lore

With submodules it can be quite easy to end up with a situation where the version in the supermodule doesn't exist in the submodule. Add to the example pre-commit hook a script which checks before every commit whether the submodule commit is likely to exist at the URL defined in the .gitmodules file.

Signed-off-by: Sam Vilain <sam.vilain@catalyst.net.nz>
---
 This should be useful for those using submodules.  Perhaps it should also
 have an equivalent in the 'git submodule' command.
 templates/hooks--pre-commit.sample |   56 +++++++++++++++++++++++++++++++++++-
 1 files changed, 55 insertions(+), 1 deletions(-)
Show changes to templates/hooks--pre-commit.sample +55 −1
diff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample
index 0e49279..26e7cf2 100755
--- a/templates/hooks--pre-commit.sample
+++ b/templates/hooks--pre-commit.sample
@@ -7,7 +7,61 @@
 #
 # To enable this hook, rename this file to "pre-commit".
 
-if git-rev-parse --verify HEAD 2>/dev/null
+# first, check that all submodule commits are available by a remote
+# which has the same URL as the one they are listed in.
+if [ -n "$GIT_WORK_TREE" ]
+then
+	cd "$GIT_WORK_TREE"
+fi
+
+git ls-tree $(git write-tree) | grep commit |
+while read mode type sha1 submodule
+do
+	module=$(GIT_CONFIG=.gitmodules git config --list |
+		grep '\.path='"$submodule"\$ |
+		sed 's/submodule\.\([^\.]*\)\..*/\1/')
+	if [ -z "$module" ]
+	then
+		echo "path $submodule is a submodule, but is not" \
+			"listed in .gitmodules" >&2
+		exit 1
+	fi
+	submodule_url=$(GIT_CONFIG=.gitmodules git config \
+		submodule.$submodule.url)
+
+	remotes=$(GIT_DIR=$submodule/.git git config --list |
+		grep '^remote\..*\.url='"$submodule_url" |
+		sed 's/remote\.\([^\.]*\)\..*/\1/')
+
+	found=
+	for remote in $remotes
+	do
+		found=$(GIT_DIR=$submodule/.git git name-rev \
+			--refs=refs/remotes/$remote/* $sha1 2>/dev/null |
+			grep -vw undefined)
+		[ -n "$found" ] && break
+	done
+
+	if [ -z "$found" ]
+	then
+		echo "submodule commit does not appear under tracked remote refs" >&2
+		echo "  submodule = $submodule"
+		echo ".gitmodules = $module"
+		echo "        url = $submodule_url"
+		echo "     commit = $sha1"
+		echo "  tried remotes:" $remotes
+		exit 2
+	fi
+done
+
+# this is the return code of the | while sub-shell above
+rc=$?
+if [ $rc -ne 0 ]
+then
+	exit $rc
+fi
+
+if git-rev-parse --verify HEAD >/dev/null 2>&1
 then
 	against=HEAD
 else
-- 
1.6.0
Junio C Hamano· May 4, 2009, 05:27 UTC · re: Sam Vilain · lore

Re: [PATCH] templates: make example pre-commit hook verify submodule commits exist

Sam Vilain <sam.vilain@catalyst.net.nz> writes:
Show 10 quoted lines
> With submodules it can be quite easy to end up with a situation where the
> version in the supermodule doesn't exist in the submodule.  Add to the
> example pre-commit hook a script which checks before every commit whether
> the submodule commit is likely to exist at the URL defined in the
> .gitmodules file.
>
> Signed-off-by: Sam Vilain <sam.vilain@catalyst.net.nz>
> ---
>  This should be useful for those using submodules.  Perhaps it should also
>  have an equivalent in the 'git submodule' command.

If I am a developer who is not interested in any of the submodules in the project at all (i.e. I do not have any submodule checked out), my commits will never touch any of the submodules. In such a case, I shouldn't be forced to fetch/clone the submodule nor have a checkout.

Which means...
Show 10 quoted lines
> +# first, check that all submodule commits are available by a remote
> +# which has the same URL as the one they are listed in.
> +if [ -n "$GIT_WORK_TREE" ]
> +then
> +	cd "$GIT_WORK_TREE"
> +fi
> +
> +git ls-tree $(git write-tree) | grep commit |
> +while read mode type sha1 submodule
> +do

... the input fed to this while look utterly wrong. At least, you should be checking *new* commit that is introduced to the tree with this commit.

I didn't read the body of the while loop; no matter what it does, if its input is wrong, its end result cannot be correct.

← back to recent threads