Skip to content

Commit cbd29ea

Browse files
dschogitster
authored andcommitted
built-in rebase: set ORIG_HEAD just once, before the rebase
Technically, the scripted version set ORIG_HEAD only in two spots (which really could have been one, because it called `git checkout $onto^0` to start the rebase and also if it could take a shortcut, and in both cases it called `git update-ref $orig_head`). Practically, it *implicitly* reset ORIG_HEAD whenever `git reset --hard` was called. However, what we really want is that it is set exactly once, at the beginning of the rebase. So let's do that. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c2d9629 commit cbd29ea

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

builtin/rebase.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ static void add_var(struct strbuf *buf, const char *name, const char *value)
369369
#define RESET_HEAD_DETACH (1<<0)
370370
#define RESET_HEAD_HARD (1<<1)
371371
#define RESET_HEAD_REFS_ONLY (1<<2)
372+
#define RESET_ORIG_HEAD (1<<3)
372373

373374
static int reset_head(struct object_id *oid, const char *action,
374375
const char *switch_to_branch, unsigned flags,
@@ -377,6 +378,7 @@ static int reset_head(struct object_id *oid, const char *action,
377378
unsigned detach_head = flags & RESET_HEAD_DETACH;
378379
unsigned reset_hard = flags & RESET_HEAD_HARD;
379380
unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
381+
unsigned update_orig_head = flags & RESET_ORIG_HEAD;
380382
struct object_id head_oid;
381383
struct tree_desc desc[2] = { { NULL }, { NULL } };
382384
struct lock_file lock = LOCK_INIT;
@@ -453,18 +455,21 @@ static int reset_head(struct object_id *oid, const char *action,
453455
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
454456
prefix_len = msg.len;
455457

456-
if (!get_oid("ORIG_HEAD", &oid_old_orig))
457-
old_orig = &oid_old_orig;
458-
if (!get_oid("HEAD", &oid_orig)) {
459-
orig = &oid_orig;
460-
if (!reflog_orig_head) {
461-
strbuf_addstr(&msg, "updating ORIG_HEAD");
462-
reflog_orig_head = msg.buf;
463-
}
464-
update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
465-
UPDATE_REFS_MSG_ON_ERR);
466-
} else if (old_orig)
467-
delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
458+
if (update_orig_head) {
459+
if (!get_oid("ORIG_HEAD", &oid_old_orig))
460+
old_orig = &oid_old_orig;
461+
if (!get_oid("HEAD", &oid_orig)) {
462+
orig = &oid_orig;
463+
if (!reflog_orig_head) {
464+
strbuf_addstr(&msg, "updating ORIG_HEAD");
465+
reflog_orig_head = msg.buf;
466+
}
467+
update_ref(reflog_orig_head, "ORIG_HEAD", orig,
468+
old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
469+
} else if (old_orig)
470+
delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
471+
}
472+
468473
if (!reflog_head) {
469474
strbuf_setlen(&msg, prefix_len);
470475
strbuf_addstr(&msg, "updating HEAD");
@@ -1725,7 +1730,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
17251730
strbuf_addf(&msg, "%s: checkout %s",
17261731
getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
17271732
if (reset_head(&options.onto->object.oid, "checkout", NULL,
1728-
RESET_HEAD_DETACH, NULL, msg.buf))
1733+
RESET_HEAD_DETACH | RESET_ORIG_HEAD, NULL, msg.buf))
17291734
die(_("Could not detach HEAD"));
17301735
strbuf_release(&msg);
17311736

t/t3400-rebase.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ test_expect_success 'rebase against master' '
5959
git rebase master
6060
'
6161

62-
test_expect_failure 'rebase sets ORIG_HEAD to pre-rebase state' '
62+
test_expect_success 'rebase sets ORIG_HEAD to pre-rebase state' '
6363
git checkout -b orig-head topic &&
6464
pre="$(git rev-parse --verify HEAD)" &&
6565
git rebase master &&

0 commit comments

Comments
 (0)