read-tree: narrow scope of index expansion for '--prefix'
[alt-git.git] / t / t7001-mv.sh
blob963356ba5f9257c3691e67267ff1d0cfb86355ad
1 #!/bin/sh
3 test_description='git mv in subdirs'
4 . ./test-lib.sh
5 . "$TEST_DIRECTORY"/lib-diff-data.sh
7 test_expect_success 'prepare reference tree' '
8 mkdir path0 path1 &&
9 COPYING_test_data >path0/COPYING &&
10 git add path0/COPYING &&
11 git commit -m add -a
14 test_expect_success 'moving the file out of subdirectory' '
15 git -C path0 mv COPYING ../path1/COPYING
18 # in path0 currently
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
32 # in path0 currently
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 &&
45 test ! -f MOVED
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' '
53 >untracked1 &&
54 git mv -k untracked1 path0 &&
55 test -f untracked1 &&
56 test ! -f path0/untracked1
59 test_expect_success 'checking -k on multiple untracked files' '
60 >untracked2 &&
61 git mv -k untracked1 untracked2 path0 &&
62 test -f untracked1 &&
63 test -f untracked2 &&
64 test ! -f path0/untracked1 &&
65 test ! -f path0/untracked2
68 test_expect_success 'checking -f on untracked file with existing target' '
69 >path0/untracked1 &&
70 test_must_fail git mv -f untracked1 path0 &&
71 test ! -f .git/index.lock &&
72 test -f untracked1 &&
73 test -f path0/untracked1
76 # clean up the mess in case bad things happen
77 rm -f idontexist untracked1 untracked2 \
78 path0/idontexist path0/untracked1 path0/untracked2 \
79 .git/index.lock
80 rmdir path1
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' '
90 git reset --hard
93 test_expect_success 'moving to existing untracked target with trailing slash' '
94 mkdir path1 &&
95 git mv path0/ path1/ &&
96 test_path_is_dir path1/path0/
99 test_expect_success 'moving to existing tracked target with trailing slash' '
100 mkdir path2 &&
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' '
107 git reset --hard
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' '
117 git mv path0 path2
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' '
135 git mv path2 path1
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' '
149 mkdir path0 &&
150 mkdir path0/path2 &&
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 &&
160 git init &&
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" '
177 rm -fr .git &&
178 git init &&
179 mkdir ab &&
180 date >ab.c &&
181 date >ab/d &&
182 git add ab.c ab &&
183 git commit -m "initial" &&
184 git mv ab a
187 test_expect_success 'absolute pathname' '
189 rm -fr mine &&
190 mkdir mine &&
191 cd mine &&
192 test_create_repo one &&
193 cd one &&
194 mkdir sub &&
195 >sub/file &&
196 git add sub/file &&
198 git mv sub "$(pwd)/in" &&
199 ! test -d sub &&
200 test -d in &&
201 git ls-files --error-unmatch in/file
205 test_expect_success 'absolute pathname outside should fail' '
207 rm -fr mine &&
208 mkdir mine &&
209 cd mine &&
210 out=$(pwd) &&
211 test_create_repo one &&
212 cd one &&
213 mkdir sub &&
214 >sub/file &&
215 git add sub/file &&
217 test_must_fail git mv sub "$out/out" &&
218 test -d sub &&
219 ! test -d ../in &&
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 &&
226 mkdir dir other &&
227 >dir/a.txt &&
228 >dir/b.txt &&
229 git add dir/?.txt &&
230 git mv dir/a.txt dir/b.txt other &&
231 git ls-files >actual &&
232 cat >expect <<-\EOF &&
233 other/a.txt
234 other/b.txt
236 test_cmp expect actual
239 test_expect_success 'git mv should not change sha1 of moved cache entry' '
240 rm -fr .git &&
241 git init &&
242 echo 1 >dirty &&
243 git add dirty &&
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)" &&
247 echo 2 >dirty2 &&
248 git mv dirty2 dirty &&
249 test "$entry" = "$(git ls-files --stage dirty | cut -f 1)"
252 rm -f dirty dirty2
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' '
257 rm -fr .git &&
258 git init &&
259 >conflict &&
260 test_when_finished "rm -f conflict" &&
261 cfhash=$(git hash-object -w conflict) &&
262 q_to_tab <<-EOF | git update-index --index-info &&
263 0 $cfhash 0Qconflict
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' '
272 rm -fr .git &&
273 git init &&
274 echo 1 >moved &&
275 test_ln_s_add moved symlink &&
276 git add moved &&
277 test_must_fail git mv moved symlink &&
278 git mv -f moved symlink &&
279 ! test -e moved &&
280 test -f symlink &&
281 test "$(cat symlink)" = 1 &&
282 git update-index --refresh &&
283 git diff-files --quiet
286 rm -f moved symlink
288 test_expect_success 'git mv should overwrite file with a symlink' '
289 rm -fr .git &&
290 git init &&
291 echo 1 >moved &&
292 test_ln_s_add moved symlink &&
293 git add moved &&
294 test_must_fail git mv symlink moved &&
295 git mv -f symlink moved &&
296 ! test -e symlink &&
297 git update-index --refresh &&
298 git diff-files --quiet
301 test_expect_success SYMLINKS 'check moved symlink' '
302 test -h moved
305 rm -f moved symlink
307 test_expect_success 'setup submodule' '
308 git commit -m initial &&
309 git reset --hard &&
310 git submodule add ./. sub &&
311 echo content >file &&
312 git add 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" &&
317 git branch 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 &&
328 cd sub &&
329 rm -f .git &&
330 cp -R -P -p ../.git/modules/sub .git &&
331 GIT_WORK_TREE=. git config --unset core.worktree
332 ) &&
333 mkdir mod &&
334 git mv sub mod/sub &&
335 ! test -e 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' '
343 rm -rf mod &&
344 git reset --hard &&
345 git submodule update &&
346 entry="$(git ls-files --stage sub | cut -f 1)" &&
348 cd sub &&
349 rm -f .git &&
350 cp -R -P -p ../.git/modules/sub .git &&
351 GIT_WORK_TREE=. git config --unset core.worktree
352 ) &&
353 mkdir mod &&
354 git mv sub mod/sub &&
355 ! test -e 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' '
366 rm -rf mod &&
367 git reset --hard &&
368 git submodule update &&
369 entry="$(git ls-files --stage sub | cut -f 1)" &&
370 mkdir mod &&
371 git -C mod mv ../sub/ . &&
372 ! test -e 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' '
383 rm -rf mod &&
384 git reset --hard &&
385 git submodule update &&
386 git rm .gitmodules &&
387 entry="$(git ls-files --stage sub | cut -f 1)" &&
388 mkdir mod &&
389 git mv sub mod/sub 2>actual.err &&
390 test_must_be_empty actual.err &&
391 ! test -e sub &&
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' '
399 rm -rf mod &&
400 git reset --hard &&
401 git submodule update &&
402 git config -f .gitmodules foo.bar true &&
403 entry="$(git ls-files --stage sub | cut -f 1)" &&
404 mkdir mod &&
405 test_must_fail git mv sub mod/sub 2>actual.err &&
406 test -s actual.err &&
407 test -e sub &&
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 &&
412 ! test -e sub &&
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' '
420 rm -rf mod &&
421 git reset --hard &&
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 &&
427 mkdir mod &&
428 git mv sub mod/sub 2>actual.err &&
429 test_cmp expect.err actual.err &&
430 ! test -e sub &&
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' '
438 rm -rf mod &&
439 git reset --hard &&
440 git submodule update &&
441 mkdir mod &&
442 git mv -n sub mod/sub 2>actual.err &&
443 test -f sub/.git &&
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' '
450 git mv sub sub2 &&
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 &&
458 test -f sub2/.git &&
459 git submodule update &&
460 test -f sub/.git &&
461 rm -rf sub2 &&
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 &&
471 mkdir dummy dest &&
472 git mv -k dummy sub dest &&
473 git status --porcelain >actual &&
474 grep "^R sub -> dest/sub" actual &&
475 git reset --hard &&
476 git checkout .
479 test_expect_success 'moving a submodule in nested directories' '
481 cd deep &&
482 git mv directory ../ &&
483 # git status would fail if the update of linking git dir to
484 # work dir of the submodule failed.
485 git status &&
486 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
487 echo "directory/hierarchy/sub" >../expect
488 ) &&
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 &&
497 >nested_level2 &&
498 git init &&
499 git add . &&
500 git commit -m "nested level 2"
501 ) &&
502 mkdir sub_nested &&
504 cd sub_nested &&
505 >nested_level1 &&
506 git init &&
507 git add . &&
508 git commit -m "nested level 1" &&
509 git submodule add ../sub_nested_nested &&
510 git commit -m "add nested level 2"
511 ) &&
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 &&
516 git status
519 test_done