Skip to content

Commit 33b842a

Browse files
committed
pull: fast-forward "pull --rebase=true"
"git pull --rebase" always runs "git rebase" after fetching the commit to serve as the new base, even when the new base is a descendant of the current HEAD, i.e. we haven't done any work. In such a case, we can instead fast-forward to the new base without invoking the rebase process. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e465796 commit 33b842a

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
@@ -878,10 +878,24 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
878878
if (merge_heads.nr > 1)
879879
die(_("Cannot merge multiple branches into empty head."));
880880
return pull_into_void(*merge_heads.sha1, curr_head);
881-
} else if (opt_rebase) {
882-
if (merge_heads.nr > 1)
883-
die(_("Cannot rebase onto multiple branches."));
881+
}
882+
if (opt_rebase && merge_heads.nr > 1)
883+
die(_("Cannot rebase onto multiple branches."));
884+
885+
if (opt_rebase) {
886+
struct commit_list *list = NULL;
887+
struct commit *merge_head, *head;
888+
889+
head = lookup_commit_reference(orig_head);
890+
commit_list_insert(head, &list);
891+
merge_head = lookup_commit_reference(merge_heads.sha1[0]);
892+
if (is_descendant_of(merge_head, list)) {
893+
/* we can fast-forward this without invoking rebase */
894+
opt_ff = "--ff-only";
895+
return run_merge();
896+
}
884897
return run_rebase(curr_head, *merge_heads.sha1, rebase_fork_point);
885-
} else
898+
} else {
886899
return run_merge();
900+
}
887901
}

t/t5520-pull.sh

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

240+
test_expect_success '--rebase fast forward' '
241+
git reset --hard before-rebase &&
242+
git checkout -b ff &&
243+
echo another modification >file &&
244+
git commit -m third file &&
245+
246+
git checkout to-rebase &&
247+
git pull --rebase . ff &&
248+
test "$(git rev-parse HEAD)" = "$(git rev-parse ff)" &&
249+
250+
# The above only validates the result. Did we actually bypass rebase?
251+
git reflog -1 >reflog.actual &&
252+
sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
253+
echo "OBJID HEAD@{0}: pull --rebase . ff: Fast-forward" >reflog.expected &&
254+
test_cmp reflog.expected reflog.fuzzy
255+
'
256+
240257
test_expect_success '--rebase fails with multiple branches' '
241258
git reset --hard before-rebase &&
242259
test_must_fail git pull --rebase . copy master 2>err &&

0 commit comments

Comments
 (0)