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

Re: [PATCH v2 1/7] run-command: add preliminary support for multiple hooks

From
Duy Nguyen <pclouds@gmail.com>
Date
May 14, 2019, 12:46 UTC
Message-ID
<CACsJy8Ba98baQ2wZnMZyEva6gxO1ROZ4qJFTOdrCUXDMwrHnXA@mail.gmail.com>
In-Reply-To
<20190514002332.121089-2-sandals@crustytoothpaste.net>

On Tue, May 14, 2019 at 7:24 AM brian m. carlson <sandals@crustytoothpaste.net> wrote:

Show 43 quoted lines
> -int run_hook_ve(const char *const *env, const char *name, va_list args)
> +int find_hooks(const char *name, struct string_list *list)
>  {
> -       struct child_process hook = CHILD_PROCESS_INIT;
> -       const char *p;
> +       struct strbuf path = STRBUF_INIT;
> +       DIR *d;
> +       struct dirent *de;
>
> -       p = find_hook(name);
> -       if (!p)
> +       /*
> +        * We look for a single hook. If present, return it, and skip the
> +        * individual directories.
> +        */
> +       strbuf_git_path(&path, "hooks/%s", name);
> +       if (has_hook(&path, 1, X_OK)) {
> +               if (list)
> +                       string_list_append(list, path.buf);
> +               return 1;
> +       }
> +
> +       if (has_hook(&path, 1, F_OK))
>                 return 0;
>
> -       argv_array_push(&hook.args, p);
> -       while ((p = va_arg(args, const char *)))
> -               argv_array_push(&hook.args, p);
> -       hook.env = env;
> +       strbuf_reset(&path);
> +       strbuf_git_path(&path, "hooks/%s.d", name);
> +       d = opendir(path.buf);
> +       if (!d) {
> +               if (list)
> +                       string_list_clear(list, 0);
> +               return 0;
> +       }
> +       while ((de = readdir(d))) {
> +               if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
> +                       continue;
> +               strbuf_reset(&path);
> +               strbuf_git_path(&path, "hooks/%s.d/%s", name, de->d_name);
> +               if (has_hook(&path, 0, X_OK)) {

Do we want to support hooks in subdirectories as well (if so, using dir-iterator.h might be more appropriate)

If not, what happens when "path" is a directory. X_OK could be set (and often are) on them too.

Show 13 quoted lines
> +                       if (list)
> +                               string_list_append(list, path.buf);
> +                       else
> +                               return 1;
> +               }
> +       }
> +       closedir(d);
> +       strbuf_reset(&path);
> +       if (!list->nr) {
> +               return 0;
> +       }
> +
> +       string_list_sort(list);

This is going to be interesting on case-insensitive filesystems because we do strcmp by default, not the friendlier fspathcmp. And the ".exe" suffix might affect sort order too.

But I suppose we just need to be clear here (in documentation). They can always prefix with a number to keep hook files in expected order.

> +       return 1;
> +}
> +
Show 9 quoted lines
> diff --git a/run-command.h b/run-command.h
> index a6950691c0..1b3677fcac 100644
> --- a/run-command.h
> +++ b/run-command.h
> @@ -4,6 +4,7 @@
>  #include "thread-utils.h"
>
>  #include "argv-array.h"
> +#include "string-list.h"

'struct string_list;' should be enough (and a bit lighter) although I don't suppose it really matters.

>
>  struct child_process {
>         const char **argv;
-- 
Duy
Previous: brian m. carlsonNext: brian m. carlson
Message 3 of 34 in “Multiple hook support”
  1. 0/7 Multiple hook supportbrian m. carlson, May 14, 2019
  2. 1/7 run-command: add preliminary support for multiple hooksbrian m. carlson, May 14, 2019
  3. Duy NguyenMay 14, 2019
  4. brian m. carlsonMay 15, 2019
  5. brian m. carlsonMay 29, 2019
  6. Johannes SchindelinMay 14, 2019
  7. brian m. carlsonMay 15, 2019
  8. Johannes SixtMay 16, 2019
  9. Johannes SchindelinMay 17, 2019
  10. 2/7 builtin/receive-pack: add support for multiple hooksbrian m. carlson, May 14, 2019
  11. 3/7 rebase: add support for multiple hooksbrian m. carlson, May 14, 2019
  12. Duy NguyenMay 14, 2019
  13. Johannes SixtMay 14, 2019
  14. brian m. carlsonMay 15, 2019
  15. Duy NguyenMay 16, 2019
  16. 3/7 sequencer: add support for multiple hooksbrian m. carlson, May 14, 2019
  17. 4/7 builtin/worktree: add support for multiple post-checkout hooksbrian m. carlson, May 14, 2019
  18. 5/7 transport: add support for multiple pre-push hooksbrian m. carlson, May 14, 2019
  19. 6/7 config: allow configuration of multiple hook error behaviorbrian m. carlson, May 14, 2019
  20. Duy NguyenMay 14, 2019
  21. brian m. carlsonMay 15, 2019
  22. Jeff KingMay 16, 2019
  23. Jeff KingMay 16, 2019
  24. brian m. carlsonMay 16, 2019
  25. Jeff KingMay 16, 2019
  26. 7/7 docs: document multiple hooksbrian m. carlson, May 14, 2019
  27. Duy NguyenMay 14, 2019
  28. Jonathan NiederMay 14, 2019
  29. brian m. carlsonMay 14, 2019
  30. Jonathan NiederMay 14, 2019
  31. brian m. carlsonMay 16, 2019
  32. Jonathan NiederMay 16, 2019
  33. Jeff KingMay 16, 2019
  34. Duy NguyenMay 14, 2019

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.