Damien Marié <[email protected]> writes:
> if (access(path.buf, X_OK) < 0) {
> + int err = errno;
OK, so we remember how/why we failed in err.
> #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.
> + 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.
> 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...
> +# 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.
> +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.
> +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.