pull --rebase: compute rebase arguments in separate function

The function 'run_rebase' is responsible for constructing the command line to be passed to 'git rebase'. This includes both forwarding pass-through options given to 'git pull' as well computing the <newbase> and <upstream> arguments to 'git rebase'. A following commit will need to access the <upstream> argument in 'cmd_pull' to fix a bug with 'git pull --rebase --recurse-submodules'. In order to do so, refactor the code so that the <newbase> and <upstream> commits are computed in a new, separate function, 'get_rebase_newbase_and_upstream'. Signed-off-by: Philippe Blain <levraiphilippeblain@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Philippe Blain committed Nov 14, 2020 at 00:34 UTC 4f66d79ae301a74cead51e703d566ec5f395beb8
1 file changed +35 -11
builtin/pull.c
+35 -11
@@ -852,21 +852,42 @@ static int get_octopus_merge_base(struct object_id *merge_base,
852
853 /**
854 * Given the current HEAD oid, the merge head returned from git-fetch and the
855 - * fork point calculated by get_rebase_fork_point(), runs git-rebase with the
856 - * appropriate arguments and returns its exit status.
855 + * fork point calculated by get_rebase_fork_point(), compute the <newbase> and
856 + * <upstream> arguments to use for the upcoming git-rebase invocation.
857 */
858 -static int run_rebase(const struct object_id *curr_head,
858 +static int get_rebase_newbase_and_upstream(struct object_id *newbase,
859 + struct object_id *upstream,
860 + const struct object_id *curr_head,
861 const struct object_id *merge_head,
862 const struct object_id *fork_point)
863 {
862 - int ret;
864 struct object_id oct_merge_base;
864 - struct strvec args = STRVEC_INIT;
865
866 if (!get_octopus_merge_base(&oct_merge_base, curr_head, merge_head, fork_point))
867 if (!is_null_oid(fork_point) && oideq(&oct_merge_base, fork_point))
868 fork_point = NULL;
869
870 + if (fork_point && !is_null_oid(fork_point))
871 + oidcpy(upstream, fork_point);
872 + else
873 + oidcpy(upstream, merge_head);
874 +
875 + oidcpy(newbase, merge_head);
876 +
877 + return 0;
878 +}
879 +
880 +/**
881 + * Given the <newbase> and <upstream> calculated by
882 + * get_rebase_newbase_and_upstream(), runs git-rebase with the
883 + * appropriate arguments and returns its exit status.
884 + */
885 +static int run_rebase(const struct object_id *newbase,
886 + const struct object_id *upstream)
887 +{
888 + int ret;
889 + struct strvec args = STRVEC_INIT;
890 +
891 strvec_push(&args, "rebase");
892
893 /* Shared options */
@@ -894,12 +915,9 @@ static int run_rebase(const struct object_id *curr_head,
915 warning(_("ignoring --verify-signatures for rebase"));
916
917 strvec_push(&args, "--onto");
897 - strvec_push(&args, oid_to_hex(merge_head));
918 + strvec_push(&args, oid_to_hex(newbase));
919
899 - if (fork_point && !is_null_oid(fork_point))
900 - strvec_push(&args, oid_to_hex(fork_point));
901 - else
902 - strvec_push(&args, oid_to_hex(merge_head));
920 + strvec_push(&args, oid_to_hex(upstream));
921
922 ret = run_command_v_opt(args.v, RUN_GIT_CMD);
923 strvec_clear(&args);
@@ -1011,6 +1029,12 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
1029 if (opt_rebase) {
1030 int ret = 0;
1031 int ran_ff = 0;
1032 +
1033 + struct object_id newbase;
1034 + struct object_id upstream;
1035 + get_rebase_newbase_and_upstream(&newbase, &upstream, &curr_head,
1036 + merge_heads.oid, &rebase_fork_point);
1037 +
1038 if ((recurse_submodules == RECURSE_SUBMODULES_ON ||
1039 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) &&
1040 submodule_touches_in_range(the_repository, &rebase_fork_point, &curr_head))
@@ -1034,7 +1058,7 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
1058 free_commit_list(list);
1059 }
1060 if (!ran_ff)
1037 - ret = run_rebase(&curr_head, merge_heads.oid, &rebase_fork_point);
1061 + ret = run_rebase(&newbase, &upstream);
1062
1063 if (!ret && (recurse_submodules == RECURSE_SUBMODULES_ON ||
1064 recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND))