]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7001-mv.sh
Sync with 2.33.5
[thirdparty/git.git] / t / t7001-mv.sh
1 #!/bin/sh
2
3 test_description='git mv in subdirs'
4 . ./test-lib.sh
5 . "$TEST_DIRECTORY"/lib-diff-data.sh
6
7 test_expect_success 'prepare reference tree' '
8 mkdir path0 path1 &&
9 COPYING_test_data >path0/COPYING &&
10 git add path0/COPYING &&
11 git commit -m add -a
12 '
13
14 test_expect_success 'moving the file out of subdirectory' '
15 git -C path0 mv COPYING ../path1/COPYING
16 '
17
18 # in path0 currently
19 test_expect_success 'commiting the change' '
20 git commit -m move-out -a
21 '
22
23 test_expect_success 'checking the commit' '
24 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
25 grep "^R100..*path0/COPYING..*path1/COPYING" actual
26 '
27
28 test_expect_success 'moving the file back into subdirectory' '
29 git -C path0 mv ../path1/COPYING COPYING
30 '
31
32 # in path0 currently
33 test_expect_success 'commiting the change' '
34 git commit -m move-in -a
35 '
36
37 test_expect_success 'checking the commit' '
38 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
39 grep "^R100..*path1/COPYING..*path0/COPYING" actual
40 '
41
42 test_expect_success 'mv --dry-run does not move file' '
43 git mv -n path0/COPYING MOVED &&
44 test -f path0/COPYING &&
45 test ! -f MOVED
46 '
47
48 test_expect_success 'checking -k on non-existing file' '
49 git mv -k idontexist path0
50 '
51
52 test_expect_success 'checking -k on untracked file' '
53 >untracked1 &&
54 git mv -k untracked1 path0 &&
55 test -f untracked1 &&
56 test ! -f path0/untracked1
57 '
58
59 test_expect_success 'checking -k on multiple untracked files' '
60 >untracked2 &&
61 git mv -k untracked1 untracked2 path0 &&
62 test -f untracked1 &&
63 test -f untracked2 &&
64 test ! -f path0/untracked1 &&
65 test ! -f path0/untracked2
66 '
67
68 test_expect_success 'checking -f on untracked file with existing target' '
69 >path0/untracked1 &&
70 test_must_fail git mv -f untracked1 path0 &&
71 test ! -f .git/index.lock &&
72 test -f untracked1 &&
73 test -f path0/untracked1
74 '
75
76 # clean up the mess in case bad things happen
77 rm -f idontexist untracked1 untracked2 \
78 path0/idontexist path0/untracked1 path0/untracked2 \
79 .git/index.lock
80 rmdir path1
81
82 test_expect_success 'moving to absent target with trailing slash' '
83 test_must_fail git mv path0/COPYING no-such-dir/ &&
84 test_must_fail git mv path0/COPYING no-such-dir// &&
85 git mv path0/ no-such-dir/ &&
86 test_path_is_dir no-such-dir
87 '
88
89 test_expect_success 'clean up' '
90 git reset --hard
91 '
92
93 test_expect_success 'moving to existing untracked target with trailing slash' '
94 mkdir path1 &&
95 git mv path0/ path1/ &&
96 test_path_is_dir path1/path0/
97 '
98
99 test_expect_success 'moving to existing tracked target with trailing slash' '
100 mkdir path2 &&
101 >path2/file && git add path2/file &&
102 git mv path1/path0/ path2/ &&
103 test_path_is_dir path2/path0/
104 '
105
106 test_expect_success 'clean up' '
107 git reset --hard
108 '
109
110 test_expect_success 'adding another file' '
111 COPYING_test_data | tr A-Za-z N-ZA-Mn-za-m >path0/README &&
112 git add path0/README &&
113 git commit -m add2 -a
114 '
115
116 test_expect_success 'moving whole subdirectory' '
117 git mv path0 path2
118 '
119
120 test_expect_success 'commiting the change' '
121 git commit -m dir-move -a
122 '
123
124 test_expect_success 'checking the commit' '
125 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
126 grep "^R100..*path0/COPYING..*path2/COPYING" actual &&
127 grep "^R100..*path0/README..*path2/README" actual
128 '
129
130 test_expect_success 'succeed when source is a prefix of destination' '
131 git mv path2/COPYING path2/COPYING-renamed
132 '
133
134 test_expect_success 'moving whole subdirectory into subdirectory' '
135 git mv path2 path1
136 '
137
138 test_expect_success 'commiting the change' '
139 git commit -m dir-move -a
140 '
141
142 test_expect_success 'checking the commit' '
143 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
144 grep "^R100..*path2/COPYING..*path1/path2/COPYING" actual &&
145 grep "^R100..*path2/README..*path1/path2/README" actual
146 '
147
148 test_expect_success 'do not move directory over existing directory' '
149 mkdir path0 &&
150 mkdir path0/path2 &&
151 test_must_fail git mv path2 path0
152 '
153
154 test_expect_success 'move into "."' '
155 git mv path1/path2/ .
156 '
157
158 test_expect_success "Michael Cassar's test case" '
159 rm -fr .git papers partA &&
160 git init &&
161 mkdir -p papers/unsorted papers/all-papers partA &&
162 echo a >papers/unsorted/Thesis.pdf &&
163 echo b >partA/outline.txt &&
164 echo c >papers/unsorted/_another &&
165 git add papers partA &&
166 T1=$(git write-tree) &&
167
168 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
169
170 T=$(git write-tree) &&
171 git ls-tree -r $T | verbose grep partA/outline.txt
172 '
173
174 rm -fr papers partA path?
175
176 test_expect_success "Sergey Vlasov's test case" '
177 rm -fr .git &&
178 git init &&
179 mkdir ab &&
180 date >ab.c &&
181 date >ab/d &&
182 git add ab.c ab &&
183 git commit -m "initial" &&
184 git mv ab a
185 '
186
187 test_expect_success 'absolute pathname' '
188 (
189 rm -fr mine &&
190 mkdir mine &&
191 cd mine &&
192 test_create_repo one &&
193 cd one &&
194 mkdir sub &&
195 >sub/file &&
196 git add sub/file &&
197
198 git mv sub "$(pwd)/in" &&
199 ! test -d sub &&
200 test -d in &&
201 git ls-files --error-unmatch in/file
202 )
203 '
204
205 test_expect_success 'absolute pathname outside should fail' '
206 (
207 rm -fr mine &&
208 mkdir mine &&
209 cd mine &&
210 out=$(pwd) &&
211 test_create_repo one &&
212 cd one &&
213 mkdir sub &&
214 >sub/file &&
215 git add sub/file &&
216
217 test_must_fail git mv sub "$out/out" &&
218 test -d sub &&
219 ! test -d ../in &&
220 git ls-files --error-unmatch sub/file
221 )
222 '
223
224 test_expect_success 'git mv to move multiple sources into a directory' '
225 rm -fr .git && git init &&
226 mkdir dir other &&
227 >dir/a.txt &&
228 >dir/b.txt &&
229 git add dir/?.txt &&
230 git mv dir/a.txt dir/b.txt other &&
231 git ls-files >actual &&
232 cat >expect <<-\EOF &&
233 other/a.txt
234 other/b.txt
235 EOF
236 test_cmp expect actual
237 '
238
239 test_expect_success 'git mv should not change sha1 of moved cache entry' '
240 rm -fr .git &&
241 git init &&
242 echo 1 >dirty &&
243 git add dirty &&
244 entry="$(git ls-files --stage dirty | cut -f 1)" &&
245 git mv dirty dirty2 &&
246 test "$entry" = "$(git ls-files --stage dirty2 | cut -f 1)" &&
247 echo 2 >dirty2 &&
248 git mv dirty2 dirty &&
249 test "$entry" = "$(git ls-files --stage dirty | cut -f 1)"
250 '
251
252 rm -f dirty dirty2
253
254 # NB: This test is about the error message
255 # as well as the failure.
256 test_expect_success 'git mv error on conflicted file' '
257 rm -fr .git &&
258 git init &&
259 >conflict &&
260 test_when_finished "rm -f conflict" &&
261 cfhash=$(git hash-object -w conflict) &&
262 q_to_tab <<-EOF | git update-index --index-info &&
263 0 $cfhash 0Qconflict
264 100644 $cfhash 1Qconflict
265 EOF
266
267 test_must_fail git mv conflict newname 2>actual &&
268 test_i18ngrep "conflicted" actual
269 '
270
271 test_expect_success 'git mv should overwrite symlink to a file' '
272 rm -fr .git &&
273 git init &&
274 echo 1 >moved &&
275 test_ln_s_add moved symlink &&
276 git add moved &&
277 test_must_fail git mv moved symlink &&
278 git mv -f moved symlink &&
279 ! test -e moved &&
280 test -f symlink &&
281 test "$(cat symlink)" = 1 &&
282 git update-index --refresh &&
283 git diff-files --quiet
284 '
285
286 rm -f moved symlink
287
288 test_expect_success 'git mv should overwrite file with a symlink' '
289 rm -fr .git &&
290 git init &&
291 echo 1 >moved &&
292 test_ln_s_add moved symlink &&
293 git add moved &&
294 test_must_fail git mv symlink moved &&
295 git mv -f symlink moved &&
296 ! test -e symlink &&
297 git update-index --refresh &&
298 git diff-files --quiet
299 '
300
301 test_expect_success SYMLINKS 'check moved symlink' '
302 test -h moved
303 '
304
305 rm -f moved symlink
306
307 test_expect_success 'setup submodule' '
308 test_config_global protocol.file.allow always &&
309 git commit -m initial &&
310 git reset --hard &&
311 git submodule add ./. sub &&
312 echo content >file &&
313 git add file &&
314 git commit -m "added sub and file" &&
315 mkdir -p deep/directory/hierarchy &&
316 git submodule add ./. deep/directory/hierarchy/sub &&
317 git commit -m "added another submodule" &&
318 git branch submodule
319 '
320
321 test_expect_success 'git mv cannot move a submodule in a file' '
322 test_must_fail git mv sub file
323 '
324
325 test_expect_success 'git mv moves a submodule with a .git directory and no .gitmodules' '
326 entry="$(git ls-files --stage sub | cut -f 1)" &&
327 git rm .gitmodules &&
328 (
329 cd sub &&
330 rm -f .git &&
331 cp -R -P -p ../.git/modules/sub .git &&
332 GIT_WORK_TREE=. git config --unset core.worktree
333 ) &&
334 mkdir mod &&
335 git mv sub mod/sub &&
336 ! test -e sub &&
337 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
338 git -C mod/sub status &&
339 git update-index --refresh &&
340 git diff-files --quiet
341 '
342
343 test_expect_success 'git mv moves a submodule with a .git directory and .gitmodules' '
344 rm -rf mod &&
345 git reset --hard &&
346 git submodule update &&
347 entry="$(git ls-files --stage sub | cut -f 1)" &&
348 (
349 cd sub &&
350 rm -f .git &&
351 cp -R -P -p ../.git/modules/sub .git &&
352 GIT_WORK_TREE=. git config --unset core.worktree
353 ) &&
354 mkdir mod &&
355 git mv sub mod/sub &&
356 ! test -e sub &&
357 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
358 git -C mod/sub status &&
359 echo mod/sub >expected &&
360 git config -f .gitmodules submodule.sub.path >actual &&
361 test_cmp expected actual &&
362 git update-index --refresh &&
363 git diff-files --quiet
364 '
365
366 test_expect_success 'git mv moves a submodule with gitfile' '
367 rm -rf mod &&
368 git reset --hard &&
369 git submodule update &&
370 entry="$(git ls-files --stage sub | cut -f 1)" &&
371 mkdir mod &&
372 git -C mod mv ../sub/ . &&
373 ! test -e sub &&
374 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
375 git -C mod/sub status &&
376 echo mod/sub >expected &&
377 git config -f .gitmodules submodule.sub.path >actual &&
378 test_cmp expected actual &&
379 git update-index --refresh &&
380 git diff-files --quiet
381 '
382
383 test_expect_success 'mv does not complain when no .gitmodules file is found' '
384 rm -rf mod &&
385 git reset --hard &&
386 git submodule update &&
387 git rm .gitmodules &&
388 entry="$(git ls-files --stage sub | cut -f 1)" &&
389 mkdir mod &&
390 git mv sub mod/sub 2>actual.err &&
391 test_must_be_empty actual.err &&
392 ! test -e sub &&
393 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
394 git -C mod/sub status &&
395 git update-index --refresh &&
396 git diff-files --quiet
397 '
398
399 test_expect_success 'mv will error out on a modified .gitmodules file unless staged' '
400 rm -rf mod &&
401 git reset --hard &&
402 git submodule update &&
403 git config -f .gitmodules foo.bar true &&
404 entry="$(git ls-files --stage sub | cut -f 1)" &&
405 mkdir mod &&
406 test_must_fail git mv sub mod/sub 2>actual.err &&
407 test -s actual.err &&
408 test -e sub &&
409 git diff-files --quiet -- sub &&
410 git add .gitmodules &&
411 git mv sub mod/sub 2>actual.err &&
412 test_must_be_empty actual.err &&
413 ! test -e sub &&
414 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
415 git -C mod/sub status &&
416 git update-index --refresh &&
417 git diff-files --quiet
418 '
419
420 test_expect_success 'mv issues a warning when section is not found in .gitmodules' '
421 rm -rf mod &&
422 git reset --hard &&
423 git submodule update &&
424 git config -f .gitmodules --remove-section submodule.sub &&
425 git add .gitmodules &&
426 entry="$(git ls-files --stage sub | cut -f 1)" &&
427 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
428 mkdir mod &&
429 git mv sub mod/sub 2>actual.err &&
430 test_cmp expect.err actual.err &&
431 ! test -e sub &&
432 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
433 git -C mod/sub status &&
434 git update-index --refresh &&
435 git diff-files --quiet
436 '
437
438 test_expect_success 'mv --dry-run does not touch the submodule or .gitmodules' '
439 rm -rf mod &&
440 git reset --hard &&
441 git submodule update &&
442 mkdir mod &&
443 git mv -n sub mod/sub 2>actual.err &&
444 test -f sub/.git &&
445 git diff-index --exit-code HEAD &&
446 git update-index --refresh &&
447 git diff-files --quiet -- sub .gitmodules
448 '
449
450 test_expect_success 'checking out a commit before submodule moved needs manual updates' '
451 git mv sub sub2 &&
452 git commit -m "moved sub to sub2" &&
453 git checkout -q HEAD^ 2>actual &&
454 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
455 git status -s sub2 >actual &&
456 echo "?? sub2/" >expected &&
457 test_cmp expected actual &&
458 ! test -f sub/.git &&
459 test -f sub2/.git &&
460 git submodule update &&
461 test -f sub/.git &&
462 rm -rf sub2 &&
463 git diff-index --exit-code HEAD &&
464 git update-index --refresh &&
465 git diff-files --quiet -- sub .gitmodules &&
466 git status -s sub2 >actual &&
467 test_must_be_empty actual
468 '
469
470 test_expect_success 'mv -k does not accidentally destroy submodules' '
471 git checkout submodule &&
472 mkdir dummy dest &&
473 git mv -k dummy sub dest &&
474 git status --porcelain >actual &&
475 grep "^R sub -> dest/sub" actual &&
476 git reset --hard &&
477 git checkout .
478 '
479
480 test_expect_success 'moving a submodule in nested directories' '
481 (
482 cd deep &&
483 git mv directory ../ &&
484 # git status would fail if the update of linking git dir to
485 # work dir of the submodule failed.
486 git status &&
487 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
488 echo "directory/hierarchy/sub" >../expect
489 ) &&
490 test_cmp expect actual
491 '
492
493 test_expect_success 'moving nested submodules' '
494 test_config_global protocol.file.allow always &&
495 git commit -am "cleanup commit" &&
496 mkdir sub_nested_nested &&
497 (
498 cd sub_nested_nested &&
499 >nested_level2 &&
500 git init &&
501 git add . &&
502 git commit -m "nested level 2"
503 ) &&
504 mkdir sub_nested &&
505 (
506 cd sub_nested &&
507 >nested_level1 &&
508 git init &&
509 git add . &&
510 git commit -m "nested level 1" &&
511 git submodule add ../sub_nested_nested &&
512 git commit -m "add nested level 2"
513 ) &&
514 git submodule add ./sub_nested nested_move &&
515 git commit -m "add nested_move" &&
516 git submodule update --init --recursive &&
517 git mv nested_move sub_nested_moved &&
518 git status
519 '
520
521 test_done