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 all commits
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
4 changes: 2 additions & 2 deletions Documentation/config/postcommand.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ postCommand.strategy::
`always`;;
run the `post-command` hook on every process (default).

`post-index-change`;;
`worktree-change`;;
run the `post-command` hook only if the current process wrote to
the index.
the index and updated the worktree.
----
51 changes: 39 additions & 12 deletions hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ static char *get_post_index_change_sentinel_name(struct repository *r)
if (slash)
*slash = 0;

repo_git_path_replace(r, &path, "hooks/index-change-%s.snt", sid);
/*
* Do not write to hooks directory, as it could be redirected
* somewhere like the source tree.
*/
repo_git_path_replace(r, &path, "info/index-change-%s.snt", sid);

return strbuf_detach(&path, NULL);
}
Expand Down Expand Up @@ -229,6 +233,37 @@ static int post_index_change_sentinel_exists(struct repository *r)
return res;
}

/**
* See if we can replace the requested hook with an internal behavior.
* Returns 0 if the real hook should run. Returns nonzero if we instead
* executed custom internal behavior and the real hook should not run.
*/
static int handle_hook_replacement(struct repository *r,
const char *hook_name,
struct strvec *args)
{
const char *strval;
if (repo_config_get_string_tmp(r, "postcommand.strategy", &strval) ||
strcasecmp(strval, "worktree-change"))
return 0;

if (!strcmp(hook_name, "post-index-change")) {
/* Create a sentinel file only if the worktree changed. */
if (!strcmp(args->v[0], "1"))
write_post_index_change_sentinel(r);

/* We don't skip post-index-change hooks that exist. */
return 0;
}
if (!strcmp(hook_name, "post-command") &&
!post_index_change_sentinel_exists(r)) {
/* We skip the post-command hook in this case. */
return 1;
}

return 0;
}

int run_hooks_opt(struct repository *r, const char *hook_name,
struct run_hooks_opt *options)
{
Expand All @@ -255,17 +290,9 @@ int run_hooks_opt(struct repository *r, const char *hook_name,
};

/* Interject hook behavior depending on strategy. */
if (r && r->gitdir) {
const char *strval;
if (!repo_config_get_string_tmp(r, "postcommand.strategy", &strval) &&
!strcasecmp(strval, "post-index-change")) {
if (!strcmp(hook_name, "post-index-change"))
return write_post_index_change_sentinel(r);
if (!strcmp(hook_name, "post-command") &&
!post_index_change_sentinel_exists(r))
return 0;
}
}
if (r && r->gitdir &&
handle_hook_replacement(r, hook_name, &options->args))
return 0;

hook_path = find_hook(r, hook_name);

Expand Down
28 changes: 21 additions & 7 deletions t/t0401-post-command-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ test_expect_success 'with failing pre-command hook' '
'

test_expect_success 'with post-index-change config' '
mkdir -p .git/hooks &&
write_script .git/hooks/post-command <<-EOF &&
mkdir -p internal-hooks &&
write_script internal-hooks/post-command <<-EOF &&
echo ran >post-command.out
EOF
write_script .git/hooks/post-index-change <<-EOF &&
write_script internal-hooks/post-index-change <<-EOF &&
echo ran >post-index-change.out
EOF

# prevent writing of sentinel files to this directory.
test_when_finished chmod 775 internal-hooks &&
chmod a-w internal-hooks &&

git config core.hooksPath internal-hooks &&

# First, show expected behavior.
echo ran >expect &&
rm -f post-command.out post-index-change.out &&
Expand All @@ -57,18 +63,26 @@ test_expect_success 'with post-index-change config' '
test_cmp expect post-command.out &&

# Now, show configured behavior
git config postCommand.strategy post-index-change &&
rm -f post-command.out post-index-change.out &&
git config postCommand.strategy worktree-change &&

# rev-parse leaves index intact and thus skips post-command.
rm -f post-command.out post-index-change.out &&
git rev-parse HEAD &&
test_path_is_missing post-index-change.out &&
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.
rm -f post-command.out post-index-change.out &&
git add file &&
test_path_is_missing post-index-change.out &&
test_cmp expect post-index-change.out &&
test_path_is_missing post-command.out &&

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

Expand Down
Loading