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

Re: [PATCH v2] run-command: add hint when a hook is ignored

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 6, 2017, 04:52 UTC
Message-ID
<xmqq4lrcyj1p.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<0102015eee503678-52126849-7580-4832-9d46-0f5ac7a52336-000000@eu-west-1.amazonses.com>
Damien Marié <damien@dam.io> writes:
>  	if (access(path.buf, X_OK) < 0) {
> +		int err = errno;
OK, so we remember how/why we failed in err.
Show 6 quoted lines
>  #ifdef STRIP_EXTENSION
>  		strbuf_addstr(&path, STRIP_EXTENSION);
>  		if (access(path.buf, X_OK) >= 0)
>  			return path.buf;
> +		else if (errno == EACCES)
> +			err = errno;

I think it is easier to reason about if this were not "else if", but just a simple "if".

 - We tried foo-hook, and failed.
 - On a platform that foo-hook.exe can also be a hook
   - We try foo-hook.exe and if it seems OK, we return with smile.
   - If not, if we know foo-hook.exe exists but we cannot execute,
     we update err (forgetting the reason why foo-hook was wrong)
     with the reason why foo-hook.exe is bad.

It is OK to forget why foo-hook was unhappy, as on a STRIP_EXTENSION build, we would have tried to run foo-hook.exe anyway.

>  #endif

So at this point, with or without STRIP_EXTENSION, err tells us why the file we wanted to be available as a hook did not pass our criteria.

> +		if (err == EACCES && advice_ignored_hook) {

And we want to do the advise thing only if we know we failed due to EACCES and for no other reason.

Show 9 quoted lines
> +			advise(_(
> +				"The '%s' hook was ignored because "
> +				"it's not set as executable.\n"
> +				"You can disable this warning with "
> +				"`git config advice.ignoredHook false`."), path.buf);
> +		}
>  		return NULL;
>  	}
>  	return path.buf;

Overall, the logic looks correct to me. Note that we may have gotten EACCES not because the path lacked the executable bit, but because the hook directory was unreadable ;-), but in such a case, you cannot tell if "it's not set as executable" is true anyway.

Show 12 quoted lines
> diff --git a/t/t7519-ignored-hook-warning.sh b/t/t7519-ignored-hook-warning.sh
> new file mode 100755
> index 0000000000000..59052a4429111
> --- /dev/null
> +++ b/t/t7519-ignored-hook-warning.sh
> @@ -0,0 +1,67 @@
> +#!/bin/sh
> +
> +test_description='ignored hook warning'
> +
> +. ./test-lib.sh
> +
These days, things like this...
Show 10 quoted lines
> +# install hook that always succeeds
> +HOOKDIR="$(git rev-parse --git-dir)/hooks"
> +HOOK="$HOOKDIR/pre-commit"
> +mkdir -p "$HOOKDIR"
> +cat > "$HOOK" <<EOF
> +#!/bin/sh
> +exit 0
> +EOF
> +
> +chmod +x "$HOOK"
...should all go to test_expect_success, i.e.
	test_expect_success setup '
		...
		mkdir -p "$hookdir" &&
		write_script "$hookdir/pre-commit" <<-\EOF
		exit 0
		EOF
	'
write_script takes care of flipping +x on.
Show 9 quoted lines
> +test_expect_success 'no warning if proper hook' '
> +
> +    if git commit -m "more" 2>&1 >/dev/null | grep hint
> +    then
> +        false
> +    else
> +        true
> +    fi
> +
 - Indents in our shell scripts are done with tab (HT).
 - We try to avoid running git command on the LHS of a pipe when we
   do not have to.
 - "git commit" may fail due to not having anything worth
   committing, even before it notices that pre-commit hook is or is
   not executable.  Avoid relying on the order of things that happen
   to be true in the current implementation when we do not have to.
 - We may see some other hint.  Avoid relying on the set of advises
   that happens to currently be defined when we do not have to.
 - Output from advise() can be localized, so grepping to expect
   something either is there or is not there would be triggered as
   an error in GETTEXT_POISON build.  We unfortunately need to use
   test_i18ngrep to work it around.
Perhaps the above should become more like so:
	git commit --allow-empty -m more 2>message &&
	test_i18ngrep ! "hook was ignored" message
> +'
> +
> +chmod -x "$HOOK"

Move this to the beginning of the next one that is protected with POSIXPERM.

Show 9 quoted lines
> +test_expect_success POSIXPERM 'warning if hook not set as executable' '
> +
> +    if git commit -m "more" 2>&1 >/dev/null | grep hint
> +    then
> +        true
> +    else
> +        false
> +    fi
> +'
	chmod -x "$hookdir/pre-commit" &&
	git commit --allow-empty -m "even more" 2>message &&
	test_i18ngrep "hook was ignored" message
or something like that.
Thanks.
Previous: Damien MariéNext: Junio C Hamano
Message 5 of 16 in “run-command.c: add hint when hook is not executable”
  1. run-command.c: add hint when hook is not executableDamien, Oct 3, 2017
  2. Junio C HamanoOct 4, 2017
  3. DamienOct 5, 2017
  4. run-command: add hint when a hook is ignoredDamien Marié, Oct 5, 2017
  5. Junio C HamanoOct 6, 2017
  6. Junio C HamanoOct 6, 2017
  7. Junio C HamanoOct 6, 2017
  8. Junio C HamanoOct 6, 2017
  9. DamienOct 6, 2017
  10. run-command: add hint when a hook is ignoredDamien Marié, Oct 6, 2017
  11. Junio C HamanoOct 10, 2017
  12. Junio C HamanoOct 11, 2017
  13. Jeff KingJan 3, 2018
  14. Junio C HamanoJan 5, 2018
  15. Jeff KingMar 3, 2018
  16. Junio C HamanoMar 5, 2018

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.