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_DIRECTORY"/lib-unique-files.sh
13 # Test the file mode "$1" of the file "$2" in the index.
14 test_mode_in_index
() {
15 case "$(git ls-files -s "$2")" in
27 test_expect_success
'Test of git add' '
28 touch foo && git add foo
31 test_expect_success
'Test with no pathspecs' '
33 Nothing specified, nothing added.
34 hint: Maybe you wanted to say ${SQ}git add .${SQ}?
35 hint: Disable this message with "git config advice.addEmptyPathspec false"
38 test_cmp expect actual
41 test_expect_success
'Post-check that foo is in the index' '
42 git ls-files foo | grep foo
45 test_expect_success
'Test that "git add -- -q" works' '
46 touch -- -q && git add -- -q
49 BATCH_CONFIGURATION
='-c core.fsync=loose-object -c core.fsyncmethod=batch'
51 test_expect_success
'git add: core.fsyncmethod=batch' "
52 test_create_unique_files 2 4 files_base_dir1 &&
53 GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION add -- ./files_base_dir1/ &&
54 git ls-files --stage files_base_dir1/ |
55 test_parse_ls_files_stage_oids >added_files_oids &&
57 # We created 2 subdirs with 4 files each (8 files total) above
58 test_line_count = 8 added_files_oids &&
59 git cat-file --batch-check='%(objectname)' <added_files_oids >added_files_actual &&
60 test_cmp added_files_oids added_files_actual
63 test_expect_success
'git update-index: core.fsyncmethod=batch' "
64 test_create_unique_files 2 4 files_base_dir2 &&
65 find files_base_dir2 ! -type d -print | xargs git $BATCH_CONFIGURATION update-index --add -- &&
66 git ls-files --stage files_base_dir2 |
67 test_parse_ls_files_stage_oids >added_files2_oids &&
69 # We created 2 subdirs with 4 files each (8 files total) above
70 test_line_count = 8 added_files2_oids &&
71 git cat-file --batch-check='%(objectname)' <added_files2_oids >added_files2_actual &&
72 test_cmp added_files2_oids added_files2_actual
76 'git add: Test that executable bit is not used if core.filemode=0' \
77 'git config core.filemode 0 &&
81 test_mode_in_index 100644 xfoo1'
83 test_expect_success
'git add: filemode=0 should not get confused by symlink' '
85 test_ln_s_add foo xfoo1 &&
86 test_mode_in_index 120000 xfoo1
90 'git update-index --add: Test that executable bit is not used...' \
91 'git config core.filemode 0 &&
94 git update-index --add xfoo2 &&
95 test_mode_in_index 100644 xfoo2'
97 test_expect_success
'git add: filemode=0 should not get confused by symlink' '
99 test_ln_s_add foo xfoo2 &&
100 test_mode_in_index 120000 xfoo2
103 test_expect_success \
104 'git update-index --add: Test that executable bit is not used...' \
105 'git config core.filemode 0 &&
106 test_ln_s_add xfoo2 xfoo3 && # runs git update-index --add
107 test_mode_in_index 120000 xfoo3'
109 test_expect_success
'.gitignore test setup' '
110 echo "*.ig" >.gitignore &&
113 >c.if/c.if && >c.if/c.ig &&
114 >d.ig/d.if && >d.ig/d.ig
117 test_expect_success
'.gitignore is honored' '
119 git ls-files >files &&
120 sed -n "/\\.ig/p" <files >actual &&
121 test_must_be_empty actual
124 test_expect_success
'error out when attempting to add ignored ones without -f' '
125 test_must_fail git add a.?? &&
126 git ls-files >files &&
127 sed -n "/\\.ig/p" <files >actual &&
128 test_must_be_empty actual
131 test_expect_success
'error out when attempting to add ignored ones without -f' '
132 test_must_fail git add d.?? &&
133 git ls-files >files &&
134 sed -n "/\\.ig/p" <files >actual &&
135 test_must_be_empty actual
138 test_expect_success
'error out when attempting to add ignored ones but add others' '
140 test_must_fail git add a.?? &&
141 git ls-files >files &&
142 sed -n "/\\.ig/p" <files >actual &&
143 test_must_be_empty actual &&
147 test_expect_success
'add ignored ones with -f' '
149 git ls-files --error-unmatch a.ig
152 test_expect_success
'add ignored ones with -f' '
154 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
157 test_expect_success
'add ignored ones with -f' '
160 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
163 test_expect_success
'.gitignore with subdirectory' '
167 echo "!dir/a.*" >sub/.gitignore &&
171 git ls-files --error-unmatch sub/dir/a.ig &&
177 git ls-files --error-unmatch sub/dir/a.ig
181 touch 1/2/a
1/3/b
1/2/c
182 test_expect_success
'check correct prefix detection' '
184 git add 1/2/a 1/3/b 1/2/c
187 test_expect_success
'git add with filemode=0, symlinks=0, and unmerged entries' '
191 echo "100755 $(git hash-object -w stage$s) $s file" &&
192 echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink" || return 1
193 done | git update-index --index-info &&
194 git config core.filemode 0 &&
195 git config core.symlinks 0 &&
197 echo new > symlink &&
198 git add file symlink &&
199 git ls-files --stage | grep "^100755 .* 0 file$" &&
200 git ls-files --stage | grep "^120000 .* 0 symlink$"
203 test_expect_success
'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
204 git rm --cached -f file symlink &&
206 echo "100644 $(git hash-object -w stage1) 1 file" &&
207 echo "100755 $(git hash-object -w stage2) 2 file" &&
208 echo "100644 $(printf 1 | git hash-object -w -t blob --stdin) 1 symlink" &&
209 echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
210 ) | git update-index --index-info &&
211 git config core.filemode 0 &&
212 git config core.symlinks 0 &&
214 echo new > symlink &&
215 git add file symlink &&
216 git ls-files --stage | grep "^100755 .* 0 file$" &&
217 git ls-files --stage | grep "^120000 .* 0 symlink$"
220 test_expect_success
'git add --refresh' '
221 >foo && git add foo && git commit -a -m "commit all" &&
222 test -z "$(git diff-index HEAD -- foo)" &&
223 git read-tree HEAD &&
224 case "$(git diff-index HEAD -- foo)" in
225 :100644" "*"M foo") echo pass;;
226 *) echo fail; false;;
228 git add --refresh -- foo &&
229 test -z "$(git diff-index HEAD -- foo)"
232 test_expect_success
'git add --refresh with pathspec' '
234 echo >foo && echo >bar && echo >baz &&
235 git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
236 echo "100644 $H 3 foo" | git update-index --index-info &&
237 test-tool chmtime -60 bar baz &&
238 git add --refresh bar >actual &&
239 test_must_be_empty actual &&
241 git diff-files --name-only >actual &&
246 test_expect_success
'git add --refresh correctly reports no match error' "
247 echo \"fatal: pathspec ':(icase)nonexistent' did not match any files\" >expect &&
248 test_must_fail git add --refresh ':(icase)nonexistent' 2>actual &&
249 test_cmp expect actual
252 test_expect_success POSIXPERM
,SANITY
'git add should fail atomically upon an unreadable file' '
257 test_must_fail git add --verbose . &&
258 ! ( git ls-files foo1 | grep foo1 )
263 test_expect_success POSIXPERM
,SANITY
'git add --ignore-errors' '
268 test_must_fail git add --verbose --ignore-errors . &&
269 git ls-files foo1 | grep foo1
274 test_expect_success POSIXPERM
,SANITY
'git add (add.ignore-errors)' '
275 git config add.ignore-errors 1 &&
280 test_must_fail git add --verbose . &&
281 git ls-files foo1 | grep foo1
285 test_expect_success POSIXPERM
,SANITY
'git add (add.ignore-errors = false)' '
286 git config add.ignore-errors 0 &&
291 test_must_fail git add --verbose . &&
292 ! ( git ls-files foo1 | grep foo1 )
296 test_expect_success POSIXPERM
,SANITY
'--no-ignore-errors overrides config' '
297 git config add.ignore-errors 1 &&
302 test_must_fail git add --verbose --no-ignore-errors . &&
303 ! ( git ls-files foo1 | grep foo1 ) &&
304 git config add.ignore-errors 0
308 test_expect_success BSLASHPSPEC
"git add 'fo\\[ou\\]bar' ignores foobar" '
310 touch fo\[ou\]bar foobar &&
311 git add '\''fo\[ou\]bar'\'' &&
312 git ls-files fo\[ou\]bar | grep -F fo\[ou\]bar &&
313 ! ( git ls-files foobar | grep foobar )
316 test_expect_success
'git add to resolve conflicts on otherwise ignored path' '
318 H=$(git rev-parse :1/2/a) &&
320 echo "100644 $H 1 track-this" &&
321 echo "100644 $H 3 track-this"
322 ) | git update-index --index-info &&
323 echo track-this >>.gitignore &&
324 echo resolved >track-this &&
328 test_expect_success
'"add non-existent" should fail' '
329 test_must_fail git add non-existent &&
330 ! (git ls-files | grep "non-existent")
333 test_expect_success
'git add -A on empty repo does not error out' '
343 test_expect_success
'"git add ." in empty repo' '
352 test_expect_success
'"git add" a embedded repository' '
353 rm -fr outer && git init outer &&
360 git -C $name commit --allow-empty -m $name ||
363 git add . 2>actual &&
364 cat >expect <<-EOF &&
365 warning: adding embedded git repository: inner1
366 hint: You${SQ}ve added another git repository inside your current repository.
367 hint: Clones of the outer repository will not contain the contents of
368 hint: the embedded repository and will not know how to obtain it.
369 hint: If you meant to add a submodule, use:
371 hint: git submodule add <url> inner1
373 hint: If you added this path by mistake, you can remove it from the
376 hint: git rm --cached inner1
378 hint: See "git help submodule" for more information.
379 hint: Disable this message with "git config advice.addEmbeddedRepo false"
380 warning: adding embedded git repository: inner2
382 test_cmp expect actual
386 test_expect_success
'error on a repository with no commits' '
389 test_must_fail git add empty >actual 2>&1 &&
390 cat >expect <<-EOF &&
391 error: '"'empty/'"' does not have a commit checked out
392 fatal: adding files failed
394 test_cmp expect actual
397 test_expect_success
'git add --dry-run of existing changed file' "
398 echo new >>track-this &&
399 git add --dry-run track-this >actual 2>&1 &&
400 echo \"add 'track-this'\" | test_cmp - actual
403 test_expect_success
'git add --dry-run of non-existing file' "
404 echo ignored-file >>.gitignore &&
405 test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
408 test_expect_success
'git add --dry-run of an existing file output' "
409 echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
410 test_cmp expect actual
413 cat >expect.err
<<\EOF
414 The following paths are ignored by one of your .gitignore files
:
416 hint
: Use
-f if you really want to add them.
417 hint
: Disable this message with
"git config advice.addIgnoredFile false"
419 cat >expect.out
<<\EOF
423 test_expect_success
'git add --dry-run --ignore-missing of non-existing file' '
424 test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
427 test_expect_success
'git add --dry-run --ignore-missing of non-existing file output' '
428 test_cmp expect.out actual.out &&
429 test_cmp expect.err actual.err
432 test_expect_success
'git add --dry-run --interactive should fail' '
433 test_must_fail git add --dry-run --interactive
436 test_expect_success
'git add empty string should fail' '
437 test_must_fail git add ""
440 test_expect_success
'git add --chmod=[+-]x stages correctly' '
443 git add --chmod=+x foo1 &&
444 test_mode_in_index 100755 foo1 &&
445 git add --chmod=-x foo1 &&
446 test_mode_in_index 100644 foo1
449 test_expect_success POSIXPERM
,SYMLINKS
'git add --chmod=+x with symlinks' '
450 git config core.filemode 1 &&
451 git config core.symlinks 1 &&
454 git add --chmod=+x foo2 &&
455 test_mode_in_index 100755 foo2
458 test_expect_success
'git add --chmod=[+-]x changes index with already added file' '
463 git add --chmod=+x foo3 &&
464 test_mode_in_index 100755 foo3 &&
468 git add --chmod=-x xfoo3 &&
469 test_mode_in_index 100644 xfoo3
472 test_expect_success POSIXPERM
'git add --chmod=[+-]x does not change the working tree' '
475 git add --chmod=+x foo4 &&
479 test_expect_success
'git add --chmod fails with non regular files (but updates the other paths)' '
481 test_ln_s_add foo foo3 &&
483 test_must_fail git add --chmod=+x foo3 foo4 2>stderr &&
484 test_grep "cannot chmod +x .foo3." stderr &&
485 test_mode_in_index 120000 foo3 &&
486 test_mode_in_index 100755 foo4
489 test_expect_success
'git add --chmod honors --dry-run' '
493 git add --chmod=+x --dry-run foo4 &&
494 test_mode_in_index 100644 foo4
497 test_expect_success
'git add --chmod --dry-run reports error for non regular files' '
499 test_ln_s_add foo foo4 &&
500 test_must_fail git add --chmod=+x --dry-run foo4 2>stderr &&
501 test_grep "cannot chmod +x .foo4." stderr
504 test_expect_success
'git add --chmod --dry-run reports error for unmatched pathspec' '
505 test_must_fail git add --chmod=+x --dry-run nonexistent 2>stderr &&
506 test_grep "pathspec .nonexistent. did not match any files" stderr
509 test_expect_success
'no file status change if no pathspec is given' '
513 git add --chmod=+x &&
514 test_mode_in_index 100644 foo5 &&
515 test_mode_in_index 100644 foo6
518 test_expect_success
'no file status change if no pathspec is given in subdir' '
525 git add --chmod=+x &&
526 test_mode_in_index 100644 sub-foo1 &&
527 test_mode_in_index 100644 sub-foo2
531 test_expect_success
'all statuses changed in folder if . is given' '
536 touch x y z sub/a sub/dir/b &&
538 git add --chmod=+x . &&
539 test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
540 git add --chmod=-x . &&
541 test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
545 test_expect_success CASE_INSENSITIVE_FS
'path is case-insensitive' '
546 path="$(pwd)/BLUB" &&
548 downcased="$(echo "$path" | tr A-Z a-z)" &&