merge: refuse to create too cool a merge by default

While it makes sense to allow merging unrelated histories of two projects that started independently into one, in the way "gitk" was merged to "git" itself aka "the coolest merge ever", such a merge is still an unusual event. Worse, if somebody creates an independent history by starting from a tarball of an established project and sends a pull request to the original project, "git merge" however happily creates such a merge without any sign of something unusual is happening. Teach "git merge" to refuse to create such a merge by default, unless the user passes a new "--allow-unrelated-histories" option to tell it that the user is aware that two unrelated projects are merged. Because such a "two project merge" is a rare event, a configuration option to always allow such a merge is not added. 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". Many tests that are updated by this patch does the pass-through manually by turning: git pull something into its equivalent: git fetch something && git merge --allow-unrelated-histories FETCH_HEAD If somebody is inclined to add such an option, updated tests in this change need to be adjusted back to: git pull --allow-unrelated-histories something Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Mar 18, 2016 at 13:21 UTC e379fdf34fee96cd205be83ff4e71699bdc32b18
12 files changed +57 -14
Documentation/git-merge.txt
+13
@@ -98,6 +98,19 @@ commit or stash your changes before running 'git merge'.
98 'git merge --abort' is equivalent to 'git reset --merge' when
99 `MERGE_HEAD` is present.
100
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 +
114 <commit>...::
115 Commits, usually other branch heads, to merge into our branch.
116 Specifying more than one commit will create a merge with
builtin/merge.c
+9 -3
@@ -64,6 +64,7 @@ static int option_renormalize;
64 static int verbosity;
65 static int allow_rerere_auto;
66 static int abort_current_merge;
67 +static int allow_unrelated_histories;
68 static int show_progress = -1;
69 static int default_to_upstream = 1;
70 static const char *sign_commit;
@@ -221,6 +222,8 @@ static struct option builtin_merge_options[] = {
222 OPT__VERBOSITY(&verbosity),
223 OPT_BOOL(0, "abort", &abort_current_merge,
224 N_("abort the current in-progress merge")),
225 + OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories,
226 + N_("allow merging unrelated histories")),
227 OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1),
228 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
229 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
@@ -1397,9 +1400,12 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1400 update_ref("updating ORIG_HEAD", "ORIG_HEAD", head_commit->object.oid.hash,
1401 NULL, 0, UPDATE_REFS_DIE_ON_ERR);
1402
1400 - if (remoteheads && !common)
1401 - ; /* No common ancestors found. We need a real merge. */
1402 - else if (!remoteheads ||
1403 + if (remoteheads && !common) {
1404 + /* No common ancestors found. */
1405 + if (!allow_unrelated_histories)
1406 + die(_("refusing to merge unrelated histories"));
1407 + /* otherwise, we need a real merge. */
1408 + } else if (!remoteheads ||
1409 (!remoteheads->next && !common->next &&
1410 common->item == remoteheads->item)) {
1411 /*
t/t3033-merge-toplevel.sh
+16
@@ -19,6 +19,8 @@ test_expect_success setup '
19 test_commit three &&
20 git checkout right &&
21 test_commit four &&
22 + git checkout --orphan five &&
23 + test_commit five &&
24 git checkout master
25 '
26
@@ -133,4 +135,18 @@ test_expect_success 'merge FETCH_HEAD octopus non-fast-forward' '
135 test_cmp expect actual
136 '
137
138 +# two-project merge
139 +test_expect_success 'refuse two-project merge by default' '
140 + t3033_reset &&
141 + git reset --hard four &&
142 + test_must_fail git merge five
143 +'
144 +
145 +test_expect_success 'two-project merge with --allow-unrelated-histories' '
146 + t3033_reset &&
147 + git reset --hard four &&
148 + git merge --allow-unrelated-histories five &&
149 + git diff --exit-code five
150 +'
151 +
152 test_done
t/t3412-rebase-root.sh
+1 -1
@@ -133,7 +133,7 @@ test_expect_success 'set up second root and merge' '
133 rm A B C &&
134 test_commit 6 D &&
135 git checkout other &&
136 - git merge third
136 + git merge --allow-unrelated-histories third
137 '
138
139 cat > expect-third <<'EOF'
t/t5500-fetch-pack.sh
+4 -2
@@ -259,7 +259,8 @@ test_expect_success 'clone shallow object count' '
259 test_expect_success 'pull in shallow repo with missing merge base' '
260 (
261 cd shallow &&
262 - test_must_fail git pull --depth 4 .. A
262 + git fetch --depth 4 .. A
263 + test_must_fail git merge --allow-unrelated-histories FETCH_HEAD
264 )
265 '
266
@@ -279,9 +280,10 @@ test_expect_success 'clone shallow depth count' '
280 test_expect_success 'clone shallow object count' '
281 (
282 cd shallow &&
283 + git prune &&
284 git count-objects -v
285 ) > count.shallow &&
284 - grep "^count: 55" count.shallow
286 + grep "^count: 54" count.shallow
287 '
288
289 test_expect_success 'fetch --no-shallow on full repo' '
t/t6009-rev-list-parent.sh
+3 -1
@@ -47,7 +47,9 @@ test_expect_success 'setup roots, merges and octopuses' '
47 git checkout -b yetanotherbranch four &&
48 test_commit eight &&
49 git checkout master &&
50 - test_merge normalmerge newroot &&
50 + test_tick &&
51 + git merge --allow-unrelated-histories -m normalmerge newroot &&
52 + git tag normalmerge &&
53 test_tick &&
54 git merge -m tripus sidebranch anotherbranch &&
55 git tag tripus &&
t/t6010-merge-base.sh
+4 -2
@@ -215,11 +215,13 @@ test_expect_success 'criss-cross merge-base for octopus-step' '
215 git reset --hard E &&
216 test_commit CC2 &&
217 test_tick &&
218 - git merge -s ours CC1 &&
218 + # E is a root commit unrelated to MMR root on which CC1 is based
219 + git merge -s ours --allow-unrelated-histories CC1 &&
220 test_commit CC-o &&
221 test_commit CCB &&
222 git reset --hard CC1 &&
222 - git merge -s ours CC2 &&
223 + # E is a root commit unrelated to MMR root on which CC1 is based
224 + git merge -s ours --allow-unrelated-histories CC2 &&
225 test_commit CCA &&
226
227 git rev-parse CC1 CC2 >expected &&
t/t6012-rev-list-simplify.sh
+1 -1
@@ -71,7 +71,7 @@ test_expect_success setup '
71 note J &&
72
73 git checkout master &&
74 - test_tick && git merge -m "Coolest" unrelated &&
74 + test_tick && git merge --allow-unrelated-histories -m "Coolest" unrelated &&
75 note K &&
76
77 echo "Immaterial" >elif &&
t/t6026-merge-attr.sh
+2 -1
@@ -176,7 +176,8 @@ test_expect_success 'up-to-date merge without common ancestor' '
176 test_tick &&
177 (
178 cd repo1 &&
179 - git pull ../repo2 master
179 + git fetch ../repo2 master &&
180 + git merge --allow-unrelated-histories FETCH_HEAD
181 )
182 '
183
t/t6029-merge-subtree.sh
+1 -1
@@ -49,7 +49,7 @@ test_expect_success 'setup' '
49
50 test_expect_success 'initial merge' '
51 git remote add -f gui ../git-gui &&
52 - git merge -s ours --no-commit gui/master &&
52 + git merge -s ours --no-commit --allow-unrelated-histories gui/master &&
53 git read-tree --prefix=git-gui/ -u gui/master &&
54 git commit -m "Merge git-gui as our subdirectory" &&
55 git checkout -b work &&
t/t6101-rev-parse-parents.sh
+1 -1
@@ -19,7 +19,7 @@ test_expect_success 'setup' '
19 git checkout --orphan tmp &&
20 test_commit start2 &&
21 git checkout master &&
22 - git merge -m next start2 &&
22 + git merge -m next --allow-unrelated-histories start2 &&
23 test_commit final &&
24
25 test_seq 40 |
t/t9400-git-cvsserver-server.sh
+2 -1
@@ -45,7 +45,8 @@ test_expect_success 'setup' '
45 touch secondrootfile &&
46 git add secondrootfile &&
47 git commit -m "second root") &&
48 - git pull secondroot master &&
48 + git fetch secondroot master &&
49 + git merge --allow-unrelated-histories FETCH_HEAD &&
50 git clone -q --bare "$WORKDIR/.git" "$SERVERDIR" >/dev/null 2>&1 &&
51 GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true &&
52 GIT_DIR="$SERVERDIR" git config gitcvs.logfile "$SERVERDIR/gitcvs.log" &&