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