Skip to content

Commit 7976721

Browse files
newrengitster
authored andcommitted
merge-tree: add a --allow-unrelated-histories flag
Folks may want to merge histories that have no common ancestry; provide a flag with the same name as used by `git merge` to allow this. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7c48b27 commit 7976721

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

Documentation/git-merge-tree.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ OPTIONS
5959
default is to include these messages if there are merge
6060
conflicts, and to omit them otherwise.
6161

62+
--allow-unrelated-histories::
63+
merge-tree will by default error out if the two branches specified
64+
share no common history. This flag can be given to override that
65+
check and make the merge proceed anyway.
66+
6267
[[OUTPUT]]
6368
OUTPUT
6469
------

builtin/merge-tree.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ enum mode {
399399

400400
struct merge_tree_options {
401401
int mode;
402+
int allow_unrelated_histories;
402403
int show_messages;
403404
int name_only;
404405
};
@@ -434,7 +435,7 @@ static int real_merge(struct merge_tree_options *o,
434435
* merge_incore_recursive in merge-ort.h
435436
*/
436437
merge_bases = get_merge_bases(parent1, parent2);
437-
if (!merge_bases)
438+
if (!merge_bases && !o->allow_unrelated_histories)
438439
die(_("refusing to merge unrelated histories"));
439440
merge_bases = reverse_commit_list(merge_bases);
440441

@@ -500,6 +501,10 @@ int cmd_merge_tree(int argc, const char **argv, const char *prefix)
500501
&o.name_only,
501502
N_("list filenames without modes/oids/stages"),
502503
PARSE_OPT_NONEG),
504+
OPT_BOOL_F(0, "allow-unrelated-histories",
505+
&o.allow_unrelated_histories,
506+
N_("allow merging unrelated histories"),
507+
PARSE_OPT_NONEG),
503508
OPT_END()
504509
};
505510

t/t4301-merge-tree-write-tree.sh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ test_expect_success setup '
4444
git checkout side3 &&
4545
git mv numbers sequence &&
4646
test_tick &&
47-
git commit -m rename-numbers
47+
git commit -m rename-numbers &&
48+
49+
git switch --orphan unrelated &&
50+
>something-else &&
51+
git add something-else &&
52+
test_tick &&
53+
git commit -m first-commit
4854
'
4955

5056
test_expect_success 'Clean merge' '
@@ -215,4 +221,20 @@ test_expect_success 'NUL terminated conflicted file "lines"' '
215221
test_cmp expect actual
216222
'
217223

224+
test_expect_success 'error out by default for unrelated histories' '
225+
test_expect_code 128 git merge-tree --write-tree side1 unrelated 2>error &&
226+
227+
grep "refusing to merge unrelated histories" error
228+
'
229+
230+
test_expect_success 'can override merge of unrelated histories' '
231+
git merge-tree --write-tree --allow-unrelated-histories side1 unrelated >tree &&
232+
TREE=$(cat tree) &&
233+
234+
git rev-parse side1:numbers side1:greeting side1:whatever unrelated:something-else >expect &&
235+
git rev-parse $TREE:numbers $TREE:greeting $TREE:whatever $TREE:something-else >actual &&
236+
237+
test_cmp expect actual
238+
'
239+
218240
test_done

0 commit comments

Comments
 (0)