Skip to content

Update behavior for postCommand.strategy=post-index-change #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Documentation/config/postcommand.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ postCommand.strategy::

`post-index-change`;;
run the `post-command` hook only if the current process wrote to
the index.
the index and updated the worktree.
----
10 changes: 8 additions & 2 deletions hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ static int post_index_change_sentinel_exists(struct repository *r)
*/
static int handle_hook_replacement(struct repository *r,
const char *hook_name,
struct strvec *args,
int *result)
{
const char *strval;
Expand All @@ -249,7 +250,11 @@ static int handle_hook_replacement(struct repository *r,
return 0;

if (!strcmp(hook_name, "post-index-change")) {
*result = write_post_index_change_sentinel(r);
/* Create a sentinel file only if the worktree changed. */
if (!strcmp(args->v[0], "1"))
*result = write_post_index_change_sentinel(r);
else
*result = 0;
return 1;
}
if (!strcmp(hook_name, "post-command") &&
Expand Down Expand Up @@ -289,7 +294,8 @@ int run_hooks_opt(struct repository *r, const char *hook_name,
/* Interject hook behavior depending on strategy. */
if (r && r->gitdir) {
int result = 0;
if (handle_hook_replacement(r, hook_name, &result))
if (handle_hook_replacement(r, hook_name,
&options->args, &result))
return result;
}

Expand Down
8 changes: 7 additions & 1 deletion t/t0401-post-command-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ test_expect_success 'with post-index-change config' '
test_path_is_missing post-command.out &&

echo stuff >>file &&
# add updates the index and runs post-command.
# add keeps the worktree the same, so does not run post-command.
git add file &&
test_path_is_missing post-index-change.out &&
test_path_is_missing post-command.out &&

echo stuff >>file &&
# reset --hard updates the worktree.
git reset --hard &&
test_path_is_missing post-index-change.out &&
test_cmp expect post-command.out
'

Expand Down
Loading