Skip to content

Commit 1e32467

Browse files
authored
Restore previous errno after post command hook (#860)
Test and fix for #859 : Repro steps: In a repository where post-command hook is configured (such as a VFSForGit clone) 1. Configure autocorrect: `git config help.autocorrect=prompt` 2. Run an misspelled command. For example, `git statuss` Expected output: ``` WARNING: You called a Git command named 'statuss', which does not exist. Run 'status' instead [y/N]? ``` Actual output: ``` failed to run command 'statuss': No error ``` I think the problem is that [run_post_command_hook](https://github.com/microsoft/git/blob/9db3c15f497c5d59a20bb2ff4d054ff687d08334/git.c#L503C1-L503C26) needs to save and restore errno, similar to how handle_alias does it. Otherwise, the errno from the failed command is overwritten by the successful errno from postcommand hook before the help autocorrect has a chance to examine it.
2 parents 82d60a0 + 7c70515 commit 1e32467

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

git.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ static int run_post_command_hook(struct repository *r)
504504
{
505505
char *lock;
506506
int ret = 0;
507+
int saved_errno = errno;
507508
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
508509

509510
/*
@@ -520,6 +521,8 @@ static int run_post_command_hook(struct repository *r)
520521
ret = run_hooks_opt(r, "post-command", &opt);
521522

522523
run_post_hook = 0;
524+
525+
errno = saved_errno;
523526
strvec_clear(&sargv);
524527
strvec_clear(&opt.args);
525528
setenv("COMMAND_HOOK_LOCK", "false", 1);

t/t0401-post-command-hook.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ test_description='post-command hook'
44

55
. ./test-lib.sh
66

7+
test_expect_success 'hook does not block git help' '
8+
git config help.autocorrect immediate &&
9+
git commit --allow-empty -m "a single log entry" &&
10+
mkdir -p .git/hooks &&
11+
write_script .git/hooks/post-command <<-EOF &&
12+
echo "\$*" | sed "s/ --git-pid=[0-9]*//" \
13+
>\$(git rev-parse --git-dir)/post-command.out
14+
EOF
15+
# intentional typo "logg" gets autocorrected to "log"
16+
git logg --format=%s --first-parent > actual &&
17+
test "log --format=%s --first-parent --exit_code=0" = "$(cat .git/post-command.out)" &&
18+
echo "a single log entry" >expect &&
19+
test_cmp expect actual
20+
'
21+
722
test_expect_success 'with no hook' '
823
echo "first" > file &&
924
git add file &&

0 commit comments

Comments
 (0)