Merge branch 'cp/git-flush-is-an-env-bool'
[alt-git.git] / t / t3207-branch-submodule.sh
blobfe72b247164e56fc9a7af134583e2b9f8cc9bbdc
1 #!/bin/sh
3 test_description='git branch submodule tests'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . ./test-lib.sh
9 . "$TEST_DIRECTORY"/lib-rebase.sh
11 pwd=$(pwd)
13 # Creates a clean test environment in "pwd" by copying the repo setup
14 # from test_dirs.
15 reset_test () {
16 rm -fr super &&
17 rm -fr sub-sub-upstream &&
18 rm -fr sub-upstream &&
19 cp -r test_dirs/* .
22 # Tests that the expected branch does not exist
23 test_no_branch () {
24 DIR=$1 &&
25 BRANCH_NAME=$2 &&
26 test_must_fail git -C "$DIR" rev-parse "$BRANCH_NAME" 2>err &&
27 grep "ambiguous argument .$BRANCH_NAME." err
30 test_expect_success 'setup superproject and submodule' '
31 git config --global protocol.file.allow always &&
32 mkdir test_dirs &&
34 cd test_dirs &&
35 git init super &&
36 test_commit -C super foo &&
37 git init sub-sub-upstream &&
38 test_commit -C sub-sub-upstream foo &&
39 git init sub-upstream &&
40 # Submodule in a submodule
41 git -C sub-upstream submodule add "${pwd}/test_dirs/sub-sub-upstream" sub-sub &&
42 git -C sub-upstream commit -m "add submodule" &&
43 # Regular submodule
44 git -C super submodule add "${pwd}/test_dirs/sub-upstream" sub &&
45 # Submodule in a subdirectory
46 git -C super submodule add "${pwd}/test_dirs/sub-sub-upstream" second/sub &&
47 git -C super commit -m "add submodule" &&
48 git -C super config submodule.propagateBranches true &&
49 git -C super/sub submodule update --init
50 ) &&
51 reset_test
54 # Test the argument parsing
55 test_expect_success '--recurse-submodules should create branches' '
56 test_when_finished "reset_test" &&
58 cd super &&
59 git branch --recurse-submodules branch-a &&
60 git rev-parse branch-a &&
61 git -C sub rev-parse branch-a &&
62 git -C sub/sub-sub rev-parse branch-a &&
63 git -C second/sub rev-parse branch-a
67 test_expect_success '--recurse-submodules should die if submodule.propagateBranches is false' '
68 test_when_finished "reset_test" &&
70 cd super &&
71 echo "fatal: branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled" >expected &&
72 test_must_fail git -c submodule.propagateBranches=false branch --recurse-submodules branch-a 2>actual &&
73 test_cmp expected actual
77 test_expect_success '--recurse-submodules should fail when not creating branches' '
78 test_when_finished "reset_test" &&
80 cd super &&
81 git branch --recurse-submodules branch-a &&
82 echo "fatal: --recurse-submodules can only be used to create branches" >expected &&
83 test_must_fail git branch --recurse-submodules -D branch-a 2>actual &&
84 test_cmp expected actual &&
85 # Assert that the branches were not deleted
86 git rev-parse branch-a &&
87 git -C sub rev-parse branch-a
91 test_expect_success 'should respect submodule.recurse when creating branches' '
92 test_when_finished "reset_test" &&
94 cd super &&
95 git -c submodule.recurse=true branch branch-a &&
96 git rev-parse branch-a &&
97 git -C sub rev-parse branch-a
101 test_expect_success 'should ignore submodule.recurse when not creating branches' '
102 test_when_finished "reset_test" &&
104 cd super &&
105 git branch --recurse-submodules branch-a &&
106 git -c submodule.recurse=true branch -D branch-a &&
107 test_no_branch . branch-a &&
108 git -C sub rev-parse branch-a
112 # Test branch creation behavior
113 test_expect_success 'should create branches based off commit id in superproject' '
114 test_when_finished "reset_test" &&
116 cd super &&
117 git branch --recurse-submodules branch-a &&
118 git checkout --recurse-submodules branch-a &&
119 git -C sub rev-parse HEAD >expected &&
120 # Move the tip of sub:branch-a so that it no longer matches the commit in super:branch-a
121 git -C sub checkout branch-a &&
122 test_commit -C sub bar &&
123 # Create a new branch-b branch with start-point=branch-a
124 git branch --recurse-submodules branch-b branch-a &&
125 git rev-parse branch-b &&
126 git -C sub rev-parse branch-b >actual &&
127 # Assert that the commit id of sub:second-branch matches super:branch-a and not sub:branch-a
128 test_cmp expected actual
132 test_expect_success 'should not create any branches if branch is not valid for all repos' '
133 test_when_finished "reset_test" &&
135 cd super &&
136 git -C sub branch branch-a &&
137 test_must_fail git branch --recurse-submodules branch-a 2>actual &&
138 test_no_branch . branch-a &&
139 grep "submodule .sub.: fatal: a branch named .branch-a. already exists" actual
143 test_expect_success 'should create branches if branch exists and --force is given' '
144 test_when_finished "reset_test" &&
146 cd super &&
147 git -C sub rev-parse HEAD >expected &&
148 test_commit -C sub baz &&
149 # branch-a in sub now points to a newer commit.
150 git -C sub branch branch-a HEAD &&
151 git -C sub rev-parse branch-a >actual-old-branch-a &&
152 git branch --recurse-submodules --force branch-a &&
153 git rev-parse branch-a &&
154 git -C sub rev-parse branch-a >actual-new-branch-a &&
155 test_cmp expected actual-new-branch-a &&
156 # assert that branch --force actually moved the sub
157 # branch
158 ! test_cmp expected actual-old-branch-a
162 test_expect_success 'should create branch when submodule is not in HEAD:.gitmodules' '
163 test_when_finished "reset_test" &&
165 cd super &&
166 git branch branch-a &&
167 git checkout -b branch-b &&
168 git submodule add ../sub-upstream sub2 &&
169 git -C sub2 submodule update --init &&
170 # branch-b now has a committed submodule not in branch-a
171 git commit -m "add second submodule" &&
172 git checkout branch-a &&
173 git branch --recurse-submodules branch-c branch-b &&
174 git checkout --recurse-submodules branch-c &&
175 git -C sub2 rev-parse branch-c &&
176 git -C sub2/sub-sub rev-parse branch-c
180 test_expect_success 'should not create branches in inactive submodules' '
181 test_when_finished "reset_test" &&
182 test_config -C super submodule.sub.active false &&
184 cd super &&
185 git branch --recurse-submodules branch-a &&
186 git rev-parse branch-a &&
187 test_no_branch sub branch-a
191 test_expect_success 'should set up tracking of local branches with track=always' '
192 test_when_finished "reset_test" &&
194 cd super &&
195 git -c branch.autoSetupMerge=always branch --recurse-submodules branch-a main &&
196 git -C sub rev-parse main &&
197 test_cmp_config -C sub . branch.branch-a.remote &&
198 test_cmp_config -C sub refs/heads/main branch.branch-a.merge
202 test_expect_success 'should set up tracking of local branches with explicit track' '
203 test_when_finished "reset_test" &&
205 cd super &&
206 git branch --track --recurse-submodules branch-a main &&
207 git -C sub rev-parse main &&
208 test_cmp_config -C sub . branch.branch-a.remote &&
209 test_cmp_config -C sub refs/heads/main branch.branch-a.merge
213 test_expect_success 'should not set up unnecessary tracking of local branches' '
214 test_when_finished "reset_test" &&
216 cd super &&
217 git branch --recurse-submodules branch-a main &&
218 git -C sub rev-parse main &&
219 test_cmp_config -C sub "" --default "" branch.branch-a.remote &&
220 test_cmp_config -C sub "" --default "" branch.branch-a.merge
224 reset_remote_test () {
225 rm -fr super-clone &&
226 reset_test
229 test_expect_success 'setup tests with remotes' '
231 cd test_dirs &&
233 cd super &&
234 git branch branch-a &&
235 git checkout -b branch-b &&
236 git submodule add ../sub-upstream sub2 &&
237 # branch-b now has a committed submodule not in branch-a
238 git commit -m "add second submodule"
239 ) &&
240 git clone --branch main --recurse-submodules super super-clone &&
241 git -C super-clone config submodule.propagateBranches true
242 ) &&
243 reset_remote_test
246 test_expect_success 'should get fatal error upon branch creation when submodule is not in .git/modules' '
247 test_when_finished "reset_remote_test" &&
249 cd super-clone &&
250 # This should succeed because super-clone has sub in .git/modules
251 git branch --recurse-submodules branch-a origin/branch-a &&
252 # This should fail because super-clone does not have sub2 .git/modules
253 test_must_fail git branch --recurse-submodules branch-b origin/branch-b 2>actual &&
254 grep "fatal: submodule .sub2.: unable to find submodule" actual &&
255 test_no_branch . branch-b &&
256 test_no_branch sub branch-b &&
257 # User can fix themselves by initializing the submodule
258 git checkout origin/branch-b &&
259 git submodule update --init --recursive &&
260 git branch --recurse-submodules branch-b origin/branch-b
264 test_expect_success 'should set up tracking of remote-tracking branches by default' '
265 test_when_finished "reset_remote_test" &&
267 cd super-clone &&
268 git branch --recurse-submodules branch-a origin/branch-a &&
269 test_cmp_config origin branch.branch-a.remote &&
270 test_cmp_config refs/heads/branch-a branch.branch-a.merge &&
271 # "origin/branch-a" does not exist for "sub", but it matches the refspec
272 # so tracking should be set up
273 test_cmp_config -C sub origin branch.branch-a.remote &&
274 test_cmp_config -C sub refs/heads/branch-a branch.branch-a.merge &&
275 test_cmp_config -C sub/sub-sub origin branch.branch-a.remote &&
276 test_cmp_config -C sub/sub-sub refs/heads/branch-a branch.branch-a.merge
280 test_expect_success 'should not fail when unable to set up tracking in submodule' '
281 test_when_finished "reset_remote_test" &&
283 cd super-clone &&
284 git remote rename origin ex-origin &&
285 git branch --recurse-submodules branch-a ex-origin/branch-a &&
286 test_cmp_config ex-origin branch.branch-a.remote &&
287 test_cmp_config refs/heads/branch-a branch.branch-a.merge &&
288 test_cmp_config -C sub "" --default "" branch.branch-a.remote &&
289 test_cmp_config -C sub "" --default "" branch.branch-a.merge
293 test_expect_success '--track=inherit should set up tracking correctly' '
294 test_when_finished "reset_remote_test" &&
296 cd super-clone &&
297 git branch --recurse-submodules branch-a origin/branch-a &&
298 # Set this manually instead of using branch --set-upstream-to
299 # to circumvent the "nonexistent upstream" check.
300 git -C sub config branch.branch-a.remote origin &&
301 git -C sub config branch.branch-a.merge refs/heads/sub-branch-a &&
302 git -C sub/sub-sub config branch.branch-a.remote other &&
303 git -C sub/sub-sub config branch.branch-a.merge refs/heads/sub-sub-branch-a &&
305 git branch --recurse-submodules --track=inherit branch-b branch-a &&
306 test_cmp_config origin branch.branch-b.remote &&
307 test_cmp_config refs/heads/branch-a branch.branch-b.merge &&
308 test_cmp_config -C sub origin branch.branch-b.remote &&
309 test_cmp_config -C sub refs/heads/sub-branch-a branch.branch-b.merge &&
310 test_cmp_config -C sub/sub-sub other branch.branch-b.remote &&
311 test_cmp_config -C sub/sub-sub refs/heads/sub-sub-branch-a branch.branch-b.merge
315 test_expect_success '--no-track should not set up tracking' '
316 test_when_finished "reset_remote_test" &&
318 cd super-clone &&
319 git branch --recurse-submodules --no-track branch-a origin/branch-a &&
320 test_cmp_config "" --default "" branch.branch-a.remote &&
321 test_cmp_config "" --default "" branch.branch-a.merge &&
322 test_cmp_config -C sub "" --default "" branch.branch-a.remote &&
323 test_cmp_config -C sub "" --default "" branch.branch-a.merge &&
324 test_cmp_config -C sub/sub-sub "" --default "" branch.branch-a.remote &&
325 test_cmp_config -C sub/sub-sub "" --default "" branch.branch-a.merge
329 test_done