git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Sample pre-push hook can crash

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 10, 2016, 21:43 UTC
Message-ID
<xmqq37rydn6b.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<CAH8BJxHeyfpKsvjGfg5ZJ5YDQk6pzZp4ufHVEV=cFriL8j_4uw@mail.gmail.com>
Stephen Morton <stephen.c.morton@gmail.com> writes:
Show 7 quoted lines
> The sample pre-push hook provided with git [1] will crash if the local
> repo is not up to date with the remote as $remote_sha is not present
> in the local repo. I'm not sure if this patch is exactly correct, it's
> just provided as an example.
>
> Given that people are likely crafting their own solutions based on the
> examples, it's probably good to get right.

It's probably good to get right, but I do not think use of @{u} is making it right, unfortunately. You may not necessarily have @{u} configured, and you may not even pushing to the configured remote branch.

The spirit of the sample hook, I think, is to validate the new commits you are publishing, so if you cannot even determine which ones are new and which ones are not, failing the "push" by exiting with non-zero status is the right behaviour for this sample.

So perhaps something like this may be more appropriate as an example.

 templates/hooks--pre-push.sample | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/templates/hooks--pre-push.sample b/templates/hooks--pre-push.sample
index 6187dbf..7ef6780 100755
--- a/templates/hooks--pre-push.sample
+++ b/templates/hooks--pre-push.sample
@@ -41,7 +41,12 @@ do
 		fi
 
 		# Check for WIP commit
-		commit=`git rev-list -n 1 --grep '^WIP' "$range"`
+		commit=`git rev-list -n 1 --grep '^WIP' "$range"` || {
+			# we do not even know about the range...
+			echo >&2 "Non-ff update to $remote_ref"
+			echo >&2 "fetch from there first"
+			exit 1
+		}
 		if [ -n "$commit" ]
 		then
 			echo >&2 "Found WIP commit in $local_ref, not pushing"
Previous: Stephen MortonNext: Stephen Morton
Message 2 of 5 in “Sample pre-push hook can crash”
  1. Stephen MortonMar 10, 2016
  2. Junio C HamanoMar 10, 2016
  3. Stephen MortonMar 11, 2016
  4. Junio C HamanoMar 11, 2016
  5. Stephen MortonMar 11, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.