From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Cc: git@vger.kernel.org, "Jeff King" <peff@peff.net>,
"Duy Nguyen" <pclouds@gmail.com>,
"Junio C Hamano" <gitster@pobox.com>,
"Johannes Sixt" <j6t@kdbg.org>,
"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
"Phillip Wood" <phillip.wood123@gmail.com>,
"Jonathan Nieder" <jrnieder@gmail.com>
Subject: Re: [PATCH v2 1/7] run-command: add preliminary support for multiple hooks
Date: Wed, 15 May 2019 22:44:25 +0000 [thread overview]
Message-ID: <20190515224425.GK7458@genre.crustytoothpaste.net> (raw)
In-Reply-To: <nycvar.QRO.7.76.6.1905141653130.44@tvgsbejvaqbjf.bet>
[-- Attachment #1: Type: text/plain, Size: 5540 bytes --]
On Tue, May 14, 2019 at 05:12:39PM +0200, Johannes Schindelin wrote:
> Hi brian,
>
> On Tue, 14 May 2019, brian m. carlson wrote:
>
> > diff --git a/builtin/commit.c b/builtin/commit.c
> > index 833ecb316a..29bf80e0d1 100644
> > --- a/builtin/commit.c
> > +++ b/builtin/commit.c
> > @@ -943,7 +943,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
> > return 0;
> > }
> >
> > - if (!no_verify && find_hook("pre-commit")) {
> > + if (!no_verify && find_hooks("pre-commit", NULL)) {
>
> Hmm. This makes me concerned, as `find_hook()` essentially boiled down to
> a semi-fast `stat()` check, but `find_hooks()` needs to really look,
> right?
>
> It might make sense to somehow keep the list of discovered and executed
> hooks, as we already have a call to `run_commit_hook()` to execute the
> `pre-commit` hook at the beginning of this function.
With NULL as an argument, we return 1 as soon as we know there's a
single hook, so this is fairly optimized. I've tried to make it as cheap
as possible to check.
> Speaking of which... Shouldn't that `run_commit_hook()` call be adjusted
> at the same time, or else we have an inconsistent situation?
Nope, it calls run_hook_ve, which is updated.
> > diff --git a/run-command.c b/run-command.c
> > index 3449db319b..eb075ac86b 100644
> > --- a/run-command.c
> > +++ b/run-command.c
> > @@ -1308,53 +1308,143 @@ int async_with_fork(void)
> > #endif
> > }
> >
> > +/*
> > + * Return 1 if a hook exists at path (which may be modified) using access(2)
> > + * with check (which should be F_OK or X_OK), 0 otherwise. If strip is true,
> > + * additionally consider the same filename but with STRIP_EXTENSION added.
> > + * If check is X_OK, warn if the hook exists but is not executable.
> > + */
> > +static int has_hook(struct strbuf *path, int strip, int check)
> > +{
> > + if (access(path->buf, check) < 0) {
> > + int err = errno;
> > +
> > + if (strip) {
> > +#ifdef STRIP_EXTENSION
> > + strbuf_addstr(path, STRIP_EXTENSION);
> > + if (access(path->buf, check) >= 0)
> > + return 1;
> > + if (errno == EACCES)
> > + err = errno;
> > +#endif
> > + }
>
> How about simply guarding the entire `if()`? It is a bit unusual to guard
> *only* the inside block ;-)
I can make that change.
> > +
> > + if (err == EACCES && advice_ignored_hook) {
>
> Didn't you want to test for `X_OK` here, too? The code comment above the
> function seems to suggest that.
Yeah, that makes sense. I'll do that.
> > + static struct string_list advise_given = STRING_LIST_INIT_DUP;
> > +
> > + if (!string_list_lookup(&advise_given, path->buf)) {
> > + string_list_insert(&advise_given, path->buf);
> > + 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 0;
> > + }
> > + return 1;
>
> Wouldn't it make sense to do this very early? Something like
>
> if (!access(path->buf, check))
> return 1;
>
> first thing in the function, that that part is already out of the way and
> we don't have to indent so much.
Sure. That's a nice improvement.
> > const char *find_hook(const char *name)
> > {
> > static struct strbuf path = STRBUF_INIT;
> >
> > strbuf_reset(&path);
> > strbuf_git_path(&path, "hooks/%s", name);
> > - if (access(path.buf, X_OK) < 0) {
> > - int err = errno;
> > -
> > -#ifdef STRIP_EXTENSION
> > - strbuf_addstr(&path, STRIP_EXTENSION);
> > - if (access(path.buf, X_OK) >= 0)
> > - return path.buf;
> > - if (errno == EACCES)
> > - err = errno;
> > -#endif
> > -
> > - if (err == EACCES && advice_ignored_hook) {
> > - static struct string_list advise_given = STRING_LIST_INIT_DUP;
> > -
> > - if (!string_list_lookup(&advise_given, name)) {
> > - string_list_insert(&advise_given, name);
> > - 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;
>
> So that's where this comes from ;-)
Exactly. I didn't make a lot of changes.
> > +/*
> > + * Returns the paths to all hook files, or NULL if all hooks are missing or
> > + * disabled.
>
> Left-over comment?
Yup, thanks for pointing it out.
> > + * Returns 1 if there are hooks; 0 otherwise. If hooks is not NULL, stores the
> > + * names of the hooks into them in the order they should be executed.
> > + */
> > +int find_hooks(const char *name, struct string_list *hooks);
> > +/*
> > + * Invokes the handler function once for each hook. Returns 0 if all hooks were
> > + * successful, or the exit status of the first failing hook.
> > + */
> > +int for_each_hook(const char *name,
> > + int (*handler)(const char *name, const char *path, void *),
> > + void *data);
>
> My gut says that it would make sense for `struct repository *` to sprout a
> hashmap for hook lookup that would be populated by demand, and allowed
> things like
>
> hash_hook(r, "pre-commit")
Knowing that we have an optimized check, do you still think we should do
this, or are you okay leaving it as it is?
--
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 868 bytes --]
next prev parent reply other threads:[~2019-05-15 22:45 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-05-14 0:23 [PATCH v2 0/7] Multiple hook support brian m. carlson
2019-05-14 0:23 ` [PATCH v2 1/7] run-command: add preliminary support for multiple hooks brian m. carlson
2019-05-14 12:46 ` Duy Nguyen
2019-05-15 22:27 ` brian m. carlson
2019-05-29 2:18 ` brian m. carlson
2019-05-14 15:12 ` Johannes Schindelin
2019-05-15 22:44 ` brian m. carlson [this message]
2019-05-16 19:11 ` Johannes Sixt
2019-05-17 20:31 ` Johannes Schindelin
2019-05-14 0:23 ` [PATCH v2 2/7] builtin/receive-pack: add " brian m. carlson
2019-05-14 0:23 ` [PATCH v2 3/7] rebase: " brian m. carlson
2019-05-14 12:56 ` Duy Nguyen
2019-05-14 17:58 ` Johannes Sixt
2019-05-15 22:55 ` brian m. carlson
2019-05-16 10:29 ` Duy Nguyen
2019-05-14 0:23 ` [PATCH v2 3/7] sequencer: " brian m. carlson
2019-05-14 0:23 ` [PATCH v2 4/7] builtin/worktree: add support for multiple post-checkout hooks brian m. carlson
2019-05-14 0:23 ` [PATCH v2 5/7] transport: add support for multiple pre-push hooks brian m. carlson
2019-05-14 0:23 ` [PATCH v2 6/7] config: allow configuration of multiple hook error behavior brian m. carlson
2019-05-14 13:20 ` Duy Nguyen
2019-05-15 23:10 ` brian m. carlson
2019-05-16 5:08 ` Jeff King
2019-05-16 5:02 ` Jeff King
2019-05-16 17:19 ` brian m. carlson
2019-05-16 21:52 ` Jeff King
2019-05-14 0:23 ` [PATCH v2 7/7] docs: document multiple hooks brian m. carlson
2019-05-14 13:38 ` Duy Nguyen
2019-05-14 0:51 ` [PATCH v2 0/7] Multiple hook support Jonathan Nieder
2019-05-14 1:59 ` brian m. carlson
2019-05-14 2:26 ` Jonathan Nieder
2019-05-16 0:42 ` brian m. carlson
2019-05-16 0:51 ` Jonathan Nieder
2019-05-16 4:51 ` Jeff King
2019-05-14 13:30 ` Duy Nguyen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190515224425.GK7458@genre.crustytoothpaste.net \
--to=sandals@crustytoothpaste.net \
--cc=Johannes.Schindelin@gmx.de \
--cc=avarab@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=j6t@kdbg.org \
--cc=jrnieder@gmail.com \
--cc=pclouds@gmail.com \
--cc=peff@peff.net \
--cc=phillip.wood123@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).