Interested in helping open source friends on HP-UX?
[git/mjg.git] / t / t7001-mv.sh
blob9780196c0ba30a41301fc268559d3e230f297316
1 #!/bin/sh
3 test_description='git mv in subdirs'
4 . ./test-lib.sh
6 test_expect_success \
7 'prepare reference tree' \
8 'mkdir path0 path1 &&
9 cp "$TEST_DIRECTORY"/../COPYING path0/COPYING &&
10 git add path0/COPYING &&
11 git commit -m add -a'
13 test_expect_success \
14 'moving the file out of subdirectory' \
15 'cd path0 && git mv COPYING ../path1/COPYING'
17 # in path0 currently
18 test_expect_success \
19 'commiting the change' \
20 'cd .. && git commit -m move-out -a'
22 test_expect_success \
23 'checking the commit' \
24 'git diff-tree -r -M --name-status HEAD^ HEAD | \
25 grep "^R100..*path0/COPYING..*path1/COPYING"'
27 test_expect_success \
28 'moving the file back into subdirectory' \
29 'cd path0 && git mv ../path1/COPYING COPYING'
31 # in path0 currently
32 test_expect_success \
33 'commiting the change' \
34 'cd .. && git commit -m move-in -a'
36 test_expect_success \
37 'checking the commit' \
38 'git diff-tree -r -M --name-status HEAD^ HEAD | \
39 grep "^R100..*path1/COPYING..*path0/COPYING"'
41 test_expect_success \
42 'checking -k on non-existing file' \
43 'git mv -k idontexist path0'
45 test_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'
52 test_expect_success \
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 &&
58 test ! -f path0/untracked1 &&
59 test ! -f path0/untracked2'
61 test_expect_success \
62 'checking -f on untracked file with existing target' \
63 'touch path0/untracked1 &&
64 test_must_fail git mv -f untracked1 path0 &&
65 test ! -f .git/index.lock &&
66 test -f untracked1 &&
67 test -f path0/untracked1'
69 # clean up the mess in case bad things happen
70 rm -f idontexist untracked1 untracked2 \
71 path0/idontexist path0/untracked1 path0/untracked2 \
72 .git/index.lock
73 rmdir path1
75 test_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'
82 test_expect_success \
83 'clean up' \
84 'git reset --hard'
86 test_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/'
92 test_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/'
99 test_expect_success \
100 'clean up' \
101 'git reset --hard'
103 test_expect_success \
104 'adding another file' \
105 'cp "$TEST_DIRECTORY"/../README path0/README &&
106 git add path0/README &&
107 git commit -m add2 -a'
109 test_expect_success \
110 'moving whole subdirectory' \
111 'git mv path0 path2'
113 test_expect_success \
114 'commiting the change' \
115 'git commit -m dir-move -a'
117 test_expect_success \
118 'checking the commit' \
119 'git diff-tree -r -M --name-status HEAD^ HEAD | \
120 grep "^R100..*path0/COPYING..*path2/COPYING" &&
121 git diff-tree -r -M --name-status HEAD^ HEAD | \
122 grep "^R100..*path0/README..*path2/README"'
124 test_expect_success \
125 'succeed when source is a prefix of destination' \
126 'git mv path2/COPYING path2/COPYING-renamed'
128 test_expect_success \
129 'moving whole subdirectory into subdirectory' \
130 'git mv path2 path1'
132 test_expect_success \
133 'commiting the change' \
134 'git commit -m dir-move -a'
136 test_expect_success \
137 'checking the commit' \
138 'git diff-tree -r -M --name-status HEAD^ HEAD | \
139 grep "^R100..*path2/COPYING..*path1/path2/COPYING" &&
140 git diff-tree -r -M --name-status HEAD^ HEAD | \
141 grep "^R100..*path2/README..*path1/path2/README"'
143 test_expect_success \
144 'do not move directory over existing directory' \
145 'mkdir path0 && mkdir path0/path2 && test_must_fail git mv path2 path0'
147 test_expect_success \
148 'move into "."' \
149 'git mv path1/path2/ .'
151 test_expect_success "Michael Cassar's test case" '
152 rm -fr .git papers partA &&
153 git init &&
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 &&
159 T1=`git write-tree` &&
161 git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
163 T=`git write-tree` &&
164 git ls-tree -r $T | grep partA/outline.txt || {
165 git ls-tree -r $T
166 (exit 1)
170 rm -fr papers partA path?
172 test_expect_success "Sergey Vlasov's test case" '
173 rm -fr .git &&
174 git init &&
175 mkdir ab &&
176 date >ab.c &&
177 date >ab/d &&
178 git add ab.c ab &&
179 git commit -m 'initial' &&
180 git mv ab a
183 test_expect_success 'absolute pathname' '(
185 rm -fr mine &&
186 mkdir mine &&
187 cd mine &&
188 test_create_repo one &&
189 cd one &&
190 mkdir sub &&
191 >sub/file &&
192 git add sub/file &&
194 git mv sub "$(pwd)/in" &&
195 ! test -d sub &&
196 test -d in &&
197 git ls-files --error-unmatch in/file
202 test_expect_success 'absolute pathname outside should fail' '(
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 &&
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
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
233 test_expect_success 'git mv should not change sha1 of moved cache entry' '
235 rm -fr .git &&
236 git init &&
237 echo 1 >dirty &&
238 git add dirty &&
239 entry="$(git ls-files --stage dirty | cut -f 1)" &&
240 git mv dirty dirty2 &&
241 [ "$entry" = "$(git ls-files --stage dirty2 | cut -f 1)" ] &&
242 echo 2 >dirty2 &&
243 git mv dirty2 dirty &&
244 [ "$entry" = "$(git ls-files --stage dirty | cut -f 1)" ]
248 rm -f dirty dirty2
250 test_expect_success 'git mv should overwrite symlink to a file' '
252 rm -fr .git &&
253 git init &&
254 echo 1 >moved &&
255 test_ln_s_add moved symlink &&
256 git add moved &&
257 test_must_fail git mv moved symlink &&
258 git mv -f moved symlink &&
259 ! test -e moved &&
260 test -f symlink &&
261 test "$(cat symlink)" = 1 &&
262 git update-index --refresh &&
263 git diff-files --quiet
267 rm -f moved symlink
269 test_expect_success 'git mv should overwrite file with a symlink' '
271 rm -fr .git &&
272 git init &&
273 echo 1 >moved &&
274 test_ln_s_add moved symlink &&
275 git add moved &&
276 test_must_fail git mv symlink moved &&
277 git mv -f symlink moved &&
278 ! test -e symlink &&
279 git update-index --refresh &&
280 git diff-files --quiet
284 test_expect_success SYMLINKS 'check moved symlink' '
286 test -h moved
289 rm -f moved symlink
291 test_expect_success 'setup submodule' '
292 git commit -m initial &&
293 git reset --hard &&
294 git submodule add ./. sub &&
295 echo content >file &&
296 git add file &&
297 git commit -m "added sub and file" &&
298 git branch submodule
301 test_expect_success 'git mv cannot move a submodule in a file' '
302 test_must_fail git mv sub file
305 test_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 &&
309 cd sub &&
310 rm -f .git &&
311 mkdir .git &&
312 (cd ../.git/modules/sub && tar cf - .) | (cd .git && tar xf -) &&
313 GIT_WORK_TREE=. git config --unset core.worktree
314 ) &&
315 mkdir mod &&
316 git mv sub mod/sub &&
317 ! test -e sub &&
318 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
320 cd mod/sub &&
321 git status
322 ) &&
323 git update-index --refresh &&
324 git diff-files --quiet
327 test_expect_success 'git mv moves a submodule with a .git directory and .gitmodules' '
328 rm -rf mod &&
329 git reset --hard &&
330 git submodule update &&
331 entry="$(git ls-files --stage sub | cut -f 1)" &&
333 cd sub &&
334 rm -f .git &&
335 mkdir .git &&
336 (cd ../.git/modules/sub && tar cf - .) | (cd .git && tar xf -) &&
337 GIT_WORK_TREE=. git config --unset core.worktree
338 ) &&
339 mkdir mod &&
340 git mv sub mod/sub &&
341 ! test -e sub &&
342 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
344 cd mod/sub &&
345 git status
346 ) &&
347 echo mod/sub >expected &&
348 git config -f .gitmodules submodule.sub.path >actual &&
349 test_cmp expected actual &&
350 git update-index --refresh &&
351 git diff-files --quiet
354 test_expect_success 'git mv moves a submodule with gitfile' '
355 rm -rf mod &&
356 git reset --hard &&
357 git submodule update &&
358 entry="$(git ls-files --stage sub | cut -f 1)" &&
359 mkdir mod &&
361 cd mod &&
362 git mv ../sub/ .
363 ) &&
364 ! test -e sub &&
365 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
367 cd mod/sub &&
368 git status
369 ) &&
370 echo mod/sub >expected &&
371 git config -f .gitmodules submodule.sub.path >actual &&
372 test_cmp expected actual &&
373 git update-index --refresh &&
374 git diff-files --quiet
377 test_expect_success 'mv does not complain when no .gitmodules file is found' '
378 rm -rf mod &&
379 git reset --hard &&
380 git submodule update &&
381 git rm .gitmodules &&
382 entry="$(git ls-files --stage sub | cut -f 1)" &&
383 mkdir mod &&
384 git mv sub mod/sub 2>actual.err &&
385 ! test -s actual.err &&
386 ! test -e sub &&
387 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
389 cd mod/sub &&
390 git status
391 ) &&
392 git update-index --refresh &&
393 git diff-files --quiet
396 test_expect_success 'mv will error out on a modified .gitmodules file unless staged' '
397 rm -rf mod &&
398 git reset --hard &&
399 git submodule update &&
400 git config -f .gitmodules foo.bar true &&
401 entry="$(git ls-files --stage sub | cut -f 1)" &&
402 mkdir mod &&
403 test_must_fail git mv sub mod/sub 2>actual.err &&
404 test -s actual.err &&
405 test -e sub &&
406 git diff-files --quiet -- sub &&
407 git add .gitmodules &&
408 git mv sub mod/sub 2>actual.err &&
409 ! test -s actual.err &&
410 ! test -e sub &&
411 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
413 cd mod/sub &&
414 git status
415 ) &&
416 git update-index --refresh &&
417 git diff-files --quiet
420 test_expect_success 'mv issues a warning when section is not found in .gitmodules' '
421 rm -rf mod &&
422 git reset --hard &&
423 git submodule update &&
424 git config -f .gitmodules --remove-section submodule.sub &&
425 git add .gitmodules &&
426 entry="$(git ls-files --stage sub | cut -f 1)" &&
427 echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
428 mkdir mod &&
429 git mv sub mod/sub 2>actual.err &&
430 test_i18ncmp expect.err actual.err &&
431 ! test -e sub &&
432 [ "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" ] &&
434 cd mod/sub &&
435 git status
436 ) &&
437 git update-index --refresh &&
438 git diff-files --quiet
441 test_expect_success 'mv --dry-run does not touch the submodule or .gitmodules' '
442 rm -rf mod &&
443 git reset --hard &&
444 git submodule update &&
445 mkdir mod &&
446 git mv -n sub mod/sub 2>actual.err &&
447 test -f sub/.git &&
448 git diff-index --exit-code HEAD &&
449 git update-index --refresh &&
450 git diff-files --quiet -- sub .gitmodules
453 test_expect_success 'checking out a commit before submodule moved needs manual updates' '
454 git mv sub sub2 &&
455 git commit -m "moved sub to sub2" &&
456 git checkout -q HEAD^ 2>actual &&
457 test_i18ngrep "^warning: unable to rmdir sub2:" actual &&
458 git status -s sub2 >actual &&
459 echo "?? sub2/" >expected &&
460 test_cmp expected actual &&
461 ! test -f sub/.git &&
462 test -f sub2/.git &&
463 git submodule update &&
464 test -f sub/.git &&
465 rm -rf sub2 &&
466 git diff-index --exit-code HEAD &&
467 git update-index --refresh &&
468 git diff-files --quiet -- sub .gitmodules &&
469 git status -s sub2 >actual &&
470 ! test -s actual
473 test_expect_success 'mv -k does not accidentally destroy submodules' '
474 git checkout submodule &&
475 mkdir dummy dest &&
476 git mv -k dummy sub dest &&
477 git status --porcelain >actual &&
478 grep "^R sub -> dest/sub" actual &&
479 git reset --hard &&
480 git checkout .
483 test_done