3 test_description
='git mv in subdirs'
5 .
"$TEST_DIRECTORY"/lib-diff-data.sh
7 test_expect_success
'mv -f refreshes updated index entry' '
15 # Wait one second to ensure ctime of rename will differ from original
16 # file creation ctime.
19 git reset --merge HEAD &&
21 # Verify the index has been reset
22 git diff-files >out &&
23 test_must_be_empty out
26 test_expect_success
'prepare reference tree' '
28 COPYING_test_data >path0/COPYING &&
29 git add path0/COPYING &&
33 test_expect_success
'moving the file out of subdirectory' '
34 git -C path0 mv COPYING ../path1/COPYING
38 test_expect_success
'commiting the change' '
39 git commit -m move-out -a
42 test_expect_success
'checking the commit' '
43 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
44 grep "^R100..*path0/COPYING..*path1/COPYING" actual
47 test_expect_success
'moving the file back into subdirectory' '
48 git -C path0 mv ../path1/COPYING COPYING
52 test_expect_success
'commiting the change' '
53 git commit -m move-in -a
56 test_expect_success
'checking the commit' '
57 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
58 grep "^R100..*path1/COPYING..*path0/COPYING" actual
61 test_expect_success
'mv --dry-run does not move file' '
62 git mv -n path0/COPYING MOVED &&
63 test -f path0/COPYING &&
67 test_expect_success
'checking -k on non-existing file' '
68 git mv -k idontexist path0
71 test_expect_success
'checking -k on untracked file' '
73 git mv -k untracked1 path0 &&
75 test ! -f path0/untracked1
78 test_expect_success
'checking -k on multiple untracked files' '
80 git mv -k untracked1 untracked2 path0 &&
83 test ! -f path0/untracked1 &&
84 test ! -f path0/untracked2
87 test_expect_success
'checking -f on untracked file with existing target' '
89 test_must_fail git mv -f untracked1 path0 &&
90 test ! -f .git/index.lock &&
92 test -f path0/untracked1
95 # clean up the mess in case bad things happen
96 rm -f idontexist untracked1 untracked2 \
97 path
0/idontexist path
0/untracked1 path
0/untracked2 \
101 test_expect_success
'moving to absent target with trailing slash' '
102 test_must_fail git mv path0/COPYING no-such-dir/ &&
103 test_must_fail git mv path0/COPYING no-such-dir// &&
104 git mv path0/ no-such-dir/ &&
105 test_path_is_dir no-such-dir
108 test_expect_success
'clean up' '
112 test_expect_success
'moving to existing untracked target with trailing slash' '
114 git mv path0/ path1/ &&
115 test_path_is_dir path1/path0/
118 test_expect_success
'moving to existing tracked target with trailing slash' '
120 >path2/file && git add path2/file &&
121 git mv path1/path0/ path2/ &&
122 test_path_is_dir path2/path0/
125 test_expect_success
'clean up' '
129 test_expect_success
'adding another file' '
130 COPYING_test_data | tr A-Za-z N-ZA-Mn-za-m >path0/README &&
131 git add path0/README &&
132 git commit -m add2 -a
135 test_expect_success
'moving whole subdirectory' '
139 test_expect_success
'commiting the change' '
140 git commit -m dir-move -a
143 test_expect_success
'checking the commit' '
144 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
145 grep "^R100..*path0/COPYING..*path2/COPYING" actual &&
146 grep "^R100..*path0/README..*path2/README" actual
149 test_expect_success
'succeed when source is a prefix of destination' '
150 git mv path2/COPYING path2/COPYING-renamed
153 test_expect_success
'moving whole subdirectory into subdirectory' '
157 test_expect_success
'commiting the change' '
158 git commit -m dir-move -a
161 test_expect_success
'checking the commit' '
162 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
163 grep "^R100..*path2/COPYING..*path1/path2/COPYING" actual &&
164 grep "^R100..*path2/README..*path1/path2/README" actual
167 test_expect_success
'do not move directory over existing directory' '
170 test_must_fail git mv path2 path0
173 test_expect_success
'move into "."' '
174 git mv path1/path2/ .
177 test_expect_success
"Michael Cassar's test case" '
178 rm -fr .git papers partA &&
180 mkdir -p papers/unsorted papers/all-papers partA &&
181 echo a >papers/unsorted/Thesis.pdf &&
182 echo b >partA/outline.txt &&
183 echo c >papers/unsorted/_another &&
184 git add papers partA &&
185 T1=$(git write-tree) &&
187 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
189 T=$(git write-tree) &&
190 git ls-tree -r $T | verbose grep partA/outline.txt
193 rm -fr papers partA path?
195 test_expect_success
"Sergey Vlasov's test case" '
202 git commit -m "initial" &&
206 test_expect_success
'absolute pathname' '
211 test_create_repo one &&
217 git mv sub "$(pwd)/in" &&
220 git ls-files --error-unmatch in/file
224 test_expect_success
'absolute pathname outside should fail' '
230 test_create_repo one &&
236 test_must_fail git mv sub "$out/out" &&
239 git ls-files --error-unmatch sub/file
243 test_expect_success
'git mv to move multiple sources into a directory' '
244 rm -fr .git && git init &&
249 git mv dir/a.txt dir/b.txt other &&
250 git ls-files >actual &&
251 cat >expect <<-\EOF &&
255 test_cmp expect actual
258 test_expect_success
'git mv should not change sha1 of moved cache entry' '
263 entry="$(git ls-files --stage dirty | cut -f 1)" &&
264 git mv dirty dirty2 &&
265 test "$entry" = "$(git ls-files --stage dirty2 | cut -f 1)" &&
267 git mv dirty2 dirty &&
268 test "$entry" = "$(git ls-files --stage dirty | cut -f 1)"
273 # NB: This test is about the error message
274 # as well as the failure.
275 test_expect_success
'git mv error on conflicted file' '
279 test_when_finished "rm -f conflict" &&
280 cfhash=$(git hash-object -w conflict) &&
281 q_to_tab <<-EOF | git update-index --index-info &&
283 100644 $cfhash 1Qconflict
286 test_must_fail git mv conflict newname 2>actual &&
287 test_i18ngrep "conflicted" actual
290 test_expect_success
'git mv should overwrite symlink to a file' '
294 test_ln_s_add moved symlink &&
296 test_must_fail git mv moved symlink &&
297 git mv -f moved symlink &&
300 test "$(cat symlink)" = 1 &&
301 git update-index --refresh &&
302 git diff-files --quiet
307 test_expect_success
'git mv should overwrite file with a symlink' '
311 test_ln_s_add moved symlink &&
313 test_must_fail git mv symlink moved &&
314 git mv -f symlink moved &&
316 git update-index --refresh &&
317 git diff-files --quiet
320 test_expect_success SYMLINKS
'check moved symlink' '
326 test_expect_success
'setup submodule' '
327 git commit -m initial &&
329 git submodule add ./. sub &&
330 echo content >file &&
332 git commit -m "added sub and file" &&
333 mkdir -p deep/directory/hierarchy &&
334 git submodule add ./. deep/directory/hierarchy/sub &&
335 git commit -m "added another submodule" &&
339 test_expect_success
'git mv cannot move a submodule in a file' '
340 test_must_fail git mv sub file
343 test_expect_success
'git mv moves a submodule with a .git directory and no .gitmodules' '
344 entry="$(git ls-files --stage sub | cut -f 1)" &&
345 git rm .gitmodules &&
349 cp -R -P -p ../.git/modules/sub .git &&
350 GIT_WORK_TREE=. git config --unset core.worktree
353 git mv sub mod/sub &&
355 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
356 git -C mod/sub status &&
357 git update-index --refresh &&
358 git diff-files --quiet
361 test_expect_success
'git mv moves a submodule with a .git directory and .gitmodules' '
364 git submodule update &&
365 entry="$(git ls-files --stage sub | cut -f 1)" &&
369 cp -R -P -p ../.git/modules/sub .git &&
370 GIT_WORK_TREE=. git config --unset core.worktree
373 git mv sub mod/sub &&
375 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
376 git -C mod/sub status &&
377 echo mod/sub >expected &&
378 git config -f .gitmodules submodule.sub.path >actual &&
379 test_cmp expected actual &&
380 git update-index --refresh &&
381 git diff-files --quiet
384 test_expect_success
'git mv moves a submodule with gitfile' '
387 git submodule update &&
388 entry="$(git ls-files --stage sub | cut -f 1)" &&
390 git -C mod mv ../sub/ . &&
392 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
393 git -C mod/sub status &&
394 echo mod/sub >expected &&
395 git config -f .gitmodules submodule.sub.path >actual &&
396 test_cmp expected actual &&
397 git update-index --refresh &&
398 git diff-files --quiet
401 test_expect_success
'mv does not complain when no .gitmodules file is found' '
404 git submodule update &&
405 git rm .gitmodules &&
406 entry="$(git ls-files --stage sub | cut -f 1)" &&
408 git mv sub mod/sub 2>actual.err &&
409 test_must_be_empty actual.err &&
411 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
412 git -C mod/sub status &&
413 git update-index --refresh &&
414 git diff-files --quiet
417 test_expect_success
'mv will error out on a modified .gitmodules file unless staged' '
420 git submodule update &&
421 git config -f .gitmodules foo.bar true &&
422 entry="$(git ls-files --stage sub | cut -f 1)" &&
424 test_must_fail git mv sub mod/sub 2>actual.err &&
425 test -s actual.err &&
427 git diff-files --quiet -- sub &&
428 git add .gitmodules &&
429 git mv sub mod/sub 2>actual.err &&
430 test_must_be_empty actual.err &&
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
438 test_expect_success
'mv issues a warning when section is not found in .gitmodules' '
441 git submodule update &&
442 git config -f .gitmodules --remove-section submodule.sub &&
443 git add .gitmodules &&
444 entry="$(git ls-files --stage sub | cut -f 1)" &&
445 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
447 git mv sub mod/sub 2>actual.err &&
448 test_cmp expect.err actual.err &&
450 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
451 git -C mod/sub status &&
452 git update-index --refresh &&
453 git diff-files --quiet
456 test_expect_success
'mv --dry-run does not touch the submodule or .gitmodules' '
459 git submodule update &&
461 git mv -n sub mod/sub 2>actual.err &&
463 git diff-index --exit-code HEAD &&
464 git update-index --refresh &&
465 git diff-files --quiet -- sub .gitmodules
468 test_expect_success
'checking out a commit before submodule moved needs manual updates' '
470 git commit -m "moved sub to sub2" &&
471 git checkout -q HEAD^ 2>actual &&
472 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
473 git status -s sub2 >actual &&
474 echo "?? sub2/" >expected &&
475 test_cmp expected actual &&
476 ! test -f sub/.git &&
478 git submodule update &&
481 git diff-index --exit-code HEAD &&
482 git update-index --refresh &&
483 git diff-files --quiet -- sub .gitmodules &&
484 git status -s sub2 >actual &&
485 test_must_be_empty actual
488 test_expect_success
'mv -k does not accidentally destroy submodules' '
489 git checkout submodule &&
491 git mv -k dummy sub dest &&
492 git status --porcelain >actual &&
493 grep "^R sub -> dest/sub" actual &&
498 test_expect_success
'moving a submodule in nested directories' '
501 git mv directory ../ &&
502 # git status would fail if the update of linking git dir to
503 # work dir of the submodule failed.
505 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
506 echo "directory/hierarchy/sub" >../expect
508 test_cmp expect actual
511 test_expect_success
'moving nested submodules' '
512 git commit -am "cleanup commit" &&
513 mkdir sub_nested_nested &&
515 cd sub_nested_nested &&
519 git commit -m "nested level 2"
527 git commit -m "nested level 1" &&
528 git submodule add ../sub_nested_nested &&
529 git commit -m "add nested level 2"
531 git submodule add ./sub_nested nested_move &&
532 git commit -m "add nested_move" &&
533 git submodule update --init --recursive &&
534 git mv nested_move sub_nested_moved &&