From: Phillip Wood <phillip.wood123@gmail.com>
To: Chandra Kethi-Reddy via GitGitGadget <gitgitgadget@gmail.com>,
git@vger.kernel.org
Cc: Ben Knoble <ben.knoble@gmail.com>,
Adrian Ratiu <adrian.ratiu@collabora.com>,
Chandra Kethi-Reddy <chandrakr@pm.me>
Subject: Re: [PATCH v5] add: support pre-add hook
Date: Thu, 5 Mar 2026 14:37:46 +0000 [thread overview]
Message-ID: <98531f78-cf04-4e64-ac7c-6a13e52aee54@gmail.com> (raw)
In-Reply-To: <pull.2045.v5.git.1772714253412.gitgitgadget@gmail.com>
On 05/03/2026 12:37, Chandra Kethi-Reddy via GitGitGadget wrote:
>
> diff --git a/Documentation/git-add.adoc b/Documentation/git-add.adoc
> index 6192daeb03..a3ff4ced83 100644
> --- a/Documentation/git-add.adoc
> +++ b/Documentation/git-add.adoc
> [...]
> @@ -42,10 +42,11 @@ use the `--force` option to add ignored files. If you specify the exact
> filename of an ignored file, `git add` will fail with a list of ignored
> files. Otherwise it will silently ignore the file.
>
> +A `pre-add` hook can be used to reject `git add` (see linkgit:githooks[5]).
git-commit.adoc has a separate section for HOOKS, perhaps we should do
the same here. It would be clearer to say the that the proposed changes
are rejected rather than `git add` itself.
> diff --git a/Documentation/githooks.adoc b/Documentation/githooks.adoc
> index 056553788d..90945a590e 100644
> --- a/Documentation/githooks.adoc
> +++ b/Documentation/githooks.adoc
> @@ -94,6 +94,36 @@ and is invoked after the patch is applied and a commit is made.
> This hook is meant primarily for notification, and cannot affect
> the outcome of `git am`.
>
> +pre-add
> +~~~~~~~
> +
> +This hook is invoked by linkgit:git-add[1], and can be bypassed with the
> +`--no-verify` option. It is not invoked for `--interactive`, `--patch`,
> +`--edit`, or `--dry-run`.
I'm struggling to see how it is helpful to the user for "git add
--dry-run $path" to succeed when "git add $path" will be rejected by the
"pre-add" hook.
The other options all use "git apply" to apply a diff to the index so
they could apply the patch to a temporary index which is then passed to
the "pre-add" hook. If the hook fails the user should be given the
option to re-edit the patch or re-select the hunks so that their work is
not wasted.
To me this hook would be much more useful if it also checked changes
staged by "git commit" - it is still staging changes after all.
> +It takes two arguments: the path to the index file for this invocation
> +of `git add`, and the path to the lockfile containing the proposed
Calling it a lockfile is rather confusing - it is just second index file
that contains the changes that would be staged.
> +index after staging. If no index exists yet, the first argument names
> +a path that does not exist and should be treated as an empty index.
> +
> +The hook is invoked after the index has been updated in memory and
> +written to the lockfile, but before it is committed to the final index
> +path. Exiting with a non-zero status causes `git add` to reject the
> +proposed state, roll back the lockfile, and leave the index unchanged.
> +Exiting with zero status allows the index update to be committed. The
> +hook accepts or rejects the entire proposed update; per-path filtering
> +is not supported. Both files should be treated as read-only by the hook.
If we don't enforce them being read-only people will write hooks that
update them just as they do for "pre-commit" hooks. Once they start
relying on that they will complain if we stop supporting it. If we lock
both index files before running the hook I think that will prevent the
hook from being able to update them.
> +Hook authors may set `GIT_INDEX_FILE="$1"` to inspect the current index
> +state and `GIT_INDEX_FILE="$2"` to inspect the proposed index state.
We should be explicit that the proposed index state contains all the
changes that would be committed so staging changes incrementally will
check them multiple times.
> +This hook can be used to prevent staging of files based on names, content,
> +or sizes (e.g., to block `.env` files, secret keys, or large files).
> +
> +This hook is not invoked by `git commit -a` or `git commit --include`
I would be more accurate to say that it is not invoked by `git commit`
at all as there are several ways of staging changes including `git
commit $path`. We should also be explicit that in order to ensure that
all staged changes are checked the checks in the "pre-add" hook must be
duplicated by the "pre-commit" hook.
> +which still can run the `pre-commit` hook, providing a control point at
> +commit time.
While I've commented on the documentation, I think it is really the
design that needs working on. I like the idea of giving feedback earlier
when staging changes rather than waiting for the user to run "git
commit" but I think we need a more coherent approach to when the hook is
run.
Thanks
Phillip
prev parent reply other threads:[~2026-03-05 14:38 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-10 15:32 [PATCH] add: support pre-add hook Chandra Kethi-Reddy via GitGitGadget
2026-02-10 18:16 ` Junio C Hamano
2026-02-10 19:00 ` Junio C Hamano
2026-02-11 15:16 ` [PATCH] " Chandra
2026-02-11 15:05 ` [PATCH v2] " Chandra Kethi-Reddy via GitGitGadget
2026-02-11 19:50 ` Junio C Hamano
2026-02-11 21:11 ` Chandra
2026-02-11 21:24 ` Junio C Hamano
2026-02-11 21:54 ` Chandra
2026-02-25 2:15 ` [PATCH v3] " Chandra
2026-02-27 5:54 ` Chandra Kethi-Reddy via GitGitGadget
2026-03-03 23:06 ` Junio C Hamano
2026-03-04 9:49 ` Ben Knoble
2026-03-05 10:47 ` Phillip Wood
2026-03-05 11:40 ` [PATCH v4] " Chandra
2026-03-05 14:48 ` [PATCH v3] " Junio C Hamano
2026-03-05 11:36 ` [PATCH v4] " Chandra Kethi-Reddy via GitGitGadget
2026-03-05 12:03 ` Adrian Ratiu
2026-03-05 12:37 ` Chandra
2026-03-05 12:37 ` [PATCH v5] " Chandra Kethi-Reddy via GitGitGadget
2026-03-05 13:41 ` Adrian Ratiu
2026-03-05 13:46 ` Chandra
2026-03-05 19:23 ` Junio C Hamano
2026-03-06 2:20 ` Chandra
2026-03-13 14:39 ` Phillip Wood
2026-03-05 14:37 ` Phillip Wood [this message]
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=98531f78-cf04-4e64-ac7c-6a13e52aee54@gmail.com \
--to=phillip.wood123@gmail.com \
--cc=adrian.ratiu@collabora.com \
--cc=ben.knoble@gmail.com \
--cc=chandrakr@pm.me \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox