3 test_description
='git mv in subdirs'
5 .
"$TEST_DIRECTORY"/lib-diff-data.sh
7 test_expect_success
'prepare reference tree' '
9 COPYING_test_data >path0/COPYING &&
10 git add path0/COPYING &&
14 test_expect_success
'moving the file out of subdirectory' '
15 git -C path0 mv COPYING ../path1/COPYING
19 test_expect_success
'commiting the change' '
20 git commit -m move-out -a
23 test_expect_success
'checking the commit' '
24 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
25 grep "^R100..*path0/COPYING..*path1/COPYING" actual
28 test_expect_success
'moving the file back into subdirectory' '
29 git -C path0 mv ../path1/COPYING COPYING
33 test_expect_success
'commiting the change' '
34 git commit -m move-in -a
37 test_expect_success
'checking the commit' '
38 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
39 grep "^R100..*path1/COPYING..*path0/COPYING" actual
42 test_expect_success
'mv --dry-run does not move file' '
43 git mv -n path0/COPYING MOVED &&
44 test -f path0/COPYING &&
48 test_expect_success
'checking -k on non-existing file' '
49 git mv -k idontexist path0
52 test_expect_success
'checking -k on untracked file' '
54 git mv -k untracked1 path0 &&
56 test ! -f path0/untracked1
59 test_expect_success
'checking -k on multiple untracked files' '
61 git mv -k untracked1 untracked2 path0 &&
64 test ! -f path0/untracked1 &&
65 test ! -f path0/untracked2
68 test_expect_success
'checking -f on untracked file with existing target' '
70 test_must_fail git mv -f untracked1 path0 &&
71 test ! -f .git/index.lock &&
73 test -f path0/untracked1
76 # clean up the mess in case bad things happen
77 rm -f idontexist untracked1 untracked2 \
78 path
0/idontexist path
0/untracked1 path
0/untracked2 \
82 test_expect_success
'moving to absent target with trailing slash' '
83 test_must_fail git mv path0/COPYING no-such-dir/ &&
84 test_must_fail git mv path0/COPYING no-such-dir// &&
85 git mv path0/ no-such-dir/ &&
86 test_path_is_dir no-such-dir
89 test_expect_success
'clean up' '
93 test_expect_success
'moving to existing untracked target with trailing slash' '
95 git mv path0/ path1/ &&
96 test_path_is_dir path1/path0/
99 test_expect_success
'moving to existing tracked target with trailing slash' '
101 >path2/file && git add path2/file &&
102 git mv path1/path0/ path2/ &&
103 test_path_is_dir path2/path0/
106 test_expect_success
'clean up' '
110 test_expect_success
'adding another file' '
111 COPYING_test_data | tr A-Za-z N-ZA-Mn-za-m >path0/README &&
112 git add path0/README &&
113 git commit -m add2 -a
116 test_expect_success
'moving whole subdirectory' '
120 test_expect_success
'commiting the change' '
121 git commit -m dir-move -a
124 test_expect_success
'checking the commit' '
125 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
126 grep "^R100..*path0/COPYING..*path2/COPYING" actual &&
127 grep "^R100..*path0/README..*path2/README" actual
130 test_expect_success
'succeed when source is a prefix of destination' '
131 git mv path2/COPYING path2/COPYING-renamed
134 test_expect_success
'moving whole subdirectory into subdirectory' '
138 test_expect_success
'commiting the change' '
139 git commit -m dir-move -a
142 test_expect_success
'checking the commit' '
143 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
144 grep "^R100..*path2/COPYING..*path1/path2/COPYING" actual &&
145 grep "^R100..*path2/README..*path1/path2/README" actual
148 test_expect_success
'do not move directory over existing directory' '
151 test_must_fail git mv path2 path0
154 test_expect_success
'move into "."' '
155 git mv path1/path2/ .
158 test_expect_success
"Michael Cassar's test case" '
159 rm -fr .git papers partA &&
161 mkdir -p papers/unsorted papers/all-papers partA &&
162 echo a >papers/unsorted/Thesis.pdf &&
163 echo b >partA/outline.txt &&
164 echo c >papers/unsorted/_another &&
165 git add papers partA &&
166 T1=$(git write-tree) &&
168 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
170 T=$(git write-tree) &&
171 git ls-tree -r $T | verbose grep partA/outline.txt
174 rm -fr papers partA path?
176 test_expect_success
"Sergey Vlasov's test case" '
183 git commit -m "initial" &&
187 test_expect_success
'absolute pathname' '
192 test_create_repo one &&
198 git mv sub "$(pwd)/in" &&
201 git ls-files --error-unmatch in/file
205 test_expect_success
'absolute pathname outside should fail' '
211 test_create_repo one &&
217 test_must_fail git mv sub "$out/out" &&
220 git ls-files --error-unmatch sub/file
224 test_expect_success
'git mv to move multiple sources into a directory' '
225 rm -fr .git && git init &&
230 git mv dir/a.txt dir/b.txt other &&
231 git ls-files >actual &&
232 cat >expect <<-\EOF &&
236 test_cmp expect actual
239 test_expect_success
'git mv should not change sha1 of moved cache entry' '
244 entry="$(git ls-files --stage dirty | cut -f 1)" &&
245 git mv dirty dirty2 &&
246 test "$entry" = "$(git ls-files --stage dirty2 | cut -f 1)" &&
248 git mv dirty2 dirty &&
249 test "$entry" = "$(git ls-files --stage dirty | cut -f 1)"
254 # NB: This test is about the error message
255 # as well as the failure.
256 test_expect_success
'git mv error on conflicted file' '
260 test_when_finished "rm -f conflict" &&
261 cfhash=$(git hash-object -w conflict) &&
262 q_to_tab <<-EOF | git update-index --index-info &&
264 100644 $cfhash 1Qconflict
267 test_must_fail git mv conflict newname 2>actual &&
268 test_i18ngrep "conflicted" actual
271 test_expect_success
'git mv should overwrite symlink to a file' '
275 test_ln_s_add moved symlink &&
277 test_must_fail git mv moved symlink &&
278 git mv -f moved symlink &&
281 test "$(cat symlink)" = 1 &&
282 git update-index --refresh &&
283 git diff-files --quiet
288 test_expect_success
'git mv should overwrite file with a symlink' '
292 test_ln_s_add moved symlink &&
294 test_must_fail git mv symlink moved &&
295 git mv -f symlink moved &&
297 git update-index --refresh &&
298 git diff-files --quiet
301 test_expect_success SYMLINKS
'check moved symlink' '
307 test_expect_success
'setup submodule' '
308 git commit -m initial &&
310 git submodule add ./. sub &&
311 echo content >file &&
313 git commit -m "added sub and file" &&
314 mkdir -p deep/directory/hierarchy &&
315 git submodule add ./. deep/directory/hierarchy/sub &&
316 git commit -m "added another submodule" &&
320 test_expect_success
'git mv cannot move a submodule in a file' '
321 test_must_fail git mv sub file
324 test_expect_success
'git mv moves a submodule with a .git directory and no .gitmodules' '
325 entry="$(git ls-files --stage sub | cut -f 1)" &&
326 git rm .gitmodules &&
330 cp -R -P -p ../.git/modules/sub .git &&
331 GIT_WORK_TREE=. git config --unset core.worktree
334 git mv sub mod/sub &&
336 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
337 git -C mod/sub status &&
338 git update-index --refresh &&
339 git diff-files --quiet
342 test_expect_success
'git mv moves a submodule with a .git directory and .gitmodules' '
345 git submodule update &&
346 entry="$(git ls-files --stage sub | cut -f 1)" &&
350 cp -R -P -p ../.git/modules/sub .git &&
351 GIT_WORK_TREE=. git config --unset core.worktree
354 git mv sub mod/sub &&
356 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
357 git -C mod/sub status &&
358 echo mod/sub >expected &&
359 git config -f .gitmodules submodule.sub.path >actual &&
360 test_cmp expected actual &&
361 git update-index --refresh &&
362 git diff-files --quiet
365 test_expect_success
'git mv moves a submodule with gitfile' '
368 git submodule update &&
369 entry="$(git ls-files --stage sub | cut -f 1)" &&
371 git -C mod mv ../sub/ . &&
373 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
374 git -C mod/sub status &&
375 echo mod/sub >expected &&
376 git config -f .gitmodules submodule.sub.path >actual &&
377 test_cmp expected actual &&
378 git update-index --refresh &&
379 git diff-files --quiet
382 test_expect_success
'mv does not complain when no .gitmodules file is found' '
385 git submodule update &&
386 git rm .gitmodules &&
387 entry="$(git ls-files --stage sub | cut -f 1)" &&
389 git mv sub mod/sub 2>actual.err &&
390 test_must_be_empty actual.err &&
392 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
393 git -C mod/sub status &&
394 git update-index --refresh &&
395 git diff-files --quiet
398 test_expect_success
'mv will error out on a modified .gitmodules file unless staged' '
401 git submodule update &&
402 git config -f .gitmodules foo.bar true &&
403 entry="$(git ls-files --stage sub | cut -f 1)" &&
405 test_must_fail git mv sub mod/sub 2>actual.err &&
406 test -s actual.err &&
408 git diff-files --quiet -- sub &&
409 git add .gitmodules &&
410 git mv sub mod/sub 2>actual.err &&
411 test_must_be_empty actual.err &&
413 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
414 git -C mod/sub status &&
415 git update-index --refresh &&
416 git diff-files --quiet
419 test_expect_success
'mv issues a warning when section is not found in .gitmodules' '
422 git submodule update &&
423 git config -f .gitmodules --remove-section submodule.sub &&
424 git add .gitmodules &&
425 entry="$(git ls-files --stage sub | cut -f 1)" &&
426 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
428 git mv sub mod/sub 2>actual.err &&
429 test_cmp expect.err actual.err &&
431 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
432 git -C mod/sub status &&
433 git update-index --refresh &&
434 git diff-files --quiet
437 test_expect_success
'mv --dry-run does not touch the submodule or .gitmodules' '
440 git submodule update &&
442 git mv -n sub mod/sub 2>actual.err &&
444 git diff-index --exit-code HEAD &&
445 git update-index --refresh &&
446 git diff-files --quiet -- sub .gitmodules
449 test_expect_success
'checking out a commit before submodule moved needs manual updates' '
451 git commit -m "moved sub to sub2" &&
452 git checkout -q HEAD^ 2>actual &&
453 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
454 git status -s sub2 >actual &&
455 echo "?? sub2/" >expected &&
456 test_cmp expected actual &&
457 ! test -f sub/.git &&
459 git submodule update &&
462 git diff-index --exit-code HEAD &&
463 git update-index --refresh &&
464 git diff-files --quiet -- sub .gitmodules &&
465 git status -s sub2 >actual &&
466 test_must_be_empty actual
469 test_expect_success
'mv -k does not accidentally destroy submodules' '
470 git checkout submodule &&
472 git mv -k dummy sub dest &&
473 git status --porcelain >actual &&
474 grep "^R sub -> dest/sub" actual &&
479 test_expect_success
'moving a submodule in nested directories' '
482 git mv directory ../ &&
483 # git status would fail if the update of linking git dir to
484 # work dir of the submodule failed.
486 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
487 echo "directory/hierarchy/sub" >../expect
489 test_cmp expect actual
492 test_expect_success
'moving nested submodules' '
493 git commit -am "cleanup commit" &&
494 mkdir sub_nested_nested &&
496 cd sub_nested_nested &&
500 git commit -m "nested level 2"
508 git commit -m "nested level 1" &&
509 git submodule add ../sub_nested_nested &&
510 git commit -m "add nested level 2"
512 git submodule add ./sub_nested nested_move &&
513 git commit -m "add nested_move" &&
514 git submodule update --init --recursive &&
515 git mv nested_move sub_nested_moved &&