3 test_description
='git mv in subdirs'
5 TEST_PASSES_SANITIZE_LEAK
=true
7 .
"$TEST_DIRECTORY"/lib-diff-data.sh
10 git ls-files
--format='%(objectmode) %(objectname) %(stage)' "$@"
13 test_expect_success
'mv -f refreshes updated index entry' '
21 # Wait one second to ensure ctime of rename will differ from original
22 # file creation ctime.
25 git reset --merge HEAD &&
27 # Verify the index has been reset
28 git diff-files >out &&
29 test_must_be_empty out
32 test_expect_success
'prepare reference tree' '
34 COPYING_test_data >path0/COPYING &&
35 git add path0/COPYING &&
39 test_expect_success
'moving the file out of subdirectory' '
40 git -C path0 mv COPYING ../path1/COPYING
44 test_expect_success
'commiting the change' '
45 git commit -m move-out -a
48 test_expect_success
'checking the commit' '
49 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
50 grep "^R100..*path0/COPYING..*path1/COPYING" actual
53 test_expect_success
'moving the file back into subdirectory' '
54 git -C path0 mv ../path1/COPYING COPYING
58 test_expect_success
'commiting the change' '
59 git commit -m move-in -a
62 test_expect_success
'checking the commit' '
63 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
64 grep "^R100..*path1/COPYING..*path0/COPYING" actual
67 test_expect_success
'mv --dry-run does not move file' '
68 git mv -n path0/COPYING MOVED &&
69 test_path_is_file path0/COPYING &&
70 test_path_is_missing MOVED
73 test_expect_success
'checking -k on non-existing file' '
74 git mv -k idontexist path0
77 test_expect_success
'checking -k on untracked file' '
79 git mv -k untracked1 path0 &&
80 test_path_is_file untracked1 &&
81 test_path_is_missing path0/untracked1
84 test_expect_success
'checking -k on multiple untracked files' '
86 git mv -k untracked1 untracked2 path0 &&
87 test_path_is_file untracked1 &&
88 test_path_is_file untracked2 &&
89 test_path_is_missing path0/untracked1 &&
90 test_path_is_missing path0/untracked2
93 test_expect_success
'checking -f on untracked file with existing target' '
95 test_must_fail git mv -f untracked1 path0 &&
96 test_path_is_missing .git/index.lock &&
97 test_path_is_file untracked1 &&
98 test_path_is_file path0/untracked1
101 # clean up the mess in case bad things happen
102 rm -f idontexist untracked1 untracked2 \
103 path
0/idontexist path
0/untracked1 path
0/untracked2 \
107 test_expect_success
'moving to absent target with trailing slash' '
108 test_must_fail git mv path0/COPYING no-such-dir/ &&
109 test_must_fail git mv path0/COPYING no-such-dir// &&
110 git mv path0/ no-such-dir/ &&
111 test_path_is_dir no-such-dir
114 test_expect_success
'clean up' '
118 test_expect_success
'moving to existing untracked target with trailing slash' '
120 git mv path0/ path1/ &&
121 test_path_is_dir path1/path0/
124 test_expect_success
'moving to existing tracked target with trailing slash' '
126 >path2/file && git add path2/file &&
127 git mv path1/path0/ path2/ &&
128 test_path_is_dir path2/path0/
131 test_expect_success
'clean up' '
135 test_expect_success
'adding another file' '
136 COPYING_test_data | tr A-Za-z N-ZA-Mn-za-m >path0/README &&
137 git add path0/README &&
138 git commit -m add2 -a
141 test_expect_success
'moving whole subdirectory' '
145 test_expect_success
'commiting the change' '
146 git commit -m dir-move -a
149 test_expect_success
'checking the commit' '
150 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
151 grep "^R100..*path0/COPYING..*path2/COPYING" actual &&
152 grep "^R100..*path0/README..*path2/README" actual
155 test_expect_success
'succeed when source is a prefix of destination' '
156 git mv path2/COPYING path2/COPYING-renamed
159 test_expect_success
'moving whole subdirectory into subdirectory' '
163 test_expect_success
'commiting the change' '
164 git commit -m dir-move -a
167 test_expect_success
'checking the commit' '
168 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
169 grep "^R100..*path2/COPYING..*path1/path2/COPYING" actual &&
170 grep "^R100..*path2/README..*path1/path2/README" actual
173 test_expect_success
'do not move directory over existing directory' '
176 test_must_fail git mv path2 path0
179 test_expect_success
'rename directory to non-existing directory' '
183 git mv dir-a non-existing-dir
186 test_expect_success
'move into "."' '
187 git mv path1/path2/ .
190 test_expect_success
"Michael Cassar's test case" '
191 rm -fr .git papers partA &&
193 mkdir -p papers/unsorted papers/all-papers partA &&
194 echo a >papers/unsorted/Thesis.pdf &&
195 echo b >partA/outline.txt &&
196 echo c >papers/unsorted/_another &&
197 git add papers partA &&
198 T1=$(git write-tree) &&
200 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
202 T=$(git write-tree) &&
203 git ls-tree -r $T >out &&
204 grep partA/outline.txt out
207 rm -fr papers partA path?
209 test_expect_success
"Sergey Vlasov's test case" '
216 git commit -m "initial" &&
220 test_expect_success
'absolute pathname' '
225 test_create_repo one &&
231 git mv sub "$(pwd)/in" &&
232 test_path_is_missing sub &&
233 test_path_is_dir in &&
234 git ls-files --error-unmatch in/file
238 test_expect_success
'absolute pathname outside should fail' '
244 test_create_repo one &&
250 test_must_fail git mv sub "$out/out" &&
251 test_path_is_dir sub &&
252 test_path_is_missing ../in &&
253 git ls-files --error-unmatch sub/file
257 test_expect_success
'git mv to move multiple sources into a directory' '
258 rm -fr .git && git init &&
263 git mv dir/a.txt dir/b.txt other &&
264 git ls-files >actual &&
265 cat >expect <<-\EOF &&
269 test_cmp expect actual
272 test_expect_success
'git mv should not change sha1 of moved cache entry' '
277 entry="$(index_at_path dirty)" &&
278 git mv dirty dirty2 &&
279 test "$entry" = "$(index_at_path dirty2)" &&
281 git mv dirty2 dirty &&
282 test "$entry" = "$(index_at_path dirty)"
287 # NB: This test is about the error message
288 # as well as the failure.
289 test_expect_success
'git mv error on conflicted file' '
293 test_when_finished "rm -f conflict" &&
294 cfhash=$(git hash-object -w conflict) &&
295 q_to_tab <<-EOF | git update-index --index-info &&
297 100644 $cfhash 1Qconflict
300 test_must_fail git mv conflict newname 2>actual &&
301 test_grep "conflicted" actual
304 test_expect_success
'git mv should overwrite symlink to a file' '
308 test_ln_s_add moved symlink &&
310 test_must_fail git mv moved symlink &&
311 git mv -f moved symlink &&
312 test_path_is_missing moved &&
313 test_path_is_file symlink &&
314 test "$(cat symlink)" = 1 &&
315 git update-index --refresh &&
316 git diff-files --quiet
321 test_expect_success
'git mv should overwrite file with a symlink' '
325 test_ln_s_add moved symlink &&
327 test_must_fail git mv symlink moved &&
328 git mv -f symlink moved &&
329 test_path_is_missing symlink &&
330 git update-index --refresh &&
331 git diff-files --quiet
334 test_expect_success SYMLINKS
'check moved symlink' '
335 test_path_is_symlink moved
340 test_expect_success
'setup submodule' '
341 test_config_global protocol.file.allow always &&
342 git commit -m initial &&
344 git submodule add ./. sub &&
345 echo content >file &&
347 git commit -m "added sub and file" &&
348 mkdir -p deep/directory/hierarchy &&
349 git submodule add ./. deep/directory/hierarchy/sub &&
350 git commit -m "added another submodule" &&
354 test_expect_success
'git mv cannot move a submodule in a file' '
355 test_must_fail git mv sub file
358 test_expect_success
'git mv moves a submodule with a .git directory and no .gitmodules' '
359 entry="$(index_at_path sub)" &&
360 git rm .gitmodules &&
364 cp -R -P -p ../.git/modules/sub .git &&
365 GIT_WORK_TREE=. git config --unset core.worktree
368 git mv sub mod/sub &&
369 test_path_is_missing sub &&
370 test "$entry" = "$(index_at_path mod/sub)" &&
371 git -C mod/sub status &&
372 git update-index --refresh &&
373 git diff-files --quiet
376 test_expect_success
'git mv moves a submodule with a .git directory and .gitmodules' '
379 git submodule update &&
380 entry="$(index_at_path sub)" &&
384 cp -R -P -p ../.git/modules/sub .git &&
385 GIT_WORK_TREE=. git config --unset core.worktree
388 git mv sub mod/sub &&
389 test_path_is_missing sub &&
390 test "$entry" = "$(index_at_path mod/sub)" &&
391 git -C mod/sub status &&
392 echo mod/sub >expected &&
393 git config -f .gitmodules submodule.sub.path >actual &&
394 test_cmp expected actual &&
395 git update-index --refresh &&
396 git diff-files --quiet
399 test_expect_success
'git mv moves a submodule with gitfile' '
402 git submodule update &&
403 entry="$(index_at_path sub)" &&
405 git -C mod mv ../sub/ . &&
406 test_path_is_missing sub &&
407 test "$entry" = "$(index_at_path mod/sub)" &&
408 git -C mod/sub status &&
409 echo mod/sub >expected &&
410 git config -f .gitmodules submodule.sub.path >actual &&
411 test_cmp expected actual &&
412 git update-index --refresh &&
413 git diff-files --quiet
416 test_expect_success
'mv does not complain when no .gitmodules file is found' '
419 git submodule update &&
420 git rm .gitmodules &&
421 entry="$(index_at_path sub)" &&
423 git mv sub mod/sub 2>actual.err &&
424 test_must_be_empty actual.err &&
425 test_path_is_missing sub &&
426 test "$entry" = "$(index_at_path mod/sub)" &&
427 git -C mod/sub status &&
428 git update-index --refresh &&
429 git diff-files --quiet
432 test_expect_success
'mv will error out on a modified .gitmodules file unless staged' '
435 git submodule update &&
436 git config -f .gitmodules foo.bar true &&
437 entry="$(index_at_path sub)" &&
439 test_must_fail git mv sub mod/sub 2>actual.err &&
440 test_file_not_empty actual.err &&
441 test_path_exists sub &&
442 git diff-files --quiet -- sub &&
443 git add .gitmodules &&
444 git mv sub mod/sub 2>actual.err &&
445 test_must_be_empty actual.err &&
446 test_path_is_missing sub &&
447 test "$entry" = "$(index_at_path mod/sub)" &&
448 git -C mod/sub status &&
449 git update-index --refresh &&
450 git diff-files --quiet
453 test_expect_success
'mv issues a warning when section is not found in .gitmodules' '
456 git submodule update &&
457 git config -f .gitmodules --remove-section submodule.sub &&
458 git add .gitmodules &&
459 entry="$(index_at_path sub)" &&
460 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
462 git mv sub mod/sub 2>actual.err &&
463 test_cmp expect.err actual.err &&
464 test_path_is_missing sub &&
465 test "$entry" = "$(index_at_path mod/sub)" &&
466 git -C mod/sub status &&
467 git update-index --refresh &&
468 git diff-files --quiet
471 test_expect_success
'mv --dry-run does not touch the submodule or .gitmodules' '
474 git submodule update &&
476 git mv -n sub mod/sub 2>actual.err &&
477 test_path_is_file sub/.git &&
478 git diff-index --exit-code HEAD &&
479 git update-index --refresh &&
480 git diff-files --quiet -- sub .gitmodules
483 test_expect_success
'checking out a commit before submodule moved needs manual updates' '
485 git commit -m "moved sub to sub2" &&
486 git checkout -q HEAD^ 2>actual &&
487 test_grep "^warning: unable to rmdir '\''sub2'\'':" actual &&
488 git status -s sub2 >actual &&
489 echo "?? sub2/" >expected &&
490 test_cmp expected actual &&
491 test_path_is_missing sub/.git &&
492 test_path_is_file sub2/.git &&
493 git submodule update &&
494 test_path_is_file sub/.git &&
496 git diff-index --exit-code HEAD &&
497 git update-index --refresh &&
498 git diff-files --quiet -- sub .gitmodules &&
499 git status -s sub2 >actual &&
500 test_must_be_empty actual
503 test_expect_success
'mv -k does not accidentally destroy submodules' '
504 git checkout submodule &&
506 git mv -k dummy sub dest &&
507 git status --porcelain >actual &&
508 grep "^R sub -> dest/sub" actual &&
513 test_expect_success
'moving a submodule in nested directories' '
516 git mv directory ../ &&
517 # git status would fail if the update of linking git dir to
518 # work dir of the submodule failed.
520 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
521 echo "directory/hierarchy/sub" >../expect
523 test_cmp expect actual
526 test_expect_success
'moving nested submodules' '
527 test_config_global protocol.file.allow always &&
528 git commit -am "cleanup commit" &&
529 mkdir sub_nested_nested &&
531 cd sub_nested_nested &&
535 git commit -m "nested level 2"
543 git commit -m "nested level 1" &&
544 git submodule add ../sub_nested_nested &&
545 git commit -m "add nested level 2"
547 git submodule add ./sub_nested nested_move &&
548 git commit -m "add nested_move" &&
549 git submodule update --init --recursive &&
550 git mv nested_move sub_nested_moved &&