Skip to content

Commit d42c9ff

Browse files
phillipwoodgitster
authored andcommitted
rebase: factor out branch_base calculation
Separate out calculating the merge base between 'onto' and 'HEAD' from the check for whether we can fast-forward or not. This means we can skip the fast-forward checks when the rebase is forced and avoid calculating the merge-base between 'HEAD' and 'onto' when --keep-base is given. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a770602 commit d42c9ff

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

builtin/rebase.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -865,13 +865,9 @@ static int can_fast_forward(struct commit *onto, struct commit *upstream,
865865
struct commit_list *merge_bases = NULL;
866866
int res = 0;
867867

868-
merge_bases = get_merge_bases(onto, head);
869-
if (!merge_bases || merge_bases->next) {
870-
oidcpy(branch_base, null_oid());
871-
goto done;
872-
}
868+
if (is_null_oid(branch_base))
869+
goto done; /* fill_branch_base() found multiple merge bases */
873870

874-
oidcpy(branch_base, &merge_bases->item->object.oid);
875871
if (!oideq(branch_base, &onto->object.oid))
876872
goto done;
877873

@@ -881,7 +877,6 @@ static int can_fast_forward(struct commit *onto, struct commit *upstream,
881877
if (!upstream)
882878
goto done;
883879

884-
free_commit_list(merge_bases);
885880
merge_bases = get_merge_bases(upstream, head);
886881
if (!merge_bases || merge_bases->next)
887882
goto done;
@@ -896,6 +891,20 @@ static int can_fast_forward(struct commit *onto, struct commit *upstream,
896891
return res && is_linear_history(onto, head);
897892
}
898893

894+
static void fill_branch_base(struct rebase_options *options,
895+
struct object_id *branch_base)
896+
{
897+
struct commit_list *merge_bases = NULL;
898+
899+
merge_bases = get_merge_bases(options->onto, options->orig_head);
900+
if (!merge_bases || merge_bases->next)
901+
oidcpy(branch_base, null_oid());
902+
else
903+
oidcpy(branch_base, &merge_bases->item->object.oid);
904+
905+
free_commit_list(merge_bases);
906+
}
907+
899908
static int parse_opt_am(const struct option *opt, const char *arg, int unset)
900909
{
901910
struct rebase_options *opts = opt->value;
@@ -1660,6 +1669,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
16601669
if (!options.onto)
16611670
die(_("Does not point to a valid commit '%s'"),
16621671
options.onto_name);
1672+
fill_branch_base(&options, &branch_base);
16631673
}
16641674

16651675
if (options.fork_point > 0)
@@ -1689,13 +1699,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
16891699
* Check if we are already based on onto with linear history,
16901700
* in which case we could fast-forward without replacing the commits
16911701
* with new commits recreated by replaying their changes.
1692-
*
1693-
* Note that can_fast_forward() initializes branch_base, so we have to
1694-
* call it before checking allow_preemptive_ff.
16951702
*/
1696-
if (can_fast_forward(options.onto, options.upstream, options.restrict_revision,
1697-
options.orig_head, &branch_base) &&
1698-
allow_preemptive_ff) {
1703+
if (allow_preemptive_ff &&
1704+
can_fast_forward(options.onto, options.upstream, options.restrict_revision,
1705+
options.orig_head, &branch_base)) {
16991706
int flag;
17001707

17011708
if (!(options.flags & REBASE_FORCE)) {

0 commit comments

Comments
 (0)