commit-reach(paint_down_to_common): plug two memory leaks
[git.git] / t / t7001-mv.sh
blob879a6dce601110ac4bf8c59a51b32654e8726dcf
1 #!/bin/sh
3 test_description='git mv in subdirs'
4 . ./test-lib.sh
5 . "$TEST_DIRECTORY"/lib-diff-data.sh
7 index_at_path () {
8 git ls-files --format='%(objectmode) %(objectname) %(stage)' "$@"
11 test_expect_success 'mv -f refreshes updated index entry' '
12 echo test >bar &&
13 git add bar &&
14 git commit -m test &&
16 echo foo >foo &&
17 git add foo &&
19 # Wait one second to ensure ctime of rename will differ from original
20 # file creation ctime.
21 sleep 1 &&
22 git mv -f foo bar &&
23 git reset --merge HEAD &&
25 # Verify the index has been reset
26 git diff-files >out &&
27 test_must_be_empty out
30 test_expect_success 'prepare reference tree' '
31 mkdir path0 path1 &&
32 COPYING_test_data >path0/COPYING &&
33 git add path0/COPYING &&
34 git commit -m add -a
37 test_expect_success 'moving the file out of subdirectory' '
38 git -C path0 mv COPYING ../path1/COPYING
41 # in path0 currently
42 test_expect_success 'commiting the change' '
43 git commit -m move-out -a
46 test_expect_success 'checking the commit' '
47 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
48 grep "^R100..*path0/COPYING..*path1/COPYING" actual
51 test_expect_success 'moving the file back into subdirectory' '
52 git -C path0 mv ../path1/COPYING COPYING
55 # in path0 currently
56 test_expect_success 'commiting the change' '
57 git commit -m move-in -a
60 test_expect_success 'checking the commit' '
61 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
62 grep "^R100..*path1/COPYING..*path0/COPYING" actual
65 test_expect_success 'mv --dry-run does not move file' '
66 git mv -n path0/COPYING MOVED &&
67 test_path_is_file path0/COPYING &&
68 test_path_is_missing MOVED
71 test_expect_success 'checking -k on non-existing file' '
72 git mv -k idontexist path0
75 test_expect_success 'checking -k on untracked file' '
76 >untracked1 &&
77 git mv -k untracked1 path0 &&
78 test_path_is_file untracked1 &&
79 test_path_is_missing path0/untracked1
82 test_expect_success 'checking -k on multiple untracked files' '
83 >untracked2 &&
84 git mv -k untracked1 untracked2 path0 &&
85 test_path_is_file untracked1 &&
86 test_path_is_file untracked2 &&
87 test_path_is_missing path0/untracked1 &&
88 test_path_is_missing path0/untracked2
91 test_expect_success 'checking -f on untracked file with existing target' '
92 >path0/untracked1 &&
93 test_must_fail git mv -f untracked1 path0 &&
94 test_path_is_missing .git/index.lock &&
95 test_path_is_file untracked1 &&
96 test_path_is_file path0/untracked1
99 # clean up the mess in case bad things happen
100 rm -f idontexist untracked1 untracked2 \
101 path0/idontexist path0/untracked1 path0/untracked2 \
102 .git/index.lock
103 rmdir path1
105 test_expect_success 'moving to absent target with trailing slash' '
106 test_must_fail git mv path0/COPYING no-such-dir/ &&
107 test_must_fail git mv path0/COPYING no-such-dir// &&
108 git mv path0/ no-such-dir/ &&
109 test_path_is_dir no-such-dir
112 test_expect_success 'clean up' '
113 git reset --hard
116 test_expect_success 'moving to existing untracked target with trailing slash' '
117 mkdir path1 &&
118 git mv path0/ path1/ &&
119 test_path_is_dir path1/path0/
122 test_expect_success 'moving to existing tracked target with trailing slash' '
123 mkdir path2 &&
124 >path2/file && git add path2/file &&
125 git mv path1/path0/ path2/ &&
126 test_path_is_dir path2/path0/
129 test_expect_success 'clean up' '
130 git reset --hard
133 test_expect_success 'adding another file' '
134 COPYING_test_data | tr A-Za-z N-ZA-Mn-za-m >path0/README &&
135 git add path0/README &&
136 git commit -m add2 -a
139 test_expect_success 'moving whole subdirectory' '
140 git mv path0 path2
143 test_expect_success 'commiting the change' '
144 git commit -m dir-move -a
147 test_expect_success 'checking the commit' '
148 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
149 grep "^R100..*path0/COPYING..*path2/COPYING" actual &&
150 grep "^R100..*path0/README..*path2/README" actual
153 test_expect_success 'succeed when source is a prefix of destination' '
154 git mv path2/COPYING path2/COPYING-renamed
157 test_expect_success 'moving whole subdirectory into subdirectory' '
158 git mv path2 path1
161 test_expect_success 'commiting the change' '
162 git commit -m dir-move -a
165 test_expect_success 'checking the commit' '
166 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
167 grep "^R100..*path2/COPYING..*path1/path2/COPYING" actual &&
168 grep "^R100..*path2/README..*path1/path2/README" actual
171 test_expect_success 'do not move directory over existing directory' '
172 mkdir path0 &&
173 mkdir path0/path2 &&
174 test_must_fail git mv path2 path0
177 test_expect_success 'rename directory to non-existing directory' '
178 mkdir dir-a &&
179 >dir-a/f &&
180 git add dir-a &&
181 git mv dir-a non-existing-dir
184 test_expect_success 'move into "."' '
185 git mv path1/path2/ .
188 test_expect_success "Michael Cassar's test case" '
189 rm -fr .git papers partA &&
190 git init &&
191 mkdir -p papers/unsorted papers/all-papers partA &&
192 echo a >papers/unsorted/Thesis.pdf &&
193 echo b >partA/outline.txt &&
194 echo c >papers/unsorted/_another &&
195 git add papers partA &&
196 T1=$(git write-tree) &&
198 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
200 T=$(git write-tree) &&
201 git ls-tree -r $T >out &&
202 grep partA/outline.txt out
205 rm -fr papers partA path?
207 test_expect_success "Sergey Vlasov's test case" '
208 rm -fr .git &&
209 git init &&
210 mkdir ab &&
211 date >ab.c &&
212 date >ab/d &&
213 git add ab.c ab &&
214 git commit -m "initial" &&
215 git mv ab a
218 test_expect_success 'absolute pathname' '
220 rm -fr mine &&
221 mkdir mine &&
222 cd mine &&
223 test_create_repo one &&
224 cd one &&
225 mkdir sub &&
226 >sub/file &&
227 git add sub/file &&
229 git mv sub "$(pwd)/in" &&
230 test_path_is_missing sub &&
231 test_path_is_dir in &&
232 git ls-files --error-unmatch in/file
236 test_expect_success 'absolute pathname outside should fail' '
238 rm -fr mine &&
239 mkdir mine &&
240 cd mine &&
241 out=$(pwd) &&
242 test_create_repo one &&
243 cd one &&
244 mkdir sub &&
245 >sub/file &&
246 git add sub/file &&
248 test_must_fail git mv sub "$out/out" &&
249 test_path_is_dir sub &&
250 test_path_is_missing ../in &&
251 git ls-files --error-unmatch sub/file
255 test_expect_success 'git mv to move multiple sources into a directory' '
256 rm -fr .git && git init &&
257 mkdir dir other &&
258 >dir/a.txt &&
259 >dir/b.txt &&
260 git add dir/?.txt &&
261 git mv dir/a.txt dir/b.txt other &&
262 git ls-files >actual &&
263 cat >expect <<-\EOF &&
264 other/a.txt
265 other/b.txt
267 test_cmp expect actual
270 test_expect_success 'git mv should not change sha1 of moved cache entry' '
271 rm -fr .git &&
272 git init &&
273 echo 1 >dirty &&
274 git add dirty &&
275 entry="$(index_at_path dirty)" &&
276 git mv dirty dirty2 &&
277 test "$entry" = "$(index_at_path dirty2)" &&
278 echo 2 >dirty2 &&
279 git mv dirty2 dirty &&
280 test "$entry" = "$(index_at_path dirty)"
283 rm -f dirty dirty2
285 # NB: This test is about the error message
286 # as well as the failure.
287 test_expect_success 'git mv error on conflicted file' '
288 rm -fr .git &&
289 git init &&
290 >conflict &&
291 test_when_finished "rm -f conflict" &&
292 cfhash=$(git hash-object -w conflict) &&
293 q_to_tab <<-EOF | git update-index --index-info &&
294 0 $cfhash 0Qconflict
295 100644 $cfhash 1Qconflict
298 test_must_fail git mv conflict newname 2>actual &&
299 test_grep "conflicted" actual
302 test_expect_success 'git mv should overwrite symlink to a file' '
303 rm -fr .git &&
304 git init &&
305 echo 1 >moved &&
306 test_ln_s_add moved symlink &&
307 git add moved &&
308 test_must_fail git mv moved symlink &&
309 git mv -f moved symlink &&
310 test_path_is_missing moved &&
311 test_path_is_file symlink &&
312 test "$(cat symlink)" = 1 &&
313 git update-index --refresh &&
314 git diff-files --quiet
317 rm -f moved symlink
319 test_expect_success 'git mv should overwrite file with a symlink' '
320 rm -fr .git &&
321 git init &&
322 echo 1 >moved &&
323 test_ln_s_add moved symlink &&
324 git add moved &&
325 test_must_fail git mv symlink moved &&
326 git mv -f symlink moved &&
327 test_path_is_missing symlink &&
328 git update-index --refresh &&
329 git diff-files --quiet
332 test_expect_success SYMLINKS 'check moved symlink' '
333 test_path_is_symlink moved
336 rm -f moved symlink
338 test_expect_success 'setup submodule' '
339 test_config_global protocol.file.allow always &&
340 git commit -m initial &&
341 git reset --hard &&
342 git submodule add ./. sub &&
343 echo content >file &&
344 git add file &&
345 git commit -m "added sub and file" &&
346 mkdir -p deep/directory/hierarchy &&
347 git submodule add ./. deep/directory/hierarchy/sub &&
348 git commit -m "added another submodule" &&
349 git branch submodule
352 test_expect_success 'git mv cannot move a submodule in a file' '
353 test_must_fail git mv sub file
356 test_expect_success 'git mv moves a submodule with a .git directory and no .gitmodules' '
357 entry="$(index_at_path sub)" &&
358 git rm .gitmodules &&
360 cd sub &&
361 rm -f .git &&
362 cp -R -P -p ../.git/modules/sub .git &&
363 GIT_WORK_TREE=. git config --unset core.worktree
364 ) &&
365 mkdir mod &&
366 git mv sub mod/sub &&
367 test_path_is_missing sub &&
368 test "$entry" = "$(index_at_path mod/sub)" &&
369 git -C mod/sub status &&
370 git update-index --refresh &&
371 git diff-files --quiet
374 test_expect_success 'git mv moves a submodule with a .git directory and .gitmodules' '
375 rm -rf mod &&
376 git reset --hard &&
377 git submodule update &&
378 entry="$(index_at_path sub)" &&
380 cd sub &&
381 rm -f .git &&
382 cp -R -P -p ../.git/modules/sub .git &&
383 GIT_WORK_TREE=. git config --unset core.worktree
384 ) &&
385 mkdir mod &&
386 git mv sub mod/sub &&
387 test_path_is_missing sub &&
388 test "$entry" = "$(index_at_path mod/sub)" &&
389 git -C mod/sub status &&
390 echo mod/sub >expected &&
391 git config -f .gitmodules submodule.sub.path >actual &&
392 test_cmp expected actual &&
393 git update-index --refresh &&
394 git diff-files --quiet
397 test_expect_success 'git mv moves a submodule with gitfile' '
398 rm -rf mod &&
399 git reset --hard &&
400 git submodule update &&
401 entry="$(index_at_path sub)" &&
402 mkdir mod &&
403 git -C mod mv ../sub/ . &&
404 test_path_is_missing sub &&
405 test "$entry" = "$(index_at_path mod/sub)" &&
406 git -C mod/sub status &&
407 echo mod/sub >expected &&
408 git config -f .gitmodules submodule.sub.path >actual &&
409 test_cmp expected actual &&
410 git update-index --refresh &&
411 git diff-files --quiet
414 test_expect_success 'mv does not complain when no .gitmodules file is found' '
415 rm -rf mod &&
416 git reset --hard &&
417 git submodule update &&
418 git rm .gitmodules &&
419 entry="$(index_at_path sub)" &&
420 mkdir mod &&
421 git mv sub mod/sub 2>actual.err &&
422 test_must_be_empty actual.err &&
423 test_path_is_missing sub &&
424 test "$entry" = "$(index_at_path mod/sub)" &&
425 git -C mod/sub status &&
426 git update-index --refresh &&
427 git diff-files --quiet
430 test_expect_success 'mv will error out on a modified .gitmodules file unless staged' '
431 rm -rf mod &&
432 git reset --hard &&
433 git submodule update &&
434 git config -f .gitmodules foo.bar true &&
435 entry="$(index_at_path sub)" &&
436 mkdir mod &&
437 test_must_fail git mv sub mod/sub 2>actual.err &&
438 test_file_not_empty actual.err &&
439 test_path_exists sub &&
440 git diff-files --quiet -- sub &&
441 git add .gitmodules &&
442 git mv sub mod/sub 2>actual.err &&
443 test_must_be_empty actual.err &&
444 test_path_is_missing sub &&
445 test "$entry" = "$(index_at_path mod/sub)" &&
446 git -C mod/sub status &&
447 git update-index --refresh &&
448 git diff-files --quiet
451 test_expect_success 'mv issues a warning when section is not found in .gitmodules' '
452 rm -rf mod &&
453 git reset --hard &&
454 git submodule update &&
455 git config -f .gitmodules --remove-section submodule.sub &&
456 git add .gitmodules &&
457 entry="$(index_at_path sub)" &&
458 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
459 mkdir mod &&
460 git mv sub mod/sub 2>actual.err &&
461 test_cmp expect.err actual.err &&
462 test_path_is_missing sub &&
463 test "$entry" = "$(index_at_path mod/sub)" &&
464 git -C mod/sub status &&
465 git update-index --refresh &&
466 git diff-files --quiet
469 test_expect_success 'mv --dry-run does not touch the submodule or .gitmodules' '
470 rm -rf mod &&
471 git reset --hard &&
472 git submodule update &&
473 mkdir mod &&
474 git mv -n sub mod/sub 2>actual.err &&
475 test_path_is_file sub/.git &&
476 git diff-index --exit-code HEAD &&
477 git update-index --refresh &&
478 git diff-files --quiet -- sub .gitmodules
481 test_expect_success 'checking out a commit before submodule moved needs manual updates' '
482 git mv sub sub2 &&
483 git commit -m "moved sub to sub2" &&
484 git checkout -q HEAD^ 2>actual &&
485 test_grep "^warning: unable to rmdir '\''sub2'\'':" actual &&
486 git status -s sub2 >actual &&
487 echo "?? sub2/" >expected &&
488 test_cmp expected actual &&
489 test_path_is_missing sub/.git &&
490 test_path_is_file sub2/.git &&
491 git submodule update &&
492 test_path_is_file sub/.git &&
493 rm -rf sub2 &&
494 git diff-index --exit-code HEAD &&
495 git update-index --refresh &&
496 git diff-files --quiet -- sub .gitmodules &&
497 git status -s sub2 >actual &&
498 test_must_be_empty actual
501 test_expect_success 'mv -k does not accidentally destroy submodules' '
502 git checkout submodule &&
503 mkdir dummy dest &&
504 git mv -k dummy sub dest &&
505 git status --porcelain >actual &&
506 grep "^R sub -> dest/sub" actual &&
507 git reset --hard &&
508 git checkout .
511 test_expect_success 'moving a submodule in nested directories' '
513 cd deep &&
514 git mv directory ../ &&
515 # git status would fail if the update of linking git dir to
516 # work dir of the submodule failed.
517 git status &&
518 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
519 echo "directory/hierarchy/sub" >../expect
520 ) &&
521 test_cmp expect actual
524 test_expect_success 'moving nested submodules' '
525 test_config_global protocol.file.allow always &&
526 git commit -am "cleanup commit" &&
527 mkdir sub_nested_nested &&
529 cd sub_nested_nested &&
530 >nested_level2 &&
531 git init &&
532 git add . &&
533 git commit -m "nested level 2"
534 ) &&
535 mkdir sub_nested &&
537 cd sub_nested &&
538 >nested_level1 &&
539 git init &&
540 git add . &&
541 git commit -m "nested level 1" &&
542 git submodule add ../sub_nested_nested &&
543 git commit -m "add nested level 2"
544 ) &&
545 git submodule add ./sub_nested nested_move &&
546 git commit -m "add nested_move" &&
547 git submodule update --init --recursive &&
548 git mv nested_move sub_nested_moved &&
549 git status
552 test_done