You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"git add" has no hook that lets users inspect what is about to be
staged. Users who want to reject certain paths or content must
wrap the command in a shell alias or wait for pre-commit, which
fires too late to prevent staging.
Introduce a "pre-add" hook that runs after "git add" computes the
new index state but before committing it to disk. The hook
receives two positional arguments:
$1 -- index path used by this invocation (may not exist yet)
$2 -- lockfile path containing proposed staged index state
While the lockfile is active the current index path remains readable
and unchanged, so a seperate copy is unnecessary. Hook authors can
inspect the computed result with ordinary tools:
GIT_INDEX_FILE="$2" git diff --cached --name-only HEAD
without needing to interpret pathspec or mode flags as the proposed
index already reflects their effect.
At the finish label, write_locked_index() writes the proposed index
to the lockfile without COMMIT_LOCK so commit_lock_file() can be
called seperately after the hook runs. However, do_write_locked_index()
unconditionally fires post-index-change after every write, and the
existing test suite (t7113) asserts that index.lock does not exist when
that hook fires. Tying the hook to COMMIT_LOCK would suppress it for
other callers that depend on it after a non-committed write (e.g.,
prepare_to_commit() in builtin/commit.c). A new SKIP_INDEX_CHANGE_HOOK
flag lets builtin/add.c suppress the automatic notification on just this
call, then emit post-index-change manually after commit_lock_file()
publishes the new index. If the hook rejects, rollback_lock_file()
discards the lockfile and the original index is left unchanged. When
no hook is installed the existing write_locked_index(COMMIT_LOCK |
SKIP_IF_UNCHANGED) path is taken.
The hook gate checks cache_changed regardless of exit_status so that
mixed-result adds (e.g., a tracked modification combined with an
ignored path) still run the hook when index content changes.
The hook is bypassed with "--no-verify" and is not invoked for
--interactive, --patch, --edit, or --dry-run, nor by "git commit -a"
which stages through its own code path.
Signed-off-by: Chandra Kethi-Reddy <chandrakr@pm.me>
0 commit comments