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
'Post-check that foo is in the index' '
32 git ls-files foo | grep foo
35 test_expect_success
'Test that "git add -- -q" works' '
36 touch -- -q && git add -- -q
39 BATCH_CONFIGURATION
='-c core.fsync=loose-object -c core.fsyncmethod=batch'
41 test_expect_success
'git add: core.fsyncmethod=batch' "
42 test_create_unique_files 2 4 files_base_dir1 &&
43 GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION add -- ./files_base_dir1/ &&
44 git ls-files --stage files_base_dir1/ |
45 test_parse_ls_files_stage_oids >added_files_oids &&
47 # We created 2 subdirs with 4 files each (8 files total) above
48 test_line_count = 8 added_files_oids &&
49 git cat-file --batch-check='%(objectname)' <added_files_oids >added_files_actual &&
50 test_cmp added_files_oids added_files_actual
53 test_expect_success
'git update-index: core.fsyncmethod=batch' "
54 test_create_unique_files 2 4 files_base_dir2 &&
55 find files_base_dir2 ! -type d -print | xargs git $BATCH_CONFIGURATION update-index --add -- &&
56 git ls-files --stage files_base_dir2 |
57 test_parse_ls_files_stage_oids >added_files2_oids &&
59 # We created 2 subdirs with 4 files each (8 files total) above
60 test_line_count = 8 added_files2_oids &&
61 git cat-file --batch-check='%(objectname)' <added_files2_oids >added_files2_actual &&
62 test_cmp added_files2_oids added_files2_actual
66 'git add: Test that executable bit is not used if core.filemode=0' \
67 'git config core.filemode 0 &&
71 test_mode_in_index 100644 xfoo1'
73 test_expect_success
'git add: filemode=0 should not get confused by symlink' '
75 test_ln_s_add foo xfoo1 &&
76 test_mode_in_index 120000 xfoo1
80 'git update-index --add: Test that executable bit is not used...' \
81 'git config core.filemode 0 &&
84 git update-index --add xfoo2 &&
85 test_mode_in_index 100644 xfoo2'
87 test_expect_success
'git add: filemode=0 should not get confused by symlink' '
89 test_ln_s_add foo xfoo2 &&
90 test_mode_in_index 120000 xfoo2
94 'git update-index --add: Test that executable bit is not used...' \
95 'git config core.filemode 0 &&
96 test_ln_s_add xfoo2 xfoo3 && # runs git update-index --add
97 test_mode_in_index 120000 xfoo3'
99 test_expect_success
'.gitignore test setup' '
100 echo "*.ig" >.gitignore &&
103 >c.if/c.if && >c.if/c.ig &&
104 >d.ig/d.if && >d.ig/d.ig
107 test_expect_success
'.gitignore is honored' '
109 git ls-files >files &&
110 sed -n "/\\.ig/p" <files >actual &&
111 test_must_be_empty actual
114 test_expect_success
'error out when attempting to add ignored ones without -f' '
115 test_must_fail git add a.?? &&
116 git ls-files >files &&
117 sed -n "/\\.ig/p" <files >actual &&
118 test_must_be_empty actual
121 test_expect_success
'error out when attempting to add ignored ones without -f' '
122 test_must_fail git add d.?? &&
123 git ls-files >files &&
124 sed -n "/\\.ig/p" <files >actual &&
125 test_must_be_empty actual
128 test_expect_success
'error out when attempting to add ignored ones but add others' '
130 test_must_fail git add a.?? &&
131 git ls-files >files &&
132 sed -n "/\\.ig/p" <files >actual &&
133 test_must_be_empty actual &&
137 test_expect_success
'add ignored ones with -f' '
139 git ls-files --error-unmatch a.ig
142 test_expect_success
'add ignored ones with -f' '
144 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
147 test_expect_success
'add ignored ones with -f' '
150 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
153 test_expect_success
'.gitignore with subdirectory' '
157 echo "!dir/a.*" >sub/.gitignore &&
161 git ls-files --error-unmatch sub/dir/a.ig &&
167 git ls-files --error-unmatch sub/dir/a.ig
171 touch 1/2/a
1/3/b
1/2/c
172 test_expect_success
'check correct prefix detection' '
174 git add 1/2/a 1/3/b 1/2/c
177 test_expect_success
'git add with filemode=0, symlinks=0, and unmerged entries' '
181 echo "100755 $(git hash-object -w stage$s) $s file" &&
182 echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink" || return 1
183 done | git update-index --index-info &&
184 git config core.filemode 0 &&
185 git config core.symlinks 0 &&
187 echo new > symlink &&
188 git add file symlink &&
189 git ls-files --stage | grep "^100755 .* 0 file$" &&
190 git ls-files --stage | grep "^120000 .* 0 symlink$"
193 test_expect_success
'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
194 git rm --cached -f file symlink &&
196 echo "100644 $(git hash-object -w stage1) 1 file" &&
197 echo "100755 $(git hash-object -w stage2) 2 file" &&
198 echo "100644 $(printf 1 | git hash-object -w -t blob --stdin) 1 symlink" &&
199 echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
200 ) | git update-index --index-info &&
201 git config core.filemode 0 &&
202 git config core.symlinks 0 &&
204 echo new > symlink &&
205 git add file symlink &&
206 git ls-files --stage | grep "^100755 .* 0 file$" &&
207 git ls-files --stage | grep "^120000 .* 0 symlink$"
210 test_expect_success
'git add --refresh' '
211 >foo && git add foo && git commit -a -m "commit all" &&
212 test -z "$(git diff-index HEAD -- foo)" &&
213 git read-tree HEAD &&
214 case "$(git diff-index HEAD -- foo)" in
215 :100644" "*"M foo") echo pass;;
216 *) echo fail; false;;
218 git add --refresh -- foo &&
219 test -z "$(git diff-index HEAD -- foo)"
222 test_expect_success
'git add --refresh with pathspec' '
224 echo >foo && echo >bar && echo >baz &&
225 git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
226 echo "100644 $H 3 foo" | git update-index --index-info &&
227 test-tool chmtime -60 bar baz &&
228 git add --refresh bar >actual &&
229 test_must_be_empty actual &&
231 git diff-files --name-only >actual &&
236 test_expect_success
'git add --refresh correctly reports no match error' "
237 echo \"fatal: pathspec ':(icase)nonexistent' did not match any files\" >expect &&
238 test_must_fail git add --refresh ':(icase)nonexistent' 2>actual &&
239 test_cmp expect actual
242 test_expect_success POSIXPERM
,SANITY
'git add should fail atomically upon an unreadable file' '
247 test_must_fail git add --verbose . &&
248 ! ( git ls-files foo1 | grep foo1 )
253 test_expect_success POSIXPERM
,SANITY
'git add --ignore-errors' '
258 test_must_fail git add --verbose --ignore-errors . &&
259 git ls-files foo1 | grep foo1
264 test_expect_success POSIXPERM
,SANITY
'git add (add.ignore-errors)' '
265 git config add.ignore-errors 1 &&
270 test_must_fail git add --verbose . &&
271 git ls-files foo1 | grep foo1
275 test_expect_success POSIXPERM
,SANITY
'git add (add.ignore-errors = false)' '
276 git config add.ignore-errors 0 &&
281 test_must_fail git add --verbose . &&
282 ! ( git ls-files foo1 | grep foo1 )
286 test_expect_success POSIXPERM
,SANITY
'--no-ignore-errors overrides config' '
287 git config add.ignore-errors 1 &&
292 test_must_fail git add --verbose --no-ignore-errors . &&
293 ! ( git ls-files foo1 | grep foo1 ) &&
294 git config add.ignore-errors 0
298 test_expect_success BSLASHPSPEC
"git add 'fo\\[ou\\]bar' ignores foobar" '
300 touch fo\[ou\]bar foobar &&
301 git add '\''fo\[ou\]bar'\'' &&
302 git ls-files fo\[ou\]bar | grep -F fo\[ou\]bar &&
303 ! ( git ls-files foobar | grep foobar )
306 test_expect_success
'git add to resolve conflicts on otherwise ignored path' '
308 H=$(git rev-parse :1/2/a) &&
310 echo "100644 $H 1 track-this" &&
311 echo "100644 $H 3 track-this"
312 ) | git update-index --index-info &&
313 echo track-this >>.gitignore &&
314 echo resolved >track-this &&
318 test_expect_success
'"add non-existent" should fail' '
319 test_must_fail git add non-existent &&
320 ! (git ls-files | grep "non-existent")
323 test_expect_success
'git add -A on empty repo does not error out' '
333 test_expect_success
'"git add ." in empty repo' '
342 test_expect_success
'error on a repository with no commits' '
345 test_must_fail git add empty >actual 2>&1 &&
346 cat >expect <<-EOF &&
347 error: '"'empty/'"' does not have a commit checked out
348 fatal: adding files failed
350 test_cmp expect actual
353 test_expect_success
'git add --dry-run of existing changed file' "
354 echo new >>track-this &&
355 git add --dry-run track-this >actual 2>&1 &&
356 echo \"add 'track-this'\" | test_cmp - actual
359 test_expect_success
'git add --dry-run of non-existing file' "
360 echo ignored-file >>.gitignore &&
361 test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
364 test_expect_success
'git add --dry-run of an existing file output' "
365 echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
366 test_cmp expect actual
369 cat >expect.err
<<\EOF
370 The following paths are ignored by one of your .gitignore files
:
372 hint
: Use
-f if you really want to add them.
373 hint
: Turn this message off by running
374 hint
: "git config advice.addIgnoredFile false"
376 cat >expect.out
<<\EOF
380 test_expect_success
'git add --dry-run --ignore-missing of non-existing file' '
381 test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
384 test_expect_success
'git add --dry-run --ignore-missing of non-existing file output' '
385 test_cmp expect.out actual.out &&
386 test_cmp expect.err actual.err
389 test_expect_success
'git add --dry-run --interactive should fail' '
390 test_must_fail git add --dry-run --interactive
393 test_expect_success
'git add empty string should fail' '
394 test_must_fail git add ""
397 test_expect_success
'git add --chmod=[+-]x stages correctly' '
400 git add --chmod=+x foo1 &&
401 test_mode_in_index 100755 foo1 &&
402 git add --chmod=-x foo1 &&
403 test_mode_in_index 100644 foo1
406 test_expect_success POSIXPERM
,SYMLINKS
'git add --chmod=+x with symlinks' '
407 git config core.filemode 1 &&
408 git config core.symlinks 1 &&
411 git add --chmod=+x foo2 &&
412 test_mode_in_index 100755 foo2
415 test_expect_success
'git add --chmod=[+-]x changes index with already added file' '
420 git add --chmod=+x foo3 &&
421 test_mode_in_index 100755 foo3 &&
425 git add --chmod=-x xfoo3 &&
426 test_mode_in_index 100644 xfoo3
429 test_expect_success POSIXPERM
'git add --chmod=[+-]x does not change the working tree' '
432 git add --chmod=+x foo4 &&
436 test_expect_success
'git add --chmod fails with non regular files (but updates the other paths)' '
438 test_ln_s_add foo foo3 &&
440 test_must_fail git add --chmod=+x foo3 foo4 2>stderr &&
441 test_grep "cannot chmod +x .foo3." stderr &&
442 test_mode_in_index 120000 foo3 &&
443 test_mode_in_index 100755 foo4
446 test_expect_success
'git add --chmod honors --dry-run' '
450 git add --chmod=+x --dry-run foo4 &&
451 test_mode_in_index 100644 foo4
454 test_expect_success
'git add --chmod --dry-run reports error for non regular files' '
456 test_ln_s_add foo foo4 &&
457 test_must_fail git add --chmod=+x --dry-run foo4 2>stderr &&
458 test_grep "cannot chmod +x .foo4." stderr
461 test_expect_success
'git add --chmod --dry-run reports error for unmatched pathspec' '
462 test_must_fail git add --chmod=+x --dry-run nonexistent 2>stderr &&
463 test_grep "pathspec .nonexistent. did not match any files" stderr
466 test_expect_success
'no file status change if no pathspec is given' '
470 git add --chmod=+x &&
471 test_mode_in_index 100644 foo5 &&
472 test_mode_in_index 100644 foo6
475 test_expect_success
'no file status change if no pathspec is given in subdir' '
482 git add --chmod=+x &&
483 test_mode_in_index 100644 sub-foo1 &&
484 test_mode_in_index 100644 sub-foo2
488 test_expect_success
'all statuses changed in folder if . is given' '
493 touch x y z sub/a sub/dir/b &&
495 git add --chmod=+x . &&
496 test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
497 git add --chmod=-x . &&
498 test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
502 test_expect_success CASE_INSENSITIVE_FS
'path is case-insensitive' '
503 path="$(pwd)/BLUB" &&
505 downcased="$(echo "$path" | tr A-Z a-z)" &&