3 # Copyright (c) 2006 Carl D. Worth
6 test_description
='Test of git add, including the -- option.'
8 TEST_PASSES_SANITIZE_LEAK
=true
11 # Test the file mode "$1" of the file "$2" in the index.
12 test_mode_in_index
() {
13 case "$(git ls-files -s "$2")" in
27 'touch foo && git add foo'
30 'Post-check that foo is in the index' \
31 'git ls-files foo | grep foo'
34 'Test that "git add -- -q" works' \
35 'touch -- -q && git add -- -q'
38 'git add: Test that executable bit is not used if core.filemode=0' \
39 'git config core.filemode 0 &&
43 test_mode_in_index 100644 xfoo1'
45 test_expect_success
'git add: filemode=0 should not get confused by symlink' '
47 test_ln_s_add foo xfoo1 &&
48 test_mode_in_index 120000 xfoo1
52 'git update-index --add: Test that executable bit is not used...' \
53 'git config core.filemode 0 &&
56 git update-index --add xfoo2 &&
57 test_mode_in_index 100644 xfoo2'
59 test_expect_success
'git add: filemode=0 should not get confused by symlink' '
61 test_ln_s_add foo xfoo2 &&
62 test_mode_in_index 120000 xfoo2
66 'git update-index --add: Test that executable bit is not used...' \
67 'git config core.filemode 0 &&
68 test_ln_s_add xfoo2 xfoo3 && # runs git update-index --add
69 test_mode_in_index 120000 xfoo3'
71 test_expect_success
'.gitignore test setup' '
72 echo "*.ig" >.gitignore &&
75 >c.if/c.if && >c.if/c.ig &&
76 >d.ig/d.if && >d.ig/d.ig
79 test_expect_success
'.gitignore is honored' '
81 ! (git ls-files | grep "\\.ig")
84 test_expect_success
'error out when attempting to add ignored ones without -f' '
85 test_must_fail git add a.?? &&
86 ! (git ls-files | grep "\\.ig")
89 test_expect_success
'error out when attempting to add ignored ones without -f' '
90 test_must_fail git add d.?? &&
91 ! (git ls-files | grep "\\.ig")
94 test_expect_success
'error out when attempting to add ignored ones but add others' '
96 test_must_fail git add a.?? &&
97 ! (git ls-files | grep "\\.ig") &&
98 (git ls-files | grep a.if)
101 test_expect_success
'add ignored ones with -f' '
103 git ls-files --error-unmatch a.ig
106 test_expect_success
'add ignored ones with -f' '
108 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
111 test_expect_success
'add ignored ones with -f' '
114 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
117 test_expect_success
'.gitignore with subdirectory' '
121 echo "!dir/a.*" >sub/.gitignore &&
125 git ls-files --error-unmatch sub/dir/a.ig &&
131 git ls-files --error-unmatch sub/dir/a.ig
135 touch 1/2/a
1/3/b
1/2/c
136 test_expect_success
'check correct prefix detection' '
138 git add 1/2/a 1/3/b 1/2/c
141 test_expect_success
'git add with filemode=0, symlinks=0, and unmerged entries' '
145 echo "100755 $(git hash-object -w stage$s) $s file" &&
146 echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink" || return 1
147 done | git update-index --index-info &&
148 git config core.filemode 0 &&
149 git config core.symlinks 0 &&
151 echo new > symlink &&
152 git add file symlink &&
153 git ls-files --stage | grep "^100755 .* 0 file$" &&
154 git ls-files --stage | grep "^120000 .* 0 symlink$"
157 test_expect_success
'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
158 git rm --cached -f file symlink &&
160 echo "100644 $(git hash-object -w stage1) 1 file" &&
161 echo "100755 $(git hash-object -w stage2) 2 file" &&
162 echo "100644 $(printf 1 | git hash-object -w -t blob --stdin) 1 symlink" &&
163 echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
164 ) | git update-index --index-info &&
165 git config core.filemode 0 &&
166 git config core.symlinks 0 &&
168 echo new > symlink &&
169 git add file symlink &&
170 git ls-files --stage | grep "^100755 .* 0 file$" &&
171 git ls-files --stage | grep "^120000 .* 0 symlink$"
174 test_expect_success
'git add --refresh' '
175 >foo && git add foo && git commit -a -m "commit all" &&
176 test -z "$(git diff-index HEAD -- foo)" &&
177 git read-tree HEAD &&
178 case "$(git diff-index HEAD -- foo)" in
179 :100644" "*"M foo") echo pass;;
180 *) echo fail; false;;
182 git add --refresh -- foo &&
183 test -z "$(git diff-index HEAD -- foo)"
186 test_expect_success
'git add --refresh with pathspec' '
188 echo >foo && echo >bar && echo >baz &&
189 git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
190 echo "100644 $H 3 foo" | git update-index --index-info &&
191 test-tool chmtime -60 bar baz &&
192 git add --refresh bar >actual &&
193 test_must_be_empty actual &&
195 git diff-files --name-only >actual &&
200 test_expect_success
'git add --refresh correctly reports no match error' "
201 echo \"fatal: pathspec ':(icase)nonexistent' did not match any files\" >expect &&
202 test_must_fail git add --refresh ':(icase)nonexistent' 2>actual &&
203 test_cmp expect actual
206 test_expect_success POSIXPERM
,SANITY
'git add should fail atomically upon an unreadable file' '
211 test_must_fail git add --verbose . &&
212 ! ( git ls-files foo1 | grep foo1 )
217 test_expect_success POSIXPERM
,SANITY
'git add --ignore-errors' '
222 test_must_fail git add --verbose --ignore-errors . &&
223 git ls-files foo1 | grep foo1
228 test_expect_success POSIXPERM
,SANITY
'git add (add.ignore-errors)' '
229 git config add.ignore-errors 1 &&
234 test_must_fail git add --verbose . &&
235 git ls-files foo1 | grep foo1
239 test_expect_success POSIXPERM
,SANITY
'git add (add.ignore-errors = false)' '
240 git config add.ignore-errors 0 &&
245 test_must_fail git add --verbose . &&
246 ! ( git ls-files foo1 | grep foo1 )
250 test_expect_success POSIXPERM
,SANITY
'--no-ignore-errors overrides config' '
251 git config add.ignore-errors 1 &&
256 test_must_fail git add --verbose --no-ignore-errors . &&
257 ! ( git ls-files foo1 | grep foo1 ) &&
258 git config add.ignore-errors 0
262 test_expect_success BSLASHPSPEC
"git add 'fo\\[ou\\]bar' ignores foobar" '
264 touch fo\[ou\]bar foobar &&
265 git add '\''fo\[ou\]bar'\'' &&
266 git ls-files fo\[ou\]bar | fgrep fo\[ou\]bar &&
267 ! ( git ls-files foobar | grep foobar )
270 test_expect_success
'git add to resolve conflicts on otherwise ignored path' '
272 H=$(git rev-parse :1/2/a) &&
274 echo "100644 $H 1 track-this" &&
275 echo "100644 $H 3 track-this"
276 ) | git update-index --index-info &&
277 echo track-this >>.gitignore &&
278 echo resolved >track-this &&
282 test_expect_success
'"add non-existent" should fail' '
283 test_must_fail git add non-existent &&
284 ! (git ls-files | grep "non-existent")
287 test_expect_success
'git add -A on empty repo does not error out' '
297 test_expect_success
'"git add ." in empty repo' '
306 test_expect_success
'error on a repository with no commits' '
309 test_must_fail git add empty >actual 2>&1 &&
310 cat >expect <<-EOF &&
311 error: '"'empty/'"' does not have a commit checked out
312 fatal: adding files failed
314 test_cmp expect actual
317 test_expect_success
'git add --dry-run of existing changed file' "
318 echo new >>track-this &&
319 git add --dry-run track-this >actual 2>&1 &&
320 echo \"add 'track-this'\" | test_cmp - actual
323 test_expect_success
'git add --dry-run of non-existing file' "
324 echo ignored-file >>.gitignore &&
325 test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
328 test_expect_success
'git add --dry-run of an existing file output' "
329 echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
330 test_cmp expect actual
333 cat >expect.err
<<\EOF
334 The following paths are ignored by one of your .gitignore files
:
336 hint
: Use
-f if you really want to add them.
337 hint
: Turn this message off by running
338 hint
: "git config advice.addIgnoredFile false"
340 cat >expect.out
<<\EOF
344 test_expect_success
'git add --dry-run --ignore-missing of non-existing file' '
345 test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
348 test_expect_success
'git add --dry-run --ignore-missing of non-existing file output' '
349 test_cmp expect.out actual.out &&
350 test_cmp expect.err actual.err
353 test_expect_success
'git add --dry-run --interactive should fail' '
354 test_must_fail git add --dry-run --interactive
357 test_expect_success
'git add empty string should fail' '
358 test_must_fail git add ""
361 test_expect_success
'git add --chmod=[+-]x stages correctly' '
364 git add --chmod=+x foo1 &&
365 test_mode_in_index 100755 foo1 &&
366 git add --chmod=-x foo1 &&
367 test_mode_in_index 100644 foo1
370 test_expect_success POSIXPERM
,SYMLINKS
'git add --chmod=+x with symlinks' '
371 git config core.filemode 1 &&
372 git config core.symlinks 1 &&
375 git add --chmod=+x foo2 &&
376 test_mode_in_index 100755 foo2
379 test_expect_success
'git add --chmod=[+-]x changes index with already added file' '
384 git add --chmod=+x foo3 &&
385 test_mode_in_index 100755 foo3 &&
389 git add --chmod=-x xfoo3 &&
390 test_mode_in_index 100644 xfoo3
393 test_expect_success POSIXPERM
'git add --chmod=[+-]x does not change the working tree' '
396 git add --chmod=+x foo4 &&
400 test_expect_success
'git add --chmod fails with non regular files (but updates the other paths)' '
402 test_ln_s_add foo foo3 &&
404 test_must_fail git add --chmod=+x foo3 foo4 2>stderr &&
405 test_i18ngrep "cannot chmod +x .foo3." stderr &&
406 test_mode_in_index 120000 foo3 &&
407 test_mode_in_index 100755 foo4
410 test_expect_success
'git add --chmod honors --dry-run' '
414 git add --chmod=+x --dry-run foo4 &&
415 test_mode_in_index 100644 foo4
418 test_expect_success
'git add --chmod --dry-run reports error for non regular files' '
420 test_ln_s_add foo foo4 &&
421 test_must_fail git add --chmod=+x --dry-run foo4 2>stderr &&
422 test_i18ngrep "cannot chmod +x .foo4." stderr
425 test_expect_success
'git add --chmod --dry-run reports error for unmatched pathspec' '
426 test_must_fail git add --chmod=+x --dry-run nonexistent 2>stderr &&
427 test_i18ngrep "pathspec .nonexistent. did not match any files" stderr
430 test_expect_success
'no file status change if no pathspec is given' '
434 git add --chmod=+x &&
435 test_mode_in_index 100644 foo5 &&
436 test_mode_in_index 100644 foo6
439 test_expect_success
'no file status change if no pathspec is given in subdir' '
446 git add --chmod=+x &&
447 test_mode_in_index 100644 sub-foo1 &&
448 test_mode_in_index 100644 sub-foo2
452 test_expect_success
'all statuses changed in folder if . is given' '
457 touch x y z sub/a sub/dir/b &&
459 git add --chmod=+x . &&
460 test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
461 git add --chmod=-x . &&
462 test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
466 test_expect_success CASE_INSENSITIVE_FS
'path is case-insensitive' '
467 path="$(pwd)/BLUB" &&
469 downcased="$(echo "$path" | tr A-Z a-z)" &&