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 test_config_global protocol.file.allow always &&
309 git commit -m initial &&
311 git submodule add ./. sub &&
312 echo content >file &&
314 git commit -m "added sub and file" &&
315 mkdir -p deep/directory/hierarchy &&
316 git submodule add ./. deep/directory/hierarchy/sub &&
317 git commit -m "added another submodule" &&
321 test_expect_success
'git mv cannot move a submodule in a file' '
322 test_must_fail git mv sub file
325 test_expect_success
'git mv moves a submodule with a .git directory and no .gitmodules' '
326 entry="$(git ls-files --stage sub | cut -f 1)" &&
327 git rm .gitmodules &&
331 cp -R -P -p ../.git/modules/sub .git &&
332 GIT_WORK_TREE=. git config --unset core.worktree
335 git mv sub mod/sub &&
337 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
338 git -C mod/sub status &&
339 git update-index --refresh &&
340 git diff-files --quiet
343 test_expect_success
'git mv moves a submodule with a .git directory and .gitmodules' '
346 git submodule update &&
347 entry="$(git ls-files --stage sub | cut -f 1)" &&
351 cp -R -P -p ../.git/modules/sub .git &&
352 GIT_WORK_TREE=. git config --unset core.worktree
355 git mv sub mod/sub &&
357 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
358 git -C mod/sub status &&
359 echo mod/sub >expected &&
360 git config -f .gitmodules submodule.sub.path >actual &&
361 test_cmp expected actual &&
362 git update-index --refresh &&
363 git diff-files --quiet
366 test_expect_success
'git mv moves a submodule with gitfile' '
369 git submodule update &&
370 entry="$(git ls-files --stage sub | cut -f 1)" &&
372 git -C mod mv ../sub/ . &&
374 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
375 git -C mod/sub status &&
376 echo mod/sub >expected &&
377 git config -f .gitmodules submodule.sub.path >actual &&
378 test_cmp expected actual &&
379 git update-index --refresh &&
380 git diff-files --quiet
383 test_expect_success
'mv does not complain when no .gitmodules file is found' '
386 git submodule update &&
387 git rm .gitmodules &&
388 entry="$(git ls-files --stage sub | cut -f 1)" &&
390 git mv sub mod/sub 2>actual.err &&
391 test_must_be_empty actual.err &&
393 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
394 git -C mod/sub status &&
395 git update-index --refresh &&
396 git diff-files --quiet
399 test_expect_success
'mv will error out on a modified .gitmodules file unless staged' '
402 git submodule update &&
403 git config -f .gitmodules foo.bar true &&
404 entry="$(git ls-files --stage sub | cut -f 1)" &&
406 test_must_fail git mv sub mod/sub 2>actual.err &&
407 test -s actual.err &&
409 git diff-files --quiet -- sub &&
410 git add .gitmodules &&
411 git mv sub mod/sub 2>actual.err &&
412 test_must_be_empty actual.err &&
414 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
415 git -C mod/sub status &&
416 git update-index --refresh &&
417 git diff-files --quiet
420 test_expect_success
'mv issues a warning when section is not found in .gitmodules' '
423 git submodule update &&
424 git config -f .gitmodules --remove-section submodule.sub &&
425 git add .gitmodules &&
426 entry="$(git ls-files --stage sub | cut -f 1)" &&
427 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
429 git mv sub mod/sub 2>actual.err &&
430 test_cmp expect.err 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 --dry-run does not touch the submodule or .gitmodules' '
441 git submodule update &&
443 git mv -n sub mod/sub 2>actual.err &&
445 git diff-index --exit-code HEAD &&
446 git update-index --refresh &&
447 git diff-files --quiet -- sub .gitmodules
450 test_expect_success
'checking out a commit before submodule moved needs manual updates' '
452 git commit -m "moved sub to sub2" &&
453 git checkout -q HEAD^ 2>actual &&
454 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
455 git status -s sub2 >actual &&
456 echo "?? sub2/" >expected &&
457 test_cmp expected actual &&
458 ! test -f sub/.git &&
460 git submodule update &&
463 git diff-index --exit-code HEAD &&
464 git update-index --refresh &&
465 git diff-files --quiet -- sub .gitmodules &&
466 git status -s sub2 >actual &&
467 test_must_be_empty actual
470 test_expect_success
'mv -k does not accidentally destroy submodules' '
471 git checkout submodule &&
473 git mv -k dummy sub dest &&
474 git status --porcelain >actual &&
475 grep "^R sub -> dest/sub" actual &&
480 test_expect_success
'moving a submodule in nested directories' '
483 git mv directory ../ &&
484 # git status would fail if the update of linking git dir to
485 # work dir of the submodule failed.
487 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
488 echo "directory/hierarchy/sub" >../expect
490 test_cmp expect actual
493 test_expect_success
'moving nested submodules' '
494 test_config_global protocol.file.allow always &&
495 git commit -am "cleanup commit" &&
496 mkdir sub_nested_nested &&
498 cd sub_nested_nested &&
502 git commit -m "nested level 2"
510 git commit -m "nested level 1" &&
511 git submodule add ../sub_nested_nested &&
512 git commit -m "add nested level 2"
514 git submodule add ./sub_nested nested_move &&
515 git commit -m "add nested_move" &&
516 git submodule update --init --recursive &&
517 git mv nested_move sub_nested_moved &&