blob: ed7866d3e955a3d093b6428b750f4d8d7ec1ae63 [file] [log] [blame]
Junio C Hamano9e623162015-04-29 13:14:50 -07001#!/bin/sh
2
3test_description='"git merge" top-level frontend'
4
Johannes Schindelin5902f5f2020-11-18 23:44:38 +00005GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00006export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
Jeff King716a6b22023-10-03 16:27:24 -04008TEST_PASSES_SANITIZE_LEAK=true
Junio C Hamano9e623162015-04-29 13:14:50 -07009. ./test-lib.sh
10
11t3033_reset () {
Johannes Schindelin5902f5f2020-11-18 23:44:38 +000012 git checkout -B main two &&
Junio C Hamano9e623162015-04-29 13:14:50 -070013 git branch -f left three &&
14 git branch -f right four
15}
16
17test_expect_success setup '
18 test_commit one &&
19 git branch left &&
20 git branch right &&
21 test_commit two &&
22 git checkout left &&
23 test_commit three &&
24 git checkout right &&
25 test_commit four &&
Junio C Hamanode224962016-04-21 11:52:33 -070026 git checkout --orphan newroot &&
Junio C Hamanoe379fdf2016-03-18 13:21:09 -070027 test_commit five &&
Johannes Schindelin5902f5f2020-11-18 23:44:38 +000028 git checkout main
Junio C Hamano9e623162015-04-29 13:14:50 -070029'
30
31# Local branches
32
33test_expect_success 'merge an octopus into void' '
34 t3033_reset &&
35 git checkout --orphan test &&
36 git rm -fr . &&
37 test_must_fail git merge left right &&
38 test_must_fail git rev-parse --verify HEAD &&
39 git diff --quiet &&
40 test_must_fail git rev-parse HEAD
41'
42
43test_expect_success 'merge an octopus, fast-forward (ff)' '
44 t3033_reset &&
45 git reset --hard one &&
46 git merge left right &&
47 # one is ancestor of three (left) and four (right)
48 test_must_fail git rev-parse --verify HEAD^3 &&
49 git rev-parse HEAD^1 HEAD^2 | sort >actual &&
50 git rev-parse three four | sort >expect &&
51 test_cmp expect actual
52'
53
54test_expect_success 'merge octopus, non-fast-forward (ff)' '
55 t3033_reset &&
56 git reset --hard one &&
57 git merge --no-ff left right &&
58 # one is ancestor of three (left) and four (right)
59 test_must_fail git rev-parse --verify HEAD^4 &&
60 git rev-parse HEAD^1 HEAD^2 HEAD^3 | sort >actual &&
61 git rev-parse one three four | sort >expect &&
62 test_cmp expect actual
63'
64
65test_expect_success 'merge octopus, fast-forward (does not ff)' '
66 t3033_reset &&
67 git merge left right &&
Johannes Schindelin5902f5f2020-11-18 23:44:38 +000068 # two (main) is not an ancestor of three (left) and four (right)
Junio C Hamano9e623162015-04-29 13:14:50 -070069 test_must_fail git rev-parse --verify HEAD^4 &&
70 git rev-parse HEAD^1 HEAD^2 HEAD^3 | sort >actual &&
71 git rev-parse two three four | sort >expect &&
72 test_cmp expect actual
73'
74
75test_expect_success 'merge octopus, non-fast-forward' '
76 t3033_reset &&
77 git merge --no-ff left right &&
78 test_must_fail git rev-parse --verify HEAD^4 &&
79 git rev-parse HEAD^1 HEAD^2 HEAD^3 | sort >actual &&
80 git rev-parse two three four | sort >expect &&
81 test_cmp expect actual
82'
83
84# The same set with FETCH_HEAD
85
Junio C Hamano74e8bc52015-04-25 18:47:21 -070086test_expect_success 'merge FETCH_HEAD octopus into void' '
Junio C Hamano9e623162015-04-29 13:14:50 -070087 t3033_reset &&
88 git checkout --orphan test &&
89 git rm -fr . &&
90 git fetch . left right &&
91 test_must_fail git merge FETCH_HEAD &&
92 test_must_fail git rev-parse --verify HEAD &&
93 git diff --quiet &&
94 test_must_fail git rev-parse HEAD
95'
96
Junio C Hamano74e8bc52015-04-25 18:47:21 -070097test_expect_success 'merge FETCH_HEAD octopus fast-forward (ff)' '
Junio C Hamano9e623162015-04-29 13:14:50 -070098 t3033_reset &&
99 git reset --hard one &&
100 git fetch . left right &&
101 git merge FETCH_HEAD &&
102 # one is ancestor of three (left) and four (right)
103 test_must_fail git rev-parse --verify HEAD^3 &&
104 git rev-parse HEAD^1 HEAD^2 | sort >actual &&
105 git rev-parse three four | sort >expect &&
106 test_cmp expect actual
107'
108
Junio C Hamano74e8bc52015-04-25 18:47:21 -0700109test_expect_success 'merge FETCH_HEAD octopus non-fast-forward (ff)' '
Junio C Hamano9e623162015-04-29 13:14:50 -0700110 t3033_reset &&
111 git reset --hard one &&
112 git fetch . left right &&
113 git merge --no-ff FETCH_HEAD &&
114 # one is ancestor of three (left) and four (right)
115 test_must_fail git rev-parse --verify HEAD^4 &&
116 git rev-parse HEAD^1 HEAD^2 HEAD^3 | sort >actual &&
117 git rev-parse one three four | sort >expect &&
118 test_cmp expect actual
119'
120
Junio C Hamano74e8bc52015-04-25 18:47:21 -0700121test_expect_success 'merge FETCH_HEAD octopus fast-forward (does not ff)' '
Junio C Hamano9e623162015-04-29 13:14:50 -0700122 t3033_reset &&
123 git fetch . left right &&
124 git merge FETCH_HEAD &&
Johannes Schindelin5902f5f2020-11-18 23:44:38 +0000125 # two (main) is not an ancestor of three (left) and four (right)
Junio C Hamano9e623162015-04-29 13:14:50 -0700126 test_must_fail git rev-parse --verify HEAD^4 &&
127 git rev-parse HEAD^1 HEAD^2 HEAD^3 | sort >actual &&
128 git rev-parse two three four | sort >expect &&
129 test_cmp expect actual
130'
131
Junio C Hamano74e8bc52015-04-25 18:47:21 -0700132test_expect_success 'merge FETCH_HEAD octopus non-fast-forward' '
Junio C Hamano9e623162015-04-29 13:14:50 -0700133 t3033_reset &&
134 git fetch . left right &&
135 git merge --no-ff FETCH_HEAD &&
136 test_must_fail git rev-parse --verify HEAD^4 &&
137 git rev-parse HEAD^1 HEAD^2 HEAD^3 | sort >actual &&
138 git rev-parse two three four | sort >expect &&
139 test_cmp expect actual
140'
141
Junio C Hamanoe379fdf2016-03-18 13:21:09 -0700142# two-project merge
143test_expect_success 'refuse two-project merge by default' '
144 t3033_reset &&
145 git reset --hard four &&
146 test_must_fail git merge five
147'
148
Denton Liua03b5552020-04-07 10:28:07 -0400149test_expect_success 'refuse two-project merge by default, quit before --autostash happens' '
150 t3033_reset &&
151 git reset --hard four &&
152 echo change >>one.t &&
153 git diff >expect &&
154 test_must_fail git merge --autostash five 2>err &&
Junio C Hamano67892752023-10-31 14:23:30 +0900155 test_grep ! "stash" err &&
Denton Liua03b5552020-04-07 10:28:07 -0400156 git diff >actual &&
157 test_cmp expect actual
158'
159
Junio C Hamanoe379fdf2016-03-18 13:21:09 -0700160test_expect_success 'two-project merge with --allow-unrelated-histories' '
161 t3033_reset &&
162 git reset --hard four &&
163 git merge --allow-unrelated-histories five &&
164 git diff --exit-code five
165'
166
Denton Liua03b5552020-04-07 10:28:07 -0400167test_expect_success 'two-project merge with --allow-unrelated-histories with --autostash' '
168 t3033_reset &&
169 git reset --hard four &&
170 echo change >>one.t &&
171 git diff one.t >expect &&
172 git merge --allow-unrelated-histories --autostash five 2>err &&
Junio C Hamano67892752023-10-31 14:23:30 +0900173 test_grep "Applied autostash." err &&
Denton Liua03b5552020-04-07 10:28:07 -0400174 git diff one.t >actual &&
175 test_cmp expect actual
176'
177
Junio C Hamano9e623162015-04-29 13:14:50 -0700178test_done