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
'move into "."' '
178 git mv path1/path2/ .
181 test_expect_success
"Michael Cassar's test case" '
182 rm -fr .git papers partA &&
184 mkdir -p papers/unsorted papers/all-papers partA &&
185 echo a >papers/unsorted/Thesis.pdf &&
186 echo b >partA/outline.txt &&
187 echo c >papers/unsorted/_another &&
188 git add papers partA &&
189 T1=$(git write-tree) &&
191 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
193 T=$(git write-tree) &&
194 git ls-tree -r $T >out &&
195 grep partA/outline.txt out
198 rm -fr papers partA path?
200 test_expect_success
"Sergey Vlasov's test case" '
207 git commit -m "initial" &&
211 test_expect_success
'absolute pathname' '
216 test_create_repo one &&
222 git mv sub "$(pwd)/in" &&
223 test_path_is_missing sub &&
224 test_path_is_dir in &&
225 git ls-files --error-unmatch in/file
229 test_expect_success
'absolute pathname outside should fail' '
235 test_create_repo one &&
241 test_must_fail git mv sub "$out/out" &&
242 test_path_is_dir sub &&
243 test_path_is_missing ../in &&
244 git ls-files --error-unmatch sub/file
248 test_expect_success
'git mv to move multiple sources into a directory' '
249 rm -fr .git && git init &&
254 git mv dir/a.txt dir/b.txt other &&
255 git ls-files >actual &&
256 cat >expect <<-\EOF &&
260 test_cmp expect actual
263 test_expect_success
'git mv should not change sha1 of moved cache entry' '
268 entry="$(index_at_path dirty)" &&
269 git mv dirty dirty2 &&
270 test "$entry" = "$(index_at_path dirty2)" &&
272 git mv dirty2 dirty &&
273 test "$entry" = "$(index_at_path dirty)"
278 # NB: This test is about the error message
279 # as well as the failure.
280 test_expect_success
'git mv error on conflicted file' '
284 test_when_finished "rm -f conflict" &&
285 cfhash=$(git hash-object -w conflict) &&
286 q_to_tab <<-EOF | git update-index --index-info &&
288 100644 $cfhash 1Qconflict
291 test_must_fail git mv conflict newname 2>actual &&
292 test_i18ngrep "conflicted" actual
295 test_expect_success
'git mv should overwrite symlink to a file' '
299 test_ln_s_add moved symlink &&
301 test_must_fail git mv moved symlink &&
302 git mv -f moved symlink &&
303 test_path_is_missing moved &&
304 test_path_is_file symlink &&
305 test "$(cat symlink)" = 1 &&
306 git update-index --refresh &&
307 git diff-files --quiet
312 test_expect_success
'git mv should overwrite file with a symlink' '
316 test_ln_s_add moved symlink &&
318 test_must_fail git mv symlink moved &&
319 git mv -f symlink moved &&
320 test_path_is_missing symlink &&
321 git update-index --refresh &&
322 git diff-files --quiet
325 test_expect_success SYMLINKS
'check moved symlink' '
326 test_path_is_symlink moved
331 test_expect_success
'setup submodule' '
332 test_config_global protocol.file.allow always &&
333 git commit -m initial &&
335 git submodule add ./. sub &&
336 echo content >file &&
338 git commit -m "added sub and file" &&
339 mkdir -p deep/directory/hierarchy &&
340 git submodule add ./. deep/directory/hierarchy/sub &&
341 git commit -m "added another submodule" &&
345 test_expect_success
'git mv cannot move a submodule in a file' '
346 test_must_fail git mv sub file
349 test_expect_success
'git mv moves a submodule with a .git directory and no .gitmodules' '
350 entry="$(index_at_path sub)" &&
351 git rm .gitmodules &&
355 cp -R -P -p ../.git/modules/sub .git &&
356 GIT_WORK_TREE=. git config --unset core.worktree
359 git mv sub mod/sub &&
360 test_path_is_missing sub &&
361 test "$entry" = "$(index_at_path mod/sub)" &&
362 git -C mod/sub status &&
363 git update-index --refresh &&
364 git diff-files --quiet
367 test_expect_success
'git mv moves a submodule with a .git directory and .gitmodules' '
370 git submodule update &&
371 entry="$(index_at_path sub)" &&
375 cp -R -P -p ../.git/modules/sub .git &&
376 GIT_WORK_TREE=. git config --unset core.worktree
379 git mv sub mod/sub &&
380 test_path_is_missing sub &&
381 test "$entry" = "$(index_at_path mod/sub)" &&
382 git -C mod/sub status &&
383 echo mod/sub >expected &&
384 git config -f .gitmodules submodule.sub.path >actual &&
385 test_cmp expected actual &&
386 git update-index --refresh &&
387 git diff-files --quiet
390 test_expect_success
'git mv moves a submodule with gitfile' '
393 git submodule update &&
394 entry="$(index_at_path sub)" &&
396 git -C mod mv ../sub/ . &&
397 test_path_is_missing sub &&
398 test "$entry" = "$(index_at_path mod/sub)" &&
399 git -C mod/sub status &&
400 echo mod/sub >expected &&
401 git config -f .gitmodules submodule.sub.path >actual &&
402 test_cmp expected actual &&
403 git update-index --refresh &&
404 git diff-files --quiet
407 test_expect_success
'mv does not complain when no .gitmodules file is found' '
410 git submodule update &&
411 git rm .gitmodules &&
412 entry="$(index_at_path sub)" &&
414 git mv sub mod/sub 2>actual.err &&
415 test_must_be_empty actual.err &&
416 test_path_is_missing sub &&
417 test "$entry" = "$(index_at_path mod/sub)" &&
418 git -C mod/sub status &&
419 git update-index --refresh &&
420 git diff-files --quiet
423 test_expect_success
'mv will error out on a modified .gitmodules file unless staged' '
426 git submodule update &&
427 git config -f .gitmodules foo.bar true &&
428 entry="$(index_at_path sub)" &&
430 test_must_fail git mv sub mod/sub 2>actual.err &&
431 test_file_not_empty actual.err &&
432 test_path_exists sub &&
433 git diff-files --quiet -- sub &&
434 git add .gitmodules &&
435 git mv sub mod/sub 2>actual.err &&
436 test_must_be_empty actual.err &&
437 test_path_is_missing sub &&
438 test "$entry" = "$(index_at_path mod/sub)" &&
439 git -C mod/sub status &&
440 git update-index --refresh &&
441 git diff-files --quiet
444 test_expect_success
'mv issues a warning when section is not found in .gitmodules' '
447 git submodule update &&
448 git config -f .gitmodules --remove-section submodule.sub &&
449 git add .gitmodules &&
450 entry="$(index_at_path sub)" &&
451 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
453 git mv sub mod/sub 2>actual.err &&
454 test_cmp expect.err actual.err &&
455 test_path_is_missing sub &&
456 test "$entry" = "$(index_at_path mod/sub)" &&
457 git -C mod/sub status &&
458 git update-index --refresh &&
459 git diff-files --quiet
462 test_expect_success
'mv --dry-run does not touch the submodule or .gitmodules' '
465 git submodule update &&
467 git mv -n sub mod/sub 2>actual.err &&
468 test_path_is_file sub/.git &&
469 git diff-index --exit-code HEAD &&
470 git update-index --refresh &&
471 git diff-files --quiet -- sub .gitmodules
474 test_expect_success
'checking out a commit before submodule moved needs manual updates' '
476 git commit -m "moved sub to sub2" &&
477 git checkout -q HEAD^ 2>actual &&
478 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
479 git status -s sub2 >actual &&
480 echo "?? sub2/" >expected &&
481 test_cmp expected actual &&
482 test_path_is_missing sub/.git &&
483 test_path_is_file sub2/.git &&
484 git submodule update &&
485 test_path_is_file sub/.git &&
487 git diff-index --exit-code HEAD &&
488 git update-index --refresh &&
489 git diff-files --quiet -- sub .gitmodules &&
490 git status -s sub2 >actual &&
491 test_must_be_empty actual
494 test_expect_success
'mv -k does not accidentally destroy submodules' '
495 git checkout submodule &&
497 git mv -k dummy sub dest &&
498 git status --porcelain >actual &&
499 grep "^R sub -> dest/sub" actual &&
504 test_expect_success
'moving a submodule in nested directories' '
507 git mv directory ../ &&
508 # git status would fail if the update of linking git dir to
509 # work dir of the submodule failed.
511 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
512 echo "directory/hierarchy/sub" >../expect
514 test_cmp expect actual
517 test_expect_success
'moving nested submodules' '
518 test_config_global protocol.file.allow always &&
519 git commit -am "cleanup commit" &&
520 mkdir sub_nested_nested &&
522 cd sub_nested_nested &&
526 git commit -m "nested level 2"
534 git commit -m "nested level 1" &&
535 git submodule add ../sub_nested_nested &&
536 git commit -m "add nested level 2"
538 git submodule add ./sub_nested nested_move &&
539 git commit -m "add nested_move" &&
540 git submodule update --init --recursive &&
541 git mv nested_move sub_nested_moved &&