Merge branch 'rj/cygwin-has-dev-tty'
[git/gitster.git] / t / t7001-mv.sh
blob86258f9f4307a34b49a22d3d217ac2a82cefc560
1 #!/bin/sh
3 test_description='git mv in subdirs'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-diff-data.sh
9 index_at_path () {
10 git ls-files --format='%(objectmode) %(objectname) %(stage)' "$@"
13 test_expect_success 'mv -f refreshes updated index entry' '
14 echo test >bar &&
15 git add bar &&
16 git commit -m test &&
18 echo foo >foo &&
19 git add foo &&
21 # Wait one second to ensure ctime of rename will differ from original
22 # file creation ctime.
23 sleep 1 &&
24 git mv -f foo bar &&
25 git reset --merge HEAD &&
27 # Verify the index has been reset
28 git diff-files >out &&
29 test_must_be_empty out
32 test_expect_success 'prepare reference tree' '
33 mkdir path0 path1 &&
34 COPYING_test_data >path0/COPYING &&
35 git add path0/COPYING &&
36 git commit -m add -a
39 test_expect_success 'moving the file out of subdirectory' '
40 git -C path0 mv COPYING ../path1/COPYING
43 # in path0 currently
44 test_expect_success 'commiting the change' '
45 git commit -m move-out -a
48 test_expect_success 'checking the commit' '
49 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
50 grep "^R100..*path0/COPYING..*path1/COPYING" actual
53 test_expect_success 'moving the file back into subdirectory' '
54 git -C path0 mv ../path1/COPYING COPYING
57 # in path0 currently
58 test_expect_success 'commiting the change' '
59 git commit -m move-in -a
62 test_expect_success 'checking the commit' '
63 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
64 grep "^R100..*path1/COPYING..*path0/COPYING" actual
67 test_expect_success 'mv --dry-run does not move file' '
68 git mv -n path0/COPYING MOVED &&
69 test_path_is_file path0/COPYING &&
70 test_path_is_missing MOVED
73 test_expect_success 'checking -k on non-existing file' '
74 git mv -k idontexist path0
77 test_expect_success 'checking -k on untracked file' '
78 >untracked1 &&
79 git mv -k untracked1 path0 &&
80 test_path_is_file untracked1 &&
81 test_path_is_missing path0/untracked1
84 test_expect_success 'checking -k on multiple untracked files' '
85 >untracked2 &&
86 git mv -k untracked1 untracked2 path0 &&
87 test_path_is_file untracked1 &&
88 test_path_is_file untracked2 &&
89 test_path_is_missing path0/untracked1 &&
90 test_path_is_missing path0/untracked2
93 test_expect_success 'checking -f on untracked file with existing target' '
94 >path0/untracked1 &&
95 test_must_fail git mv -f untracked1 path0 &&
96 test_path_is_missing .git/index.lock &&
97 test_path_is_file untracked1 &&
98 test_path_is_file path0/untracked1
101 # clean up the mess in case bad things happen
102 rm -f idontexist untracked1 untracked2 \
103 path0/idontexist path0/untracked1 path0/untracked2 \
104 .git/index.lock
105 rmdir path1
107 test_expect_success 'moving to absent target with trailing slash' '
108 test_must_fail git mv path0/COPYING no-such-dir/ &&
109 test_must_fail git mv path0/COPYING no-such-dir// &&
110 git mv path0/ no-such-dir/ &&
111 test_path_is_dir no-such-dir
114 test_expect_success 'clean up' '
115 git reset --hard
118 test_expect_success 'moving to existing untracked target with trailing slash' '
119 mkdir path1 &&
120 git mv path0/ path1/ &&
121 test_path_is_dir path1/path0/
124 test_expect_success 'moving to existing tracked target with trailing slash' '
125 mkdir path2 &&
126 >path2/file && git add path2/file &&
127 git mv path1/path0/ path2/ &&
128 test_path_is_dir path2/path0/
131 test_expect_success 'clean up' '
132 git reset --hard
135 test_expect_success 'adding another file' '
136 COPYING_test_data | tr A-Za-z N-ZA-Mn-za-m >path0/README &&
137 git add path0/README &&
138 git commit -m add2 -a
141 test_expect_success 'moving whole subdirectory' '
142 git mv path0 path2
145 test_expect_success 'commiting the change' '
146 git commit -m dir-move -a
149 test_expect_success 'checking the commit' '
150 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
151 grep "^R100..*path0/COPYING..*path2/COPYING" actual &&
152 grep "^R100..*path0/README..*path2/README" actual
155 test_expect_success 'succeed when source is a prefix of destination' '
156 git mv path2/COPYING path2/COPYING-renamed
159 test_expect_success 'moving whole subdirectory into subdirectory' '
160 git mv path2 path1
163 test_expect_success 'commiting the change' '
164 git commit -m dir-move -a
167 test_expect_success 'checking the commit' '
168 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
169 grep "^R100..*path2/COPYING..*path1/path2/COPYING" actual &&
170 grep "^R100..*path2/README..*path1/path2/README" actual
173 test_expect_success 'do not move directory over existing directory' '
174 mkdir path0 &&
175 mkdir path0/path2 &&
176 test_must_fail git mv path2 path0
179 test_expect_success 'rename directory to non-existing directory' '
180 mkdir dir-a &&
181 >dir-a/f &&
182 git add dir-a &&
183 git mv dir-a non-existing-dir
186 test_expect_success 'move into "."' '
187 git mv path1/path2/ .
190 test_expect_success "Michael Cassar's test case" '
191 rm -fr .git papers partA &&
192 git init &&
193 mkdir -p papers/unsorted papers/all-papers partA &&
194 echo a >papers/unsorted/Thesis.pdf &&
195 echo b >partA/outline.txt &&
196 echo c >papers/unsorted/_another &&
197 git add papers partA &&
198 T1=$(git write-tree) &&
200 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
202 T=$(git write-tree) &&
203 git ls-tree -r $T >out &&
204 grep partA/outline.txt out
207 rm -fr papers partA path?
209 test_expect_success "Sergey Vlasov's test case" '
210 rm -fr .git &&
211 git init &&
212 mkdir ab &&
213 date >ab.c &&
214 date >ab/d &&
215 git add ab.c ab &&
216 git commit -m "initial" &&
217 git mv ab a
220 test_expect_success 'absolute pathname' '
222 rm -fr mine &&
223 mkdir mine &&
224 cd mine &&
225 test_create_repo one &&
226 cd one &&
227 mkdir sub &&
228 >sub/file &&
229 git add sub/file &&
231 git mv sub "$(pwd)/in" &&
232 test_path_is_missing sub &&
233 test_path_is_dir in &&
234 git ls-files --error-unmatch in/file
238 test_expect_success 'absolute pathname outside should fail' '
240 rm -fr mine &&
241 mkdir mine &&
242 cd mine &&
243 out=$(pwd) &&
244 test_create_repo one &&
245 cd one &&
246 mkdir sub &&
247 >sub/file &&
248 git add sub/file &&
250 test_must_fail git mv sub "$out/out" &&
251 test_path_is_dir sub &&
252 test_path_is_missing ../in &&
253 git ls-files --error-unmatch sub/file
257 test_expect_success 'git mv to move multiple sources into a directory' '
258 rm -fr .git && git init &&
259 mkdir dir other &&
260 >dir/a.txt &&
261 >dir/b.txt &&
262 git add dir/?.txt &&
263 git mv dir/a.txt dir/b.txt other &&
264 git ls-files >actual &&
265 cat >expect <<-\EOF &&
266 other/a.txt
267 other/b.txt
269 test_cmp expect actual
272 test_expect_success 'git mv should not change sha1 of moved cache entry' '
273 rm -fr .git &&
274 git init &&
275 echo 1 >dirty &&
276 git add dirty &&
277 entry="$(index_at_path dirty)" &&
278 git mv dirty dirty2 &&
279 test "$entry" = "$(index_at_path dirty2)" &&
280 echo 2 >dirty2 &&
281 git mv dirty2 dirty &&
282 test "$entry" = "$(index_at_path dirty)"
285 rm -f dirty dirty2
287 # NB: This test is about the error message
288 # as well as the failure.
289 test_expect_success 'git mv error on conflicted file' '
290 rm -fr .git &&
291 git init &&
292 >conflict &&
293 test_when_finished "rm -f conflict" &&
294 cfhash=$(git hash-object -w conflict) &&
295 q_to_tab <<-EOF | git update-index --index-info &&
296 0 $cfhash 0Qconflict
297 100644 $cfhash 1Qconflict
300 test_must_fail git mv conflict newname 2>actual &&
301 test_grep "conflicted" actual
304 test_expect_success 'git mv should overwrite symlink to a file' '
305 rm -fr .git &&
306 git init &&
307 echo 1 >moved &&
308 test_ln_s_add moved symlink &&
309 git add moved &&
310 test_must_fail git mv moved symlink &&
311 git mv -f moved symlink &&
312 test_path_is_missing moved &&
313 test_path_is_file symlink &&
314 test "$(cat symlink)" = 1 &&
315 git update-index --refresh &&
316 git diff-files --quiet
319 rm -f moved symlink
321 test_expect_success 'git mv should overwrite file with a symlink' '
322 rm -fr .git &&
323 git init &&
324 echo 1 >moved &&
325 test_ln_s_add moved symlink &&
326 git add moved &&
327 test_must_fail git mv symlink moved &&
328 git mv -f symlink moved &&
329 test_path_is_missing symlink &&
330 git update-index --refresh &&
331 git diff-files --quiet
334 test_expect_success SYMLINKS 'check moved symlink' '
335 test_path_is_symlink moved
338 rm -f moved symlink
340 test_expect_success 'setup submodule' '
341 test_config_global protocol.file.allow always &&
342 git commit -m initial &&
343 git reset --hard &&
344 git submodule add ./. sub &&
345 echo content >file &&
346 git add file &&
347 git commit -m "added sub and file" &&
348 mkdir -p deep/directory/hierarchy &&
349 git submodule add ./. deep/directory/hierarchy/sub &&
350 git commit -m "added another submodule" &&
351 git branch submodule
354 test_expect_success 'git mv cannot move a submodule in a file' '
355 test_must_fail git mv sub file
358 test_expect_success 'git mv moves a submodule with a .git directory and no .gitmodules' '
359 entry="$(index_at_path sub)" &&
360 git rm .gitmodules &&
362 cd sub &&
363 rm -f .git &&
364 cp -R -P -p ../.git/modules/sub .git &&
365 GIT_WORK_TREE=. git config --unset core.worktree
366 ) &&
367 mkdir mod &&
368 git mv sub mod/sub &&
369 test_path_is_missing sub &&
370 test "$entry" = "$(index_at_path mod/sub)" &&
371 git -C mod/sub status &&
372 git update-index --refresh &&
373 git diff-files --quiet
376 test_expect_success 'git mv moves a submodule with a .git directory and .gitmodules' '
377 rm -rf mod &&
378 git reset --hard &&
379 git submodule update &&
380 entry="$(index_at_path sub)" &&
382 cd sub &&
383 rm -f .git &&
384 cp -R -P -p ../.git/modules/sub .git &&
385 GIT_WORK_TREE=. git config --unset core.worktree
386 ) &&
387 mkdir mod &&
388 git mv sub mod/sub &&
389 test_path_is_missing sub &&
390 test "$entry" = "$(index_at_path mod/sub)" &&
391 git -C mod/sub status &&
392 echo mod/sub >expected &&
393 git config -f .gitmodules submodule.sub.path >actual &&
394 test_cmp expected actual &&
395 git update-index --refresh &&
396 git diff-files --quiet
399 test_expect_success 'git mv moves a submodule with gitfile' '
400 rm -rf mod &&
401 git reset --hard &&
402 git submodule update &&
403 entry="$(index_at_path sub)" &&
404 mkdir mod &&
405 git -C mod mv ../sub/ . &&
406 test_path_is_missing sub &&
407 test "$entry" = "$(index_at_path mod/sub)" &&
408 git -C mod/sub status &&
409 echo mod/sub >expected &&
410 git config -f .gitmodules submodule.sub.path >actual &&
411 test_cmp expected actual &&
412 git update-index --refresh &&
413 git diff-files --quiet
416 test_expect_success 'mv does not complain when no .gitmodules file is found' '
417 rm -rf mod &&
418 git reset --hard &&
419 git submodule update &&
420 git rm .gitmodules &&
421 entry="$(index_at_path sub)" &&
422 mkdir mod &&
423 git mv sub mod/sub 2>actual.err &&
424 test_must_be_empty actual.err &&
425 test_path_is_missing sub &&
426 test "$entry" = "$(index_at_path mod/sub)" &&
427 git -C mod/sub status &&
428 git update-index --refresh &&
429 git diff-files --quiet
432 test_expect_success 'mv will error out on a modified .gitmodules file unless staged' '
433 rm -rf mod &&
434 git reset --hard &&
435 git submodule update &&
436 git config -f .gitmodules foo.bar true &&
437 entry="$(index_at_path sub)" &&
438 mkdir mod &&
439 test_must_fail git mv sub mod/sub 2>actual.err &&
440 test_file_not_empty actual.err &&
441 test_path_exists sub &&
442 git diff-files --quiet -- sub &&
443 git add .gitmodules &&
444 git mv sub mod/sub 2>actual.err &&
445 test_must_be_empty actual.err &&
446 test_path_is_missing sub &&
447 test "$entry" = "$(index_at_path mod/sub)" &&
448 git -C mod/sub status &&
449 git update-index --refresh &&
450 git diff-files --quiet
453 test_expect_success 'mv issues a warning when section is not found in .gitmodules' '
454 rm -rf mod &&
455 git reset --hard &&
456 git submodule update &&
457 git config -f .gitmodules --remove-section submodule.sub &&
458 git add .gitmodules &&
459 entry="$(index_at_path sub)" &&
460 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
461 mkdir mod &&
462 git mv sub mod/sub 2>actual.err &&
463 test_cmp expect.err actual.err &&
464 test_path_is_missing sub &&
465 test "$entry" = "$(index_at_path mod/sub)" &&
466 git -C mod/sub status &&
467 git update-index --refresh &&
468 git diff-files --quiet
471 test_expect_success 'mv --dry-run does not touch the submodule or .gitmodules' '
472 rm -rf mod &&
473 git reset --hard &&
474 git submodule update &&
475 mkdir mod &&
476 git mv -n sub mod/sub 2>actual.err &&
477 test_path_is_file sub/.git &&
478 git diff-index --exit-code HEAD &&
479 git update-index --refresh &&
480 git diff-files --quiet -- sub .gitmodules
483 test_expect_success 'checking out a commit before submodule moved needs manual updates' '
484 git mv sub sub2 &&
485 git commit -m "moved sub to sub2" &&
486 git checkout -q HEAD^ 2>actual &&
487 test_grep "^warning: unable to rmdir '\''sub2'\'':" actual &&
488 git status -s sub2 >actual &&
489 echo "?? sub2/" >expected &&
490 test_cmp expected actual &&
491 test_path_is_missing sub/.git &&
492 test_path_is_file sub2/.git &&
493 git submodule update &&
494 test_path_is_file sub/.git &&
495 rm -rf sub2 &&
496 git diff-index --exit-code HEAD &&
497 git update-index --refresh &&
498 git diff-files --quiet -- sub .gitmodules &&
499 git status -s sub2 >actual &&
500 test_must_be_empty actual
503 test_expect_success 'mv -k does not accidentally destroy submodules' '
504 git checkout submodule &&
505 mkdir dummy dest &&
506 git mv -k dummy sub dest &&
507 git status --porcelain >actual &&
508 grep "^R sub -> dest/sub" actual &&
509 git reset --hard &&
510 git checkout .
513 test_expect_success 'moving a submodule in nested directories' '
515 cd deep &&
516 git mv directory ../ &&
517 # git status would fail if the update of linking git dir to
518 # work dir of the submodule failed.
519 git status &&
520 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
521 echo "directory/hierarchy/sub" >../expect
522 ) &&
523 test_cmp expect actual
526 test_expect_success 'moving nested submodules' '
527 test_config_global protocol.file.allow always &&
528 git commit -am "cleanup commit" &&
529 mkdir sub_nested_nested &&
531 cd sub_nested_nested &&
532 >nested_level2 &&
533 git init &&
534 git add . &&
535 git commit -m "nested level 2"
536 ) &&
537 mkdir sub_nested &&
539 cd sub_nested &&
540 >nested_level1 &&
541 git init &&
542 git add . &&
543 git commit -m "nested level 1" &&
544 git submodule add ../sub_nested_nested &&
545 git commit -m "add nested level 2"
546 ) &&
547 git submodule add ./sub_nested nested_move &&
548 git commit -m "add nested_move" &&
549 git submodule update --init --recursive &&
550 git mv nested_move sub_nested_moved &&
551 git status
554 test_done