3 test_description
='git mv in subdirs'
5 .
"$TEST_DIRECTORY"/lib-diff-data.sh
8 git ls-files
--format='%(objectmode) %(objectname) %(stage)' "$@"
11 test_expect_success
'mv -f refreshes updated index entry' '
19 # Wait one second to ensure ctime of rename will differ from original
20 # file creation ctime.
23 git reset --merge HEAD &&
25 # Verify the index has been reset
26 git diff-files >out &&
27 test_must_be_empty out
30 test_expect_success
'prepare reference tree' '
32 COPYING_test_data >path0/COPYING &&
33 git add path0/COPYING &&
37 test_expect_success
'moving the file out of subdirectory' '
38 git -C path0 mv COPYING ../path1/COPYING
42 test_expect_success
'commiting the change' '
43 git commit -m move-out -a
46 test_expect_success
'checking the commit' '
47 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
48 grep "^R100..*path0/COPYING..*path1/COPYING" actual
51 test_expect_success
'moving the file back into subdirectory' '
52 git -C path0 mv ../path1/COPYING COPYING
56 test_expect_success
'commiting the change' '
57 git commit -m move-in -a
60 test_expect_success
'checking the commit' '
61 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
62 grep "^R100..*path1/COPYING..*path0/COPYING" actual
65 test_expect_success
'mv --dry-run does not move file' '
66 git mv -n path0/COPYING MOVED &&
67 test_path_is_file path0/COPYING &&
68 test_path_is_missing MOVED
71 test_expect_success
'checking -k on non-existing file' '
72 git mv -k idontexist path0
75 test_expect_success
'checking -k on untracked file' '
77 git mv -k untracked1 path0 &&
78 test_path_is_file untracked1 &&
79 test_path_is_missing path0/untracked1
82 test_expect_success
'checking -k on multiple untracked files' '
84 git mv -k untracked1 untracked2 path0 &&
85 test_path_is_file untracked1 &&
86 test_path_is_file untracked2 &&
87 test_path_is_missing path0/untracked1 &&
88 test_path_is_missing path0/untracked2
91 test_expect_success
'checking -f on untracked file with existing target' '
93 test_must_fail git mv -f untracked1 path0 &&
94 test_path_is_missing .git/index.lock &&
95 test_path_is_file untracked1 &&
96 test_path_is_file path0/untracked1
99 # clean up the mess in case bad things happen
100 rm -f idontexist untracked1 untracked2 \
101 path
0/idontexist path
0/untracked1 path
0/untracked2 \
105 test_expect_success
'moving to absent target with trailing slash' '
106 test_must_fail git mv path0/COPYING no-such-dir/ &&
107 test_must_fail git mv path0/COPYING no-such-dir// &&
108 git mv path0/ no-such-dir/ &&
109 test_path_is_dir no-such-dir
112 test_expect_success
'clean up' '
116 test_expect_success
'moving to existing untracked target with trailing slash' '
118 git mv path0/ path1/ &&
119 test_path_is_dir path1/path0/
122 test_expect_success
'moving to existing tracked target with trailing slash' '
124 >path2/file && git add path2/file &&
125 git mv path1/path0/ path2/ &&
126 test_path_is_dir path2/path0/
129 test_expect_success
'clean up' '
133 test_expect_success
'adding another file' '
134 COPYING_test_data | tr A-Za-z N-ZA-Mn-za-m >path0/README &&
135 git add path0/README &&
136 git commit -m add2 -a
139 test_expect_success
'moving whole subdirectory' '
143 test_expect_success
'commiting the change' '
144 git commit -m dir-move -a
147 test_expect_success
'checking the commit' '
148 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
149 grep "^R100..*path0/COPYING..*path2/COPYING" actual &&
150 grep "^R100..*path0/README..*path2/README" actual
153 test_expect_success
'succeed when source is a prefix of destination' '
154 git mv path2/COPYING path2/COPYING-renamed
157 test_expect_success
'moving whole subdirectory into subdirectory' '
161 test_expect_success
'commiting the change' '
162 git commit -m dir-move -a
165 test_expect_success
'checking the commit' '
166 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
167 grep "^R100..*path2/COPYING..*path1/path2/COPYING" actual &&
168 grep "^R100..*path2/README..*path1/path2/README" actual
171 test_expect_success
'do not move directory over existing directory' '
174 test_must_fail git mv path2 path0
177 test_expect_success
'rename directory to non-existing directory' '
181 git mv dir-a non-existing-dir
184 test_expect_success
'move into "."' '
185 git mv path1/path2/ .
188 test_expect_success
"Michael Cassar's test case" '
189 rm -fr .git papers partA &&
191 mkdir -p papers/unsorted papers/all-papers partA &&
192 echo a >papers/unsorted/Thesis.pdf &&
193 echo b >partA/outline.txt &&
194 echo c >papers/unsorted/_another &&
195 git add papers partA &&
196 T1=$(git write-tree) &&
198 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
200 T=$(git write-tree) &&
201 git ls-tree -r $T >out &&
202 grep partA/outline.txt out
205 rm -fr papers partA path?
207 test_expect_success
"Sergey Vlasov's test case" '
214 git commit -m "initial" &&
218 test_expect_success
'absolute pathname' '
223 test_create_repo one &&
229 git mv sub "$(pwd)/in" &&
230 test_path_is_missing sub &&
231 test_path_is_dir in &&
232 git ls-files --error-unmatch in/file
236 test_expect_success
'absolute pathname outside should fail' '
242 test_create_repo one &&
248 test_must_fail git mv sub "$out/out" &&
249 test_path_is_dir sub &&
250 test_path_is_missing ../in &&
251 git ls-files --error-unmatch sub/file
255 test_expect_success
'git mv to move multiple sources into a directory' '
256 rm -fr .git && git init &&
261 git mv dir/a.txt dir/b.txt other &&
262 git ls-files >actual &&
263 cat >expect <<-\EOF &&
267 test_cmp expect actual
270 test_expect_success
'git mv should not change sha1 of moved cache entry' '
275 entry="$(index_at_path dirty)" &&
276 git mv dirty dirty2 &&
277 test "$entry" = "$(index_at_path dirty2)" &&
279 git mv dirty2 dirty &&
280 test "$entry" = "$(index_at_path dirty)"
285 # NB: This test is about the error message
286 # as well as the failure.
287 test_expect_success
'git mv error on conflicted file' '
291 test_when_finished "rm -f conflict" &&
292 cfhash=$(git hash-object -w conflict) &&
293 q_to_tab <<-EOF | git update-index --index-info &&
295 100644 $cfhash 1Qconflict
298 test_must_fail git mv conflict newname 2>actual &&
299 test_grep "conflicted" actual
302 test_expect_success
'git mv should overwrite symlink to a file' '
306 test_ln_s_add moved symlink &&
308 test_must_fail git mv moved symlink &&
309 git mv -f moved symlink &&
310 test_path_is_missing moved &&
311 test_path_is_file symlink &&
312 test "$(cat symlink)" = 1 &&
313 git update-index --refresh &&
314 git diff-files --quiet
319 test_expect_success
'git mv should overwrite file with a symlink' '
323 test_ln_s_add moved symlink &&
325 test_must_fail git mv symlink moved &&
326 git mv -f symlink moved &&
327 test_path_is_missing symlink &&
328 git update-index --refresh &&
329 git diff-files --quiet
332 test_expect_success SYMLINKS
'check moved symlink' '
333 test_path_is_symlink moved
338 test_expect_success
'setup submodule' '
339 test_config_global protocol.file.allow always &&
340 git commit -m initial &&
342 git submodule add ./. sub &&
343 echo content >file &&
345 git commit -m "added sub and file" &&
346 mkdir -p deep/directory/hierarchy &&
347 git submodule add ./. deep/directory/hierarchy/sub &&
348 git commit -m "added another submodule" &&
352 test_expect_success
'git mv cannot move a submodule in a file' '
353 test_must_fail git mv sub file
356 test_expect_success
'git mv moves a submodule with a .git directory and no .gitmodules' '
357 entry="$(index_at_path sub)" &&
358 git rm .gitmodules &&
362 cp -R -P -p ../.git/modules/sub .git &&
363 GIT_WORK_TREE=. git config --unset core.worktree
366 git mv sub mod/sub &&
367 test_path_is_missing sub &&
368 test "$entry" = "$(index_at_path mod/sub)" &&
369 git -C mod/sub status &&
370 git update-index --refresh &&
371 git diff-files --quiet
374 test_expect_success
'git mv moves a submodule with a .git directory and .gitmodules' '
377 git submodule update &&
378 entry="$(index_at_path sub)" &&
382 cp -R -P -p ../.git/modules/sub .git &&
383 GIT_WORK_TREE=. git config --unset core.worktree
386 git mv sub mod/sub &&
387 test_path_is_missing sub &&
388 test "$entry" = "$(index_at_path mod/sub)" &&
389 git -C mod/sub status &&
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
'git mv moves a submodule with gitfile' '
400 git submodule update &&
401 entry="$(index_at_path sub)" &&
403 git -C mod mv ../sub/ . &&
404 test_path_is_missing sub &&
405 test "$entry" = "$(index_at_path mod/sub)" &&
406 git -C mod/sub status &&
407 echo mod/sub >expected &&
408 git config -f .gitmodules submodule.sub.path >actual &&
409 test_cmp expected actual &&
410 git update-index --refresh &&
411 git diff-files --quiet
414 test_expect_success
'mv does not complain when no .gitmodules file is found' '
417 git submodule update &&
418 git rm .gitmodules &&
419 entry="$(index_at_path sub)" &&
421 git mv sub mod/sub 2>actual.err &&
422 test_must_be_empty actual.err &&
423 test_path_is_missing sub &&
424 test "$entry" = "$(index_at_path mod/sub)" &&
425 git -C mod/sub status &&
426 git update-index --refresh &&
427 git diff-files --quiet
430 test_expect_success
'mv will error out on a modified .gitmodules file unless staged' '
433 git submodule update &&
434 git config -f .gitmodules foo.bar true &&
435 entry="$(index_at_path sub)" &&
437 test_must_fail git mv sub mod/sub 2>actual.err &&
438 test_file_not_empty actual.err &&
439 test_path_exists sub &&
440 git diff-files --quiet -- sub &&
441 git add .gitmodules &&
442 git mv sub mod/sub 2>actual.err &&
443 test_must_be_empty actual.err &&
444 test_path_is_missing sub &&
445 test "$entry" = "$(index_at_path mod/sub)" &&
446 git -C mod/sub status &&
447 git update-index --refresh &&
448 git diff-files --quiet
451 test_expect_success
'mv issues a warning when section is not found in .gitmodules' '
454 git submodule update &&
455 git config -f .gitmodules --remove-section submodule.sub &&
456 git add .gitmodules &&
457 entry="$(index_at_path sub)" &&
458 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
460 git mv sub mod/sub 2>actual.err &&
461 test_cmp expect.err actual.err &&
462 test_path_is_missing sub &&
463 test "$entry" = "$(index_at_path mod/sub)" &&
464 git -C mod/sub status &&
465 git update-index --refresh &&
466 git diff-files --quiet
469 test_expect_success
'mv --dry-run does not touch the submodule or .gitmodules' '
472 git submodule update &&
474 git mv -n sub mod/sub 2>actual.err &&
475 test_path_is_file sub/.git &&
476 git diff-index --exit-code HEAD &&
477 git update-index --refresh &&
478 git diff-files --quiet -- sub .gitmodules
481 test_expect_success
'checking out a commit before submodule moved needs manual updates' '
483 git commit -m "moved sub to sub2" &&
484 git checkout -q HEAD^ 2>actual &&
485 test_grep "^warning: unable to rmdir '\''sub2'\'':" actual &&
486 git status -s sub2 >actual &&
487 echo "?? sub2/" >expected &&
488 test_cmp expected actual &&
489 test_path_is_missing sub/.git &&
490 test_path_is_file sub2/.git &&
491 git submodule update &&
492 test_path_is_file sub/.git &&
494 git diff-index --exit-code HEAD &&
495 git update-index --refresh &&
496 git diff-files --quiet -- sub .gitmodules &&
497 git status -s sub2 >actual &&
498 test_must_be_empty actual
501 test_expect_success
'mv -k does not accidentally destroy submodules' '
502 git checkout submodule &&
504 git mv -k dummy sub dest &&
505 git status --porcelain >actual &&
506 grep "^R sub -> dest/sub" actual &&
511 test_expect_success
'moving a submodule in nested directories' '
514 git mv directory ../ &&
515 # git status would fail if the update of linking git dir to
516 # work dir of the submodule failed.
518 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
519 echo "directory/hierarchy/sub" >../expect
521 test_cmp expect actual
524 test_expect_success
'moving nested submodules' '
525 test_config_global protocol.file.allow always &&
526 git commit -am "cleanup commit" &&
527 mkdir sub_nested_nested &&
529 cd sub_nested_nested &&
533 git commit -m "nested level 2"
541 git commit -m "nested level 1" &&
542 git submodule add ../sub_nested_nested &&
543 git commit -m "add nested level 2"
545 git submodule add ./sub_nested nested_move &&
546 git commit -m "add nested_move" &&
547 git submodule update --init --recursive &&
548 git mv nested_move sub_nested_moved &&