3 # Copyright (c) 2006 Carl D. Worth
6 test_description='Test of git add, including the -- option.'
12 'touch foo && git add foo'
15 'Post-check that foo is in the index' \
16 'git ls-files foo | grep foo'
19 'Test that "git add -- -q" works' \
20 'touch -- -q && git add -- -q'
23 'git add: Test that executable bit is not used if core.filemode=0' \
24 'git config core.filemode 0 &&
28 case "`git ls-files --stage xfoo1`" in
29 100644" "*xfoo1) echo pass;;
30 *) echo fail; git ls-files --stage xfoo1; (exit 1);;
33 test_expect_success SYMLINKS 'git add: filemode=0 should not get confused by symlink' '
37 case "`git ls-files --stage xfoo1`" in
38 120000" "*xfoo1) echo pass;;
39 *) echo fail; git ls-files --stage xfoo1; (exit 1);;
44 'git update-index --add: Test that executable bit is not used...' \
45 'git config core.filemode 0 &&
48 git update-index --add xfoo2 &&
49 case "`git ls-files --stage xfoo2`" in
50 100644" "*xfoo2) echo pass;;
51 *) echo fail; git ls-files --stage xfoo2; (exit 1);;
54 test_expect_success SYMLINKS 'git add: filemode=0 should not get confused by symlink' '
57 git update-index --add xfoo2 &&
58 case "`git ls-files --stage xfoo2`" in
59 120000" "*xfoo2) echo pass;;
60 *) echo fail; git ls-files --stage xfoo2; (exit 1);;
64 test_expect_success SYMLINKS \
65 'git update-index --add: Test that executable bit is not used...' \
66 'git config core.filemode 0 &&
68 git update-index --add xfoo3 &&
69 case "`git ls-files --stage xfoo3`" in
70 120000" "*xfoo3) echo pass;;
71 *) echo fail; git ls-files --stage xfoo3; (exit 1);;
74 test_expect_success '.gitignore test setup' '
75 echo "*.ig" >.gitignore &&
78 >c.if/c.if && >c.if/c.ig &&
79 >d.ig/d.if && >d.ig/d.ig
82 test_expect_success '.gitignore is honored' '
84 ! (git ls-files | grep "\\.ig")
87 test_expect_success 'error out when attempting to add ignored ones without -f' '
88 test_must_fail git add a.?? &&
89 ! (git ls-files | grep "\\.ig")
92 test_expect_success 'error out when attempting to add ignored ones without -f' '
93 test_must_fail git add d.?? &&
94 ! (git ls-files | grep "\\.ig")
97 test_expect_success 'add ignored ones with -f' '
99 git ls-files --error-unmatch a.ig
102 test_expect_success 'add ignored ones with -f' '
104 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
107 test_expect_success 'add ignored ones with -f' '
110 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
113 test_expect_success '.gitignore with subdirectory' '
117 echo "!dir/a.*" >sub/.gitignore &&
121 git ls-files --error-unmatch sub/dir/a.ig &&
127 git ls-files --error-unmatch sub/dir/a.ig
131 touch 1/2/a 1/3/b 1/2/c
132 test_expect_success 'check correct prefix detection' '
134 git add 1/2/a 1/3/b 1/2/c
137 test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries' '
141 echo "100755 $(git hash-object -w stage$s) $s file"
142 echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink"
143 done | git update-index --index-info &&
144 git config core.filemode 0 &&
145 git config core.symlinks 0 &&
147 echo new > symlink &&
148 git add file symlink &&
149 git ls-files --stage | grep "^100755 .* 0 file$" &&
150 git ls-files --stage | grep "^120000 .* 0 symlink$"
153 test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
154 git rm --cached -f file symlink &&
156 echo "100644 $(git hash-object -w stage1) 1 file"
157 echo "100755 $(git hash-object -w stage2) 2 file"
158 echo "100644 $(printf 1 | git hash-object -w -t blob --stdin) 1 symlink"
159 echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
160 ) | git update-index --index-info &&
161 git config core.filemode 0 &&
162 git config core.symlinks 0 &&
164 echo new > symlink &&
165 git add file symlink &&
166 git ls-files --stage | grep "^100755 .* 0 file$" &&
167 git ls-files --stage | grep "^120000 .* 0 symlink$"
170 test_expect_success 'git add --refresh' '
171 >foo && git add foo && git commit -a -m "commit all" &&
172 test -z "`git diff-index HEAD -- foo`" &&
173 git read-tree HEAD &&
174 case "`git diff-index HEAD -- foo`" in
175 :100644" "*"M foo") echo pass;;
176 *) echo fail; (exit 1);;
178 git add --refresh -- foo &&
179 test -z "`git diff-index HEAD -- foo`"
182 test_expect_success POSIXPERM 'git add should fail atomically upon an unreadable file' '
187 test_must_fail git add --verbose . &&
188 ! ( git ls-files foo1 | grep foo1 )
193 test_expect_success POSIXPERM 'git add --ignore-errors' '
198 test_must_fail git add --verbose --ignore-errors . &&
199 git ls-files foo1 | grep foo1
204 test_expect_success POSIXPERM 'git add (add.ignore-errors)' '
205 git config add.ignore-errors 1 &&
210 test_must_fail git add --verbose . &&
211 git ls-files foo1 | grep foo1
215 test_expect_success POSIXPERM 'git add (add.ignore-errors = false)' '
216 git config add.ignore-errors 0 &&
221 test_must_fail git add --verbose . &&
222 ! ( git ls-files foo1 | grep foo1 )
226 test_expect_success POSIXPERM '--no-ignore-errors overrides config' '
227 git config add.ignore-errors 1 &&
232 test_must_fail git add --verbose --no-ignore-errors . &&
233 ! ( git ls-files foo1 | grep foo1 ) &&
234 git config add.ignore-errors 0
238 test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
240 touch fo\[ou\]bar foobar &&
241 git add '\''fo\[ou\]bar'\'' &&
242 git ls-files fo\[ou\]bar | fgrep fo\[ou\]bar &&
243 ! ( git ls-files foobar | grep foobar )
246 test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
248 H=$(git rev-parse :1/2/a) &&
250 echo "100644 $H 1 track-this"
251 echo "100644 $H 3 track-this"
252 ) | git update-index --index-info &&
253 echo track-this >>.gitignore &&
254 echo resolved >track-this &&
258 test_expect_success '"add non-existent" should fail' '
259 test_must_fail git add non-existent &&
260 ! (git ls-files | grep "non-existent")
263 test_expect_success 'git add --dry-run of existing changed file' "
264 echo new >>track-this &&
265 git add --dry-run track-this >actual 2>&1 &&
266 echo \"add 'track-this'\" | test_cmp - actual
269 test_expect_success 'git add --dry-run of non-existing file' "
270 echo ignored-file >>.gitignore &&
271 test_must_fail git add --dry-run track-this ignored-file >actual 2>&1 &&
272 echo \"fatal: pathspec 'ignored-file' did not match any files\" | test_cmp - actual
275 cat >expect.err <<\EOF
276 The following paths are ignored by one of your .gitignore files:
278 Use -f if you really want to add them.
279 fatal: no files added
281 cat >expect.out <<\EOF
285 test_expect_success 'git add --dry-run --ignore-missing of non-existing file' '
286 test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err &&
287 test_cmp expect.out actual.out &&
288 test_cmp expect.err actual.err