Skip to content

Commit e379fdf

Browse files
committed
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 <[email protected]>
1 parent 937978e commit e379fdf

12 files changed

+57
-14
lines changed

Documentation/git-merge.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ commit or stash your changes before running 'git merge'.
9898
'git merge --abort' is equivalent to 'git reset --merge' when
9999
`MERGE_HEAD` is present.
100100

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+
101114
<commit>...::
102115
Commits, usually other branch heads, to merge into our branch.
103116
Specifying more than one commit will create a merge with

builtin/merge.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static int option_renormalize;
6464
static int verbosity;
6565
static int allow_rerere_auto;
6666
static int abort_current_merge;
67+
static int allow_unrelated_histories;
6768
static int show_progress = -1;
6869
static int default_to_upstream = 1;
6970
static const char *sign_commit;
@@ -221,6 +222,8 @@ static struct option builtin_merge_options[] = {
221222
OPT__VERBOSITY(&verbosity),
222223
OPT_BOOL(0, "abort", &abort_current_merge,
223224
N_("abort the current in-progress merge")),
225+
OPT_BOOL(0, "allow-unrelated-histories", &allow_unrelated_histories,
226+
N_("allow merging unrelated histories")),
224227
OPT_SET_INT(0, "progress", &show_progress, N_("force progress reporting"), 1),
225228
{ OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
226229
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)
13971400
update_ref("updating ORIG_HEAD", "ORIG_HEAD", head_commit->object.oid.hash,
13981401
NULL, 0, UPDATE_REFS_DIE_ON_ERR);
13991402

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 ||
14031409
(!remoteheads->next && !common->next &&
14041410
common->item == remoteheads->item)) {
14051411
/*

t/t3033-merge-toplevel.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ test_expect_success setup '
1919
test_commit three &&
2020
git checkout right &&
2121
test_commit four &&
22+
git checkout --orphan five &&
23+
test_commit five &&
2224
git checkout master
2325
'
2426

@@ -133,4 +135,18 @@ test_expect_success 'merge FETCH_HEAD octopus non-fast-forward' '
133135
test_cmp expect actual
134136
'
135137

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+
136152
test_done

t/t3412-rebase-root.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ test_expect_success 'set up second root and merge' '
133133
rm A B C &&
134134
test_commit 6 D &&
135135
git checkout other &&
136-
git merge third
136+
git merge --allow-unrelated-histories third
137137
'
138138

139139
cat > expect-third <<'EOF'

t/t5500-fetch-pack.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ test_expect_success 'clone shallow object count' '
259259
test_expect_success 'pull in shallow repo with missing merge base' '
260260
(
261261
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
263264
)
264265
'
265266

@@ -279,9 +280,10 @@ test_expect_success 'clone shallow depth count' '
279280
test_expect_success 'clone shallow object count' '
280281
(
281282
cd shallow &&
283+
git prune &&
282284
git count-objects -v
283285
) > count.shallow &&
284-
grep "^count: 55" count.shallow
286+
grep "^count: 54" count.shallow
285287
'
286288

287289
test_expect_success 'fetch --no-shallow on full repo' '

t/t6009-rev-list-parent.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ test_expect_success 'setup roots, merges and octopuses' '
4747
git checkout -b yetanotherbranch four &&
4848
test_commit eight &&
4949
git checkout master &&
50-
test_merge normalmerge newroot &&
50+
test_tick &&
51+
git merge --allow-unrelated-histories -m normalmerge newroot &&
52+
git tag normalmerge &&
5153
test_tick &&
5254
git merge -m tripus sidebranch anotherbranch &&
5355
git tag tripus &&

t/t6010-merge-base.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,13 @@ test_expect_success 'criss-cross merge-base for octopus-step' '
215215
git reset --hard E &&
216216
test_commit CC2 &&
217217
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 &&
219220
test_commit CC-o &&
220221
test_commit CCB &&
221222
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 &&
223225
test_commit CCA &&
224226
225227
git rev-parse CC1 CC2 >expected &&

t/t6012-rev-list-simplify.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ test_expect_success setup '
7171
note J &&
7272
7373
git checkout master &&
74-
test_tick && git merge -m "Coolest" unrelated &&
74+
test_tick && git merge --allow-unrelated-histories -m "Coolest" unrelated &&
7575
note K &&
7676
7777
echo "Immaterial" >elif &&

t/t6026-merge-attr.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ test_expect_success 'up-to-date merge without common ancestor' '
176176
test_tick &&
177177
(
178178
cd repo1 &&
179-
git pull ../repo2 master
179+
git fetch ../repo2 master &&
180+
git merge --allow-unrelated-histories FETCH_HEAD
180181
)
181182
'
182183

t/t6029-merge-subtree.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ test_expect_success 'setup' '
4949

5050
test_expect_success 'initial merge' '
5151
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 &&
5353
git read-tree --prefix=git-gui/ -u gui/master &&
5454
git commit -m "Merge git-gui as our subdirectory" &&
5555
git checkout -b work &&

t/t6101-rev-parse-parents.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test_expect_success 'setup' '
1919
git checkout --orphan tmp &&
2020
test_commit start2 &&
2121
git checkout master &&
22-
git merge -m next start2 &&
22+
git merge -m next --allow-unrelated-histories start2 &&
2323
test_commit final &&
2424
2525
test_seq 40 |

t/t9400-git-cvsserver-server.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ test_expect_success 'setup' '
4545
touch secondrootfile &&
4646
git add secondrootfile &&
4747
git commit -m "second root") &&
48-
git pull secondroot master &&
48+
git fetch secondroot master &&
49+
git merge --allow-unrelated-histories FETCH_HEAD &&
4950
git clone -q --bare "$WORKDIR/.git" "$SERVERDIR" >/dev/null 2>&1 &&
5051
GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true &&
5152
GIT_DIR="$SERVERDIR" git config gitcvs.logfile "$SERVERDIR/gitcvs.log" &&

0 commit comments

Comments
 (0)