clone_submodule: avoid using `access()` on directories
[git.git] / t / t3700-add.sh
blob51afbd7b24a10c247520ba2f9be702da57f83afd
1 #!/bin/sh
3 # Copyright (c) 2006 Carl D. Worth
6 test_description='Test of git add, including the -- option.'
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
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
16 "$1 "*" $2")
17 echo pass
20 echo fail
21 git ls-files -s "$2"
22 return 1
24 esac
27 test_expect_success \
28 'Test of git add' \
29 'touch foo && git add foo'
31 test_expect_success \
32 'Post-check that foo is in the index' \
33 'git ls-files foo | grep foo'
35 test_expect_success \
36 'Test that "git add -- -q" works' \
37 '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
65 test_expect_success \
66 'git add: Test that executable bit is not used if core.filemode=0' \
67 'git config core.filemode 0 &&
68 echo foo >xfoo1 &&
69 chmod 755 xfoo1 &&
70 git add xfoo1 &&
71 test_mode_in_index 100644 xfoo1'
73 test_expect_success 'git add: filemode=0 should not get confused by symlink' '
74 rm -f xfoo1 &&
75 test_ln_s_add foo xfoo1 &&
76 test_mode_in_index 120000 xfoo1
79 test_expect_success \
80 'git update-index --add: Test that executable bit is not used...' \
81 'git config core.filemode 0 &&
82 echo foo >xfoo2 &&
83 chmod 755 xfoo2 &&
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' '
88 rm -f xfoo2 &&
89 test_ln_s_add foo xfoo2 &&
90 test_mode_in_index 120000 xfoo2
93 test_expect_success \
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 &&
101 mkdir c.if d.ig &&
102 >a.ig && >b.if &&
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' '
108 git add . &&
109 ! (git ls-files | grep "\\.ig")
112 test_expect_success 'error out when attempting to add ignored ones without -f' '
113 test_must_fail git add a.?? &&
114 ! (git ls-files | grep "\\.ig")
117 test_expect_success 'error out when attempting to add ignored ones without -f' '
118 test_must_fail git add d.?? &&
119 ! (git ls-files | grep "\\.ig")
122 test_expect_success 'error out when attempting to add ignored ones but add others' '
123 touch a.if &&
124 test_must_fail git add a.?? &&
125 ! (git ls-files | grep "\\.ig") &&
126 (git ls-files | grep a.if)
129 test_expect_success 'add ignored ones with -f' '
130 git add -f a.?? &&
131 git ls-files --error-unmatch a.ig
134 test_expect_success 'add ignored ones with -f' '
135 git add -f d.??/* &&
136 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
139 test_expect_success 'add ignored ones with -f' '
140 rm -f .git/index &&
141 git add -f d.?? &&
142 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
145 test_expect_success '.gitignore with subdirectory' '
147 rm -f .git/index &&
148 mkdir -p sub/dir &&
149 echo "!dir/a.*" >sub/.gitignore &&
150 >sub/a.ig &&
151 >sub/dir/a.ig &&
152 git add sub/dir &&
153 git ls-files --error-unmatch sub/dir/a.ig &&
154 rm -f .git/index &&
156 cd sub/dir &&
157 git add .
158 ) &&
159 git ls-files --error-unmatch sub/dir/a.ig
162 mkdir 1 1/2 1/3
163 touch 1/2/a 1/3/b 1/2/c
164 test_expect_success 'check correct prefix detection' '
165 rm -f .git/index &&
166 git add 1/2/a 1/3/b 1/2/c
169 test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries' '
170 for s in 1 2 3
172 echo $s > stage$s &&
173 echo "100755 $(git hash-object -w stage$s) $s file" &&
174 echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink" || return 1
175 done | git update-index --index-info &&
176 git config core.filemode 0 &&
177 git config core.symlinks 0 &&
178 echo new > file &&
179 echo new > symlink &&
180 git add file symlink &&
181 git ls-files --stage | grep "^100755 .* 0 file$" &&
182 git ls-files --stage | grep "^120000 .* 0 symlink$"
185 test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
186 git rm --cached -f file symlink &&
188 echo "100644 $(git hash-object -w stage1) 1 file" &&
189 echo "100755 $(git hash-object -w stage2) 2 file" &&
190 echo "100644 $(printf 1 | git hash-object -w -t blob --stdin) 1 symlink" &&
191 echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
192 ) | git update-index --index-info &&
193 git config core.filemode 0 &&
194 git config core.symlinks 0 &&
195 echo new > file &&
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 --refresh' '
203 >foo && git add foo && git commit -a -m "commit all" &&
204 test -z "$(git diff-index HEAD -- foo)" &&
205 git read-tree HEAD &&
206 case "$(git diff-index HEAD -- foo)" in
207 :100644" "*"M foo") echo pass;;
208 *) echo fail; false;;
209 esac &&
210 git add --refresh -- foo &&
211 test -z "$(git diff-index HEAD -- foo)"
214 test_expect_success 'git add --refresh with pathspec' '
215 git reset --hard &&
216 echo >foo && echo >bar && echo >baz &&
217 git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
218 echo "100644 $H 3 foo" | git update-index --index-info &&
219 test-tool chmtime -60 bar baz &&
220 git add --refresh bar >actual &&
221 test_must_be_empty actual &&
223 git diff-files --name-only >actual &&
224 ! grep bar actual &&
225 grep baz actual
228 test_expect_success 'git add --refresh correctly reports no match error' "
229 echo \"fatal: pathspec ':(icase)nonexistent' did not match any files\" >expect &&
230 test_must_fail git add --refresh ':(icase)nonexistent' 2>actual &&
231 test_cmp expect actual
234 test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unreadable file' '
235 git reset --hard &&
236 date >foo1 &&
237 date >foo2 &&
238 chmod 0 foo2 &&
239 test_must_fail git add --verbose . &&
240 ! ( git ls-files foo1 | grep foo1 )
243 rm -f foo2
245 test_expect_success POSIXPERM,SANITY 'git add --ignore-errors' '
246 git reset --hard &&
247 date >foo1 &&
248 date >foo2 &&
249 chmod 0 foo2 &&
250 test_must_fail git add --verbose --ignore-errors . &&
251 git ls-files foo1 | grep foo1
254 rm -f foo2
256 test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors)' '
257 git config add.ignore-errors 1 &&
258 git reset --hard &&
259 date >foo1 &&
260 date >foo2 &&
261 chmod 0 foo2 &&
262 test_must_fail git add --verbose . &&
263 git ls-files foo1 | grep foo1
265 rm -f foo2
267 test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' '
268 git config add.ignore-errors 0 &&
269 git reset --hard &&
270 date >foo1 &&
271 date >foo2 &&
272 chmod 0 foo2 &&
273 test_must_fail git add --verbose . &&
274 ! ( git ls-files foo1 | grep foo1 )
276 rm -f foo2
278 test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' '
279 git config add.ignore-errors 1 &&
280 git reset --hard &&
281 date >foo1 &&
282 date >foo2 &&
283 chmod 0 foo2 &&
284 test_must_fail git add --verbose --no-ignore-errors . &&
285 ! ( git ls-files foo1 | grep foo1 ) &&
286 git config add.ignore-errors 0
288 rm -f foo2
290 test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
291 git reset --hard &&
292 touch fo\[ou\]bar foobar &&
293 git add '\''fo\[ou\]bar'\'' &&
294 git ls-files fo\[ou\]bar | grep -F fo\[ou\]bar &&
295 ! ( git ls-files foobar | grep foobar )
298 test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
299 git reset --hard &&
300 H=$(git rev-parse :1/2/a) &&
302 echo "100644 $H 1 track-this" &&
303 echo "100644 $H 3 track-this"
304 ) | git update-index --index-info &&
305 echo track-this >>.gitignore &&
306 echo resolved >track-this &&
307 git add track-this
310 test_expect_success '"add non-existent" should fail' '
311 test_must_fail git add non-existent &&
312 ! (git ls-files | grep "non-existent")
315 test_expect_success 'git add -A on empty repo does not error out' '
316 rm -fr empty &&
317 git init empty &&
319 cd empty &&
320 git add -A . &&
321 git add -A
325 test_expect_success '"git add ." in empty repo' '
326 rm -fr empty &&
327 git init empty &&
329 cd empty &&
330 git add .
334 test_expect_success 'error on a repository with no commits' '
335 rm -fr empty &&
336 git init empty &&
337 test_must_fail git add empty >actual 2>&1 &&
338 cat >expect <<-EOF &&
339 error: '"'empty/'"' does not have a commit checked out
340 fatal: adding files failed
342 test_cmp expect actual
345 test_expect_success 'git add --dry-run of existing changed file' "
346 echo new >>track-this &&
347 git add --dry-run track-this >actual 2>&1 &&
348 echo \"add 'track-this'\" | test_cmp - actual
351 test_expect_success 'git add --dry-run of non-existing file' "
352 echo ignored-file >>.gitignore &&
353 test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
356 test_expect_success 'git add --dry-run of an existing file output' "
357 echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
358 test_cmp expect actual
361 cat >expect.err <<\EOF
362 The following paths are ignored by one of your .gitignore files:
363 ignored-file
364 hint: Use -f if you really want to add them.
365 hint: Turn this message off by running
366 hint: "git config advice.addIgnoredFile false"
368 cat >expect.out <<\EOF
369 add 'track-this'
372 test_expect_success 'git add --dry-run --ignore-missing of non-existing file' '
373 test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
376 test_expect_success 'git add --dry-run --ignore-missing of non-existing file output' '
377 test_cmp expect.out actual.out &&
378 test_cmp expect.err actual.err
381 test_expect_success 'git add --dry-run --interactive should fail' '
382 test_must_fail git add --dry-run --interactive
385 test_expect_success 'git add empty string should fail' '
386 test_must_fail git add ""
389 test_expect_success 'git add --chmod=[+-]x stages correctly' '
390 rm -f foo1 &&
391 echo foo >foo1 &&
392 git add --chmod=+x foo1 &&
393 test_mode_in_index 100755 foo1 &&
394 git add --chmod=-x foo1 &&
395 test_mode_in_index 100644 foo1
398 test_expect_success POSIXPERM,SYMLINKS 'git add --chmod=+x with symlinks' '
399 git config core.filemode 1 &&
400 git config core.symlinks 1 &&
401 rm -f foo2 &&
402 echo foo >foo2 &&
403 git add --chmod=+x foo2 &&
404 test_mode_in_index 100755 foo2
407 test_expect_success 'git add --chmod=[+-]x changes index with already added file' '
408 rm -f foo3 xfoo3 &&
409 git reset --hard &&
410 echo foo >foo3 &&
411 git add foo3 &&
412 git add --chmod=+x foo3 &&
413 test_mode_in_index 100755 foo3 &&
414 echo foo >xfoo3 &&
415 chmod 755 xfoo3 &&
416 git add xfoo3 &&
417 git add --chmod=-x xfoo3 &&
418 test_mode_in_index 100644 xfoo3
421 test_expect_success POSIXPERM 'git add --chmod=[+-]x does not change the working tree' '
422 echo foo >foo4 &&
423 git add foo4 &&
424 git add --chmod=+x foo4 &&
425 ! test -x foo4
428 test_expect_success 'git add --chmod fails with non regular files (but updates the other paths)' '
429 git reset --hard &&
430 test_ln_s_add foo foo3 &&
431 touch foo4 &&
432 test_must_fail git add --chmod=+x foo3 foo4 2>stderr &&
433 test_i18ngrep "cannot chmod +x .foo3." stderr &&
434 test_mode_in_index 120000 foo3 &&
435 test_mode_in_index 100755 foo4
438 test_expect_success 'git add --chmod honors --dry-run' '
439 git reset --hard &&
440 echo foo >foo4 &&
441 git add foo4 &&
442 git add --chmod=+x --dry-run foo4 &&
443 test_mode_in_index 100644 foo4
446 test_expect_success 'git add --chmod --dry-run reports error for non regular files' '
447 git reset --hard &&
448 test_ln_s_add foo foo4 &&
449 test_must_fail git add --chmod=+x --dry-run foo4 2>stderr &&
450 test_i18ngrep "cannot chmod +x .foo4." stderr
453 test_expect_success 'git add --chmod --dry-run reports error for unmatched pathspec' '
454 test_must_fail git add --chmod=+x --dry-run nonexistent 2>stderr &&
455 test_i18ngrep "pathspec .nonexistent. did not match any files" stderr
458 test_expect_success 'no file status change if no pathspec is given' '
459 >foo5 &&
460 >foo6 &&
461 git add foo5 foo6 &&
462 git add --chmod=+x &&
463 test_mode_in_index 100644 foo5 &&
464 test_mode_in_index 100644 foo6
467 test_expect_success 'no file status change if no pathspec is given in subdir' '
468 mkdir -p sub &&
470 cd sub &&
471 >sub-foo1 &&
472 >sub-foo2 &&
473 git add . &&
474 git add --chmod=+x &&
475 test_mode_in_index 100644 sub-foo1 &&
476 test_mode_in_index 100644 sub-foo2
480 test_expect_success 'all statuses changed in folder if . is given' '
481 git init repo &&
483 cd repo &&
484 mkdir -p sub/dir &&
485 touch x y z sub/a sub/dir/b &&
486 git add -A &&
487 git add --chmod=+x . &&
488 test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
489 git add --chmod=-x . &&
490 test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
494 test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
495 path="$(pwd)/BLUB" &&
496 touch "$path" &&
497 downcased="$(echo "$path" | tr A-Z a-z)" &&
498 git add "$downcased"
501 test_done