From 69f4c23009ee30faeb61a831f4265791c945783e Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 26 Jan 2022 13:05:38 +0000 Subject: rebase: pass correct arguments to post-checkout hook If a rebase started with "rebase [--apply|--merge] " detects that is an ancestor of then it fast-forwards and checks out . 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 Signed-off-by: Junio C Hamano --- reset.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'reset.c') diff --git a/reset.c b/reset.c index f214df3d96..315fef91d3 100644 --- a/reset.c +++ b/reset.c @@ -18,7 +18,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; unsigned refs_only = flags & RESET_HEAD_REFS_ONLY; unsigned update_orig_head = flags & RESET_ORIG_HEAD; - struct object_id head_oid; + struct object_id *head = NULL, head_oid; struct tree_desc desc[2] = { { NULL }, { NULL } }; struct lock_file lock = LOCK_INIT; 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, const char *reflog_action; struct strbuf msg = STRBUF_INIT; size_t prefix_len; - struct object_id *orig = NULL, oid_orig, - *old_orig = NULL, oid_old_orig; + struct object_id *old_orig = NULL, oid_old_orig; int ret = 0, nr = 0; 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, goto leave_reset_head; } - if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) { + if (!get_oid("HEAD", &head_oid)) { + head = &head_oid; + } else if (!oid || !reset_hard) { ret = error(_("could not determine HEAD revision")); goto leave_reset_head; } @@ -98,13 +99,12 @@ reset_head_refs: if (update_orig_head) { if (!get_oid("ORIG_HEAD", &oid_old_orig)) old_orig = &oid_old_orig; - if (!get_oid("HEAD", &oid_orig)) { - orig = &oid_orig; + if (head) { if (!reflog_orig_head) { strbuf_addstr(&msg, "updating ORIG_HEAD"); reflog_orig_head = msg.buf; } - update_ref(reflog_orig_head, "ORIG_HEAD", orig, + update_ref(reflog_orig_head, "ORIG_HEAD", head, old_orig, 0, UPDATE_REFS_MSG_ON_ERR); } else if (old_orig) delete_ref(NULL, "ORIG_HEAD", old_orig, 0); @@ -116,7 +116,7 @@ reset_head_refs: reflog_head = msg.buf; } if (!switch_to_branch) - ret = update_ref(reflog_head, "HEAD", oid, orig, + ret = update_ref(reflog_head, "HEAD", oid, head, detach_head ? REF_NO_DEREF : 0, UPDATE_REFS_MSG_ON_ERR); else { @@ -128,7 +128,7 @@ reset_head_refs: } if (run_hook) run_hook_le(NULL, "post-checkout", - oid_to_hex(orig ? orig : null_oid()), + oid_to_hex(head ? head : null_oid()), oid_to_hex(oid), "1", NULL); leave_reset_head: -- cgit v1.2.3 From ab2fba0868860e610619f885031217d9cc63097a Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 26 Jan 2022 13:05:39 +0000 Subject: rebase: do not remove untracked files on checkout If "git rebase [--apply|--merge] " detects that is an ancestor of then it will fast-forward and checkout . Normally a checkout or picking a commit during a rebase will refuse to overwrite untracked files, however rebase does overwrite untracked files when checking out . The fix is to only set reset in `unpack_tree_opts` if flags contains `RESET_HEAD_HARD`. t5403 may seem like an odd home for the new test but it will be extended in the next commit to check that the post-checkout hook is not run when the checkout fails. The test for `!detach_head` dates back to the original implementation of reset_head() in ac7f467fef ("builtin/rebase: support running "git rebase "", 2018-08-07) and was correct until e65123a71d ("builtin rebase: support `git rebase `", 2018-09-04) started using reset_head() to checkout when fast-forwarding. Note that 480d3d6bf9 ("Change unpack_trees' 'reset' flag into an enum", 2021-09-27) also fixes this bug as it changes reset_head() to never remove untracked files. I think this fix is still worthwhile as it makes it clear that the same settings are used for detached and non-detached checkouts. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano --- reset.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'reset.c') diff --git a/reset.c b/reset.c index 315fef91d3..3e7b9e2e13 100644 --- a/reset.c +++ b/reset.c @@ -59,7 +59,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, unpack_tree_opts.merge = 1; unpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */ init_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL); - if (!detach_head) + if (reset_hard) unpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED; if (repo_read_index_unmerged(r) < 0) { -- cgit v1.2.3 From 4840002a5f44a4c256c55f70c59d3b0506d14e21 Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 26 Jan 2022 13:05:40 +0000 Subject: 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 Signed-off-by: Junio C Hamano --- reset.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'reset.c') diff --git a/reset.c b/reset.c index 3e7b9e2e13..3537de91f6 100644 --- a/reset.c +++ b/reset.c @@ -126,7 +126,7 @@ reset_head_refs: ret = create_symref("HEAD", switch_to_branch, reflog_head); } - if (run_hook) + if (!ret && run_hook) run_hook_le(NULL, "post-checkout", oid_to_hex(head ? head : null_oid()), oid_to_hex(oid), "1", NULL); -- cgit v1.2.3 From 1946d45844c65ede4e3a514a5decf16612ad79f0 Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 26 Jan 2022 13:05:41 +0000 Subject: reset_head(): remove action parameter The only use of the action parameter is to setup the error messages for unpack_trees(). All but two cases pass either "checkout" or "reset". The case that passes "reset --hard" would be better passing "reset" so that the error messages match the builtin reset command like all the other callers that are doing a reset. The case that passes "Fast-forwarded" is only updating HEAD and so the parameter is unused in that case as it does not call unpack_trees(). The value to pass to setup_unpack_trees_porcelain() can be determined by checking whether flags contains RESET_HEAD_HARD without the caller having to specify it. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano --- reset.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'reset.c') diff --git a/reset.c b/reset.c index 3537de91f6..7841b2b2a0 100644 --- a/reset.c +++ b/reset.c @@ -8,7 +8,7 @@ #include "tree.h" #include "unpack-trees.h" -int reset_head(struct repository *r, struct object_id *oid, const char *action, +int reset_head(struct repository *r, struct object_id *oid, const char *switch_to_branch, unsigned flags, const char *reflog_orig_head, const char *reflog_head, const char *default_reflog_action) @@ -23,7 +23,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, struct lock_file lock = LOCK_INIT; struct unpack_trees_options unpack_tree_opts = { 0 }; struct tree *tree; - const char *reflog_action; + const char *action, *reflog_action; struct strbuf msg = STRBUF_INIT; size_t prefix_len; struct object_id *old_orig = NULL, oid_old_orig; @@ -50,6 +50,7 @@ int reset_head(struct repository *r, struct object_id *oid, const char *action, if (refs_only) goto reset_head_refs; + action = reset_hard ? "reset" : "checkout"; setup_unpack_trees_porcelain(&unpack_tree_opts, action); unpack_tree_opts.head_idx = 1; unpack_tree_opts.src_index = r->index; -- cgit v1.2.3 From d6a9f5ea8e97dd9435e5fa02cc129c1b241934f2 Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 26 Jan 2022 13:05:42 +0000 Subject: reset_head(): factor out ref updates In the next commit we will stop trying to update HEAD when we are removing uncommitted changes from the working tree. Move the code that updates the refs to its own function in preparation for that. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano --- reset.c | 110 ++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 62 insertions(+), 48 deletions(-) (limited to 'reset.c') diff --git a/reset.c b/reset.c index 7841b2b2a0..56d6e2a06d 100644 --- a/reset.c +++ b/reset.c @@ -8,25 +8,75 @@ #include "tree.h" #include "unpack-trees.h" +static int update_refs(const struct object_id *oid, const char *switch_to_branch, + const struct object_id *head, const char *reflog_head, + const char *reflog_orig_head, + const char *default_reflog_action, unsigned flags) +{ + unsigned detach_head = flags & RESET_HEAD_DETACH; + unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; + unsigned update_orig_head = flags & RESET_ORIG_HEAD; + struct object_id *old_orig = NULL, oid_old_orig; + struct strbuf msg = STRBUF_INIT; + const char *reflog_action; + size_t prefix_len; + int ret; + + reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); + strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : default_reflog_action); + prefix_len = msg.len; + + if (update_orig_head) { + if (!get_oid("ORIG_HEAD", &oid_old_orig)) + old_orig = &oid_old_orig; + if (head) { + if (!reflog_orig_head) { + strbuf_addstr(&msg, "updating ORIG_HEAD"); + reflog_orig_head = msg.buf; + } + update_ref(reflog_orig_head, "ORIG_HEAD", head, + old_orig, 0, UPDATE_REFS_MSG_ON_ERR); + } else if (old_orig) + delete_ref(NULL, "ORIG_HEAD", old_orig, 0); + } + + if (!reflog_head) { + strbuf_setlen(&msg, prefix_len); + strbuf_addstr(&msg, "updating HEAD"); + reflog_head = msg.buf; + } + if (!switch_to_branch) + ret = update_ref(reflog_head, "HEAD", oid, head, + detach_head ? REF_NO_DEREF : 0, + UPDATE_REFS_MSG_ON_ERR); + else { + ret = update_ref(reflog_head, switch_to_branch, oid, + NULL, 0, UPDATE_REFS_MSG_ON_ERR); + if (!ret) + ret = create_symref("HEAD", switch_to_branch, + reflog_head); + } + if (!ret && run_hook) + run_hook_le(NULL, "post-checkout", + oid_to_hex(head ? head : null_oid()), + oid_to_hex(oid), "1", NULL); + strbuf_release(&msg); + return ret; +} + int reset_head(struct repository *r, struct object_id *oid, const char *switch_to_branch, unsigned flags, const char *reflog_orig_head, const char *reflog_head, const char *default_reflog_action) { - unsigned detach_head = flags & RESET_HEAD_DETACH; unsigned reset_hard = flags & RESET_HEAD_HARD; - unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; unsigned refs_only = flags & RESET_HEAD_REFS_ONLY; - unsigned update_orig_head = flags & RESET_ORIG_HEAD; struct object_id *head = NULL, head_oid; struct tree_desc desc[2] = { { NULL }, { NULL } }; struct lock_file lock = LOCK_INIT; struct unpack_trees_options unpack_tree_opts = { 0 }; struct tree *tree; - const char *action, *reflog_action; - struct strbuf msg = STRBUF_INIT; - size_t prefix_len; - struct object_id *old_orig = NULL, oid_old_orig; + const char *action; int ret = 0, nr = 0; if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) @@ -48,7 +98,9 @@ int reset_head(struct repository *r, struct object_id *oid, oid = &head_oid; if (refs_only) - goto reset_head_refs; + return update_refs(oid, switch_to_branch, head, reflog_head, + reflog_orig_head, default_reflog_action, + flags); action = reset_hard ? "reset" : "checkout"; setup_unpack_trees_porcelain(&unpack_tree_opts, action); @@ -92,48 +144,10 @@ int reset_head(struct repository *r, struct object_id *oid, goto leave_reset_head; } -reset_head_refs: - reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); - strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : default_reflog_action); - prefix_len = msg.len; - - if (update_orig_head) { - if (!get_oid("ORIG_HEAD", &oid_old_orig)) - old_orig = &oid_old_orig; - if (head) { - if (!reflog_orig_head) { - strbuf_addstr(&msg, "updating ORIG_HEAD"); - reflog_orig_head = msg.buf; - } - update_ref(reflog_orig_head, "ORIG_HEAD", head, - old_orig, 0, UPDATE_REFS_MSG_ON_ERR); - } else if (old_orig) - delete_ref(NULL, "ORIG_HEAD", old_orig, 0); - } - - if (!reflog_head) { - strbuf_setlen(&msg, prefix_len); - strbuf_addstr(&msg, "updating HEAD"); - reflog_head = msg.buf; - } - if (!switch_to_branch) - ret = update_ref(reflog_head, "HEAD", oid, head, - detach_head ? REF_NO_DEREF : 0, - UPDATE_REFS_MSG_ON_ERR); - else { - ret = update_ref(reflog_head, switch_to_branch, oid, - NULL, 0, UPDATE_REFS_MSG_ON_ERR); - if (!ret) - ret = create_symref("HEAD", switch_to_branch, - reflog_head); - } - if (!ret && run_hook) - run_hook_le(NULL, "post-checkout", - oid_to_hex(head ? head : null_oid()), - oid_to_hex(oid), "1", NULL); + ret = update_refs(oid, switch_to_branch, head, reflog_head, + reflog_orig_head, default_reflog_action, flags); leave_reset_head: - strbuf_release(&msg); rollback_lock_file(&lock); clear_unpack_trees_porcelain(&unpack_tree_opts); while (nr) -- cgit v1.2.3 From 1526d0fcfd20efca24bc96a4bc14c8d5459ec470 Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 26 Jan 2022 13:05:43 +0000 Subject: reset_head(): make default_reflog_action optional This parameter is only needed when a ref is going to be updated and the caller does not pass an explicit reflog message. Callers that are only discarding uncommitted changes in the working tree such as such as "rebase --skip" or create_autostash() do not update any refs so should not have to worry about passing this parameter. This change is not intended to have any user visible changes. The pointer comparison between `oid` and `&head_oid` checks that the caller did not pass an oid to be checked out. As no callers pass RESET_HEAD_RUN_POST_CHECKOUT_HOOK without passing an oid there are no changes to when the post-checkout hook is run. As update_ref() only updates the ref if the oid passed to it differs from the current ref there are no changes to when HEAD is updated. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano --- reset.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'reset.c') diff --git a/reset.c b/reset.c index 56d6e2a06d..4a92e4bc30 100644 --- a/reset.c +++ b/reset.c @@ -22,8 +22,13 @@ static int update_refs(const struct object_id *oid, const char *switch_to_branch size_t prefix_len; int ret; - reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); - strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : default_reflog_action); + if ((update_orig_head && !reflog_orig_head) || !reflog_head) { + if (!default_reflog_action) + BUG("default_reflog_action must be given when reflog messages are omitted"); + reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); + strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : + default_reflog_action); + } prefix_len = msg.len; if (update_orig_head) { @@ -71,6 +76,7 @@ int reset_head(struct repository *r, struct object_id *oid, { unsigned reset_hard = flags & RESET_HEAD_HARD; unsigned refs_only = flags & RESET_HEAD_REFS_ONLY; + unsigned update_orig_head = flags & RESET_ORIG_HEAD; struct object_id *head = NULL, head_oid; struct tree_desc desc[2] = { { NULL }, { NULL } }; struct lock_file lock = LOCK_INIT; @@ -144,8 +150,10 @@ int reset_head(struct repository *r, struct object_id *oid, goto leave_reset_head; } - ret = update_refs(oid, switch_to_branch, head, reflog_head, - reflog_orig_head, default_reflog_action, flags); + if (oid != &head_oid || update_orig_head || switch_to_branch) + ret = update_refs(oid, switch_to_branch, head, reflog_head, + reflog_orig_head, default_reflog_action, + flags); leave_reset_head: rollback_lock_file(&lock); -- cgit v1.2.3 From 6ae8086161d81a707ff36dfdc07f57e4f473e0fd Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 26 Jan 2022 13:05:46 +0000 Subject: reset_head(): take struct rebase_head_opts This function takes a confusingly large number of parameters which makes it difficult to remember which order to pass them in. The following commits will add a couple more parameters which makes the problem worse. To address this change the function to take a struct of options. Using a struct means that it is no longer necessary to remember which order to pass the parameters in and anyone reading the code can easily see which value is passed to each parameter. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano --- reset.c | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'reset.c') diff --git a/reset.c b/reset.c index 4a92e4bc30..78145d5c45 100644 --- a/reset.c +++ b/reset.c @@ -8,14 +8,17 @@ #include "tree.h" #include "unpack-trees.h" -static int update_refs(const struct object_id *oid, const char *switch_to_branch, - const struct object_id *head, const char *reflog_head, - const char *reflog_orig_head, - const char *default_reflog_action, unsigned flags) +static int update_refs(const struct reset_head_opts *opts, + const struct object_id *oid, + const struct object_id *head) { - unsigned detach_head = flags & RESET_HEAD_DETACH; - unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; - unsigned update_orig_head = flags & RESET_ORIG_HEAD; + unsigned detach_head = opts->flags & RESET_HEAD_DETACH; + unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; + unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; + const char *switch_to_branch = opts->branch; + const char *reflog_head = opts->head_msg; + const char *reflog_orig_head = opts->orig_head_msg; + const char *default_reflog_action = opts->default_reflog_action; struct object_id *old_orig = NULL, oid_old_orig; struct strbuf msg = STRBUF_INIT; const char *reflog_action; @@ -69,14 +72,13 @@ static int update_refs(const struct object_id *oid, const char *switch_to_branch return ret; } -int reset_head(struct repository *r, struct object_id *oid, - const char *switch_to_branch, unsigned flags, - const char *reflog_orig_head, const char *reflog_head, - const char *default_reflog_action) +int reset_head(struct repository *r, const struct reset_head_opts *opts) { - unsigned reset_hard = flags & RESET_HEAD_HARD; - unsigned refs_only = flags & RESET_HEAD_REFS_ONLY; - unsigned update_orig_head = flags & RESET_ORIG_HEAD; + const struct object_id *oid = opts->oid; + const char *switch_to_branch = opts->branch; + unsigned reset_hard = opts->flags & RESET_HEAD_HARD; + unsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY; + unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; struct object_id *head = NULL, head_oid; struct tree_desc desc[2] = { { NULL }, { NULL } }; struct lock_file lock = LOCK_INIT; @@ -104,9 +106,7 @@ int reset_head(struct repository *r, struct object_id *oid, oid = &head_oid; if (refs_only) - return update_refs(oid, switch_to_branch, head, reflog_head, - reflog_orig_head, default_reflog_action, - flags); + return update_refs(opts, oid, head); action = reset_hard ? "reset" : "checkout"; setup_unpack_trees_porcelain(&unpack_tree_opts, action); @@ -151,9 +151,7 @@ int reset_head(struct repository *r, struct object_id *oid, } if (oid != &head_oid || update_orig_head || switch_to_branch) - ret = update_refs(oid, switch_to_branch, head, reflog_head, - reflog_orig_head, default_reflog_action, - flags); + ret = update_refs(opts, oid, head); leave_reset_head: rollback_lock_file(&lock); -- cgit v1.2.3 From 7700ab087b82f71d19134141045b95063e407344 Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 26 Jan 2022 13:05:47 +0000 Subject: rebase --apply: fix reflog move_to_original_branch() passes the message intended for the branch reflog as `orig_head_msg`. Fix this by adding a `branch_msg` member to struct reset_head_opts and add a regression test. Note that these reflog messages do not respect GIT_REFLOG_ACTION. They are not alone in that and will be fixed in a future series. The "merge" backend already has tests that check both the branch and HEAD reflogs. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano --- reset.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'reset.c') diff --git a/reset.c b/reset.c index 78145d5c45..e02915c0f6 100644 --- a/reset.c +++ b/reset.c @@ -16,6 +16,7 @@ static int update_refs(const struct reset_head_opts *opts, unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; const char *switch_to_branch = opts->branch; + const char *reflog_branch = opts->branch_msg; const char *reflog_head = opts->head_msg; const char *reflog_orig_head = opts->orig_head_msg; const char *default_reflog_action = opts->default_reflog_action; @@ -58,8 +59,9 @@ static int update_refs(const struct reset_head_opts *opts, detach_head ? REF_NO_DEREF : 0, UPDATE_REFS_MSG_ON_ERR); else { - ret = update_ref(reflog_head, switch_to_branch, oid, - NULL, 0, UPDATE_REFS_MSG_ON_ERR); + ret = update_ref(reflog_branch ? reflog_branch : reflog_head, + switch_to_branch, oid, NULL, 0, + UPDATE_REFS_MSG_ON_ERR); if (!ret) ret = create_symref("HEAD", switch_to_branch, reflog_head); @@ -90,6 +92,12 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts) if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) BUG("Not a fully qualified branch: '%s'", switch_to_branch); + if (opts->orig_head_msg && !update_orig_head) + BUG("ORIG_HEAD reflog message given without updating ORIG_HEAD"); + + if (opts->branch_msg && !opts->branch) + BUG("branch reflog message given without a branch"); + if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) { ret = -1; goto leave_reset_head; -- cgit v1.2.3 From cd1528ef8ef9847fc27cff4016bf073f04729504 Mon Sep 17 00:00:00 2001 From: Phillip Wood Date: Wed, 26 Jan 2022 13:05:48 +0000 Subject: rebase --apply: set ORIG_HEAD correctly At the start of a rebase, ORIG_HEAD is updated to the tip of the branch being rebased. Unfortunately reset_head() always uses the current value of HEAD for this which is incorrect if the rebase is started with "git rebase " as in that case ORIG_HEAD should be updated to . This only affects the "apply" backend as the "merge" backend does not yet use reset_head() for the initial checkout. Fix this by passing in orig_head when calling reset_head() and add some regression tests. Signed-off-by: Phillip Wood Signed-off-by: Junio C Hamano --- reset.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'reset.c') diff --git a/reset.c b/reset.c index e02915c0f6..448cb3fd78 100644 --- a/reset.c +++ b/reset.c @@ -15,6 +15,7 @@ static int update_refs(const struct reset_head_opts *opts, unsigned detach_head = opts->flags & RESET_HEAD_DETACH; unsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; unsigned update_orig_head = opts->flags & RESET_ORIG_HEAD; + const struct object_id *orig_head = opts->orig_head; const char *switch_to_branch = opts->branch; const char *reflog_branch = opts->branch_msg; const char *reflog_head = opts->head_msg; @@ -43,7 +44,8 @@ static int update_refs(const struct reset_head_opts *opts, strbuf_addstr(&msg, "updating ORIG_HEAD"); reflog_orig_head = msg.buf; } - update_ref(reflog_orig_head, "ORIG_HEAD", head, + update_ref(reflog_orig_head, "ORIG_HEAD", + orig_head ? orig_head : head, old_orig, 0, UPDATE_REFS_MSG_ON_ERR); } else if (old_orig) delete_ref(NULL, "ORIG_HEAD", old_orig, 0); -- cgit v1.2.3