{"thread":{"id":"19170","subject":"[PATCH] templates: make example pre-commit hook verify submodule commits exist","startedAt":"2009-05-04T03:32:29Z","lastAt":"2009-05-04T05:27:50Z","messageCount":2,"participants":["Sam Vilain","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"112945","messageId":"E1M0oxK-0002uG-RT@mail1.catalyst.net.nz","threadId":"19170","inReplyTo":null,"subject":"[PATCH] templates: make example pre-commit hook verify submodule commits exist","fromName":"Sam Vilain","fromEmail":"sam.vilain@catalyst.net.nz","sentAt":"2009-05-04T03:32:29Z","receivedAt":"2009-05-04T03:32:29Z","isPatch":true,"sender":{"key":"sam@vilain.net","avatar":"https://gravatar.com/avatar/8fc840ca854dbf6f7065b4335e3b934951c1dca3b11db688e95e471901f8f4a8?d=mp&s=160"},"body":"With submodules it can be quite easy to end up with a situation where the\nversion in the supermodule doesn't exist in the submodule.  Add to the\nexample pre-commit hook a script which checks before every commit whether\nthe submodule commit is likely to exist at the URL defined in the\n.gitmodules file.\n\nSigned-off-by: Sam Vilain <sam.vilain@catalyst.net.nz>\n---\n This should be useful for those using submodules.  Perhaps it should also\n have an equivalent in the 'git submodule' command.\n\n templates/hooks--pre-commit.sample |   56 +++++++++++++++++++++++++++++++++++-\n 1 files changed, 55 insertions(+), 1 deletions(-)\n\ndiff --git a/templates/hooks--pre-commit.sample b/templates/hooks--pre-commit.sample\nindex 0e49279..26e7cf2 100755\n--- a/templates/hooks--pre-commit.sample\n+++ b/templates/hooks--pre-commit.sample\n@@ -7,7 +7,61 @@\n #\n # To enable this hook, rename this file to \"pre-commit\".\n \n-if git-rev-parse --verify HEAD 2>/dev/null\n+# first, check that all submodule commits are available by a remote\n+# which has the same URL as the one they are listed in.\n+if [ -n \"$GIT_WORK_TREE\" ]\n+then\n+\tcd \"$GIT_WORK_TREE\"\n+fi\n+\n+git ls-tree $(git write-tree) | grep commit |\n+while read mode type sha1 submodule\n+do\n+\tmodule=$(GIT_CONFIG=.gitmodules git config --list |\n+\t\tgrep '\\.path='\"$submodule\"\\$ |\n+\t\tsed 's/submodule\\.\\([^\\.]*\\)\\..*/\\1/')\n+\tif [ -z \"$module\" ]\n+\tthen\n+\t\techo \"path $submodule is a submodule, but is not\" \\\n+\t\t\t\"listed in .gitmodules\" >&2\n+\t\texit 1\n+\tfi\n+\tsubmodule_url=$(GIT_CONFIG=.gitmodules git config \\\n+\t\tsubmodule.$submodule.url)\n+\n+\tremotes=$(GIT_DIR=$submodule/.git git config --list |\n+\t\tgrep '^remote\\..*\\.url='\"$submodule_url\" |\n+\t\tsed 's/remote\\.\\([^\\.]*\\)\\..*/\\1/')\n+\n+\tfound=\n+\tfor remote in $remotes\n+\tdo\n+\t\tfound=$(GIT_DIR=$submodule/.git git name-rev \\\n+\t\t\t--refs=refs/remotes/$remote/* $sha1 2>/dev/null |\n+\t\t\tgrep -vw undefined)\n+\t\t[ -n \"$found\" ] && break\n+\tdone\n+\n+\tif [ -z \"$found\" ]\n+\tthen\n+\t\techo \"submodule commit does not appear under tracked remote refs\" >&2\n+\t\techo \"  submodule = $submodule\"\n+\t\techo \".gitmodules = $module\"\n+\t\techo \"        url = $submodule_url\"\n+\t\techo \"     commit = $sha1\"\n+\t\techo \"  tried remotes:\" $remotes\n+\t\texit 2\n+\tfi\n+done\n+\n+# this is the return code of the | while sub-shell above\n+rc=$?\n+if [ $rc -ne 0 ]\n+then\n+\texit $rc\n+fi\n+\n+if git-rev-parse --verify HEAD >/dev/null 2>&1\n then\n \tagainst=HEAD\n else\n-- \n1.6.0\n"},{"id":"112946","messageId":"7vzldt4euh.fsf@alter.siamese.dyndns.org","threadId":"19170","inReplyTo":"E1M0oxK-0002uG-RT@mail1.catalyst.net.nz","subject":"Re: [PATCH] templates: make example pre-commit hook verify submodule commits exist","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-05-04T05:27:50Z","receivedAt":"2009-05-04T05:27:50Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Sam Vilain <sam.vilain@catalyst.net.nz> writes:\n\n> With submodules it can be quite easy to end up with a situation where the\n> version in the supermodule doesn't exist in the submodule.  Add to the\n> example pre-commit hook a script which checks before every commit whether\n> the submodule commit is likely to exist at the URL defined in the\n> .gitmodules file.\n>\n> Signed-off-by: Sam Vilain <sam.vilain@catalyst.net.nz>\n> ---\n>  This should be useful for those using submodules.  Perhaps it should also\n>  have an equivalent in the 'git submodule' command.\n\nIf I am a developer who is not interested in any of the submodules in the\nproject at all (i.e. I do not have any submodule checked out), my commits\nwill never touch any of the submodules.  In such a case, I shouldn't be\nforced to fetch/clone the submodule nor have a checkout.\n\nWhich means...\n\n> +# first, check that all submodule commits are available by a remote\n> +# which has the same URL as the one they are listed in.\n> +if [ -n \"$GIT_WORK_TREE\" ]\n> +then\n> +\tcd \"$GIT_WORK_TREE\"\n> +fi\n> +\n> +git ls-tree $(git write-tree) | grep commit |\n> +while read mode type sha1 submodule\n> +do\n\n... the input fed to this while look utterly wrong.  At least, you should\nbe checking *new* commit that is introduced to the tree with this commit.\n\nI didn't read the body of the while loop; no matter what it does, if its\ninput is wrong, its end result cannot be correct.\n"}]}