]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3700-add.sh
Start the 2.46 cycle
[thirdparty/git.git] / t / t3700-add.sh
CommitLineData
60ace879
CW
1#!/bin/sh
2#
3# Copyright (c) 2006 Carl D. Worth
4#
5
5be60078 6test_description='Test of git add, including the -- option.'
60ace879 7
eab4ac6a 8TEST_PASSES_SANITIZE_LEAK=true
60ace879
CW
9. ./test-lib.sh
10
386e7a9d 11. "$TEST_DIRECTORY"/lib-unique-files.sh
d42bab44 12
766cdc41
IB
13# Test the file mode "$1" of the file "$2" in the index.
14test_mode_in_index () {
15 case "$(git ls-files -s "$2")" in
16 "$1 "*" $2")
17 echo pass
18 ;;
19 *)
20 echo fail
21 git ls-files -s "$2"
22 return 1
23 ;;
24 esac
25}
26
0aa0266c
JC
27test_expect_success 'Test of git add' '
28 touch foo && git add foo
29'
60ace879 30
1028db00
RJ
31test_expect_success 'Test with no pathspecs' '
32 cat >expect <<-EOF &&
33 Nothing specified, nothing added.
34 hint: Maybe you wanted to say ${SQ}git add .${SQ}?
35 hint: Disable this message with "git config advice.addEmptyPathspec false"
36 EOF
37 git add 2>actual &&
38 test_cmp expect actual
39'
40
0aa0266c
JC
41test_expect_success 'Post-check that foo is in the index' '
42 git ls-files foo | grep foo
43'
60ace879 44
0aa0266c
JC
45test_expect_success 'Test that "git add -- -q" works' '
46 touch -- -q && git add -- -q
47'
60ace879 48
d42bab44
NS
49BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch'
50
51test_expect_success 'git add: core.fsyncmethod=batch' "
52 test_create_unique_files 2 4 files_base_dir1 &&
53 GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION add -- ./files_base_dir1/ &&
54 git ls-files --stage files_base_dir1/ |
55 test_parse_ls_files_stage_oids >added_files_oids &&
56
57 # We created 2 subdirs with 4 files each (8 files total) above
58 test_line_count = 8 added_files_oids &&
59 git cat-file --batch-check='%(objectname)' <added_files_oids >added_files_actual &&
60 test_cmp added_files_oids added_files_actual
61"
62
63test_expect_success 'git update-index: core.fsyncmethod=batch' "
64 test_create_unique_files 2 4 files_base_dir2 &&
65 find files_base_dir2 ! -type d -print | xargs git $BATCH_CONFIGURATION update-index --add -- &&
66 git ls-files --stage files_base_dir2 |
67 test_parse_ls_files_stage_oids >added_files2_oids &&
68
69 # We created 2 subdirs with 4 files each (8 files total) above
70 test_line_count = 8 added_files2_oids &&
71 git cat-file --batch-check='%(objectname)' <added_files2_oids >added_files2_actual &&
72 test_cmp added_files2_oids added_files2_actual
73"
74
fd28b34a 75test_expect_success \
5be60078 76 'git add: Test that executable bit is not used if core.filemode=0' \
e0d10e1c 77 'git config core.filemode 0 &&
fd28b34a
SP
78 echo foo >xfoo1 &&
79 chmod 755 xfoo1 &&
5be60078 80 git add xfoo1 &&
766cdc41 81 test_mode_in_index 100644 xfoo1'
fd28b34a 82
889c6f0e 83test_expect_success 'git add: filemode=0 should not get confused by symlink' '
185c975f 84 rm -f xfoo1 &&
889c6f0e 85 test_ln_s_add foo xfoo1 &&
766cdc41 86 test_mode_in_index 120000 xfoo1
185c975f
JH
87'
88
fd28b34a 89test_expect_success \
5be60078 90 'git update-index --add: Test that executable bit is not used...' \
e0d10e1c 91 'git config core.filemode 0 &&
fd28b34a
SP
92 echo foo >xfoo2 &&
93 chmod 755 xfoo2 &&
5be60078 94 git update-index --add xfoo2 &&
766cdc41 95 test_mode_in_index 100644 xfoo2'
2bbaaed9 96
889c6f0e 97test_expect_success 'git add: filemode=0 should not get confused by symlink' '
185c975f 98 rm -f xfoo2 &&
889c6f0e 99 test_ln_s_add foo xfoo2 &&
766cdc41 100 test_mode_in_index 120000 xfoo2
185c975f
JH
101'
102
889c6f0e 103test_expect_success \
5be60078 104 'git update-index --add: Test that executable bit is not used...' \
e0d10e1c 105 'git config core.filemode 0 &&
889c6f0e 106 test_ln_s_add xfoo2 xfoo3 && # runs git update-index --add
766cdc41 107 test_mode_in_index 120000 xfoo3'
fd28b34a 108
4d06f8ac
JH
109test_expect_success '.gitignore test setup' '
110 echo "*.ig" >.gitignore &&
111 mkdir c.if d.ig &&
112 >a.ig && >b.if &&
113 >c.if/c.if && >c.if/c.ig &&
114 >d.ig/d.if && >d.ig/d.ig
115'
116
117test_expect_success '.gitignore is honored' '
5be60078 118 git add . &&
c7e03b4e
ÆAB
119 git ls-files >files &&
120 sed -n "/\\.ig/p" <files >actual &&
121 test_must_be_empty actual
4d06f8ac
JH
122'
123
124test_expect_success 'error out when attempting to add ignored ones without -f' '
d492b31c 125 test_must_fail git add a.?? &&
c7e03b4e
ÆAB
126 git ls-files >files &&
127 sed -n "/\\.ig/p" <files >actual &&
128 test_must_be_empty actual
4d06f8ac
JH
129'
130
131test_expect_success 'error out when attempting to add ignored ones without -f' '
d492b31c 132 test_must_fail git add d.?? &&
c7e03b4e
ÆAB
133 git ls-files >files &&
134 sed -n "/\\.ig/p" <files >actual &&
135 test_must_be_empty actual
4d06f8ac
JH
136'
137
1d31e5a2
MG
138test_expect_success 'error out when attempting to add ignored ones but add others' '
139 touch a.if &&
140 test_must_fail git add a.?? &&
c7e03b4e
ÆAB
141 git ls-files >files &&
142 sed -n "/\\.ig/p" <files >actual &&
143 test_must_be_empty actual &&
144 grep a.if files
1d31e5a2
MG
145'
146
4d06f8ac 147test_expect_success 'add ignored ones with -f' '
5be60078
JH
148 git add -f a.?? &&
149 git ls-files --error-unmatch a.ig
4d06f8ac
JH
150'
151
152test_expect_success 'add ignored ones with -f' '
5be60078
JH
153 git add -f d.??/* &&
154 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
4d06f8ac
JH
155'
156
41a7aa58
JH
157test_expect_success 'add ignored ones with -f' '
158 rm -f .git/index &&
159 git add -f d.?? &&
160 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
161'
162
163test_expect_success '.gitignore with subdirectory' '
164
165 rm -f .git/index &&
166 mkdir -p sub/dir &&
167 echo "!dir/a.*" >sub/.gitignore &&
168 >sub/a.ig &&
169 >sub/dir/a.ig &&
170 git add sub/dir &&
171 git ls-files --error-unmatch sub/dir/a.ig &&
172 rm -f .git/index &&
173 (
174 cd sub/dir &&
175 git add .
176 ) &&
177 git ls-files --error-unmatch sub/dir/a.ig
178'
179
c7f34c18
JS
180mkdir 1 1/2 1/3
181touch 1/2/a 1/3/b 1/2/c
182test_expect_success 'check correct prefix detection' '
41a7aa58 183 rm -f .git/index &&
c7f34c18
JS
184 git add 1/2/a 1/3/b 1/2/c
185'
186
05dcd698 187test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries' '
20314271
JS
188 for s in 1 2 3
189 do
74d2f569
ES
190 echo $s > stage$s &&
191 echo "100755 $(git hash-object -w stage$s) $s file" &&
db5875aa 192 echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink" || return 1
20314271
JS
193 done | git update-index --index-info &&
194 git config core.filemode 0 &&
05dcd698 195 git config core.symlinks 0 &&
20314271 196 echo new > file &&
05dcd698
JS
197 echo new > symlink &&
198 git add file symlink &&
199 git ls-files --stage | grep "^100755 .* 0 file$" &&
200 git ls-files --stage | grep "^120000 .* 0 symlink$"
20314271
JS
201'
202
05dcd698
JS
203test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
204 git rm --cached -f file symlink &&
20314271 205 (
3ea67379
ES
206 echo "100644 $(git hash-object -w stage1) 1 file" &&
207 echo "100755 $(git hash-object -w stage2) 2 file" &&
208 echo "100644 $(printf 1 | git hash-object -w -t blob --stdin) 1 symlink" &&
a697ec69 209 echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
20314271
JS
210 ) | git update-index --index-info &&
211 git config core.filemode 0 &&
05dcd698 212 git config core.symlinks 0 &&
20314271 213 echo new > file &&
05dcd698
JS
214 echo new > symlink &&
215 git add file symlink &&
216 git ls-files --stage | grep "^100755 .* 0 file$" &&
217 git ls-files --stage | grep "^120000 .* 0 symlink$"
20314271
JS
218'
219
d616813d
AJ
220test_expect_success 'git add --refresh' '
221 >foo && git add foo && git commit -a -m "commit all" &&
ed6c2314 222 test -z "$(git diff-index HEAD -- foo)" &&
d616813d 223 git read-tree HEAD &&
ed6c2314 224 case "$(git diff-index HEAD -- foo)" in
335f8787 225 :100644" "*"M foo") echo pass;;
77b1d9f3 226 *) echo fail; false;;
d616813d
AJ
227 esac &&
228 git add --refresh -- foo &&
ed6c2314 229 test -z "$(git diff-index HEAD -- foo)"
d616813d
AJ
230'
231
3d1f148c
JH
232test_expect_success 'git add --refresh with pathspec' '
233 git reset --hard &&
234 echo >foo && echo >bar && echo >baz &&
235 git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
236 echo "100644 $H 3 foo" | git update-index --index-info &&
0e496492 237 test-tool chmtime -60 bar baz &&
3d1f148c 238 git add --refresh bar >actual &&
d3c6751b 239 test_must_be_empty actual &&
3d1f148c
JH
240
241 git diff-files --name-only >actual &&
64d1022e 242 ! grep bar actual &&
3d1f148c
JH
243 grep baz actual
244'
245
4e956983
MT
246test_expect_success 'git add --refresh correctly reports no match error' "
247 echo \"fatal: pathspec ':(icase)nonexistent' did not match any files\" >expect &&
248 test_must_fail git add --refresh ':(icase)nonexistent' 2>actual &&
249 test_cmp expect actual
250"
251
c91cfd19 252test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unreadable file' '
89597436
AR
253 git reset --hard &&
254 date >foo1 &&
255 date >foo2 &&
256 chmod 0 foo2 &&
257 test_must_fail git add --verbose . &&
258 ! ( git ls-files foo1 | grep foo1 )
259'
260
261rm -f foo2
262
c91cfd19 263test_expect_success POSIXPERM,SANITY 'git add --ignore-errors' '
89597436
AR
264 git reset --hard &&
265 date >foo1 &&
266 date >foo2 &&
267 chmod 0 foo2 &&
268 test_must_fail git add --verbose --ignore-errors . &&
269 git ls-files foo1 | grep foo1
270'
271
272rm -f foo2
273
c91cfd19 274test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors)' '
dad25e4a
AR
275 git config add.ignore-errors 1 &&
276 git reset --hard &&
277 date >foo1 &&
278 date >foo2 &&
279 chmod 0 foo2 &&
280 test_must_fail git add --verbose . &&
281 git ls-files foo1 | grep foo1
282'
283rm -f foo2
284
c91cfd19 285test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' '
dad25e4a
AR
286 git config add.ignore-errors 0 &&
287 git reset --hard &&
288 date >foo1 &&
289 date >foo2 &&
290 chmod 0 foo2 &&
291 test_must_fail git add --verbose . &&
292 ! ( git ls-files foo1 | grep foo1 )
293'
ed342fde
SB
294rm -f foo2
295
c91cfd19 296test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' '
0aa0266c
JC
297 git config add.ignore-errors 1 &&
298 git reset --hard &&
299 date >foo1 &&
300 date >foo2 &&
301 chmod 0 foo2 &&
302 test_must_fail git add --verbose --no-ignore-errors . &&
303 ! ( git ls-files foo1 | grep foo1 ) &&
304 git config add.ignore-errors 0
ed342fde
SB
305'
306rm -f foo2
dad25e4a 307
6fd1106a 308test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
ea335b56 309 git reset --hard &&
8134a003
AR
310 touch fo\[ou\]bar foobar &&
311 git add '\''fo\[ou\]bar'\'' &&
37eb90f7 312 git ls-files fo\[ou\]bar | grep -F fo\[ou\]bar &&
ea335b56
KB
313 ! ( git ls-files foobar | grep foobar )
314'
315
6e4f981f
JK
316test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
317 git reset --hard &&
318 H=$(git rev-parse :1/2/a) &&
319 (
3ea67379 320 echo "100644 $H 1 track-this" &&
6e4f981f
JK
321 echo "100644 $H 3 track-this"
322 ) | git update-index --index-info &&
323 echo track-this >>.gitignore &&
324 echo resolved >track-this &&
325 git add track-this
326'
327
1e7ef746
CP
328test_expect_success '"add non-existent" should fail' '
329 test_must_fail git add non-existent &&
330 ! (git ls-files | grep "non-existent")
331'
332
64ed07ce
NTND
333test_expect_success 'git add -A on empty repo does not error out' '
334 rm -fr empty &&
335 git init empty &&
336 (
337 cd empty &&
338 git add -A . &&
339 git add -A
340 )
341'
342
343test_expect_success '"git add ." in empty repo' '
344 rm -fr empty &&
345 git init empty &&
346 (
347 cd empty &&
348 git add .
349 )
350'
351
6412d015
RJ
352test_expect_success '"git add" a embedded repository' '
353 rm -fr outer && git init outer &&
354 (
355 cd outer &&
356 for i in 1 2
357 do
358 name=inner$i &&
359 git init $name &&
360 git -C $name commit --allow-empty -m $name ||
361 return 1
362 done &&
363 git add . 2>actual &&
364 cat >expect <<-EOF &&
365 warning: adding embedded git repository: inner1
366 hint: You${SQ}ve added another git repository inside your current repository.
367 hint: Clones of the outer repository will not contain the contents of
368 hint: the embedded repository and will not know how to obtain it.
369 hint: If you meant to add a submodule, use:
370 hint:
371 hint: git submodule add <url> inner1
372 hint:
373 hint: If you added this path by mistake, you can remove it from the
374 hint: index with:
375 hint:
376 hint: git rm --cached inner1
377 hint:
378 hint: See "git help submodule" for more information.
379 hint: Disable this message with "git config advice.addEmbeddedRepo false"
380 warning: adding embedded git repository: inner2
381 EOF
382 test_cmp expect actual
383 )
384'
385
f937bc2f
KM
386test_expect_success 'error on a repository with no commits' '
387 rm -fr empty &&
388 git init empty &&
389 test_must_fail git add empty >actual 2>&1 &&
390 cat >expect <<-EOF &&
391 error: '"'empty/'"' does not have a commit checked out
392 fatal: adding files failed
393 EOF
1108cea7 394 test_cmp expect actual
f937bc2f
KM
395'
396
108da0db
JL
397test_expect_success 'git add --dry-run of existing changed file' "
398 echo new >>track-this &&
399 git add --dry-run track-this >actual 2>&1 &&
400 echo \"add 'track-this'\" | test_cmp - actual
401"
402
403test_expect_success 'git add --dry-run of non-existing file' "
404 echo ignored-file >>.gitignore &&
48168851
ÆAB
405 test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
406"
407
68b2a005 408test_expect_success 'git add --dry-run of an existing file output' "
48168851 409 echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
1108cea7 410 test_cmp expect actual
108da0db
JL
411"
412
c1e02b2b 413cat >expect.err <<\EOF
108da0db
JL
414The following paths are ignored by one of your .gitignore files:
415ignored-file
bf66db37 416hint: Use -f if you really want to add them.
9da49bef 417hint: Disable this message with "git config advice.addIgnoredFile false"
c1e02b2b
JS
418EOF
419cat >expect.out <<\EOF
108da0db
JL
420add 'track-this'
421EOF
422
423test_expect_success 'git add --dry-run --ignore-missing of non-existing file' '
439fb829
ÆAB
424 test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
425'
426
68b2a005 427test_expect_success 'git add --dry-run --ignore-missing of non-existing file output' '
1108cea7
ÆAB
428 test_cmp expect.out actual.out &&
429 test_cmp expect.err actual.err
108da0db
JL
430'
431
a1989cf7
ØW
432test_expect_success 'git add --dry-run --interactive should fail' '
433 test_must_fail git add --dry-run --interactive
434'
435
9e4e8a64
EX
436test_expect_success 'git add empty string should fail' '
437 test_must_fail git add ""
d426430e
EX
438'
439
b38ab197 440test_expect_success 'git add --chmod=[+-]x stages correctly' '
c0fa44d8 441 rm -f foo1 &&
4e55ed32
ET
442 echo foo >foo1 &&
443 git add --chmod=+x foo1 &&
766cdc41 444 test_mode_in_index 100755 foo1 &&
b38ab197 445 git add --chmod=-x foo1 &&
766cdc41 446 test_mode_in_index 100644 foo1
4e55ed32
ET
447'
448
449test_expect_success POSIXPERM,SYMLINKS 'git add --chmod=+x with symlinks' '
450 git config core.filemode 1 &&
451 git config core.symlinks 1 &&
c0fa44d8 452 rm -f foo2 &&
4e55ed32
ET
453 echo foo >foo2 &&
454 git add --chmod=+x foo2 &&
766cdc41 455 test_mode_in_index 100755 foo2
4e55ed32
ET
456'
457
610d55af 458test_expect_success 'git add --chmod=[+-]x changes index with already added file' '
76e368c3 459 rm -f foo3 xfoo3 &&
57ea241e 460 git reset --hard &&
610d55af
TG
461 echo foo >foo3 &&
462 git add foo3 &&
463 git add --chmod=+x foo3 &&
464 test_mode_in_index 100755 foo3 &&
465 echo foo >xfoo3 &&
466 chmod 755 xfoo3 &&
467 git add xfoo3 &&
468 git add --chmod=-x xfoo3 &&
469 test_mode_in_index 100644 xfoo3
470'
471
40e0dc17 472test_expect_success POSIXPERM 'git add --chmod=[+-]x does not change the working tree' '
610d55af
TG
473 echo foo >foo4 &&
474 git add foo4 &&
475 git add --chmod=+x foo4 &&
40e0dc17 476 ! test -x foo4
610d55af
TG
477'
478
9ebd7fe1
MT
479test_expect_success 'git add --chmod fails with non regular files (but updates the other paths)' '
480 git reset --hard &&
481 test_ln_s_add foo foo3 &&
482 touch foo4 &&
483 test_must_fail git add --chmod=+x foo3 foo4 2>stderr &&
6789275d 484 test_grep "cannot chmod +x .foo3." stderr &&
9ebd7fe1
MT
485 test_mode_in_index 120000 foo3 &&
486 test_mode_in_index 100755 foo4
487'
488
c937d70b
MT
489test_expect_success 'git add --chmod honors --dry-run' '
490 git reset --hard &&
491 echo foo >foo4 &&
492 git add foo4 &&
493 git add --chmod=+x --dry-run foo4 &&
494 test_mode_in_index 100644 foo4
495'
496
497test_expect_success 'git add --chmod --dry-run reports error for non regular files' '
498 git reset --hard &&
499 test_ln_s_add foo foo4 &&
9ebd7fe1 500 test_must_fail git add --chmod=+x --dry-run foo4 2>stderr &&
6789275d 501 test_grep "cannot chmod +x .foo4." stderr
c937d70b
MT
502'
503
504test_expect_success 'git add --chmod --dry-run reports error for unmatched pathspec' '
505 test_must_fail git add --chmod=+x --dry-run nonexistent 2>stderr &&
6789275d 506 test_grep "pathspec .nonexistent. did not match any files" stderr
c937d70b
MT
507'
508
610d55af
TG
509test_expect_success 'no file status change if no pathspec is given' '
510 >foo5 &&
511 >foo6 &&
512 git add foo5 foo6 &&
513 git add --chmod=+x &&
514 test_mode_in_index 100644 foo5 &&
515 test_mode_in_index 100644 foo6
516'
517
518test_expect_success 'no file status change if no pathspec is given in subdir' '
b07ad464 519 mkdir -p sub &&
610d55af
TG
520 (
521 cd sub &&
522 >sub-foo1 &&
523 >sub-foo2 &&
524 git add . &&
525 git add --chmod=+x &&
526 test_mode_in_index 100644 sub-foo1 &&
527 test_mode_in_index 100644 sub-foo2
528 )
529'
530
531test_expect_success 'all statuses changed in folder if . is given' '
9ebd7fe1
MT
532 git init repo &&
533 (
534 cd repo &&
535 mkdir -p sub/dir &&
536 touch x y z sub/a sub/dir/b &&
537 git add -A &&
538 git add --chmod=+x . &&
539 test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
540 git add --chmod=-x . &&
541 test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
542 )
610d55af
TG
543'
544
d8727b36
JS
545test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
546 path="$(pwd)/BLUB" &&
547 touch "$path" &&
548 downcased="$(echo "$path" | tr A-Z a-z)" &&
549 git add "$downcased"
550'
551
60ace879 552test_done