Git 2.30.9
[git.git] / t / t7001-mv.sh
blob6156eeb9ade72cb1ccbbe6763fd595f90a07dfc1
1 #!/bin/sh
3 test_description='git mv in subdirs'
4 . ./test-lib.sh
6 test_expect_success \
7 'prepare reference tree' \
8 'mkdir path0 path1 &&
9 cp "$TEST_DIRECTORY"/../COPYING path0/COPYING &&
10 git add path0/COPYING &&
11 git commit -m add -a'
13 test_expect_success \
14 'moving the file out of subdirectory' \
15 'cd path0 && git mv COPYING ../path1/COPYING'
17 # in path0 currently
18 test_expect_success \
19 'commiting the change' \
20 'cd .. && git commit -m move-out -a'
22 test_expect_success \
23 'checking the commit' \
24 'git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
25 grep "^R100..*path0/COPYING..*path1/COPYING" actual'
27 test_expect_success \
28 'moving the file back into subdirectory' \
29 'cd path0 && git mv ../path1/COPYING COPYING'
31 # in path0 currently
32 test_expect_success \
33 'commiting the change' \
34 'cd .. && git commit -m move-in -a'
36 test_expect_success \
37 'checking the commit' \
38 'git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
39 grep "^R100..*path1/COPYING..*path0/COPYING" actual'
41 test_expect_success \
42 'mv --dry-run does not move file' \
43 'git mv -n path0/COPYING MOVED &&
44 test -f path0/COPYING &&
45 test ! -f MOVED'
47 test_expect_success \
48 'checking -k on non-existing file' \
49 'git mv -k idontexist path0'
51 test_expect_success \
52 'checking -k on untracked file' \
53 'touch untracked1 &&
54 git mv -k untracked1 path0 &&
55 test -f untracked1 &&
56 test ! -f path0/untracked1'
58 test_expect_success \
59 'checking -k on multiple untracked files' \
60 'touch 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'
67 test_expect_success \
68 'checking -f on untracked file with existing target' \
69 'touch 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'
75 # clean up the mess in case bad things happen
76 rm -f idontexist untracked1 untracked2 \
77 path0/idontexist path0/untracked1 path0/untracked2 \
78 .git/index.lock
79 rmdir path1
81 test_expect_success \
82 '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'
88 test_expect_success \
89 'clean up' \
90 'git reset --hard'
92 test_expect_success \
93 'moving to existing untracked target with trailing slash' \
94 'mkdir path1 &&
95 git mv path0/ path1/ &&
96 test_path_is_dir path1/path0/'
98 test_expect_success \
99 '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/'
105 test_expect_success \
106 'clean up' \
107 'git reset --hard'
109 test_expect_success \
110 'adding another file' \
111 'cp "$TEST_DIRECTORY"/../README.md path0/README &&
112 git add path0/README &&
113 git commit -m add2 -a'
115 test_expect_success \
116 'moving whole subdirectory' \
117 'git mv path0 path2'
119 test_expect_success \
120 'commiting the change' \
121 'git commit -m dir-move -a'
123 test_expect_success \
124 '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'
129 test_expect_success \
130 'succeed when source is a prefix of destination' \
131 'git mv path2/COPYING path2/COPYING-renamed'
133 test_expect_success \
134 'moving whole subdirectory into subdirectory' \
135 'git mv path2 path1'
137 test_expect_success \
138 'commiting the change' \
139 'git commit -m dir-move -a'
141 test_expect_success \
142 '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'
147 test_expect_success \
148 'do not move directory over existing directory' \
149 'mkdir path0 && mkdir path0/path2 && test_must_fail git mv path2 path0'
151 test_expect_success \
152 'move into "."' \
153 'git mv path1/path2/ .'
155 test_expect_success "Michael Cassar's test case" '
156 rm -fr .git papers partA &&
157 git init &&
158 mkdir -p papers/unsorted papers/all-papers partA &&
159 echo a > papers/unsorted/Thesis.pdf &&
160 echo b > partA/outline.txt &&
161 echo c > papers/unsorted/_another &&
162 git add papers partA &&
163 T1=$(git write-tree) &&
165 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
167 T=$(git write-tree) &&
168 git ls-tree -r $T | verbose grep partA/outline.txt
171 rm -fr papers partA path?
173 test_expect_success "Sergey Vlasov's test case" '
174 rm -fr .git &&
175 git init &&
176 mkdir ab &&
177 date >ab.c &&
178 date >ab/d &&
179 git add ab.c ab &&
180 git commit -m "initial" &&
181 git mv ab a
184 test_expect_success 'absolute pathname' '(
186 rm -fr mine &&
187 mkdir mine &&
188 cd mine &&
189 test_create_repo one &&
190 cd one &&
191 mkdir sub &&
192 >sub/file &&
193 git add sub/file &&
195 git mv sub "$(pwd)/in" &&
196 ! test -d sub &&
197 test -d in &&
198 git ls-files --error-unmatch in/file
203 test_expect_success 'absolute pathname outside should fail' '(
205 rm -fr mine &&
206 mkdir mine &&
207 cd mine &&
208 out=$(pwd) &&
209 test_create_repo one &&
210 cd one &&
211 mkdir sub &&
212 >sub/file &&
213 git add sub/file &&
215 test_must_fail git mv sub "$out/out" &&
216 test -d sub &&
217 ! test -d ../in &&
218 git ls-files --error-unmatch sub/file
222 test_expect_success 'git mv to move multiple sources into a directory' '
223 rm -fr .git && git init &&
224 mkdir dir other &&
225 >dir/a.txt &&
226 >dir/b.txt &&
227 git add dir/?.txt &&
228 git mv dir/a.txt dir/b.txt other &&
229 git ls-files >actual &&
230 { echo other/a.txt; echo other/b.txt; } >expect &&
231 test_cmp expect actual
234 test_expect_success 'git mv should not change sha1 of moved cache entry' '
236 rm -fr .git &&
237 git init &&
238 echo 1 >dirty &&
239 git add dirty &&
240 entry="$(git ls-files --stage dirty | cut -f 1)" &&
241 git mv dirty dirty2 &&
242 [ "$entry" = "$(git ls-files --stage dirty2 | cut -f 1)" ] &&
243 echo 2 >dirty2 &&
244 git mv dirty2 dirty &&
245 [ "$entry" = "$(git ls-files --stage dirty | cut -f 1)" ]
249 rm -f dirty dirty2
251 # NB: This test is about the error message
252 # as well as the failure.
253 test_expect_success 'git mv error on conflicted file' '
254 rm -fr .git &&
255 git init &&
256 >conflict &&
257 test_when_finished "rm -f conflict" &&
258 cfhash=$(git hash-object -w conflict) &&
259 q_to_tab <<-EOF | git update-index --index-info &&
260 0 $cfhash 0Qconflict
261 100644 $cfhash 1Qconflict
264 test_must_fail git mv conflict newname 2>actual &&
265 test_i18ngrep "conflicted" actual
268 test_expect_success 'git mv should overwrite symlink to a file' '
270 rm -fr .git &&
271 git init &&
272 echo 1 >moved &&
273 test_ln_s_add moved symlink &&
274 git add moved &&
275 test_must_fail git mv moved symlink &&
276 git mv -f moved symlink &&
277 ! test -e moved &&
278 test -f symlink &&
279 test "$(cat symlink)" = 1 &&
280 git update-index --refresh &&
281 git diff-files --quiet
285 rm -f moved symlink
287 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
302 test_expect_success SYMLINKS 'check moved symlink' '
304 test -h moved
307 rm -f moved symlink
309 test_expect_success 'setup submodule' '
310 test_config_global protocol.file.allow always &&
311 git commit -m initial &&
312 git reset --hard &&
313 git submodule add ./. sub &&
314 echo content >file &&
315 git add file &&
316 git commit -m "added sub and file" &&
317 mkdir -p deep/directory/hierarchy &&
318 git submodule add ./. deep/directory/hierarchy/sub &&
319 git commit -m "added another submodule" &&
320 git branch submodule
323 test_expect_success 'git mv cannot move a submodule in a file' '
324 test_must_fail git mv sub file
327 test_expect_success 'git mv moves a submodule with a .git directory and no .gitmodules' '
328 entry="$(git ls-files --stage sub | cut -f 1)" &&
329 git rm .gitmodules &&
331 cd sub &&
332 rm -f .git &&
333 cp -R -P -p ../.git/modules/sub .git &&
334 GIT_WORK_TREE=. git config --unset core.worktree
335 ) &&
336 mkdir mod &&
337 git mv sub mod/sub &&
338 ! test -e sub &&
339 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
341 cd mod/sub &&
342 git status
343 ) &&
344 git update-index --refresh &&
345 git diff-files --quiet
348 test_expect_success 'git mv moves a submodule with a .git directory and .gitmodules' '
349 rm -rf mod &&
350 git reset --hard &&
351 git submodule update &&
352 entry="$(git ls-files --stage sub | cut -f 1)" &&
354 cd sub &&
355 rm -f .git &&
356 cp -R -P -p ../.git/modules/sub .git &&
357 GIT_WORK_TREE=. git config --unset core.worktree
358 ) &&
359 mkdir mod &&
360 git mv sub mod/sub &&
361 ! test -e sub &&
362 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
364 cd mod/sub &&
365 git status
366 ) &&
367 echo mod/sub >expected &&
368 git config -f .gitmodules submodule.sub.path >actual &&
369 test_cmp expected actual &&
370 git update-index --refresh &&
371 git diff-files --quiet
374 test_expect_success 'git mv moves a submodule with gitfile' '
375 rm -rf mod &&
376 git reset --hard &&
377 git submodule update &&
378 entry="$(git ls-files --stage sub | cut -f 1)" &&
379 mkdir mod &&
381 cd mod &&
382 git mv ../sub/ .
383 ) &&
384 ! test -e sub &&
385 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
387 cd mod/sub &&
388 git status
389 ) &&
390 echo mod/sub >expected &&
391 git config -f .gitmodules submodule.sub.path >actual &&
392 test_cmp expected actual &&
393 git update-index --refresh &&
394 git diff-files --quiet
397 test_expect_success 'mv does not complain when no .gitmodules file is found' '
398 rm -rf mod &&
399 git reset --hard &&
400 git submodule update &&
401 git rm .gitmodules &&
402 entry="$(git ls-files --stage sub | cut -f 1)" &&
403 mkdir mod &&
404 git mv sub mod/sub 2>actual.err &&
405 test_must_be_empty actual.err &&
406 ! test -e sub &&
407 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
409 cd mod/sub &&
410 git status
411 ) &&
412 git update-index --refresh &&
413 git diff-files --quiet
416 test_expect_success 'mv will error out on a modified .gitmodules file unless staged' '
417 rm -rf mod &&
418 git reset --hard &&
419 git submodule update &&
420 git config -f .gitmodules foo.bar true &&
421 entry="$(git ls-files --stage sub | cut -f 1)" &&
422 mkdir mod &&
423 test_must_fail git mv sub mod/sub 2>actual.err &&
424 test -s actual.err &&
425 test -e sub &&
426 git diff-files --quiet -- sub &&
427 git add .gitmodules &&
428 git mv sub mod/sub 2>actual.err &&
429 test_must_be_empty actual.err &&
430 ! test -e sub &&
431 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
433 cd mod/sub &&
434 git status
435 ) &&
436 git update-index --refresh &&
437 git diff-files --quiet
440 test_expect_success 'mv issues a warning when section is not found in .gitmodules' '
441 rm -rf mod &&
442 git reset --hard &&
443 git submodule update &&
444 git config -f .gitmodules --remove-section submodule.sub &&
445 git add .gitmodules &&
446 entry="$(git ls-files --stage sub | cut -f 1)" &&
447 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
448 mkdir mod &&
449 git mv sub mod/sub 2>actual.err &&
450 test_i18ncmp expect.err actual.err &&
451 ! test -e sub &&
452 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
454 cd mod/sub &&
455 git status
456 ) &&
457 git update-index --refresh &&
458 git diff-files --quiet
461 test_expect_success 'mv --dry-run does not touch the submodule or .gitmodules' '
462 rm -rf mod &&
463 git reset --hard &&
464 git submodule update &&
465 mkdir mod &&
466 git mv -n sub mod/sub 2>actual.err &&
467 test -f sub/.git &&
468 git diff-index --exit-code HEAD &&
469 git update-index --refresh &&
470 git diff-files --quiet -- sub .gitmodules
473 test_expect_success 'checking out a commit before submodule moved needs manual updates' '
474 git mv sub sub2 &&
475 git commit -m "moved sub to sub2" &&
476 git checkout -q HEAD^ 2>actual &&
477 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
478 git status -s sub2 >actual &&
479 echo "?? sub2/" >expected &&
480 test_cmp expected actual &&
481 ! test -f sub/.git &&
482 test -f sub2/.git &&
483 git submodule update &&
484 test -f sub/.git &&
485 rm -rf sub2 &&
486 git diff-index --exit-code HEAD &&
487 git update-index --refresh &&
488 git diff-files --quiet -- sub .gitmodules &&
489 git status -s sub2 >actual &&
490 test_must_be_empty actual
493 test_expect_success 'mv -k does not accidentally destroy submodules' '
494 git checkout submodule &&
495 mkdir dummy dest &&
496 git mv -k dummy sub dest &&
497 git status --porcelain >actual &&
498 grep "^R sub -> dest/sub" actual &&
499 git reset --hard &&
500 git checkout .
503 test_expect_success 'moving a submodule in nested directories' '
505 cd deep &&
506 git mv directory ../ &&
507 # git status would fail if the update of linking git dir to
508 # work dir of the submodule failed.
509 git status &&
510 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
511 echo "directory/hierarchy/sub" >../expect
512 ) &&
513 test_cmp expect actual
516 test_expect_success 'moving nested submodules' '
517 test_config_global protocol.file.allow always &&
518 git commit -am "cleanup commit" &&
519 mkdir sub_nested_nested &&
520 (cd sub_nested_nested &&
521 touch nested_level2 &&
522 git init &&
523 git add . &&
524 git commit -m "nested level 2"
525 ) &&
526 mkdir sub_nested &&
527 (cd sub_nested &&
528 touch nested_level1 &&
529 git init &&
530 git add . &&
531 git commit -m "nested level 1" &&
532 git submodule add ../sub_nested_nested &&
533 git commit -m "add nested level 2"
534 ) &&
535 git submodule add ./sub_nested nested_move &&
536 git commit -m "add nested_move" &&
537 git submodule update --init --recursive &&
538 git mv nested_move sub_nested_moved &&
539 git status
542 test_done