3 # Copyright (c) 2006 Carl D. Worth
6 test_description
='Test of git add, including the -- option.'
10 # Test the file mode "$1" of the file "$2" in the index.
11 test_mode_in_index
() {
12 case "$(git ls-files -s "$2")" in
26 'touch foo && git add foo'
29 'Post-check that foo is in the index' \
30 'git ls-files foo | grep foo'
33 'Test that "git add -- -q" works' \
34 'touch -- -q && git add -- -q'
37 'git add: Test that executable bit is not used if core.filemode=0' \
38 'git config core.filemode 0 &&
42 test_mode_in_index 100644 xfoo1'
44 test_expect_success
'git add: filemode=0 should not get confused by symlink' '
46 test_ln_s_add foo xfoo1 &&
47 test_mode_in_index 120000 xfoo1
51 'git update-index --add: Test that executable bit is not used...' \
52 'git config core.filemode 0 &&
55 git update-index --add xfoo2 &&
56 test_mode_in_index 100644 xfoo2'
58 test_expect_success
'git add: filemode=0 should not get confused by symlink' '
60 test_ln_s_add foo xfoo2 &&
61 test_mode_in_index 120000 xfoo2
65 'git update-index --add: Test that executable bit is not used...' \
66 'git config core.filemode 0 &&
67 test_ln_s_add xfoo2 xfoo3 && # runs git update-index --add
68 test_mode_in_index 120000 xfoo3'
70 test_expect_success
'.gitignore test setup' '
71 echo "*.ig" >.gitignore &&
74 >c.if/c.if && >c.if/c.ig &&
75 >d.ig/d.if && >d.ig/d.ig
78 test_expect_success
'.gitignore is honored' '
80 ! (git ls-files | grep "\\.ig")
83 test_expect_success
'error out when attempting to add ignored ones without -f' '
84 test_must_fail git add a.?? &&
85 ! (git ls-files | grep "\\.ig")
88 test_expect_success
'error out when attempting to add ignored ones without -f' '
89 test_must_fail git add d.?? &&
90 ! (git ls-files | grep "\\.ig")
93 test_expect_success
'error out when attempting to add ignored ones but add others' '
95 test_must_fail git add a.?? &&
96 ! (git ls-files | grep "\\.ig") &&
97 (git ls-files | grep a.if)
100 test_expect_success
'add ignored ones with -f' '
102 git ls-files --error-unmatch a.ig
105 test_expect_success
'add ignored ones with -f' '
107 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
110 test_expect_success
'add ignored ones with -f' '
113 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
116 test_expect_success
'.gitignore with subdirectory' '
120 echo "!dir/a.*" >sub/.gitignore &&
124 git ls-files --error-unmatch sub/dir/a.ig &&
130 git ls-files --error-unmatch sub/dir/a.ig
134 touch 1/2/a
1/3/b
1/2/c
135 test_expect_success
'check correct prefix detection' '
137 git add 1/2/a 1/3/b 1/2/c
140 test_expect_success
'git add with filemode=0, symlinks=0, and unmerged entries' '
144 echo "100755 $(git hash-object -w stage$s) $s file"
145 echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink"
146 done | git update-index --index-info &&
147 git config core.filemode 0 &&
148 git config core.symlinks 0 &&
150 echo new > symlink &&
151 git add file symlink &&
152 git ls-files --stage | grep "^100755 .* 0 file$" &&
153 git ls-files --stage | grep "^120000 .* 0 symlink$"
156 test_expect_success
'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
157 git rm --cached -f file symlink &&
159 echo "100644 $(git hash-object -w stage1) 1 file"
160 echo "100755 $(git hash-object -w stage2) 2 file"
161 echo "100644 $(printf 1 | git hash-object -w -t blob --stdin) 1 symlink"
162 echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
163 ) | git update-index --index-info &&
164 git config core.filemode 0 &&
165 git config core.symlinks 0 &&
167 echo new > symlink &&
168 git add file symlink &&
169 git ls-files --stage | grep "^100755 .* 0 file$" &&
170 git ls-files --stage | grep "^120000 .* 0 symlink$"
173 test_expect_success
'git add --refresh' '
174 >foo && git add foo && git commit -a -m "commit all" &&
175 test -z "$(git diff-index HEAD -- foo)" &&
176 git read-tree HEAD &&
177 case "$(git diff-index HEAD -- foo)" in
178 :100644" "*"M foo") echo pass;;
179 *) echo fail; (exit 1);;
181 git add --refresh -- foo &&
182 test -z "$(git diff-index HEAD -- foo)"
185 test_expect_success
'git add --refresh with pathspec' '
187 echo >foo && echo >bar && echo >baz &&
188 git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
189 echo "100644 $H 3 foo" | git update-index --index-info &&
190 test-chmtime -60 bar baz &&
192 git add --refresh bar >actual &&
193 test_cmp expect actual &&
195 git diff-files --name-only >actual &&
200 test_expect_success POSIXPERM
,SANITY
'git add should fail atomically upon an unreadable file' '
205 test_must_fail git add --verbose . &&
206 ! ( git ls-files foo1 | grep foo1 )
211 test_expect_success POSIXPERM
,SANITY
'git add --ignore-errors' '
216 test_must_fail git add --verbose --ignore-errors . &&
217 git ls-files foo1 | grep foo1
222 test_expect_success POSIXPERM
,SANITY
'git add (add.ignore-errors)' '
223 git config add.ignore-errors 1 &&
228 test_must_fail git add --verbose . &&
229 git ls-files foo1 | grep foo1
233 test_expect_success POSIXPERM
,SANITY
'git add (add.ignore-errors = false)' '
234 git config add.ignore-errors 0 &&
239 test_must_fail git add --verbose . &&
240 ! ( git ls-files foo1 | grep foo1 )
244 test_expect_success POSIXPERM
,SANITY
'--no-ignore-errors overrides config' '
245 git config add.ignore-errors 1 &&
250 test_must_fail git add --verbose --no-ignore-errors . &&
251 ! ( git ls-files foo1 | grep foo1 ) &&
252 git config add.ignore-errors 0
256 test_expect_success BSLASHPSPEC
"git add 'fo\\[ou\\]bar' ignores foobar" '
258 touch fo\[ou\]bar foobar &&
259 git add '\''fo\[ou\]bar'\'' &&
260 git ls-files fo\[ou\]bar | fgrep fo\[ou\]bar &&
261 ! ( git ls-files foobar | grep foobar )
264 test_expect_success
'git add to resolve conflicts on otherwise ignored path' '
266 H=$(git rev-parse :1/2/a) &&
268 echo "100644 $H 1 track-this"
269 echo "100644 $H 3 track-this"
270 ) | git update-index --index-info &&
271 echo track-this >>.gitignore &&
272 echo resolved >track-this &&
276 test_expect_success
'"add non-existent" should fail' '
277 test_must_fail git add non-existent &&
278 ! (git ls-files | grep "non-existent")
281 test_expect_success
'git add -A on empty repo does not error out' '
291 test_expect_success
'"git add ." in empty repo' '
300 test_expect_success
'git add --dry-run of existing changed file' "
301 echo new >>track-this &&
302 git add --dry-run track-this >actual 2>&1 &&
303 echo \"add 'track-this'\" | test_cmp - actual
306 test_expect_success
'git add --dry-run of non-existing file' "
307 echo ignored-file >>.gitignore &&
308 test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
311 test_expect_success
'git add --dry-run of an existing file output' "
312 echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
313 test_i18ncmp expect actual
316 cat >expect.err
<<\EOF
317 The following paths are ignored by one of your .gitignore files
:
319 Use
-f if you really want to add them.
321 cat >expect.out
<<\EOF
325 test_expect_success
'git add --dry-run --ignore-missing of non-existing file' '
326 test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
329 test_expect_success
'git add --dry-run --ignore-missing of non-existing file output' '
330 test_i18ncmp expect.out actual.out &&
331 test_i18ncmp expect.err actual.err
334 test_expect_success
'git add empty string should fail' '
335 test_must_fail git add ""
338 test_expect_success
'git add --chmod=[+-]x stages correctly' '
341 git add --chmod=+x foo1 &&
342 test_mode_in_index 100755 foo1 &&
343 git add --chmod=-x foo1 &&
344 test_mode_in_index 100644 foo1
347 test_expect_success POSIXPERM
,SYMLINKS
'git add --chmod=+x with symlinks' '
348 git config core.filemode 1 &&
349 git config core.symlinks 1 &&
352 git add --chmod=+x foo2 &&
353 test_mode_in_index 100755 foo2
356 test_expect_success
'git add --chmod=[+-]x changes index with already added file' '
361 git add --chmod=+x foo3 &&
362 test_mode_in_index 100755 foo3 &&
366 git add --chmod=-x xfoo3 &&
367 test_mode_in_index 100644 xfoo3
370 test_expect_success POSIXPERM
'git add --chmod=[+-]x does not change the working tree' '
373 git add --chmod=+x foo4 &&
377 test_expect_success
'no file status change if no pathspec is given' '
381 git add --chmod=+x &&
382 test_mode_in_index 100644 foo5 &&
383 test_mode_in_index 100644 foo6
386 test_expect_success
'no file status change if no pathspec is given in subdir' '
393 git add --chmod=+x &&
394 test_mode_in_index 100644 sub-foo1 &&
395 test_mode_in_index 100644 sub-foo2
399 test_expect_success
'all statuses changed in folder if . is given' '
400 git add --chmod=+x . &&
401 test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
402 git add --chmod=-x . &&
403 test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0