Skip to content

Commit 7e57a28

Browse files
committed
Merge pull request #2112 from dscho/gfw/rebase-am-and-orig-head
built-in rebase: pick up the ORIG_HEAD fix early
2 parents 40349f4 + a3d1fde commit 7e57a28

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

builtin/rebase.c

Lines changed: 22 additions & 17 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_HARD (1<<1)
370370
#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
371371
#define RESET_HEAD_REFS_ONLY (1<<3)
372+
#define RESET_ORIG_HEAD (1<<4)
372373

373374
static int reset_head(struct object_id *oid, const char *action,
374375
const char *switch_to_branch, unsigned flags,
@@ -378,6 +379,7 @@ static int reset_head(struct object_id *oid, const char *action,
378379
unsigned reset_hard = flags & RESET_HEAD_HARD;
379380
unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
380381
unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
382+
unsigned update_orig_head = flags & RESET_ORIG_HEAD;
381383
struct object_id head_oid;
382384
struct tree_desc desc[2] = { { NULL }, { NULL } };
383385
struct lock_file lock = LOCK_INIT;
@@ -454,18 +456,21 @@ static int reset_head(struct object_id *oid, const char *action,
454456
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
455457
prefix_len = msg.len;
456458

457-
if (!get_oid("ORIG_HEAD", &oid_old_orig))
458-
old_orig = &oid_old_orig;
459-
if (!get_oid("HEAD", &oid_orig)) {
460-
orig = &oid_orig;
461-
if (!reflog_orig_head) {
462-
strbuf_addstr(&msg, "updating ORIG_HEAD");
463-
reflog_orig_head = msg.buf;
464-
}
465-
update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
466-
UPDATE_REFS_MSG_ON_ERR);
467-
} else if (old_orig)
468-
delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
459+
if (update_orig_head) {
460+
if (!get_oid("ORIG_HEAD", &oid_old_orig))
461+
old_orig = &oid_old_orig;
462+
if (!get_oid("HEAD", &oid_orig)) {
463+
orig = &oid_orig;
464+
if (!reflog_orig_head) {
465+
strbuf_addstr(&msg, "updating ORIG_HEAD");
466+
reflog_orig_head = msg.buf;
467+
}
468+
update_ref(reflog_orig_head, "ORIG_HEAD", orig,
469+
old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
470+
} else if (old_orig)
471+
delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
472+
}
473+
469474
if (!reflog_head) {
470475
strbuf_setlen(&msg, prefix_len);
471476
strbuf_addstr(&msg, "updating HEAD");
@@ -476,7 +481,7 @@ static int reset_head(struct object_id *oid, const char *action,
476481
detach_head ? REF_NO_DEREF : 0,
477482
UPDATE_REFS_MSG_ON_ERR);
478483
else {
479-
ret = update_ref(reflog_orig_head, switch_to_branch, oid,
484+
ret = update_ref(reflog_head, switch_to_branch, oid,
480485
NULL, 0, UPDATE_REFS_MSG_ON_ERR);
481486
if (!ret)
482487
ret = create_symref("HEAD", switch_to_branch,
@@ -1760,8 +1765,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
17601765
strbuf_addf(&msg, "%s: checkout %s",
17611766
getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
17621767
if (reset_head(&options.onto->object.oid, "checkout", NULL,
1763-
RESET_HEAD_DETACH | RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1764-
NULL, msg.buf))
1768+
RESET_HEAD_DETACH | RESET_HEAD_RUN_POST_CHECKOUT_HOOK |
1769+
RESET_ORIG_HEAD, NULL, msg.buf))
17651770
die(_("Could not detach HEAD"));
17661771
strbuf_release(&msg);
17671772

@@ -1776,8 +1781,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
17761781
strbuf_addf(&msg, "rebase finished: %s onto %s",
17771782
options.head_name ? options.head_name : "detached HEAD",
17781783
oid_to_hex(&options.onto->object.oid));
1779-
reset_head(NULL, "Fast-forwarded", options.head_name, 0,
1780-
"HEAD", msg.buf);
1784+
reset_head(NULL, "Fast-forwarded", options.head_name,
1785+
RESET_HEAD_REFS_ONLY, "HEAD", msg.buf);
17811786
strbuf_release(&msg);
17821787
ret = !!finish_rebase(&options);
17831788
goto cleanup;

t/t3400-rebase.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ test_expect_success 'rebase against master' '
5959
git rebase master
6060
'
6161

62+
test_expect_success 'rebase sets ORIG_HEAD to pre-rebase state' '
63+
git checkout -b orig-head topic &&
64+
pre="$(git rev-parse --verify HEAD)" &&
65+
git rebase master &&
66+
test_cmp_rev "$pre" ORIG_HEAD &&
67+
! test_cmp_rev "$pre" HEAD
68+
'
69+
6270
test_expect_success 'rebase, with <onto> and <upstream> specified as :/quuxery' '
6371
test_when_finished "git branch -D torebase" &&
6472
git checkout -b torebase my-topic-branch^ &&

0 commit comments

Comments
 (0)