rebase --apply: don't run post-checkout hook if there is an error

The hook should only be run if the worktree and refs were successfully updated. This primarily affects "rebase --apply" but also "rebase --merge" when it fast-forwards. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Jan 26, 2022 at 13:05 UTC 4840002a5f44a4c256c55f70c59d3b0506d14e21
2 files changed +6 -2
reset.c
+1 -1
@@ -126,7 +126,7 @@ reset_head_refs:
126 ret = create_symref("HEAD", switch_to_branch,
127 reflog_head);
128 }
129 - if (run_hook)
129 + if (!ret && run_hook)
130 run_hook_le(NULL, "post-checkout",
131 oid_to_hex(head ? head : null_oid()),
132 oid_to_hex(oid), "1", NULL);
t/t5403-post-checkout-hook.sh
+5 -1
@@ -88,12 +88,16 @@ test_rebase () {
88
89 test_expect_success "rebase $args checkout does not remove untracked files" '
90 test_when_finished "test_might_fail git rebase --abort" &&
91 + test_when_finished "rm -f .git/post-checkout.args" &&
92 git update-ref refs/heads/rebase-fast-forward three &&
93 git checkout two &&
94 + rm -f .git/post-checkout.args &&
95 echo untracked >three.t &&
96 test_when_finished "rm three.t" &&
97 test_must_fail git rebase $args HEAD rebase-fast-forward 2>err &&
96 - grep "untracked working tree files would be overwritten by checkout" err
98 + grep "untracked working tree files would be overwritten by checkout" err &&
99 + test_path_is_missing .git/post-checkout.args
100 +
101 '
102 }
103