Skip to content

built-in rebase: pick up the ORIG_HEAD fix early #2112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions builtin/rebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ static void add_var(struct strbuf *buf, const char *name, const char *value)
#define RESET_HEAD_HARD (1<<1)
#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
#define RESET_HEAD_REFS_ONLY (1<<3)
#define RESET_ORIG_HEAD (1<<4)

static int reset_head(struct object_id *oid, const char *action,
const char *switch_to_branch, unsigned flags,
Expand All @@ -378,6 +379,7 @@ static int reset_head(struct object_id *oid, const char *action,
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_oid;
struct tree_desc desc[2] = { { NULL }, { NULL } };
struct lock_file lock = LOCK_INIT;
Expand Down Expand Up @@ -454,18 +456,21 @@ static int reset_head(struct object_id *oid, const char *action,
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
prefix_len = msg.len;

if (!get_oid("ORIG_HEAD", &oid_old_orig))
old_orig = &oid_old_orig;
if (!get_oid("HEAD", &oid_orig)) {
orig = &oid_orig;
if (!reflog_orig_head) {
strbuf_addstr(&msg, "updating ORIG_HEAD");
reflog_orig_head = msg.buf;
}
update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
UPDATE_REFS_MSG_ON_ERR);
} else if (old_orig)
delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
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 (!reflog_orig_head) {
strbuf_addstr(&msg, "updating ORIG_HEAD");
reflog_orig_head = msg.buf;
}
update_ref(reflog_orig_head, "ORIG_HEAD", orig,
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");
Expand All @@ -476,7 +481,7 @@ static int reset_head(struct object_id *oid, const char *action,
detach_head ? REF_NO_DEREF : 0,
UPDATE_REFS_MSG_ON_ERR);
else {
ret = update_ref(reflog_orig_head, switch_to_branch, oid,
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,
Expand Down Expand Up @@ -1760,8 +1765,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
strbuf_addf(&msg, "%s: checkout %s",
getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
if (reset_head(&options.onto->object.oid, "checkout", NULL,
RESET_HEAD_DETACH | RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
NULL, msg.buf))
RESET_HEAD_DETACH | RESET_HEAD_RUN_POST_CHECKOUT_HOOK |
RESET_ORIG_HEAD, NULL, msg.buf))
die(_("Could not detach HEAD"));
strbuf_release(&msg);

Expand All @@ -1776,8 +1781,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
strbuf_addf(&msg, "rebase finished: %s onto %s",
options.head_name ? options.head_name : "detached HEAD",
oid_to_hex(&options.onto->object.oid));
reset_head(NULL, "Fast-forwarded", options.head_name, 0,
"HEAD", msg.buf);
reset_head(NULL, "Fast-forwarded", options.head_name,
RESET_HEAD_REFS_ONLY, "HEAD", msg.buf);
strbuf_release(&msg);
ret = !!finish_rebase(&options);
goto cleanup;
Expand Down
8 changes: 8 additions & 0 deletions t/t3400-rebase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ test_expect_success 'rebase against master' '
git rebase master
'

test_expect_success 'rebase sets ORIG_HEAD to pre-rebase state' '
git checkout -b orig-head topic &&
pre="$(git rev-parse --verify HEAD)" &&
git rebase master &&
test_cmp_rev "$pre" ORIG_HEAD &&
! test_cmp_rev "$pre" HEAD
'

test_expect_success 'rebase, with <onto> and <upstream> specified as :/quuxery' '
test_when_finished "git branch -D torebase" &&
git checkout -b torebase my-topic-branch^ &&
Expand Down