]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7001-mv.sh
t9001: fix indentation in test_no_confirm()
[thirdparty/git.git] / t / t7001-mv.sh
1 #!/bin/sh
2
3 test_description='git mv in subdirs'
4 . ./test-lib.sh
5 . "$TEST_DIRECTORY"/lib-diff-data.sh
6
7 test_expect_success 'mv -f refreshes updated index entry' '
8 echo test >bar &&
9 git add bar &&
10 git commit -m test &&
11
12 echo foo >foo &&
13 git add foo &&
14
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 &&
20
21 # Verify the index has been reset
22 git diff-files >out &&
23 test_must_be_empty out
24 '
25
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
31 '
32
33 test_expect_success 'moving the file out of subdirectory' '
34 git -C path0 mv COPYING ../path1/COPYING
35 '
36
37 # in path0 currently
38 test_expect_success 'commiting the change' '
39 git commit -m move-out -a
40 '
41
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
45 '
46
47 test_expect_success 'moving the file back into subdirectory' '
48 git -C path0 mv ../path1/COPYING COPYING
49 '
50
51 # in path0 currently
52 test_expect_success 'commiting the change' '
53 git commit -m move-in -a
54 '
55
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
59 '
60
61 test_expect_success 'mv --dry-run does not move file' '
62 git mv -n path0/COPYING MOVED &&
63 test_path_is_file path0/COPYING &&
64 test_path_is_missing MOVED
65 '
66
67 test_expect_success 'checking -k on non-existing file' '
68 git mv -k idontexist path0
69 '
70
71 test_expect_success 'checking -k on untracked file' '
72 >untracked1 &&
73 git mv -k untracked1 path0 &&
74 test_path_is_file untracked1 &&
75 test_path_is_missing path0/untracked1
76 '
77
78 test_expect_success 'checking -k on multiple untracked files' '
79 >untracked2 &&
80 git mv -k untracked1 untracked2 path0 &&
81 test_path_is_file untracked1 &&
82 test_path_is_file untracked2 &&
83 test_path_is_missing path0/untracked1 &&
84 test_path_is_missing path0/untracked2
85 '
86
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_path_is_missing .git/index.lock &&
91 test_path_is_file untracked1 &&
92 test_path_is_file path0/untracked1
93 '
94
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
100
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
106 '
107
108 test_expect_success 'clean up' '
109 git reset --hard
110 '
111
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/
116 '
117
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/
123 '
124
125 test_expect_success 'clean up' '
126 git reset --hard
127 '
128
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
133 '
134
135 test_expect_success 'moving whole subdirectory' '
136 git mv path0 path2
137 '
138
139 test_expect_success 'commiting the change' '
140 git commit -m dir-move -a
141 '
142
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
147 '
148
149 test_expect_success 'succeed when source is a prefix of destination' '
150 git mv path2/COPYING path2/COPYING-renamed
151 '
152
153 test_expect_success 'moving whole subdirectory into subdirectory' '
154 git mv path2 path1
155 '
156
157 test_expect_success 'commiting the change' '
158 git commit -m dir-move -a
159 '
160
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
165 '
166
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
171 '
172
173 test_expect_success 'move into "."' '
174 git mv path1/path2/ .
175 '
176
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) &&
186
187 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
188
189 T=$(git write-tree) &&
190 git ls-tree -r $T | verbose grep partA/outline.txt
191 '
192
193 rm -fr papers partA path?
194
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
204 '
205
206 test_expect_success 'absolute pathname' '
207 (
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 &&
216
217 git mv sub "$(pwd)/in" &&
218 test_path_is_missing sub &&
219 test_path_is_dir in &&
220 git ls-files --error-unmatch in/file
221 )
222 '
223
224 test_expect_success 'absolute pathname outside should fail' '
225 (
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 &&
235
236 test_must_fail git mv sub "$out/out" &&
237 test_path_is_dir sub &&
238 test_path_is_missing ../in &&
239 git ls-files --error-unmatch sub/file
240 )
241 '
242
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
254 EOF
255 test_cmp expect actual
256 '
257
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)"
269 '
270
271 rm -f dirty dirty2
272
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
284 EOF
285
286 test_must_fail git mv conflict newname 2>actual &&
287 test_i18ngrep "conflicted" actual
288 '
289
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_path_is_missing moved &&
299 test_path_is_file symlink &&
300 test "$(cat symlink)" = 1 &&
301 git update-index --refresh &&
302 git diff-files --quiet
303 '
304
305 rm -f moved symlink
306
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_path_is_missing symlink &&
316 git update-index --refresh &&
317 git diff-files --quiet
318 '
319
320 test_expect_success SYMLINKS 'check moved symlink' '
321 test_path_is_symlink moved
322 '
323
324 rm -f moved symlink
325
326 test_expect_success 'setup submodule' '
327 test_config_global protocol.file.allow always &&
328 git commit -m initial &&
329 git reset --hard &&
330 git submodule add ./. sub &&
331 echo content >file &&
332 git add file &&
333 git commit -m "added sub and file" &&
334 mkdir -p deep/directory/hierarchy &&
335 git submodule add ./. deep/directory/hierarchy/sub &&
336 git commit -m "added another submodule" &&
337 git branch submodule
338 '
339
340 test_expect_success 'git mv cannot move a submodule in a file' '
341 test_must_fail git mv sub file
342 '
343
344 test_expect_success 'git mv moves a submodule with a .git directory and no .gitmodules' '
345 entry="$(git ls-files --stage sub | cut -f 1)" &&
346 git rm .gitmodules &&
347 (
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_path_is_missing sub &&
356 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
357 git -C mod/sub status &&
358 git update-index --refresh &&
359 git diff-files --quiet
360 '
361
362 test_expect_success 'git mv moves a submodule with a .git directory and .gitmodules' '
363 rm -rf mod &&
364 git reset --hard &&
365 git submodule update &&
366 entry="$(git ls-files --stage sub | cut -f 1)" &&
367 (
368 cd sub &&
369 rm -f .git &&
370 cp -R -P -p ../.git/modules/sub .git &&
371 GIT_WORK_TREE=. git config --unset core.worktree
372 ) &&
373 mkdir mod &&
374 git mv sub mod/sub &&
375 test_path_is_missing sub &&
376 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
377 git -C mod/sub status &&
378 echo mod/sub >expected &&
379 git config -f .gitmodules submodule.sub.path >actual &&
380 test_cmp expected actual &&
381 git update-index --refresh &&
382 git diff-files --quiet
383 '
384
385 test_expect_success 'git mv moves a submodule with gitfile' '
386 rm -rf mod &&
387 git reset --hard &&
388 git submodule update &&
389 entry="$(git ls-files --stage sub | cut -f 1)" &&
390 mkdir mod &&
391 git -C mod mv ../sub/ . &&
392 test_path_is_missing sub &&
393 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
394 git -C mod/sub status &&
395 echo mod/sub >expected &&
396 git config -f .gitmodules submodule.sub.path >actual &&
397 test_cmp expected actual &&
398 git update-index --refresh &&
399 git diff-files --quiet
400 '
401
402 test_expect_success 'mv does not complain when no .gitmodules file is found' '
403 rm -rf mod &&
404 git reset --hard &&
405 git submodule update &&
406 git rm .gitmodules &&
407 entry="$(git ls-files --stage sub | cut -f 1)" &&
408 mkdir mod &&
409 git mv sub mod/sub 2>actual.err &&
410 test_must_be_empty actual.err &&
411 test_path_is_missing sub &&
412 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
413 git -C mod/sub status &&
414 git update-index --refresh &&
415 git diff-files --quiet
416 '
417
418 test_expect_success 'mv will error out on a modified .gitmodules file unless staged' '
419 rm -rf mod &&
420 git reset --hard &&
421 git submodule update &&
422 git config -f .gitmodules foo.bar true &&
423 entry="$(git ls-files --stage sub | cut -f 1)" &&
424 mkdir mod &&
425 test_must_fail git mv sub mod/sub 2>actual.err &&
426 test_file_not_empty actual.err &&
427 test_path_exists sub &&
428 git diff-files --quiet -- sub &&
429 git add .gitmodules &&
430 git mv sub mod/sub 2>actual.err &&
431 test_must_be_empty actual.err &&
432 test_path_is_missing sub &&
433 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
434 git -C mod/sub status &&
435 git update-index --refresh &&
436 git diff-files --quiet
437 '
438
439 test_expect_success 'mv issues a warning when section is not found in .gitmodules' '
440 rm -rf mod &&
441 git reset --hard &&
442 git submodule update &&
443 git config -f .gitmodules --remove-section submodule.sub &&
444 git add .gitmodules &&
445 entry="$(git ls-files --stage sub | cut -f 1)" &&
446 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
447 mkdir mod &&
448 git mv sub mod/sub 2>actual.err &&
449 test_cmp expect.err actual.err &&
450 test_path_is_missing sub &&
451 test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
452 git -C mod/sub status &&
453 git update-index --refresh &&
454 git diff-files --quiet
455 '
456
457 test_expect_success 'mv --dry-run does not touch the submodule or .gitmodules' '
458 rm -rf mod &&
459 git reset --hard &&
460 git submodule update &&
461 mkdir mod &&
462 git mv -n sub mod/sub 2>actual.err &&
463 test_path_is_file sub/.git &&
464 git diff-index --exit-code HEAD &&
465 git update-index --refresh &&
466 git diff-files --quiet -- sub .gitmodules
467 '
468
469 test_expect_success 'checking out a commit before submodule moved needs manual updates' '
470 git mv sub sub2 &&
471 git commit -m "moved sub to sub2" &&
472 git checkout -q HEAD^ 2>actual &&
473 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
474 git status -s sub2 >actual &&
475 echo "?? sub2/" >expected &&
476 test_cmp expected actual &&
477 test_path_is_missing sub/.git &&
478 test_path_is_file sub2/.git &&
479 git submodule update &&
480 test_path_is_file sub/.git &&
481 rm -rf sub2 &&
482 git diff-index --exit-code HEAD &&
483 git update-index --refresh &&
484 git diff-files --quiet -- sub .gitmodules &&
485 git status -s sub2 >actual &&
486 test_must_be_empty actual
487 '
488
489 test_expect_success 'mv -k does not accidentally destroy submodules' '
490 git checkout submodule &&
491 mkdir dummy dest &&
492 git mv -k dummy sub dest &&
493 git status --porcelain >actual &&
494 grep "^R sub -> dest/sub" actual &&
495 git reset --hard &&
496 git checkout .
497 '
498
499 test_expect_success 'moving a submodule in nested directories' '
500 (
501 cd deep &&
502 git mv directory ../ &&
503 # git status would fail if the update of linking git dir to
504 # work dir of the submodule failed.
505 git status &&
506 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
507 echo "directory/hierarchy/sub" >../expect
508 ) &&
509 test_cmp expect actual
510 '
511
512 test_expect_success 'moving nested submodules' '
513 test_config_global protocol.file.allow always &&
514 git commit -am "cleanup commit" &&
515 mkdir sub_nested_nested &&
516 (
517 cd sub_nested_nested &&
518 >nested_level2 &&
519 git init &&
520 git add . &&
521 git commit -m "nested level 2"
522 ) &&
523 mkdir sub_nested &&
524 (
525 cd sub_nested &&
526 >nested_level1 &&
527 git init &&
528 git add . &&
529 git commit -m "nested level 1" &&
530 git submodule add ../sub_nested_nested &&
531 git commit -m "add nested level 2"
532 ) &&
533 git submodule add ./sub_nested nested_move &&
534 git commit -m "add nested_move" &&
535 git submodule update --init --recursive &&
536 git mv nested_move sub_nested_moved &&
537 git status
538 '
539
540 test_done