Skip to content
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
34 changes: 28 additions & 6 deletions hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,15 @@ static int write_post_index_change_sentinel(struct repository *r)
*/
static int post_index_change_sentinel_exists(struct repository *r)
{
char *path = get_post_index_change_sentinel_name(r);
char *path;
int res = 1;

/* It can't exist if we don't have a gitdir. */
if (!r->gitdir)
return 0;

path = get_post_index_change_sentinel_name(r);

if (unlink(path)) {
if (is_missing_file_error(errno))
res = 0;
Expand All @@ -233,6 +239,21 @@ static int post_index_change_sentinel_exists(struct repository *r)
return res;
}

static int check_worktree_change(const char *key, const char *value,
UNUSED const struct config_context *ctx,
void *data)
{
int *enabled = data;

if (!strcmp(key, "postcommand.strategy") &&
!strcasecmp(value, "worktree-change")) {
*enabled = 1;
return 1;
}

return 0;
}

/**
* 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
Expand All @@ -242,9 +263,11 @@ 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"))
int enabled = 0;

read_early_config(r, check_worktree_change, &enabled);

if (!enabled)
return 0;

if (!strcmp(hook_name, "post-index-change")) {
Expand Down Expand Up @@ -290,8 +313,7 @@ int run_hooks_opt(struct repository *r, const char *hook_name,
};

/* Interject hook behavior depending on strategy. */
if (r && r->gitdir &&
handle_hook_replacement(r, hook_name, &options->args))
if (r && handle_hook_replacement(r, hook_name, &options->args))
return 0;

hook_path = find_hook(r, hook_name);
Expand Down
27 changes: 25 additions & 2 deletions t/t0401-post-command-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,35 @@ test_expect_success 'with post-index-change config' '
test_cmp expect post-index-change.out &&
test_path_is_missing post-command.out &&

# add keeps the worktree the same, so does not run post-command.
# and this should work through an alias.
git config alias.addalias add &&
rm -f post-command.out post-index-change.out &&
echo more stuff >>file &&
git addalias file &&
test_cmp expect post-index-change.out &&
test_path_is_missing post-command.out &&

echo stuff >>file &&
# reset --hard updates the worktree.
# even through an alias
git config alias.resetalias "reset --hard" &&
rm -f post-command.out post-index-change.out &&
git reset --hard &&
git resetalias &&
test_cmp expect post-index-change.out &&
test_cmp expect post-command.out
test_cmp expect post-command.out &&

rm -f post-command.out &&
test_must_fail git && # get help text
test_path_is_missing post-command.out &&

rm -f post-command.out &&
git version &&
test_path_is_missing post-command.out &&

rm -f post-command.out &&
test_must_fail git typo &&
test_path_is_missing post-command.out
'

test_done
Loading