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