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