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