Merge branch 'gc/submodule-update-part2' into maint
[alt-git.git] / t / t7001-mv.sh
bloba402908142d0d34a7145df03b6ea863d513a7200
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 'mv -f refreshes updated index entry' '
8 echo test >bar &&
9 git add bar &&
10 git commit -m test &&
12 echo foo >foo &&
13 git add foo &&
15 # Wait one second to ensure ctime of rename will differ from original
16 # file creation ctime.
17 sleep 1 &&
18 git mv -f foo bar &&
19 git reset --merge HEAD &&
21 # Verify the index has been reset
22 git diff-files >out &&
23 test_must_be_empty out
26 test_expect_success 'prepare reference tree' '
27 mkdir path0 path1 &&
28 COPYING_test_data >path0/COPYING &&
29 git add path0/COPYING &&
30 git commit -m add -a
33 test_expect_success 'moving the file out of subdirectory' '
34 git -C path0 mv COPYING ../path1/COPYING
37 # in path0 currently
38 test_expect_success 'commiting the change' '
39 git commit -m move-out -a
42 test_expect_success 'checking the commit' '
43 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
44 grep "^R100..*path0/COPYING..*path1/COPYING" actual
47 test_expect_success 'moving the file back into subdirectory' '
48 git -C path0 mv ../path1/COPYING COPYING
51 # in path0 currently
52 test_expect_success 'commiting the change' '
53 git commit -m move-in -a
56 test_expect_success 'checking the commit' '
57 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
58 grep "^R100..*path1/COPYING..*path0/COPYING" actual
61 test_expect_success 'mv --dry-run does not move file' '
62 git mv -n path0/COPYING MOVED &&
63 test -f path0/COPYING &&
64 test ! -f MOVED
67 test_expect_success 'checking -k on non-existing file' '
68 git mv -k idontexist path0
71 test_expect_success 'checking -k on untracked file' '
72 >untracked1 &&
73 git mv -k untracked1 path0 &&
74 test -f untracked1 &&
75 test ! -f path0/untracked1
78 test_expect_success 'checking -k on multiple untracked files' '
79 >untracked2 &&
80 git mv -k untracked1 untracked2 path0 &&
81 test -f untracked1 &&
82 test -f untracked2 &&
83 test ! -f path0/untracked1 &&
84 test ! -f path0/untracked2
87 test_expect_success 'checking -f on untracked file with existing target' '
88 >path0/untracked1 &&
89 test_must_fail git mv -f untracked1 path0 &&
90 test ! -f .git/index.lock &&
91 test -f untracked1 &&
92 test -f path0/untracked1
95 # clean up the mess in case bad things happen
96 rm -f idontexist untracked1 untracked2 \
97 path0/idontexist path0/untracked1 path0/untracked2 \
98 .git/index.lock
99 rmdir path1
101 test_expect_success 'moving to absent target with trailing slash' '
102 test_must_fail git mv path0/COPYING no-such-dir/ &&
103 test_must_fail git mv path0/COPYING no-such-dir// &&
104 git mv path0/ no-such-dir/ &&
105 test_path_is_dir no-such-dir
108 test_expect_success 'clean up' '
109 git reset --hard
112 test_expect_success 'moving to existing untracked target with trailing slash' '
113 mkdir path1 &&
114 git mv path0/ path1/ &&
115 test_path_is_dir path1/path0/
118 test_expect_success 'moving to existing tracked target with trailing slash' '
119 mkdir path2 &&
120 >path2/file && git add path2/file &&
121 git mv path1/path0/ path2/ &&
122 test_path_is_dir path2/path0/
125 test_expect_success 'clean up' '
126 git reset --hard
129 test_expect_success 'adding another file' '
130 COPYING_test_data | tr A-Za-z N-ZA-Mn-za-m >path0/README &&
131 git add path0/README &&
132 git commit -m add2 -a
135 test_expect_success 'moving whole subdirectory' '
136 git mv path0 path2
139 test_expect_success 'commiting the change' '
140 git commit -m dir-move -a
143 test_expect_success 'checking the commit' '
144 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
145 grep "^R100..*path0/COPYING..*path2/COPYING" actual &&
146 grep "^R100..*path0/README..*path2/README" actual
149 test_expect_success 'succeed when source is a prefix of destination' '
150 git mv path2/COPYING path2/COPYING-renamed
153 test_expect_success 'moving whole subdirectory into subdirectory' '
154 git mv path2 path1
157 test_expect_success 'commiting the change' '
158 git commit -m dir-move -a
161 test_expect_success 'checking the commit' '
162 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
163 grep "^R100..*path2/COPYING..*path1/path2/COPYING" actual &&
164 grep "^R100..*path2/README..*path1/path2/README" actual
167 test_expect_success 'do not move directory over existing directory' '
168 mkdir path0 &&
169 mkdir path0/path2 &&
170 test_must_fail git mv path2 path0
173 test_expect_success 'move into "."' '
174 git mv path1/path2/ .
177 test_expect_success "Michael Cassar's test case" '
178 rm -fr .git papers partA &&
179 git init &&
180 mkdir -p papers/unsorted papers/all-papers partA &&
181 echo a >papers/unsorted/Thesis.pdf &&
182 echo b >partA/outline.txt &&
183 echo c >papers/unsorted/_another &&
184 git add papers partA &&
185 T1=$(git write-tree) &&
187 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
189 T=$(git write-tree) &&
190 git ls-tree -r $T | verbose grep partA/outline.txt
193 rm -fr papers partA path?
195 test_expect_success "Sergey Vlasov's test case" '
196 rm -fr .git &&
197 git init &&
198 mkdir ab &&
199 date >ab.c &&
200 date >ab/d &&
201 git add ab.c ab &&
202 git commit -m "initial" &&
203 git mv ab a
206 test_expect_success 'absolute pathname' '
208 rm -fr mine &&
209 mkdir mine &&
210 cd mine &&
211 test_create_repo one &&
212 cd one &&
213 mkdir sub &&
214 >sub/file &&
215 git add sub/file &&
217 git mv sub "$(pwd)/in" &&
218 ! test -d sub &&
219 test -d in &&
220 git ls-files --error-unmatch in/file
224 test_expect_success 'absolute pathname outside should fail' '
226 rm -fr mine &&
227 mkdir mine &&
228 cd mine &&
229 out=$(pwd) &&
230 test_create_repo one &&
231 cd one &&
232 mkdir sub &&
233 >sub/file &&
234 git add sub/file &&
236 test_must_fail git mv sub "$out/out" &&
237 test -d sub &&
238 ! test -d ../in &&
239 git ls-files --error-unmatch sub/file
243 test_expect_success 'git mv to move multiple sources into a directory' '
244 rm -fr .git && git init &&
245 mkdir dir other &&
246 >dir/a.txt &&
247 >dir/b.txt &&
248 git add dir/?.txt &&
249 git mv dir/a.txt dir/b.txt other &&
250 git ls-files >actual &&
251 cat >expect <<-\EOF &&
252 other/a.txt
253 other/b.txt
255 test_cmp expect actual
258 test_expect_success 'git mv should not change sha1 of moved cache entry' '
259 rm -fr .git &&
260 git init &&
261 echo 1 >dirty &&
262 git add dirty &&
263 entry="$(git ls-files --stage dirty | cut -f 1)" &&
264 git mv dirty dirty2 &&
265 test "$entry" = "$(git ls-files --stage dirty2 | cut -f 1)" &&
266 echo 2 >dirty2 &&
267 git mv dirty2 dirty &&
268 test "$entry" = "$(git ls-files --stage dirty | cut -f 1)"
271 rm -f dirty dirty2
273 # NB: This test is about the error message
274 # as well as the failure.
275 test_expect_success 'git mv error on conflicted file' '
276 rm -fr .git &&
277 git init &&
278 >conflict &&
279 test_when_finished "rm -f conflict" &&
280 cfhash=$(git hash-object -w conflict) &&
281 q_to_tab <<-EOF | git update-index --index-info &&
282 0 $cfhash 0Qconflict
283 100644 $cfhash 1Qconflict
286 test_must_fail git mv conflict newname 2>actual &&
287 test_i18ngrep "conflicted" actual
290 test_expect_success 'git mv should overwrite symlink to a file' '
291 rm -fr .git &&
292 git init &&
293 echo 1 >moved &&
294 test_ln_s_add moved symlink &&
295 git add moved &&
296 test_must_fail git mv moved symlink &&
297 git mv -f moved symlink &&
298 ! test -e moved &&
299 test -f symlink &&
300 test "$(cat symlink)" = 1 &&
301 git update-index --refresh &&
302 git diff-files --quiet
305 rm -f moved symlink
307 test_expect_success 'git mv should overwrite file with a symlink' '
308 rm -fr .git &&
309 git init &&
310 echo 1 >moved &&
311 test_ln_s_add moved symlink &&
312 git add moved &&
313 test_must_fail git mv symlink moved &&
314 git mv -f symlink moved &&
315 ! test -e symlink &&
316 git update-index --refresh &&
317 git diff-files --quiet
320 test_expect_success SYMLINKS 'check moved symlink' '
321 test -h moved
324 rm -f moved symlink
326 test_expect_success 'setup submodule' '
327 git commit -m initial &&
328 git reset --hard &&
329 git submodule add ./. sub &&
330 echo content >file &&
331 git add file &&
332 git commit -m "added sub and file" &&
333 mkdir -p deep/directory/hierarchy &&
334 git submodule add ./. deep/directory/hierarchy/sub &&
335 git commit -m "added another submodule" &&
336 git branch submodule
339 test_expect_success 'git mv cannot move a submodule in a file' '
340 test_must_fail git mv sub file
343 test_expect_success 'git mv moves a submodule with a .git directory and no .gitmodules' '
344 entry="$(git ls-files --stage sub | cut -f 1)" &&
345 git rm .gitmodules &&
347 cd sub &&
348 rm -f .git &&
349 cp -R -P -p ../.git/modules/sub .git &&
350 GIT_WORK_TREE=. git config --unset core.worktree
351 ) &&
352 mkdir mod &&
353 git mv sub mod/sub &&
354 ! test -e sub &&
355 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
356 git -C mod/sub status &&
357 git update-index --refresh &&
358 git diff-files --quiet
361 test_expect_success 'git mv moves a submodule with a .git directory and .gitmodules' '
362 rm -rf mod &&
363 git reset --hard &&
364 git submodule update &&
365 entry="$(git ls-files --stage sub | cut -f 1)" &&
367 cd sub &&
368 rm -f .git &&
369 cp -R -P -p ../.git/modules/sub .git &&
370 GIT_WORK_TREE=. git config --unset core.worktree
371 ) &&
372 mkdir mod &&
373 git mv sub mod/sub &&
374 ! test -e sub &&
375 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
376 git -C mod/sub status &&
377 echo mod/sub >expected &&
378 git config -f .gitmodules submodule.sub.path >actual &&
379 test_cmp expected actual &&
380 git update-index --refresh &&
381 git diff-files --quiet
384 test_expect_success 'git mv moves a submodule with gitfile' '
385 rm -rf mod &&
386 git reset --hard &&
387 git submodule update &&
388 entry="$(git ls-files --stage sub | cut -f 1)" &&
389 mkdir mod &&
390 git -C mod mv ../sub/ . &&
391 ! test -e sub &&
392 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
393 git -C mod/sub status &&
394 echo mod/sub >expected &&
395 git config -f .gitmodules submodule.sub.path >actual &&
396 test_cmp expected actual &&
397 git update-index --refresh &&
398 git diff-files --quiet
401 test_expect_success 'mv does not complain when no .gitmodules file is found' '
402 rm -rf mod &&
403 git reset --hard &&
404 git submodule update &&
405 git rm .gitmodules &&
406 entry="$(git ls-files --stage sub | cut -f 1)" &&
407 mkdir mod &&
408 git mv sub mod/sub 2>actual.err &&
409 test_must_be_empty actual.err &&
410 ! test -e sub &&
411 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
412 git -C mod/sub status &&
413 git update-index --refresh &&
414 git diff-files --quiet
417 test_expect_success 'mv will error out on a modified .gitmodules file unless staged' '
418 rm -rf mod &&
419 git reset --hard &&
420 git submodule update &&
421 git config -f .gitmodules foo.bar true &&
422 entry="$(git ls-files --stage sub | cut -f 1)" &&
423 mkdir mod &&
424 test_must_fail git mv sub mod/sub 2>actual.err &&
425 test -s actual.err &&
426 test -e sub &&
427 git diff-files --quiet -- sub &&
428 git add .gitmodules &&
429 git mv sub mod/sub 2>actual.err &&
430 test_must_be_empty actual.err &&
431 ! test -e sub &&
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 issues a warning when section is not found in .gitmodules' '
439 rm -rf mod &&
440 git reset --hard &&
441 git submodule update &&
442 git config -f .gitmodules --remove-section submodule.sub &&
443 git add .gitmodules &&
444 entry="$(git ls-files --stage sub | cut -f 1)" &&
445 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
446 mkdir mod &&
447 git mv sub mod/sub 2>actual.err &&
448 test_cmp expect.err actual.err &&
449 ! test -e sub &&
450 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
451 git -C mod/sub status &&
452 git update-index --refresh &&
453 git diff-files --quiet
456 test_expect_success 'mv --dry-run does not touch the submodule or .gitmodules' '
457 rm -rf mod &&
458 git reset --hard &&
459 git submodule update &&
460 mkdir mod &&
461 git mv -n sub mod/sub 2>actual.err &&
462 test -f sub/.git &&
463 git diff-index --exit-code HEAD &&
464 git update-index --refresh &&
465 git diff-files --quiet -- sub .gitmodules
468 test_expect_success 'checking out a commit before submodule moved needs manual updates' '
469 git mv sub sub2 &&
470 git commit -m "moved sub to sub2" &&
471 git checkout -q HEAD^ 2>actual &&
472 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
473 git status -s sub2 >actual &&
474 echo "?? sub2/" >expected &&
475 test_cmp expected actual &&
476 ! test -f sub/.git &&
477 test -f sub2/.git &&
478 git submodule update &&
479 test -f sub/.git &&
480 rm -rf sub2 &&
481 git diff-index --exit-code HEAD &&
482 git update-index --refresh &&
483 git diff-files --quiet -- sub .gitmodules &&
484 git status -s sub2 >actual &&
485 test_must_be_empty actual
488 test_expect_success 'mv -k does not accidentally destroy submodules' '
489 git checkout submodule &&
490 mkdir dummy dest &&
491 git mv -k dummy sub dest &&
492 git status --porcelain >actual &&
493 grep "^R sub -> dest/sub" actual &&
494 git reset --hard &&
495 git checkout .
498 test_expect_success 'moving a submodule in nested directories' '
500 cd deep &&
501 git mv directory ../ &&
502 # git status would fail if the update of linking git dir to
503 # work dir of the submodule failed.
504 git status &&
505 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
506 echo "directory/hierarchy/sub" >../expect
507 ) &&
508 test_cmp expect actual
511 test_expect_success 'moving nested submodules' '
512 git commit -am "cleanup commit" &&
513 mkdir sub_nested_nested &&
515 cd sub_nested_nested &&
516 >nested_level2 &&
517 git init &&
518 git add . &&
519 git commit -m "nested level 2"
520 ) &&
521 mkdir sub_nested &&
523 cd sub_nested &&
524 >nested_level1 &&
525 git init &&
526 git add . &&
527 git commit -m "nested level 1" &&
528 git submodule add ../sub_nested_nested &&
529 git commit -m "add nested level 2"
530 ) &&
531 git submodule add ./sub_nested nested_move &&
532 git commit -m "add nested_move" &&
533 git submodule update --init --recursive &&
534 git mv nested_move sub_nested_moved &&
535 git status
538 test_done