3 # Copyright (c) 2006 Carl D. Worth
6 test_description
='Test of git add, including the -- option.'
10 .
"$TEST_DIRECTORY"/lib-unique-files.sh
12 # Test the file mode "$1" of the file "$2" in the index.
13 test_mode_in_index
() {
14 case "$(git ls-files -s "$2")" in
26 test_expect_success
'Test of git add' '
27 touch foo && git add foo
30 test_expect_success
'Test with no pathspecs' '
32 Nothing specified, nothing added.
33 hint: Maybe you wanted to say ${SQ}git add .${SQ}?
34 hint: Disable this message with "git config set advice.addEmptyPathspec false"
37 test_cmp expect actual
40 test_expect_success
'Post-check that foo is in the index' '
41 git ls-files foo | grep foo
44 test_expect_success
'Test that "git add -- -q" works' '
45 touch -- -q && git add -- -q
48 BATCH_CONFIGURATION
='-c core.fsync=loose-object -c core.fsyncmethod=batch'
50 test_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 &&
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
62 test_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 &&
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
75 'git add: Test that executable bit is not used if core.filemode=0' \
76 'git config core.filemode 0 &&
80 test_mode_in_index 100644 xfoo1'
82 test_expect_success
'git add: filemode=0 should not get confused by symlink' '
84 test_ln_s_add foo xfoo1 &&
85 test_mode_in_index 120000 xfoo1
89 'git update-index --add: Test that executable bit is not used...' \
90 'git config core.filemode 0 &&
93 git update-index --add xfoo2 &&
94 test_mode_in_index 100644 xfoo2'
96 test_expect_success
'git add: filemode=0 should not get confused by symlink' '
98 test_ln_s_add foo xfoo2 &&
99 test_mode_in_index 120000 xfoo2
102 test_expect_success \
103 'git update-index --add: Test that executable bit is not used...' \
104 'git config core.filemode 0 &&
105 test_ln_s_add xfoo2 xfoo3 && # runs git update-index --add
106 test_mode_in_index 120000 xfoo3'
108 test_expect_success
'.gitignore test setup' '
109 echo "*.ig" >.gitignore &&
112 >c.if/c.if && >c.if/c.ig &&
113 >d.ig/d.if && >d.ig/d.ig
116 test_expect_success
'.gitignore is honored' '
118 git ls-files >files &&
119 sed -n "/\\.ig/p" <files >actual &&
120 test_must_be_empty actual
123 test_expect_success
'error out when attempting to add ignored ones without -f' '
124 test_must_fail git add a.?? &&
125 git ls-files >files &&
126 sed -n "/\\.ig/p" <files >actual &&
127 test_must_be_empty actual
130 test_expect_success
'error out when attempting to add ignored ones without -f' '
131 test_must_fail git add d.?? &&
132 git ls-files >files &&
133 sed -n "/\\.ig/p" <files >actual &&
134 test_must_be_empty actual
137 test_expect_success
'error out when attempting to add ignored ones but add others' '
139 test_must_fail git add a.?? &&
140 git ls-files >files &&
141 sed -n "/\\.ig/p" <files >actual &&
142 test_must_be_empty actual &&
146 test_expect_success
'add ignored ones with -f' '
148 git ls-files --error-unmatch a.ig
151 test_expect_success
'add ignored ones with -f' '
153 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
156 test_expect_success
'add ignored ones with -f' '
159 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
162 test_expect_success
'.gitignore with subdirectory' '
166 echo "!dir/a.*" >sub/.gitignore &&
170 git ls-files --error-unmatch sub/dir/a.ig &&
176 git ls-files --error-unmatch sub/dir/a.ig
180 touch 1/2/a
1/3/b
1/2/c
181 test_expect_success
'check correct prefix detection' '
183 git add 1/2/a 1/3/b 1/2/c
186 test_expect_success
'git add with filemode=0, symlinks=0, and unmerged entries' '
190 echo "100755 $(git hash-object -w stage$s) $s file" &&
191 echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink" || return 1
192 done | git update-index --index-info &&
193 git config core.filemode 0 &&
194 git config core.symlinks 0 &&
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$"
202 test_expect_success
'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
203 git rm --cached -f file symlink &&
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" &&
208 echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
209 ) | git update-index --index-info &&
210 git config core.filemode 0 &&
211 git config core.symlinks 0 &&
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$"
219 test_expect_success
'git add --refresh' '
220 >foo && git add foo && git commit -a -m "commit all" &&
221 test -z "$(git diff-index HEAD -- foo)" &&
222 git read-tree HEAD &&
223 case "$(git diff-index HEAD -- foo)" in
224 :100644" "*"M foo") echo pass;;
225 *) echo fail; false;;
227 git add --refresh -- foo &&
228 test -z "$(git diff-index HEAD -- foo)"
231 test_expect_success
'git add --refresh with pathspec' '
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 &&
236 test-tool chmtime -60 bar baz &&
237 git add --refresh bar >actual &&
238 test_must_be_empty actual &&
240 git diff-files --name-only >actual &&
245 test_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
251 test_expect_success POSIXPERM
,SANITY
'git add should fail atomically upon an unreadable file' '
256 test_must_fail git add --verbose . &&
257 ! ( git ls-files foo1 | grep foo1 )
262 test_expect_success POSIXPERM
,SANITY
'git add --ignore-errors' '
267 test_must_fail git add --verbose --ignore-errors . &&
268 git ls-files foo1 | grep foo1
273 test_expect_success POSIXPERM
,SANITY
'git add (add.ignore-errors)' '
274 git config add.ignore-errors 1 &&
279 test_must_fail git add --verbose . &&
280 git ls-files foo1 | grep foo1
284 test_expect_success POSIXPERM
,SANITY
'git add (add.ignore-errors = false)' '
285 git config add.ignore-errors 0 &&
290 test_must_fail git add --verbose . &&
291 ! ( git ls-files foo1 | grep foo1 )
295 test_expect_success POSIXPERM
,SANITY
'--no-ignore-errors overrides config' '
296 git config add.ignore-errors 1 &&
301 test_must_fail git add --verbose --no-ignore-errors . &&
302 ! ( git ls-files foo1 | grep foo1 ) &&
303 git config add.ignore-errors 0
307 test_expect_success BSLASHPSPEC
"git add 'fo\\[ou\\]bar' ignores foobar" '
309 touch fo\[ou\]bar foobar &&
310 git add '\''fo\[ou\]bar'\'' &&
311 git ls-files fo\[ou\]bar | grep -F fo\[ou\]bar &&
312 ! ( git ls-files foobar | grep foobar )
315 test_expect_success
'git add to resolve conflicts on otherwise ignored path' '
317 H=$(git rev-parse :1/2/a) &&
319 echo "100644 $H 1 track-this" &&
320 echo "100644 $H 3 track-this"
321 ) | git update-index --index-info &&
322 echo track-this >>.gitignore &&
323 echo resolved >track-this &&
327 test_expect_success
'"add non-existent" should fail' '
328 test_must_fail git add non-existent &&
329 ! (git ls-files | grep "non-existent")
332 test_expect_success
'git add -A on empty repo does not error out' '
342 test_expect_success
'"git add ." in empty repo' '
351 test_expect_success
'"git add" a embedded repository' '
352 rm -fr outer && git init outer &&
359 git -C $name commit --allow-empty -m $name ||
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:
370 hint: git submodule add <url> inner1
372 hint: If you added this path by mistake, you can remove it from the
375 hint: git rm --cached inner1
377 hint: See "git help submodule" for more information.
378 hint: Disable this message with "git config set advice.addEmbeddedRepo false"
379 warning: adding embedded git repository: inner2
381 test_cmp expect actual
385 test_expect_success
'error on a repository with no commits' '
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
393 test_cmp expect actual
396 test_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
402 test_expect_success
'git add --dry-run of non-existing file' "
403 echo ignored-file >>.gitignore &&
404 test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
407 test_expect_success
'git add --dry-run of an existing file output' "
408 echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
409 test_cmp expect actual
412 cat >expect.err
<<\EOF
413 The following paths are ignored by one of your .gitignore files
:
415 hint
: Use
-f if you really want to add them.
416 hint
: Disable this message with
"git config set advice.addIgnoredFile false"
418 cat >expect.out
<<\EOF
422 test_expect_success
'git add --dry-run --ignore-missing of non-existing file' '
423 test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
426 test_expect_success
'git add --dry-run --ignore-missing of non-existing file output' '
427 test_cmp expect.out actual.out &&
428 test_cmp expect.err actual.err
431 test_expect_success
'git add --dry-run --interactive should fail' '
432 test_must_fail git add --dry-run --interactive
435 test_expect_success
'git add empty string should fail' '
436 test_must_fail git add ""
439 test_expect_success
'git add --chmod=[+-]x stages correctly' '
442 git add --chmod=+x foo1 &&
443 test_mode_in_index 100755 foo1 &&
444 git add --chmod=-x foo1 &&
445 test_mode_in_index 100644 foo1
448 test_expect_success POSIXPERM
,SYMLINKS
'git add --chmod=+x with symlinks' '
449 git config core.filemode 1 &&
450 git config core.symlinks 1 &&
453 git add --chmod=+x foo2 &&
454 test_mode_in_index 100755 foo2
457 test_expect_success
'git add --chmod=[+-]x changes index with already added file' '
462 git add --chmod=+x foo3 &&
463 test_mode_in_index 100755 foo3 &&
467 git add --chmod=-x xfoo3 &&
468 test_mode_in_index 100644 xfoo3
471 test_expect_success POSIXPERM
'git add --chmod=[+-]x does not change the working tree' '
474 git add --chmod=+x foo4 &&
478 test_expect_success
'git add --chmod fails with non regular files (but updates the other paths)' '
480 test_ln_s_add foo foo3 &&
482 test_must_fail git add --chmod=+x foo3 foo4 2>stderr &&
483 test_grep "cannot chmod +x .foo3." stderr &&
484 test_mode_in_index 120000 foo3 &&
485 test_mode_in_index 100755 foo4
488 test_expect_success
'git add --chmod honors --dry-run' '
492 git add --chmod=+x --dry-run foo4 &&
493 test_mode_in_index 100644 foo4
496 test_expect_success
'git add --chmod --dry-run reports error for non regular files' '
498 test_ln_s_add foo foo4 &&
499 test_must_fail git add --chmod=+x --dry-run foo4 2>stderr &&
500 test_grep "cannot chmod +x .foo4." stderr
503 test_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 &&
505 test_grep "pathspec .nonexistent. did not match any files" stderr
508 test_expect_success
'no file status change if no pathspec is given' '
512 git add --chmod=+x &&
513 test_mode_in_index 100644 foo5 &&
514 test_mode_in_index 100644 foo6
517 test_expect_success
'no file status change if no pathspec is given in subdir' '
524 git add --chmod=+x &&
525 test_mode_in_index 100644 sub-foo1 &&
526 test_mode_in_index 100644 sub-foo2
530 test_expect_success
'all statuses changed in folder if . is given' '
535 touch x y z sub/a sub/dir/b &&
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
544 test_expect_success CASE_INSENSITIVE_FS
'path is case-insensitive' '
545 path="$(pwd)/BLUB" &&
547 downcased="$(echo "$path" | tr A-Z a-z)" &&