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_path_is_file path0/COPYING &&
64 test_path_is_missing MOVED
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 &&
74 test_path_is_file untracked1 &&
75 test_path_is_missing path0/untracked1
78 test_expect_success
'checking -k on multiple untracked files' '
80 git mv -k untracked1 untracked2 path0 &&
81 test_path_is_file untracked1 &&
82 test_path_is_file untracked2 &&
83 test_path_is_missing path0/untracked1 &&
84 test_path_is_missing 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_path_is_missing .git/index.lock &&
91 test_path_is_file untracked1 &&
92 test_path_is_file 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" &&
218 test_path_is_missing sub &&
219 test_path_is_dir 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" &&
237 test_path_is_dir sub &&
238 test_path_is_missing ../in &&
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 &&
298 test_path_is_missing moved &&
299 test_path_is_file 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 &&
315 test_path_is_missing symlink &&
316 git update-index --refresh &&
317 git diff-files --quiet
320 test_expect_success SYMLINKS
'check moved symlink' '
321 test_path_is_symlink moved
326 test_expect_success
'setup submodule' '
327 test_config_global protocol.file.allow always &&
328 git commit -m initial &&
330 git submodule add ./. sub &&
331 echo content >file &&
333 git commit -m "added sub and file" &&
334 mkdir -p deep/directory/hierarchy &&
335 git submodule add ./. deep/directory/hierarchy/sub &&
336 git commit -m "added another submodule" &&
340 test_expect_success
'git mv cannot move a submodule in a file' '
341 test_must_fail git mv sub file
344 test_expect_success
'git mv moves a submodule with a .git directory and no .gitmodules' '
345 entry="$(git ls-files --stage sub | cut -f 1)" &&
346 git rm .gitmodules &&
350 cp -R -P -p ../.git/modules/sub .git &&
351 GIT_WORK_TREE=. git config --unset core.worktree
354 git mv sub mod/sub &&
355 test_path_is_missing sub &&
356 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
357 git -C mod/sub status &&
358 git update-index --refresh &&
359 git diff-files --quiet
362 test_expect_success
'git mv moves a submodule with a .git directory and .gitmodules' '
365 git submodule update &&
366 entry="$(git ls-files --stage sub | cut -f 1)" &&
370 cp -R -P -p ../.git/modules/sub .git &&
371 GIT_WORK_TREE=. git config --unset core.worktree
374 git mv sub mod/sub &&
375 test_path_is_missing sub &&
376 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
377 git -C mod/sub status &&
378 echo mod/sub >expected &&
379 git config -f .gitmodules submodule.sub.path >actual &&
380 test_cmp expected actual &&
381 git update-index --refresh &&
382 git diff-files --quiet
385 test_expect_success
'git mv moves a submodule with gitfile' '
388 git submodule update &&
389 entry="$(git ls-files --stage sub | cut -f 1)" &&
391 git -C mod mv ../sub/ . &&
392 test_path_is_missing sub &&
393 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
394 git -C mod/sub status &&
395 echo mod/sub >expected &&
396 git config -f .gitmodules submodule.sub.path >actual &&
397 test_cmp expected actual &&
398 git update-index --refresh &&
399 git diff-files --quiet
402 test_expect_success
'mv does not complain when no .gitmodules file is found' '
405 git submodule update &&
406 git rm .gitmodules &&
407 entry="$(git ls-files --stage sub | cut -f 1)" &&
409 git mv sub mod/sub 2>actual.err &&
410 test_must_be_empty actual.err &&
411 test_path_is_missing sub &&
412 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
413 git -C mod/sub status &&
414 git update-index --refresh &&
415 git diff-files --quiet
418 test_expect_success
'mv will error out on a modified .gitmodules file unless staged' '
421 git submodule update &&
422 git config -f .gitmodules foo.bar true &&
423 entry="$(git ls-files --stage sub | cut -f 1)" &&
425 test_must_fail git mv sub mod/sub 2>actual.err &&
426 test_file_not_empty actual.err &&
427 test_path_exists sub &&
428 git diff-files --quiet -- sub &&
429 git add .gitmodules &&
430 git mv sub mod/sub 2>actual.err &&
431 test_must_be_empty actual.err &&
432 test_path_is_missing sub &&
433 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
434 git -C mod/sub status &&
435 git update-index --refresh &&
436 git diff-files --quiet
439 test_expect_success
'mv issues a warning when section is not found in .gitmodules' '
442 git submodule update &&
443 git config -f .gitmodules --remove-section submodule.sub &&
444 git add .gitmodules &&
445 entry="$(git ls-files --stage sub | cut -f 1)" &&
446 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
448 git mv sub mod/sub 2>actual.err &&
449 test_cmp expect.err actual.err &&
450 test_path_is_missing sub &&
451 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
452 git -C mod/sub status &&
453 git update-index --refresh &&
454 git diff-files --quiet
457 test_expect_success
'mv --dry-run does not touch the submodule or .gitmodules' '
460 git submodule update &&
462 git mv -n sub mod/sub 2>actual.err &&
463 test_path_is_file sub/.git &&
464 git diff-index --exit-code HEAD &&
465 git update-index --refresh &&
466 git diff-files --quiet -- sub .gitmodules
469 test_expect_success
'checking out a commit before submodule moved needs manual updates' '
471 git commit -m "moved sub to sub2" &&
472 git checkout -q HEAD^ 2>actual &&
473 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
474 git status -s sub2 >actual &&
475 echo "?? sub2/" >expected &&
476 test_cmp expected actual &&
477 test_path_is_missing sub/.git &&
478 test_path_is_file sub2/.git &&
479 git submodule update &&
480 test_path_is_file sub/.git &&
482 git diff-index --exit-code HEAD &&
483 git update-index --refresh &&
484 git diff-files --quiet -- sub .gitmodules &&
485 git status -s sub2 >actual &&
486 test_must_be_empty actual
489 test_expect_success
'mv -k does not accidentally destroy submodules' '
490 git checkout submodule &&
492 git mv -k dummy sub dest &&
493 git status --porcelain >actual &&
494 grep "^R sub -> dest/sub" actual &&
499 test_expect_success
'moving a submodule in nested directories' '
502 git mv directory ../ &&
503 # git status would fail if the update of linking git dir to
504 # work dir of the submodule failed.
506 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
507 echo "directory/hierarchy/sub" >../expect
509 test_cmp expect actual
512 test_expect_success
'moving nested submodules' '
513 test_config_global protocol.file.allow always &&
514 git commit -am "cleanup commit" &&
515 mkdir sub_nested_nested &&
517 cd sub_nested_nested &&
521 git commit -m "nested level 2"
529 git commit -m "nested level 1" &&
530 git submodule add ../sub_nested_nested &&
531 git commit -m "add nested level 2"
533 git submodule add ./sub_nested nested_move &&
534 git commit -m "add nested_move" &&
535 git submodule update --init --recursive &&
536 git mv nested_move sub_nested_moved &&