pull: pass --allow-unrelated-histories to "git merge"

The previous commit said: We could add the same option to "git pull" and have it passed through to underlying "git merge". I do not have a fundamental opposition against such a feature, but this commit does not do so and instead leaves it as low-hanging fruit for others, because such a "two project merge" would be done after fetching the other project into some location in the working tree of an existing project and making sure how well they fit together, it is sufficient to allow a local merge without such an option pass-through from "git pull" to "git merge". Prepare a patch to make it a reality, just in case it is needed. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Mar 18, 2016 at 13:21 UTC 09c2cb877a9cc0f6f25eac8d43663f4e8f3309a4
4 files changed +36 -13
Documentation/git-merge.txt
+1 -13
@@ -11,6 +11,7 @@ SYNOPSIS
11 [verse]
12 'git merge' [-n] [--stat] [--no-commit] [--squash] [--[no-]edit]
13 [-s <strategy>] [-X <strategy-option>] [-S[<keyid>]]
14 + [--[no-]allow-unrelated-histories]
15 [--[no-]rerere-autoupdate] [-m <msg>] [<commit>...]
16 'git merge' <msg> HEAD <commit>...
17 'git merge' --abort
@@ -98,19 +99,6 @@ commit or stash your changes before running 'git merge'.
99 'git merge --abort' is equivalent to 'git reset --merge' when
100 `MERGE_HEAD` is present.
101
101 ---allow-unrelated-histories::
102 - By default, `git merge` command refuses to merge histories
103 - that do not share a common ancestor. This option can be
104 - used to override this safety when merging histories of two
105 - projects that started their lives independently. As that is
106 - a very rare occasion, no configuration variable to enable
107 - this by default exists and will not be added, and the list
108 - of options at the top of this documentation does not mention
109 - this option. Also `git pull` does not pass this option down
110 - to `git merge` (instead, you `git fetch` first, examine what
111 - you will be merging and then `git merge` locally with this
112 - option).
113 -
102 <commit>...::
103 Commits, usually other branch heads, to merge into our branch.
104 Specifying more than one commit will create a merge with
Documentation/merge-options.txt
+8
@@ -114,3 +114,11 @@ ifndef::git-pull[]
114 reporting.
115
116 endif::git-pull[]
117 +
118 +--allow-unrelated-histories::
119 + By default, `git merge` command refuses to merge histories
120 + that do not share a common ancestor. This option can be
121 + used to override this safety when merging histories of two
122 + projects that started their lives independently. As that is
123 + a very rare occasion, no configuration variable to enable
124 + this by default exists and will not be added.
builtin/pull.c
+6
@@ -86,6 +86,7 @@ static char *opt_verify_signatures;
86 static struct argv_array opt_strategies = ARGV_ARRAY_INIT;
87 static struct argv_array opt_strategy_opts = ARGV_ARRAY_INIT;
88 static char *opt_gpg_sign;
89 +static int opt_allow_unrelated_histories;
90
91 /* Options passed to git-fetch */
92 static char *opt_all;
@@ -155,6 +156,9 @@ static struct option pull_options[] = {
156 OPT_PASSTHRU('S', "gpg-sign", &opt_gpg_sign, N_("key-id"),
157 N_("GPG sign commit"),
158 PARSE_OPT_OPTARG),
159 + OPT_SET_INT(0, "allow-unrelated-histories",
160 + &opt_allow_unrelated_histories,
161 + N_("allow merging unrelated histories"), 1),
162
163 /* Options passed to git-fetch */
164 OPT_GROUP(N_("Options related to fetching")),
@@ -603,6 +607,8 @@ static int run_merge(void)
607 argv_array_pushv(&args, opt_strategy_opts.argv);
608 if (opt_gpg_sign)
609 argv_array_push(&args, opt_gpg_sign);
610 + if (opt_allow_unrelated_histories > 0)
611 + argv_array_push(&args, "--allow-unrelated-histories");
612
613 argv_array_push(&args, "FETCH_HEAD");
614 ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
t/t5521-pull-options.sh
+21
@@ -144,4 +144,25 @@ test_expect_success 'git pull --all --dry-run' '
144 )
145 '
146
147 +test_expect_success 'git pull --allow-unrelated-histories' '
148 + test_when_finished "rm -fr src dst" &&
149 + git init src &&
150 + (
151 + cd src &&
152 + test_commit one &&
153 + test_commit two
154 + ) &&
155 + git clone src dst &&
156 + (
157 + cd src &&
158 + git checkout --orphan side HEAD^ &&
159 + test_commit three
160 + ) &&
161 + (
162 + cd dst &&
163 + test_must_fail git pull ../src side &&
164 + git pull --allow-unrelated-histories ../src side
165 + )
166 +'
167 +
168 test_done