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

Re: [RFC] Hook management via 'git hooks' command

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Nov 16, 2019, 05:45 UTC
Message-ID
<20191116054501.GA6538@camp.crustytoothpaste.net>
In-Reply-To
<20191116011125.GG22855@google.com>
On 2019-11-16 at 01:11:25, Emily Shaffer wrote:
Show 12 quoted lines
> Here's my suggestion.
> 
>  - Let configs handle the hook list and ordering, via 'hook.{hookname}' which
>    can be specified more than once.
>    - global config: hook.update = /path/to/firsthook
>      user config: hook.update = /path/to/secondhook
>      worktree config: hook.update = -/path/to/firsthook #eliminate the firsthook
>       call
>    - global config: hook.update = /path/to/firsthook
>      repo config: hook.update = /path/to/secondhook
>      repo config: hook.update = ^/path/to/firsthook #move firsthook execution
>        after secondhook for this project

I'd like to hear more about how we handle multiple hooks that are repo-specific and don't live in the PATH. This is a common situation for existing tools that handle multiple hooks, and it's one I think we should support.

Perhaps we add a "git hook execute" subcommand that executes scripts from the hook directory.

Show 31 quoted lines
>  - Let users decide whether they want to check core.hookDir in addition to their
>    config, instead of their config, or not at all, via a config
>    'hook.runHookDir' "hookdir-first", "hookdir-only", "config-only", etc.
>  - Let users ask to be notified if running a hook from .git/hooks via a config
>    'hook.warnHookDir'. (Mentioning .git/hooks rather than core.hookDir is
>    intentional here.) Alternatively, ask for 'hook.warn' with values "hookdir",
>    "all" depending on how trusting the user feels.
>    - If we want to phase out .git/hooks, we can make this notification opt-out
>      instead of opt-in, someday.
>    - between runHookDir and warnHookDir, users are able to phase out
>      .git/hooks scripts at their own pace.
>  - Abstract most of this via a 'git hooks' command.
>    - 'git hooks add <hookname> [--<scope>] <path>' to append another hook;
>      let the scope setting match 'git config'.
>    - 'git hooks show [<hookname>]' to indicate which hooks will be run, either
>      on a specific event or for all events, and in which order, as well as each
>      hook's config origin
>    - 'git hooks edit <hookname>' to modify the hook order, or choose not to run
>      a hook locally
>    - By managing it through a command, we can reorder the contents of config
>      files if it makes sense to do so.
>  - Add a hook library.
>    - Optionally specify all hook events via enum, so we aren't string-typing
>      calls to find_hook() anymore.
>    - Resolve configs into a list of hooks to run by concatenating them together
>      in config precedence order.
>      - Also allow configs formatted like "-<path>" to remove an
>        earlier-configured invocation of that hook, or "^<path>" to move the
>        earlier-configured invocation to this point in the execution order
>    - Move the find_hook() implementation to here, to account for the multihook
>      ordering

I think this addresses most of the concerns that I had about ordering. It is still a little suboptimal that we're relying on the ordering of the config file, since it makes things equivalent to numbered files in .d directories hard.

Possibly as an alternative to the ^ syntax, we could make the hook value be of the form "key program", where key is a sort key (e.g., a number) and program is the program to run. We pick normal config file ordering if the keys are identical. Then if the system config wants to have a hook that always runs at the end, it can do so easily.

In addition, we should be sure that attempting to remove a hook which doesn't exist isn't an error, since a user might want to set their ~/.gitconfig file to always unset a global hook that may or may not exist.

We also need to address exit codes with multiple hooks and whether we fail fast or not. My series may provide some valuable options here, or we may choose to go with a single, simpler approach. Whatever we do, we should document the behavior clearly.

Overall, though, I think this approach has a lot of potential and I feel positive about it. Thanks for bringing this up again in a productive and helpful way.

-- 
brian m. carlson: Houston, Texas, US
OpenPGP: https://keybase.io/bk2204
Previous: Emily ShafferNext: Emily Shaffer
Message 2 of 12 in “[RFC] Hook management via 'git hooks' command”
  1. Emily ShafferNov 16, 2019
  2. brian m. carlsonNov 16, 2019
  3. Emily ShafferNov 18, 2019
  4. brian m. carlsonNov 19, 2019
  5. Emily ShafferNov 23, 2019
  6. brian m. carlsonNov 25, 2019
  7. Emily ShafferNov 25, 2019
  8. Emily ShafferNov 25, 2019
  9. brian m. carlsonNov 26, 2019
  10. Emily ShafferNov 26, 2019
  11. brian m. carlsonNov 26, 2019
  12. Emily ShafferDec 2, 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.