rebase: pass correct arguments to post-checkout hook

If a rebase started with "rebase [--apply|--merge] <upstream> <branch>" detects that <upstream> is an ancestor of <branch> then it fast-forwards and checks out <branch>. Unfortunately in that case it passed the null oid as the first argument to the post-checkout hook rather than the oid of HEAD. A side effect of this change is that the call to update_ref() which updates HEAD now always receives the old value of HEAD. This provides protection against another process updating HEAD during the checkout. 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 69f4c23009ee30faeb61a831f4265791c945783e
2 files changed +22 -9
reset.c
+9 -9
@@ -18,7 +18,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action,
18 unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
19 unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
20 unsigned update_orig_head = flags & RESET_ORIG_HEAD;
21 - struct object_id head_oid;
21 + struct object_id *head = NULL, head_oid;
22 struct tree_desc desc[2] = { { NULL }, { NULL } };
23 struct lock_file lock = LOCK_INIT;
24 struct unpack_trees_options unpack_tree_opts = { 0 };
@@ -26,8 +26,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action,
26 const char *reflog_action;
27 struct strbuf msg = STRBUF_INIT;
28 size_t prefix_len;
29 - struct object_id *orig = NULL, oid_orig,
30 - *old_orig = NULL, oid_old_orig;
29 + struct object_id *old_orig = NULL, oid_old_orig;
30 int ret = 0, nr = 0;
31
32 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
@@ -38,7 +37,9 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action,
37 goto leave_reset_head;
38 }
39
41 - if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
40 + if (!get_oid("HEAD", &head_oid)) {
41 + head = &head_oid;
42 + } else if (!oid || !reset_hard) {
43 ret = error(_("could not determine HEAD revision"));
44 goto leave_reset_head;
45 }
@@ -98,13 +99,12 @@ reset_head_refs:
99 if (update_orig_head) {
100 if (!get_oid("ORIG_HEAD", &oid_old_orig))
101 old_orig = &oid_old_orig;
101 - if (!get_oid("HEAD", &oid_orig)) {
102 - orig = &oid_orig;
102 + if (head) {
103 if (!reflog_orig_head) {
104 strbuf_addstr(&msg, "updating ORIG_HEAD");
105 reflog_orig_head = msg.buf;
106 }
107 - update_ref(reflog_orig_head, "ORIG_HEAD", orig,
107 + update_ref(reflog_orig_head, "ORIG_HEAD", head,
108 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
109 } else if (old_orig)
110 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
@@ -116,7 +116,7 @@ reset_head_refs:
116 reflog_head = msg.buf;
117 }
118 if (!switch_to_branch)
119 - ret = update_ref(reflog_head, "HEAD", oid, orig,
119 + ret = update_ref(reflog_head, "HEAD", oid, head,
120 detach_head ? REF_NO_DEREF : 0,
121 UPDATE_REFS_MSG_ON_ERR);
122 else {
@@ -128,7 +128,7 @@ reset_head_refs:
128 }
129 if (run_hook)
130 run_hook_le(NULL, "post-checkout",
131 - oid_to_hex(orig ? orig : null_oid()),
131 + oid_to_hex(head ? head : null_oid()),
132 oid_to_hex(oid), "1", NULL);
133
134 leave_reset_head:
t/t5403-post-checkout-hook.sh
+13
@@ -72,6 +72,19 @@ test_rebase () {
72 test_cmp_rev rebase-on-me $new &&
73 test $flag = 1
74 '
75 +
76 + test_expect_success "rebase $args fast-forward branch checkout runs post-checkout hook" '
77 + test_when_finished "test_might_fail git rebase --abort" &&
78 + test_when_finished "rm -f .git/post-checkout.args" &&
79 + git update-ref refs/heads/rebase-fast-forward three &&
80 + git checkout two &&
81 + rm -f .git/post-checkout.args &&
82 + git rebase $args HEAD rebase-fast-forward &&
83 + read old new flag <.git/post-checkout.args &&
84 + test_cmp_rev two $old &&
85 + test_cmp_rev three $new &&
86 + test $flag = 1
87 + '
88 }
89
90 test_rebase --apply &&