]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7001-mv.sh
rebase: allow overriding the maximal length of the generated labels
[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 index_at_path () {
8 git ls-files --format='%(objectmode) %(objectname) %(stage)' "$@"
9 }
10
11 test_expect_success 'mv -f refreshes updated index entry' '
12 echo test >bar &&
13 git add bar &&
14 git commit -m test &&
15
16 echo foo >foo &&
17 git add foo &&
18
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 &&
24
25 # Verify the index has been reset
26 git diff-files >out &&
27 test_must_be_empty out
28 '
29
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
35 '
36
37 test_expect_success 'moving the file out of subdirectory' '
38 git -C path0 mv COPYING ../path1/COPYING
39 '
40
41 # in path0 currently
42 test_expect_success 'commiting the change' '
43 git commit -m move-out -a
44 '
45
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
49 '
50
51 test_expect_success 'moving the file back into subdirectory' '
52 git -C path0 mv ../path1/COPYING COPYING
53 '
54
55 # in path0 currently
56 test_expect_success 'commiting the change' '
57 git commit -m move-in -a
58 '
59
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
63 '
64
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
69 '
70
71 test_expect_success 'checking -k on non-existing file' '
72 git mv -k idontexist path0
73 '
74
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
80 '
81
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
89 '
90
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
97 '
98
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
104
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
110 '
111
112 test_expect_success 'clean up' '
113 git reset --hard
114 '
115
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/
120 '
121
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/
127 '
128
129 test_expect_success 'clean up' '
130 git reset --hard
131 '
132
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
137 '
138
139 test_expect_success 'moving whole subdirectory' '
140 git mv path0 path2
141 '
142
143 test_expect_success 'commiting the change' '
144 git commit -m dir-move -a
145 '
146
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
151 '
152
153 test_expect_success 'succeed when source is a prefix of destination' '
154 git mv path2/COPYING path2/COPYING-renamed
155 '
156
157 test_expect_success 'moving whole subdirectory into subdirectory' '
158 git mv path2 path1
159 '
160
161 test_expect_success 'commiting the change' '
162 git commit -m dir-move -a
163 '
164
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
169 '
170
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
175 '
176
177 test_expect_success 'move into "."' '
178 git mv path1/path2/ .
179 '
180
181 test_expect_success "Michael Cassar's test case" '
182 rm -fr .git papers partA &&
183 git init &&
184 mkdir -p papers/unsorted papers/all-papers partA &&
185 echo a >papers/unsorted/Thesis.pdf &&
186 echo b >partA/outline.txt &&
187 echo c >papers/unsorted/_another &&
188 git add papers partA &&
189 T1=$(git write-tree) &&
190
191 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
192
193 T=$(git write-tree) &&
194 git ls-tree -r $T >out &&
195 grep partA/outline.txt out
196 '
197
198 rm -fr papers partA path?
199
200 test_expect_success "Sergey Vlasov's test case" '
201 rm -fr .git &&
202 git init &&
203 mkdir ab &&
204 date >ab.c &&
205 date >ab/d &&
206 git add ab.c ab &&
207 git commit -m "initial" &&
208 git mv ab a
209 '
210
211 test_expect_success 'absolute pathname' '
212 (
213 rm -fr mine &&
214 mkdir mine &&
215 cd mine &&
216 test_create_repo one &&
217 cd one &&
218 mkdir sub &&
219 >sub/file &&
220 git add sub/file &&
221
222 git mv sub "$(pwd)/in" &&
223 test_path_is_missing sub &&
224 test_path_is_dir in &&
225 git ls-files --error-unmatch in/file
226 )
227 '
228
229 test_expect_success 'absolute pathname outside should fail' '
230 (
231 rm -fr mine &&
232 mkdir mine &&
233 cd mine &&
234 out=$(pwd) &&
235 test_create_repo one &&
236 cd one &&
237 mkdir sub &&
238 >sub/file &&
239 git add sub/file &&
240
241 test_must_fail git mv sub "$out/out" &&
242 test_path_is_dir sub &&
243 test_path_is_missing ../in &&
244 git ls-files --error-unmatch sub/file
245 )
246 '
247
248 test_expect_success 'git mv to move multiple sources into a directory' '
249 rm -fr .git && git init &&
250 mkdir dir other &&
251 >dir/a.txt &&
252 >dir/b.txt &&
253 git add dir/?.txt &&
254 git mv dir/a.txt dir/b.txt other &&
255 git ls-files >actual &&
256 cat >expect <<-\EOF &&
257 other/a.txt
258 other/b.txt
259 EOF
260 test_cmp expect actual
261 '
262
263 test_expect_success 'git mv should not change sha1 of moved cache entry' '
264 rm -fr .git &&
265 git init &&
266 echo 1 >dirty &&
267 git add dirty &&
268 entry="$(index_at_path dirty)" &&
269 git mv dirty dirty2 &&
270 test "$entry" = "$(index_at_path dirty2)" &&
271 echo 2 >dirty2 &&
272 git mv dirty2 dirty &&
273 test "$entry" = "$(index_at_path dirty)"
274 '
275
276 rm -f dirty dirty2
277
278 # NB: This test is about the error message
279 # as well as the failure.
280 test_expect_success 'git mv error on conflicted file' '
281 rm -fr .git &&
282 git init &&
283 >conflict &&
284 test_when_finished "rm -f conflict" &&
285 cfhash=$(git hash-object -w conflict) &&
286 q_to_tab <<-EOF | git update-index --index-info &&
287 0 $cfhash 0Qconflict
288 100644 $cfhash 1Qconflict
289 EOF
290
291 test_must_fail git mv conflict newname 2>actual &&
292 test_i18ngrep "conflicted" actual
293 '
294
295 test_expect_success 'git mv should overwrite symlink to a file' '
296 rm -fr .git &&
297 git init &&
298 echo 1 >moved &&
299 test_ln_s_add moved symlink &&
300 git add moved &&
301 test_must_fail git mv moved symlink &&
302 git mv -f moved symlink &&
303 test_path_is_missing moved &&
304 test_path_is_file symlink &&
305 test "$(cat symlink)" = 1 &&
306 git update-index --refresh &&
307 git diff-files --quiet
308 '
309
310 rm -f moved symlink
311
312 test_expect_success 'git mv should overwrite file with a symlink' '
313 rm -fr .git &&
314 git init &&
315 echo 1 >moved &&
316 test_ln_s_add moved symlink &&
317 git add moved &&
318 test_must_fail git mv symlink moved &&
319 git mv -f symlink moved &&
320 test_path_is_missing symlink &&
321 git update-index --refresh &&
322 git diff-files --quiet
323 '
324
325 test_expect_success SYMLINKS 'check moved symlink' '
326 test_path_is_symlink moved
327 '
328
329 rm -f moved symlink
330
331 test_expect_success 'setup submodule' '
332 test_config_global protocol.file.allow always &&
333 git commit -m initial &&
334 git reset --hard &&
335 git submodule add ./. sub &&
336 echo content >file &&
337 git add file &&
338 git commit -m "added sub and file" &&
339 mkdir -p deep/directory/hierarchy &&
340 git submodule add ./. deep/directory/hierarchy/sub &&
341 git commit -m "added another submodule" &&
342 git branch submodule
343 '
344
345 test_expect_success 'git mv cannot move a submodule in a file' '
346 test_must_fail git mv sub file
347 '
348
349 test_expect_success 'git mv moves a submodule with a .git directory and no .gitmodules' '
350 entry="$(index_at_path sub)" &&
351 git rm .gitmodules &&
352 (
353 cd sub &&
354 rm -f .git &&
355 cp -R -P -p ../.git/modules/sub .git &&
356 GIT_WORK_TREE=. git config --unset core.worktree
357 ) &&
358 mkdir mod &&
359 git mv sub mod/sub &&
360 test_path_is_missing sub &&
361 test "$entry" = "$(index_at_path mod/sub)" &&
362 git -C mod/sub status &&
363 git update-index --refresh &&
364 git diff-files --quiet
365 '
366
367 test_expect_success 'git mv moves a submodule with a .git directory and .gitmodules' '
368 rm -rf mod &&
369 git reset --hard &&
370 git submodule update &&
371 entry="$(index_at_path sub)" &&
372 (
373 cd sub &&
374 rm -f .git &&
375 cp -R -P -p ../.git/modules/sub .git &&
376 GIT_WORK_TREE=. git config --unset core.worktree
377 ) &&
378 mkdir mod &&
379 git mv sub mod/sub &&
380 test_path_is_missing sub &&
381 test "$entry" = "$(index_at_path mod/sub)" &&
382 git -C mod/sub status &&
383 echo mod/sub >expected &&
384 git config -f .gitmodules submodule.sub.path >actual &&
385 test_cmp expected actual &&
386 git update-index --refresh &&
387 git diff-files --quiet
388 '
389
390 test_expect_success 'git mv moves a submodule with gitfile' '
391 rm -rf mod &&
392 git reset --hard &&
393 git submodule update &&
394 entry="$(index_at_path sub)" &&
395 mkdir mod &&
396 git -C mod mv ../sub/ . &&
397 test_path_is_missing sub &&
398 test "$entry" = "$(index_at_path mod/sub)" &&
399 git -C mod/sub status &&
400 echo mod/sub >expected &&
401 git config -f .gitmodules submodule.sub.path >actual &&
402 test_cmp expected actual &&
403 git update-index --refresh &&
404 git diff-files --quiet
405 '
406
407 test_expect_success 'mv does not complain when no .gitmodules file is found' '
408 rm -rf mod &&
409 git reset --hard &&
410 git submodule update &&
411 git rm .gitmodules &&
412 entry="$(index_at_path sub)" &&
413 mkdir mod &&
414 git mv sub mod/sub 2>actual.err &&
415 test_must_be_empty actual.err &&
416 test_path_is_missing sub &&
417 test "$entry" = "$(index_at_path mod/sub)" &&
418 git -C mod/sub status &&
419 git update-index --refresh &&
420 git diff-files --quiet
421 '
422
423 test_expect_success 'mv will error out on a modified .gitmodules file unless staged' '
424 rm -rf mod &&
425 git reset --hard &&
426 git submodule update &&
427 git config -f .gitmodules foo.bar true &&
428 entry="$(index_at_path sub)" &&
429 mkdir mod &&
430 test_must_fail git mv sub mod/sub 2>actual.err &&
431 test_file_not_empty actual.err &&
432 test_path_exists sub &&
433 git diff-files --quiet -- sub &&
434 git add .gitmodules &&
435 git mv sub mod/sub 2>actual.err &&
436 test_must_be_empty actual.err &&
437 test_path_is_missing sub &&
438 test "$entry" = "$(index_at_path mod/sub)" &&
439 git -C mod/sub status &&
440 git update-index --refresh &&
441 git diff-files --quiet
442 '
443
444 test_expect_success 'mv issues a warning when section is not found in .gitmodules' '
445 rm -rf mod &&
446 git reset --hard &&
447 git submodule update &&
448 git config -f .gitmodules --remove-section submodule.sub &&
449 git add .gitmodules &&
450 entry="$(index_at_path sub)" &&
451 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
452 mkdir mod &&
453 git mv sub mod/sub 2>actual.err &&
454 test_cmp expect.err actual.err &&
455 test_path_is_missing sub &&
456 test "$entry" = "$(index_at_path mod/sub)" &&
457 git -C mod/sub status &&
458 git update-index --refresh &&
459 git diff-files --quiet
460 '
461
462 test_expect_success 'mv --dry-run does not touch the submodule or .gitmodules' '
463 rm -rf mod &&
464 git reset --hard &&
465 git submodule update &&
466 mkdir mod &&
467 git mv -n sub mod/sub 2>actual.err &&
468 test_path_is_file sub/.git &&
469 git diff-index --exit-code HEAD &&
470 git update-index --refresh &&
471 git diff-files --quiet -- sub .gitmodules
472 '
473
474 test_expect_success 'checking out a commit before submodule moved needs manual updates' '
475 git mv sub sub2 &&
476 git commit -m "moved sub to sub2" &&
477 git checkout -q HEAD^ 2>actual &&
478 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
479 git status -s sub2 >actual &&
480 echo "?? sub2/" >expected &&
481 test_cmp expected actual &&
482 test_path_is_missing sub/.git &&
483 test_path_is_file sub2/.git &&
484 git submodule update &&
485 test_path_is_file sub/.git &&
486 rm -rf sub2 &&
487 git diff-index --exit-code HEAD &&
488 git update-index --refresh &&
489 git diff-files --quiet -- sub .gitmodules &&
490 git status -s sub2 >actual &&
491 test_must_be_empty actual
492 '
493
494 test_expect_success 'mv -k does not accidentally destroy submodules' '
495 git checkout submodule &&
496 mkdir dummy dest &&
497 git mv -k dummy sub dest &&
498 git status --porcelain >actual &&
499 grep "^R sub -> dest/sub" actual &&
500 git reset --hard &&
501 git checkout .
502 '
503
504 test_expect_success 'moving a submodule in nested directories' '
505 (
506 cd deep &&
507 git mv directory ../ &&
508 # git status would fail if the update of linking git dir to
509 # work dir of the submodule failed.
510 git status &&
511 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
512 echo "directory/hierarchy/sub" >../expect
513 ) &&
514 test_cmp expect actual
515 '
516
517 test_expect_success 'moving nested submodules' '
518 test_config_global protocol.file.allow always &&
519 git commit -am "cleanup commit" &&
520 mkdir sub_nested_nested &&
521 (
522 cd sub_nested_nested &&
523 >nested_level2 &&
524 git init &&
525 git add . &&
526 git commit -m "nested level 2"
527 ) &&
528 mkdir sub_nested &&
529 (
530 cd sub_nested &&
531 >nested_level1 &&
532 git init &&
533 git add . &&
534 git commit -m "nested level 1" &&
535 git submodule add ../sub_nested_nested &&
536 git commit -m "add nested level 2"
537 ) &&
538 git submodule add ./sub_nested nested_move &&
539 git commit -m "add nested_move" &&
540 git submodule update --init --recursive &&
541 git mv nested_move sub_nested_moved &&
542 git status
543 '
544
545 test_done