]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7001-mv.sh
t7001: use '>' rather than 'touch'
[thirdparty/git.git] / t / t7001-mv.sh
1 #!/bin/sh
2
3 test_description='git mv in subdirs'
4 . ./test-lib.sh
5
6 test_expect_success 'prepare reference tree' '
7 mkdir path0 path1 &&
8 cp "$TEST_DIRECTORY"/../COPYING path0/COPYING &&
9 git add path0/COPYING &&
10 git commit -m add -a
11 '
12
13 test_expect_success 'moving the file out of subdirectory' '
14 git -C path0 mv COPYING ../path1/COPYING
15 '
16
17 # in path0 currently
18 test_expect_success 'commiting the change' '
19 git commit -m move-out -a
20 '
21
22 test_expect_success 'checking the commit' '
23 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
24 grep "^R100..*path0/COPYING..*path1/COPYING" actual
25 '
26
27 test_expect_success 'moving the file back into subdirectory' '
28 git -C path0 mv ../path1/COPYING COPYING
29 '
30
31 # in path0 currently
32 test_expect_success 'commiting the change' '
33 git commit -m move-in -a
34 '
35
36 test_expect_success 'checking the commit' '
37 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
38 grep "^R100..*path1/COPYING..*path0/COPYING" actual
39 '
40
41 test_expect_success 'mv --dry-run does not move file' '
42 git mv -n path0/COPYING MOVED &&
43 test -f path0/COPYING &&
44 test ! -f MOVED
45 '
46
47 test_expect_success 'checking -k on non-existing file' '
48 git mv -k idontexist path0
49 '
50
51 test_expect_success 'checking -k on untracked file' '
52 >untracked1 &&
53 git mv -k untracked1 path0 &&
54 test -f untracked1 &&
55 test ! -f path0/untracked1
56 '
57
58 test_expect_success 'checking -k on multiple untracked files' '
59 >untracked2 &&
60 git mv -k untracked1 untracked2 path0 &&
61 test -f untracked1 &&
62 test -f untracked2 &&
63 test ! -f path0/untracked1 &&
64 test ! -f path0/untracked2
65 '
66
67 test_expect_success 'checking -f on untracked file with existing target' '
68 >path0/untracked1 &&
69 test_must_fail git mv -f untracked1 path0 &&
70 test ! -f .git/index.lock &&
71 test -f untracked1 &&
72 test -f path0/untracked1
73 '
74
75 # clean up the mess in case bad things happen
76 rm -f idontexist untracked1 untracked2 \
77 path0/idontexist path0/untracked1 path0/untracked2 \
78 .git/index.lock
79 rmdir path1
80
81 test_expect_success 'moving to absent target with trailing slash' '
82 test_must_fail git mv path0/COPYING no-such-dir/ &&
83 test_must_fail git mv path0/COPYING no-such-dir// &&
84 git mv path0/ no-such-dir/ &&
85 test_path_is_dir no-such-dir
86 '
87
88 test_expect_success 'clean up' '
89 git reset --hard
90 '
91
92 test_expect_success 'moving to existing untracked target with trailing slash' '
93 mkdir path1 &&
94 git mv path0/ path1/ &&
95 test_path_is_dir path1/path0/
96 '
97
98 test_expect_success 'moving to existing tracked target with trailing slash' '
99 mkdir path2 &&
100 >path2/file && git add path2/file &&
101 git mv path1/path0/ path2/ &&
102 test_path_is_dir path2/path0/
103 '
104
105 test_expect_success 'clean up' '
106 git reset --hard
107 '
108
109 test_expect_success 'adding another file' '
110 cp "$TEST_DIRECTORY"/../README.md path0/README &&
111 git add path0/README &&
112 git commit -m add2 -a
113 '
114
115 test_expect_success 'moving whole subdirectory' '
116 git mv path0 path2
117 '
118
119 test_expect_success 'commiting the change' '
120 git commit -m dir-move -a
121 '
122
123 test_expect_success 'checking the commit' '
124 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
125 grep "^R100..*path0/COPYING..*path2/COPYING" actual &&
126 grep "^R100..*path0/README..*path2/README" actual
127 '
128
129 test_expect_success 'succeed when source is a prefix of destination' '
130 git mv path2/COPYING path2/COPYING-renamed
131 '
132
133 test_expect_success 'moving whole subdirectory into subdirectory' '
134 git mv path2 path1
135 '
136
137 test_expect_success 'commiting the change' '
138 git commit -m dir-move -a
139 '
140
141 test_expect_success 'checking the commit' '
142 git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
143 grep "^R100..*path2/COPYING..*path1/path2/COPYING" actual &&
144 grep "^R100..*path2/README..*path1/path2/README" actual
145 '
146
147 test_expect_success 'do not move directory over existing directory' '
148 mkdir path0 && mkdir path0/path2 && test_must_fail git mv path2 path0
149 '
150
151 test_expect_success 'move into "."' '
152 git mv path1/path2/ .
153 '
154
155 test_expect_success "Michael Cassar's test case" '
156 rm -fr .git papers partA &&
157 git init &&
158 mkdir -p papers/unsorted papers/all-papers partA &&
159 echo a >papers/unsorted/Thesis.pdf &&
160 echo b >partA/outline.txt &&
161 echo c >papers/unsorted/_another &&
162 git add papers partA &&
163 T1=$(git write-tree) &&
164
165 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
166
167 T=$(git write-tree) &&
168 git ls-tree -r $T | verbose grep partA/outline.txt
169 '
170
171 rm -fr papers partA path?
172
173 test_expect_success "Sergey Vlasov's test case" '
174 rm -fr .git &&
175 git init &&
176 mkdir ab &&
177 date >ab.c &&
178 date >ab/d &&
179 git add ab.c ab &&
180 git commit -m "initial" &&
181 git mv ab a
182 '
183
184 test_expect_success 'absolute pathname' '
185 (
186 rm -fr mine &&
187 mkdir mine &&
188 cd mine &&
189 test_create_repo one &&
190 cd one &&
191 mkdir sub &&
192 >sub/file &&
193 git add sub/file &&
194
195 git mv sub "$(pwd)/in" &&
196 ! test -d sub &&
197 test -d in &&
198 git ls-files --error-unmatch in/file
199 )
200 '
201
202 test_expect_success 'absolute pathname outside should fail' '
203 (
204 rm -fr mine &&
205 mkdir mine &&
206 cd mine &&
207 out=$(pwd) &&
208 test_create_repo one &&
209 cd one &&
210 mkdir sub &&
211 >sub/file &&
212 git add sub/file &&
213
214 test_must_fail git mv sub "$out/out" &&
215 test -d sub &&
216 ! test -d ../in &&
217 git ls-files --error-unmatch sub/file
218 )
219 '
220
221 test_expect_success 'git mv to move multiple sources into a directory' '
222 rm -fr .git && git init &&
223 mkdir dir other &&
224 >dir/a.txt &&
225 >dir/b.txt &&
226 git add dir/?.txt &&
227 git mv dir/a.txt dir/b.txt other &&
228 git ls-files >actual &&
229 { echo other/a.txt; echo other/b.txt; } >expect &&
230 test_cmp expect actual
231 '
232
233 test_expect_success 'git mv should not change sha1 of moved cache entry' '
234 rm -fr .git &&
235 git init &&
236 echo 1 >dirty &&
237 git add dirty &&
238 entry="$(git ls-files --stage dirty | cut -f 1)" &&
239 git mv dirty dirty2 &&
240 [ "$entry" = "$(git ls-files --stage dirty2 | cut -f 1)" ] &&
241 echo 2 >dirty2 &&
242 git mv dirty2 dirty &&
243 [ "$entry" = "$(git ls-files --stage dirty | cut -f 1)" ]
244 '
245
246 rm -f dirty dirty2
247
248 # NB: This test is about the error message
249 # as well as the failure.
250 test_expect_success 'git mv error on conflicted file' '
251 rm -fr .git &&
252 git init &&
253 >conflict &&
254 test_when_finished "rm -f conflict" &&
255 cfhash=$(git hash-object -w conflict) &&
256 q_to_tab <<-EOF | git update-index --index-info &&
257 0 $cfhash 0Qconflict
258 100644 $cfhash 1Qconflict
259 EOF
260
261 test_must_fail git mv conflict newname 2>actual &&
262 test_i18ngrep "conflicted" actual
263 '
264
265 test_expect_success 'git mv should overwrite symlink to a file' '
266 rm -fr .git &&
267 git init &&
268 echo 1 >moved &&
269 test_ln_s_add moved symlink &&
270 git add moved &&
271 test_must_fail git mv moved symlink &&
272 git mv -f moved symlink &&
273 ! test -e moved &&
274 test -f symlink &&
275 test "$(cat symlink)" = 1 &&
276 git update-index --refresh &&
277 git diff-files --quiet
278 '
279
280 rm -f moved symlink
281
282 test_expect_success 'git mv should overwrite file with a symlink' '
283 rm -fr .git &&
284 git init &&
285 echo 1 >moved &&
286 test_ln_s_add moved symlink &&
287 git add moved &&
288 test_must_fail git mv symlink moved &&
289 git mv -f symlink moved &&
290 ! test -e symlink &&
291 git update-index --refresh &&
292 git diff-files --quiet
293 '
294
295 test_expect_success SYMLINKS 'check moved symlink' '
296 test -h moved
297 '
298
299 rm -f moved symlink
300
301 test_expect_success 'setup submodule' '
302 git commit -m initial &&
303 git reset --hard &&
304 git submodule add ./. sub &&
305 echo content >file &&
306 git add file &&
307 git commit -m "added sub and file" &&
308 mkdir -p deep/directory/hierarchy &&
309 git submodule add ./. deep/directory/hierarchy/sub &&
310 git commit -m "added another submodule" &&
311 git branch submodule
312 '
313
314 test_expect_success 'git mv cannot move a submodule in a file' '
315 test_must_fail git mv sub file
316 '
317
318 test_expect_success 'git mv moves a submodule with a .git directory and no .gitmodules' '
319 entry="$(git ls-files --stage sub | cut -f 1)" &&
320 git rm .gitmodules &&
321 (
322 cd sub &&
323 rm -f .git &&
324 cp -R -P -p ../.git/modules/sub .git &&
325 GIT_WORK_TREE=. git config --unset core.worktree
326 ) &&
327 mkdir mod &&
328 git mv sub mod/sub &&
329 ! test -e sub &&
330 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
331 git -C mod/sub status &&
332 git update-index --refresh &&
333 git diff-files --quiet
334 '
335
336 test_expect_success 'git mv moves a submodule with a .git directory and .gitmodules' '
337 rm -rf mod &&
338 git reset --hard &&
339 git submodule update &&
340 entry="$(git ls-files --stage sub | cut -f 1)" &&
341 (
342 cd sub &&
343 rm -f .git &&
344 cp -R -P -p ../.git/modules/sub .git &&
345 GIT_WORK_TREE=. git config --unset core.worktree
346 ) &&
347 mkdir mod &&
348 git mv sub mod/sub &&
349 ! test -e sub &&
350 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
351 git -C mod/sub status &&
352 echo mod/sub >expected &&
353 git config -f .gitmodules submodule.sub.path >actual &&
354 test_cmp expected actual &&
355 git update-index --refresh &&
356 git diff-files --quiet
357 '
358
359 test_expect_success 'git mv moves a submodule with gitfile' '
360 rm -rf mod &&
361 git reset --hard &&
362 git submodule update &&
363 entry="$(git ls-files --stage sub | cut -f 1)" &&
364 mkdir mod &&
365 git -C mod mv ../sub/ . &&
366 ! test -e sub &&
367 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
368 git -C mod/sub status &&
369 echo mod/sub >expected &&
370 git config -f .gitmodules submodule.sub.path >actual &&
371 test_cmp expected actual &&
372 git update-index --refresh &&
373 git diff-files --quiet
374 '
375
376 test_expect_success 'mv does not complain when no .gitmodules file is found' '
377 rm -rf mod &&
378 git reset --hard &&
379 git submodule update &&
380 git rm .gitmodules &&
381 entry="$(git ls-files --stage sub | cut -f 1)" &&
382 mkdir mod &&
383 git mv sub mod/sub 2>actual.err &&
384 test_must_be_empty actual.err &&
385 ! test -e sub &&
386 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
387 git -C mod/sub status &&
388 git update-index --refresh &&
389 git diff-files --quiet
390 '
391
392 test_expect_success 'mv will error out on a modified .gitmodules file unless staged' '
393 rm -rf mod &&
394 git reset --hard &&
395 git submodule update &&
396 git config -f .gitmodules foo.bar true &&
397 entry="$(git ls-files --stage sub | cut -f 1)" &&
398 mkdir mod &&
399 test_must_fail git mv sub mod/sub 2>actual.err &&
400 test -s actual.err &&
401 test -e sub &&
402 git diff-files --quiet -- sub &&
403 git add .gitmodules &&
404 git mv sub mod/sub 2>actual.err &&
405 test_must_be_empty actual.err &&
406 ! test -e sub &&
407 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
408 git -C mod/sub status &&
409 git update-index --refresh &&
410 git diff-files --quiet
411 '
412
413 test_expect_success 'mv issues a warning when section is not found in .gitmodules' '
414 rm -rf mod &&
415 git reset --hard &&
416 git submodule update &&
417 git config -f .gitmodules --remove-section submodule.sub &&
418 git add .gitmodules &&
419 entry="$(git ls-files --stage sub | cut -f 1)" &&
420 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
421 mkdir mod &&
422 git mv sub mod/sub 2>actual.err &&
423 test_i18ncmp expect.err actual.err &&
424 ! test -e sub &&
425 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
426 git -C mod/sub status &&
427 git update-index --refresh &&
428 git diff-files --quiet
429 '
430
431 test_expect_success 'mv --dry-run does not touch the submodule or .gitmodules' '
432 rm -rf mod &&
433 git reset --hard &&
434 git submodule update &&
435 mkdir mod &&
436 git mv -n sub mod/sub 2>actual.err &&
437 test -f sub/.git &&
438 git diff-index --exit-code HEAD &&
439 git update-index --refresh &&
440 git diff-files --quiet -- sub .gitmodules
441 '
442
443 test_expect_success 'checking out a commit before submodule moved needs manual updates' '
444 git mv sub sub2 &&
445 git commit -m "moved sub to sub2" &&
446 git checkout -q HEAD^ 2>actual &&
447 test_i18ngrep "^warning: unable to rmdir '\''sub2'\'':" actual &&
448 git status -s sub2 >actual &&
449 echo "?? sub2/" >expected &&
450 test_cmp expected actual &&
451 ! test -f sub/.git &&
452 test -f sub2/.git &&
453 git submodule update &&
454 test -f sub/.git &&
455 rm -rf sub2 &&
456 git diff-index --exit-code HEAD &&
457 git update-index --refresh &&
458 git diff-files --quiet -- sub .gitmodules &&
459 git status -s sub2 >actual &&
460 test_must_be_empty actual
461 '
462
463 test_expect_success 'mv -k does not accidentally destroy submodules' '
464 git checkout submodule &&
465 mkdir dummy dest &&
466 git mv -k dummy sub dest &&
467 git status --porcelain >actual &&
468 grep "^R sub -> dest/sub" actual &&
469 git reset --hard &&
470 git checkout .
471 '
472
473 test_expect_success 'moving a submodule in nested directories' '
474 (
475 cd deep &&
476 git mv directory ../ &&
477 # git status would fail if the update of linking git dir to
478 # work dir of the submodule failed.
479 git status &&
480 git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
481 echo "directory/hierarchy/sub" >../expect
482 ) &&
483 test_cmp expect actual
484 '
485
486 test_expect_success 'moving nested submodules' '
487 git commit -am "cleanup commit" &&
488 mkdir sub_nested_nested &&
489 (
490 cd sub_nested_nested &&
491 >nested_level2 &&
492 git init &&
493 git add . &&
494 git commit -m "nested level 2"
495 ) &&
496 mkdir sub_nested &&
497 (
498 cd sub_nested &&
499 >nested_level1 &&
500 git init &&
501 git add . &&
502 git commit -m "nested level 1" &&
503 git submodule add ../sub_nested_nested &&
504 git commit -m "add nested level 2"
505 ) &&
506 git submodule add ./sub_nested nested_move &&
507 git commit -m "add nested_move" &&
508 git submodule update --init --recursive &&
509 git mv nested_move sub_nested_moved &&
510 git status
511 '
512
513 test_done