3 test_description
='git mv in subdirs'
6 test_expect_success
'prepare reference tree' '
8 cp "$TEST_DIRECTORY"/../COPYING path0/COPYING &&
9 git add path0/COPYING &&
13 test_expect_success
'moving the file out of subdirectory' '
14 git -C path0 mv COPYING ../path1/COPYING
18 test_expect_success
'commiting the change' '
19 git commit -m move-out -a
22 test_expect_success
'checking the commit' '
23 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
24 grep "^R100..*path0/COPYING..*path1/COPYING" actual
27 test_expect_success
'moving the file back into subdirectory' '
28 git -C path0 mv ../path1/COPYING COPYING
32 test_expect_success
'commiting the change' '
33 git commit -m move-in -a
36 test_expect_success
'checking the commit' '
37 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
38 grep "^R100..*path1/COPYING..*path0/COPYING" actual
41 test_expect_success
'mv --dry-run does not move file' '
42 git mv -n path0/COPYING MOVED &&
43 test -f path0/COPYING &&
47 test_expect_success
'checking -k on non-existing file' '
48 git mv -k idontexist path0
51 test_expect_success
'checking -k on untracked file' '
53 git mv -k untracked1 path0 &&
55 test ! -f path0/untracked1
58 test_expect_success
'checking -k on multiple untracked files' '
60 git mv -k untracked1 untracked2 path0 &&
63 test ! -f path0/untracked1 &&
64 test ! -f path0/untracked2
67 test_expect_success
'checking -f on untracked file with existing target' '
69 test_must_fail git mv -f untracked1 path0 &&
70 test ! -f .git/index.lock &&
72 test -f path0/untracked1
75 # clean up the mess in case bad things happen
76 rm -f idontexist untracked1 untracked2 \
77 path
0/idontexist path
0/untracked1 path
0/untracked2 \
81 test_expect_success
'moving to absent target with trailing slash' '
82 test_must_fail git mv path0/COPYING no-such-dir/ &&
83 test_must_fail git mv path0/COPYING no-such-dir// &&
84 git mv path0/ no-such-dir/ &&
85 test_path_is_dir no-such-dir
88 test_expect_success
'clean up' '
92 test_expect_success
'moving to existing untracked target with trailing slash' '
94 git mv path0/ path1/ &&
95 test_path_is_dir path1/path0/
98 test_expect_success
'moving to existing tracked target with trailing slash' '
100 >path2/file && git add path2/file &&
101 git mv path1/path0/ path2/ &&
102 test_path_is_dir path2/path0/
105 test_expect_success
'clean up' '
109 test_expect_success
'adding another file' '
110 cp "$TEST_DIRECTORY"/../README.md path0/README &&
111 git add path0/README &&
112 git commit -m add2 -a
115 test_expect_success
'moving whole subdirectory' '
119 test_expect_success
'commiting the change' '
120 git commit -m dir-move -a
123 test_expect_success
'checking the commit' '
124 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
125 grep "^R100..*path0/COPYING..*path2/COPYING" actual &&
126 grep "^R100..*path0/README..*path2/README" actual
129 test_expect_success
'succeed when source is a prefix of destination' '
130 git mv path2/COPYING path2/COPYING-renamed
133 test_expect_success
'moving whole subdirectory into subdirectory' '
137 test_expect_success
'commiting the change' '
138 git commit -m dir-move -a
141 test_expect_success
'checking the commit' '
142 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
143 grep "^R100..*path2/COPYING..*path1/path2/COPYING" actual &&
144 grep "^R100..*path2/README..*path1/path2/README" actual
147 test_expect_success
'do not move directory over existing directory' '
148 mkdir path0 && mkdir path0/path2 && test_must_fail git mv path2 path0
151 test_expect_success
'move into "."' '
152 git mv path1/path2/ .
155 test_expect_success
"Michael Cassar's test case" '
156 rm -fr .git papers partA &&
158 mkdir -p papers/unsorted papers/all-papers partA &&
159 echo a >papers/unsorted/Thesis.pdf &&
160 echo b >partA/outline.txt &&
161 echo c >papers/unsorted/_another &&
162 git add papers partA &&
163 T1=$(git write-tree) &&
165 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
167 T=$(git write-tree) &&
168 git ls-tree -r $T | verbose grep partA/outline.txt
171 rm -fr papers partA path?
173 test_expect_success
"Sergey Vlasov's test case" '
180 git commit -m "initial" &&
184 test_expect_success
'absolute pathname' '
189 test_create_repo one &&
195 git mv sub "$(pwd)/in" &&
198 git ls-files --error-unmatch in/file
202 test_expect_success
'absolute pathname outside should fail' '
208 test_create_repo one &&
214 test_must_fail git mv sub "$out/out" &&
217 git ls-files --error-unmatch sub/file
221 test_expect_success
'git mv to move multiple sources into a directory' '
222 rm -fr .git && git init &&
227 git mv dir/a.txt dir/b.txt other &&
228 git ls-files >actual &&
229 { echo other/a.txt; echo other/b.txt; } >expect &&
230 test_cmp expect actual
233 test_expect_success
'git mv should not change sha1 of moved cache entry' '
238 entry="$(git ls-files --stage dirty | cut -f 1)" &&
239 git mv dirty dirty2 &&
240 [ "$entry" = "$(git ls-files --stage dirty2 | cut -f 1)" ] &&
242 git mv dirty2 dirty &&
243 [ "$entry" = "$(git ls-files --stage dirty | cut -f 1)" ]
248 # NB: This test is about the error message
249 # as well as the failure.
250 test_expect_success
'git mv error on conflicted file' '
254 test_when_finished "rm -f conflict" &&
255 cfhash=$(git hash-object -w conflict) &&
256 q_to_tab <<-EOF | git update-index --index-info &&
258 100644 $cfhash 1Qconflict
261 test_must_fail git mv conflict newname 2>actual &&
262 test_i18ngrep "conflicted" actual
265 test_expect_success
'git mv should overwrite symlink to a file' '
269 test_ln_s_add moved symlink &&
271 test_must_fail git mv moved symlink &&
272 git mv -f moved symlink &&
275 test "$(cat symlink)" = 1 &&
276 git update-index --refresh &&
277 git diff-files --quiet
282 test_expect_success
'git mv should overwrite file with a symlink' '
286 test_ln_s_add moved symlink &&
288 test_must_fail git mv symlink moved &&
289 git mv -f symlink moved &&
291 git update-index --refresh &&
292 git diff-files --quiet
295 test_expect_success SYMLINKS
'check moved symlink' '
301 test_expect_success
'setup submodule' '
302 git commit -m initial &&
304 git submodule add ./. sub &&
305 echo content >file &&
307 git commit -m "added sub and file" &&
308 mkdir -p deep/directory/hierarchy &&
309 git submodule add ./. deep/directory/hierarchy/sub &&
310 git commit -m "added another submodule" &&
314 test_expect_success
'git mv cannot move a submodule in a file' '
315 test_must_fail git mv sub file
318 test_expect_success
'git mv moves a submodule with a .git directory and no .gitmodules' '
319 entry="$(git ls-files --stage sub | cut -f 1)" &&
320 git rm .gitmodules &&
324 cp -R -P -p ../.git/modules/sub .git &&
325 GIT_WORK_TREE=. git config --unset core.worktree
328 git mv sub mod/sub &&
330 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
331 git -C mod/sub status &&
332 git update-index --refresh &&
333 git diff-files --quiet
336 test_expect_success
'git mv moves a submodule with a .git directory and .gitmodules' '
339 git submodule update &&
340 entry="$(git ls-files --stage sub | cut -f 1)" &&
344 cp -R -P -p ../.git/modules/sub .git &&
345 GIT_WORK_TREE=. git config --unset core.worktree
348 git mv sub mod/sub &&
350 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
351 git -C mod/sub status &&
352 echo mod/sub >expected &&
353 git config -f .gitmodules submodule.sub.path >actual &&
354 test_cmp expected actual &&
355 git update-index --refresh &&
356 git diff-files --quiet
359 test_expect_success
'git mv moves a submodule with gitfile' '
362 git submodule update &&
363 entry="$(git ls-files --stage sub | cut -f 1)" &&
365 git -C mod mv ../sub/ . &&
367 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
368 git -C mod/sub status &&
369 echo mod/sub >expected &&
370 git config -f .gitmodules submodule.sub.path >actual &&
371 test_cmp expected actual &&
372 git update-index --refresh &&
373 git diff-files --quiet
376 test_expect_success
'mv does not complain when no .gitmodules file is found' '
379 git submodule update &&
380 git rm .gitmodules &&
381 entry="$(git ls-files --stage sub | cut -f 1)" &&
383 git mv sub mod/sub 2>actual.err &&
384 test_must_be_empty actual.err &&
386 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
387 git -C mod/sub status &&
388 git update-index --refresh &&
389 git diff-files --quiet
392 test_expect_success
'mv will error out on a modified .gitmodules file unless staged' '
395 git submodule update &&
396 git config -f .gitmodules foo.bar true &&
397 entry="$(git ls-files --stage sub | cut -f 1)" &&
399 test_must_fail git mv sub mod/sub 2>actual.err &&
400 test -s actual.err &&
402 git diff-files --quiet -- sub &&
403 git add .gitmodules &&
404 git mv sub mod/sub 2>actual.err &&
405 test_must_be_empty actual.err &&
407 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
408 git -C mod/sub status &&
409 git update-index --refresh &&
410 git diff-files --quiet
413 test_expect_success
'mv issues a warning when section is not found in .gitmodules' '
416 git submodule update &&
417 git config -f .gitmodules --remove-section submodule.sub &&
418 git add .gitmodules &&
419 entry="$(git ls-files --stage sub | cut -f 1)" &&
420 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
422 git mv sub mod/sub 2>actual.err &&
423 test_i18ncmp expect.err actual.err &&
425 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
426 git -C mod/sub status &&
427 git update-index --refresh &&
428 git diff-files --quiet
431 test_expect_success
'mv --dry-run does not touch the submodule or .gitmodules' '
434 git submodule update &&
436 git mv -n sub mod/sub 2>actual.err &&
438 git diff-index --exit-code HEAD &&
439 git update-index --refresh &&
440 git diff-files --quiet -- sub .gitmodules
443 test_expect_success
'checking out a commit before submodule moved needs manual updates' '
445 git commit -m "moved sub to sub2" &&
446 git checkout -q HEAD^ 2>actual &&
447 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
448 git status -s sub2 >actual &&
449 echo "?? sub2/" >expected &&
450 test_cmp expected actual &&
451 ! test -f sub/.git &&
453 git submodule update &&
456 git diff-index --exit-code HEAD &&
457 git update-index --refresh &&
458 git diff-files --quiet -- sub .gitmodules &&
459 git status -s sub2 >actual &&
460 test_must_be_empty actual
463 test_expect_success
'mv -k does not accidentally destroy submodules' '
464 git checkout submodule &&
466 git mv -k dummy sub dest &&
467 git status --porcelain >actual &&
468 grep "^R sub -> dest/sub" actual &&
473 test_expect_success
'moving a submodule in nested directories' '
476 git mv directory ../ &&
477 # git status would fail if the update of linking git dir to
478 # work dir of the submodule failed.
480 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
481 echo "directory/hierarchy/sub" >../expect
483 test_cmp expect actual
486 test_expect_success
'moving nested submodules' '
487 git commit -am "cleanup commit" &&
488 mkdir sub_nested_nested &&
490 cd sub_nested_nested &&
494 git commit -m "nested level 2"
502 git commit -m "nested level 1" &&
503 git submodule add ../sub_nested_nested &&
504 git commit -m "add nested level 2"
506 git submodule add ./sub_nested nested_move &&
507 git commit -m "add nested_move" &&
508 git submodule update --init --recursive &&
509 git mv nested_move sub_nested_moved &&