Skip to content

Commit 8c178e8

Browse files
committed
hook: restrict post-command hook even more
The postCommand.strategy=post-index-change config was designed to replace some internal hook usage. This internal hook was more restrictive than the initial feature was designed. Specifically, it ignored index updates that did not update the worktree. With the existing implementation, this causes more work done in the post-command hook than intended. Update the code to write a sentinel file only when the worktree is changed. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 3eb2bd0 commit 8c178e8

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

Documentation/config/postcommand.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ postCommand.strategy::
99

1010
`post-index-change`;;
1111
run the `post-command` hook only if the current process wrote to
12-
the index.
12+
the index and updated the worktree.
1313
----

hook.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ static int post_index_change_sentinel_exists(struct repository *r)
241241
*/
242242
static int handle_hook_replacement(struct repository *r,
243243
const char *hook_name,
244+
struct strvec *args,
244245
int *result)
245246
{
246247
const char *strval;
@@ -249,7 +250,11 @@ static int handle_hook_replacement(struct repository *r,
249250
return 0;
250251

251252
if (!strcmp(hook_name, "post-index-change")) {
252-
*result = write_post_index_change_sentinel(r);
253+
/* Create a sentinel file only if the worktree changed. */
254+
if (!strcmp(args->v[0], "1"))
255+
*result = write_post_index_change_sentinel(r);
256+
else
257+
*result = 0;
253258
return 1;
254259
}
255260
if (!strcmp(hook_name, "post-command") &&
@@ -289,7 +294,8 @@ int run_hooks_opt(struct repository *r, const char *hook_name,
289294
/* Interject hook behavior depending on strategy. */
290295
if (r && r->gitdir) {
291296
int result = 0;
292-
if (handle_hook_replacement(r, hook_name, &result))
297+
if (handle_hook_replacement(r, hook_name,
298+
&options->args, &result))
293299
return result;
294300
}
295301

t/t0401-post-command-hook.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ test_expect_success 'with post-index-change config' '
7272
test_path_is_missing post-command.out &&
7373
7474
echo stuff >>file &&
75-
# add updates the index and runs post-command.
75+
# add keeps the worktree the same, so does not run post-command.
7676
git add file &&
7777
test_path_is_missing post-index-change.out &&
78+
test_path_is_missing post-command.out &&
79+
80+
echo stuff >>file &&
81+
# reset --hard updates the worktree.
82+
git reset --hard &&
83+
test_path_is_missing post-index-change.out &&
7884
test_cmp expect post-command.out
7985
'
8086

0 commit comments

Comments
 (0)