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>
Junio C Hamano committed
Jun 29, 2016 at 10:22 UTC
33b842a1e9bf28bcffe953ad9dcdbb0561336314
2 files changed
+35
-4
builtin/pull.c
+18
-4
@@ -878,10 +878,24 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
878
if (merge_heads.nr > 1)
879
die(_("Cannot merge multiple branches into empty head."));
880
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
+ }
897
return run_rebase(curr_head, *merge_heads.sha1, rebase_fork_point);
885
- } else
898
+ } else {
899
return run_merge();
900
+ }
901
}
t/t5520-pull.sh
+17
@@ -237,6 +237,23 @@ test_expect_success '--rebase' '
237
test new = "$(git show HEAD:file2)"
238
'
239
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
+
257
test_expect_success '--rebase fails with multiple branches' '
258
git reset --hard before-rebase &&
259
test_must_fail git pull --rebase . copy master 2>err &&