Skip to content

Commit 2fb11ec

Browse files
committed
Merge branch 'jc/pull-rebase-ff'
"git pull --rebase", when there is no new commits on our side since we forked from the upstream, should be able to fast-forward without invoking "git rebase", but it didn't. * jc/pull-rebase-ff: pull: fast-forward "pull --rebase=true"
2 parents 101f3dc + 33b842a commit 2fb11ec

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

builtin/pull.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -857,10 +857,24 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
857857
if (merge_heads.nr > 1)
858858
die(_("Cannot merge multiple branches into empty head."));
859859
return pull_into_void(*merge_heads.sha1, curr_head);
860-
} else if (opt_rebase) {
861-
if (merge_heads.nr > 1)
862-
die(_("Cannot rebase onto multiple branches."));
860+
}
861+
if (opt_rebase && merge_heads.nr > 1)
862+
die(_("Cannot rebase onto multiple branches."));
863+
864+
if (opt_rebase) {
865+
struct commit_list *list = NULL;
866+
struct commit *merge_head, *head;
867+
868+
head = lookup_commit_reference(orig_head);
869+
commit_list_insert(head, &list);
870+
merge_head = lookup_commit_reference(merge_heads.sha1[0]);
871+
if (is_descendant_of(merge_head, list)) {
872+
/* we can fast-forward this without invoking rebase */
873+
opt_ff = "--ff-only";
874+
return run_merge();
875+
}
863876
return run_rebase(curr_head, *merge_heads.sha1, rebase_fork_point);
864-
} else
877+
} else {
865878
return run_merge();
879+
}
866880
}

t/t5520-pull.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,23 @@ test_expect_success '--rebase' '
255255
test new = "$(git show HEAD:file2)"
256256
'
257257

258+
test_expect_success '--rebase fast forward' '
259+
git reset --hard before-rebase &&
260+
git checkout -b ff &&
261+
echo another modification >file &&
262+
git commit -m third file &&
263+
264+
git checkout to-rebase &&
265+
git pull --rebase . ff &&
266+
test "$(git rev-parse HEAD)" = "$(git rev-parse ff)" &&
267+
268+
# The above only validates the result. Did we actually bypass rebase?
269+
git reflog -1 >reflog.actual &&
270+
sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
271+
echo "OBJID HEAD@{0}: pull --rebase . ff: Fast-forward" >reflog.expected &&
272+
test_cmp reflog.expected reflog.fuzzy
273+
'
274+
258275
test_expect_success '--rebase with conflicts shows advice' '
259276
test_when_finished "git rebase --abort; git checkout -f to-rebase" &&
260277
git checkout -b seq &&

0 commit comments

Comments
 (0)