3 test_description
='git mv in subdirs'
7 'prepare reference tree' \
9 cp "$TEST_DIRECTORY"/../COPYING path0/COPYING &&
10 git add path0/COPYING &&
14 'moving the file out of subdirectory' \
15 'cd path0 && git mv COPYING ../path1/COPYING'
19 'commiting the change' \
20 'cd .. && git commit -m move-out -a'
23 'checking the commit' \
24 'git diff-tree -r -M --name-status HEAD^ HEAD | \
25 grep "^R100..*path0/COPYING..*path1/COPYING"'
28 'moving the file back into subdirectory' \
29 'cd path0 && git mv ../path1/COPYING COPYING'
33 'commiting the change' \
34 'cd .. && git commit -m move-in -a'
37 'checking the commit' \
38 'git diff-tree -r -M --name-status HEAD^ HEAD | \
39 grep "^R100..*path1/COPYING..*path0/COPYING"'
42 'mv --dry-run does not move file' \
43 'git mv -n path0/COPYING MOVED &&
44 test -f path0/COPYING &&
48 'checking -k on non-existing file' \
49 'git mv -k idontexist path0'
52 'checking -k on untracked file' \
54 git mv -k untracked1 path0 &&
56 test ! -f path0/untracked1'
59 'checking -k on multiple untracked files' \
61 git mv -k untracked1 untracked2 path0 &&
64 test ! -f path0/untracked1 &&
65 test ! -f path0/untracked2'
68 'checking -f on untracked file with existing target' \
69 'touch path0/untracked1 &&
70 test_must_fail git mv -f untracked1 path0 &&
71 test ! -f .git/index.lock &&
73 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 \
82 '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'
93 'moving to existing untracked target with trailing slash' \
95 git mv path0/ path1/ &&
96 test_path_is_dir path1/path0/'
99 '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/'
105 test_expect_success \
109 test_expect_success \
110 'adding another file' \
111 'cp "$TEST_DIRECTORY"/../README.md path0/README &&
112 git add path0/README &&
113 git commit -m add2 -a'
115 test_expect_success \
116 'moving whole subdirectory' \
119 test_expect_success \
120 'commiting the change' \
121 'git commit -m dir-move -a'
123 test_expect_success \
124 'checking the commit' \
125 'git diff-tree -r -M --name-status HEAD^ HEAD | \
126 grep "^R100..*path0/COPYING..*path2/COPYING" &&
127 git diff-tree -r -M --name-status HEAD^ HEAD | \
128 grep "^R100..*path0/README..*path2/README"'
130 test_expect_success \
131 'succeed when source is a prefix of destination' \
132 'git mv path2/COPYING path2/COPYING-renamed'
134 test_expect_success \
135 'moving whole subdirectory into subdirectory' \
138 test_expect_success \
139 'commiting the change' \
140 'git commit -m dir-move -a'
142 test_expect_success \
143 'checking the commit' \
144 'git diff-tree -r -M --name-status HEAD^ HEAD | \
145 grep "^R100..*path2/COPYING..*path1/path2/COPYING" &&
146 git diff-tree -r -M --name-status HEAD^ HEAD | \
147 grep "^R100..*path2/README..*path1/path2/README"'
149 test_expect_success \
150 'do not move directory over existing directory' \
151 'mkdir path0 && mkdir path0/path2 && test_must_fail git mv path2 path0'
153 test_expect_success \
155 'git mv path1/path2/ .'
157 test_expect_success
"Michael Cassar's test case" '
158 rm -fr .git papers partA &&
160 mkdir -p papers/unsorted papers/all-papers partA &&
161 echo a > papers/unsorted/Thesis.pdf &&
162 echo b > partA/outline.txt &&
163 echo c > papers/unsorted/_another &&
164 git add papers partA &&
165 T1=$(git write-tree) &&
167 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
169 T=$(git write-tree) &&
170 git ls-tree -r $T | verbose grep partA/outline.txt
173 rm -fr papers partA path?
175 test_expect_success
"Sergey Vlasov's test case" '
182 git commit -m 'initial
' &&
186 test_expect_success
'absolute pathname' '(
191 test_create_repo one &&
197 git mv sub "$(pwd)/in" &&
200 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 { echo other/a.txt; echo other/b.txt; } >expect &&
233 test_cmp expect actual
236 test_expect_success
'git mv should not change sha1 of moved cache entry' '
242 entry="$(git ls-files --stage dirty | cut -f 1)" &&
243 git mv dirty dirty2 &&
244 [ "$entry" = "$(git ls-files --stage dirty2 | cut -f 1)" ] &&
246 git mv dirty2 dirty &&
247 [ "$entry" = "$(git ls-files --stage dirty | cut -f 1)" ]
253 test_expect_success
'git mv should overwrite symlink to a file' '
258 test_ln_s_add moved symlink &&
260 test_must_fail git mv moved symlink &&
261 git mv -f moved symlink &&
264 test "$(cat symlink)" = 1 &&
265 git update-index --refresh &&
266 git diff-files --quiet
272 test_expect_success
'git mv should overwrite file with a symlink' '
277 test_ln_s_add moved symlink &&
279 test_must_fail git mv symlink moved &&
280 git mv -f symlink moved &&
282 git update-index --refresh &&
283 git diff-files --quiet
287 test_expect_success SYMLINKS
'check moved symlink' '
294 test_expect_success
'setup submodule' '
295 git commit -m initial &&
297 git submodule add ./. sub &&
298 echo content >file &&
300 git commit -m "added sub and file" &&
301 mkdir -p deep/directory/hierarchy &&
302 git submodule add ./. deep/directory/hierarchy/sub &&
303 git commit -m "added another submodule" &&
307 test_expect_success
'git mv cannot move a submodule in a file' '
308 test_must_fail git mv sub file
311 test_expect_success
'git mv moves a submodule with a .git directory and no .gitmodules' '
312 entry="$(git ls-files --stage sub | cut -f 1)" &&
313 git rm .gitmodules &&
317 cp -R -P -p ../.git/modules/sub .git &&
318 GIT_WORK_TREE=. git config --unset core.worktree
321 git mv sub mod/sub &&
323 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
328 git update-index --refresh &&
329 git diff-files --quiet
332 test_expect_success
'git mv moves a submodule with a .git directory and .gitmodules' '
335 git submodule update &&
336 entry="$(git ls-files --stage sub | cut -f 1)" &&
340 cp -R -P -p ../.git/modules/sub .git &&
341 GIT_WORK_TREE=. git config --unset core.worktree
344 git mv sub mod/sub &&
346 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
351 echo mod/sub >expected &&
352 git config -f .gitmodules submodule.sub.path >actual &&
353 test_cmp expected actual &&
354 git update-index --refresh &&
355 git diff-files --quiet
358 test_expect_success
'git mv moves a submodule with gitfile' '
361 git submodule update &&
362 entry="$(git ls-files --stage sub | cut -f 1)" &&
369 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
374 echo mod/sub >expected &&
375 git config -f .gitmodules submodule.sub.path >actual &&
376 test_cmp expected actual &&
377 git update-index --refresh &&
378 git diff-files --quiet
381 test_expect_success
'mv does not complain when no .gitmodules file is found' '
384 git submodule update &&
385 git rm .gitmodules &&
386 entry="$(git ls-files --stage sub | cut -f 1)" &&
388 git mv sub mod/sub 2>actual.err &&
389 ! test -s actual.err &&
391 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
396 git update-index --refresh &&
397 git diff-files --quiet
400 test_expect_success
'mv will error out on a modified .gitmodules file unless staged' '
403 git submodule update &&
404 git config -f .gitmodules foo.bar true &&
405 entry="$(git ls-files --stage sub | cut -f 1)" &&
407 test_must_fail git mv sub mod/sub 2>actual.err &&
408 test -s actual.err &&
410 git diff-files --quiet -- sub &&
411 git add .gitmodules &&
412 git mv sub mod/sub 2>actual.err &&
413 ! test -s actual.err &&
415 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
420 git update-index --refresh &&
421 git diff-files --quiet
424 test_expect_success
'mv issues a warning when section is not found in .gitmodules' '
427 git submodule update &&
428 git config -f .gitmodules --remove-section submodule.sub &&
429 git add .gitmodules &&
430 entry="$(git ls-files --stage sub | cut -f 1)" &&
431 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
433 git mv sub mod/sub 2>actual.err &&
434 test_i18ncmp expect.err actual.err &&
436 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
441 git update-index --refresh &&
442 git diff-files --quiet
445 test_expect_success
'mv --dry-run does not touch the submodule or .gitmodules' '
448 git submodule update &&
450 git mv -n sub mod/sub 2>actual.err &&
452 git diff-index --exit-code HEAD &&
453 git update-index --refresh &&
454 git diff-files --quiet -- sub .gitmodules
457 test_expect_success
'checking out a commit before submodule moved needs manual updates' '
459 git commit -m "moved sub to sub2" &&
460 git checkout -q HEAD^ 2>actual &&
461 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
462 git status -s sub2 >actual &&
463 echo "?? sub2/" >expected &&
464 test_cmp expected actual &&
465 ! test -f sub/.git &&
467 git submodule update &&
470 git diff-index --exit-code HEAD &&
471 git update-index --refresh &&
472 git diff-files --quiet -- sub .gitmodules &&
473 git status -s sub2 >actual &&
477 test_expect_success
'mv -k does not accidentally destroy submodules' '
478 git checkout submodule &&
480 git mv -k dummy sub dest &&
481 git status --porcelain >actual &&
482 grep "^R sub -> dest/sub" actual &&
487 test_expect_success
'moving a submodule in nested directories' '
490 git mv directory ../ &&
491 # git status would fail if the update of linking git dir to
492 # work dir of the submodule failed.
494 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
495 echo "directory/hierarchy/sub" >../expect
497 test_cmp expect actual
500 test_expect_failure
'moving nested submodules' '
501 git commit -am "cleanup commit" &&
502 mkdir sub_nested_nested &&
503 (cd sub_nested_nested &&
504 touch nested_level2 &&
507 git commit -m "nested level 2"
511 touch nested_level1 &&
514 git commit -m "nested level 1"
515 git submodule add ../sub_nested_nested &&
516 git commit -m "add nested level 2"
518 git submodule add ./sub_nested nested_move &&
519 git commit -m "add nested_move" &&
520 git submodule update --init --recursive &&
521 git mv nested_move sub_nested_moved &&