Git 2.45
[git/gitster.git] / t / t3700-add.sh
blob839c904745a2861487e04363060a951aaeb902c9
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 'Test of git add' '
28 touch foo && git add foo
31 test_expect_success 'Test with no pathspecs' '
32 cat >expect <<-EOF &&
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"
36 EOF
37 git add 2>actual &&
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
75 test_expect_success \
76 'git add: Test that executable bit is not used if core.filemode=0' \
77 'git config core.filemode 0 &&
78 echo foo >xfoo1 &&
79 chmod 755 xfoo1 &&
80 git add xfoo1 &&
81 test_mode_in_index 100644 xfoo1'
83 test_expect_success 'git add: filemode=0 should not get confused by symlink' '
84 rm -f xfoo1 &&
85 test_ln_s_add foo xfoo1 &&
86 test_mode_in_index 120000 xfoo1
89 test_expect_success \
90 'git update-index --add: Test that executable bit is not used...' \
91 'git config core.filemode 0 &&
92 echo foo >xfoo2 &&
93 chmod 755 xfoo2 &&
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' '
98 rm -f xfoo2 &&
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 &&
111 mkdir c.if d.ig &&
112 >a.ig && >b.if &&
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' '
118 git add . &&
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' '
139 touch a.if &&
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 &&
144 grep a.if files
147 test_expect_success 'add ignored ones with -f' '
148 git add -f a.?? &&
149 git ls-files --error-unmatch a.ig
152 test_expect_success 'add ignored ones with -f' '
153 git add -f d.??/* &&
154 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
157 test_expect_success 'add ignored ones with -f' '
158 rm -f .git/index &&
159 git add -f d.?? &&
160 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
163 test_expect_success '.gitignore with subdirectory' '
165 rm -f .git/index &&
166 mkdir -p sub/dir &&
167 echo "!dir/a.*" >sub/.gitignore &&
168 >sub/a.ig &&
169 >sub/dir/a.ig &&
170 git add sub/dir &&
171 git ls-files --error-unmatch sub/dir/a.ig &&
172 rm -f .git/index &&
174 cd sub/dir &&
175 git add .
176 ) &&
177 git ls-files --error-unmatch sub/dir/a.ig
180 mkdir 1 1/2 1/3
181 touch 1/2/a 1/3/b 1/2/c
182 test_expect_success 'check correct prefix detection' '
183 rm -f .git/index &&
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' '
188 for s in 1 2 3
190 echo $s > stage$s &&
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 &&
196 echo new > file &&
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 &&
213 echo new > file &&
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;;
227 esac &&
228 git add --refresh -- foo &&
229 test -z "$(git diff-index HEAD -- foo)"
232 test_expect_success 'git add --refresh with pathspec' '
233 git reset --hard &&
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 &&
242 ! grep bar actual &&
243 grep baz 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' '
253 git reset --hard &&
254 date >foo1 &&
255 date >foo2 &&
256 chmod 0 foo2 &&
257 test_must_fail git add --verbose . &&
258 ! ( git ls-files foo1 | grep foo1 )
261 rm -f foo2
263 test_expect_success POSIXPERM,SANITY 'git add --ignore-errors' '
264 git reset --hard &&
265 date >foo1 &&
266 date >foo2 &&
267 chmod 0 foo2 &&
268 test_must_fail git add --verbose --ignore-errors . &&
269 git ls-files foo1 | grep foo1
272 rm -f foo2
274 test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors)' '
275 git config add.ignore-errors 1 &&
276 git reset --hard &&
277 date >foo1 &&
278 date >foo2 &&
279 chmod 0 foo2 &&
280 test_must_fail git add --verbose . &&
281 git ls-files foo1 | grep foo1
283 rm -f foo2
285 test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' '
286 git config add.ignore-errors 0 &&
287 git reset --hard &&
288 date >foo1 &&
289 date >foo2 &&
290 chmod 0 foo2 &&
291 test_must_fail git add --verbose . &&
292 ! ( git ls-files foo1 | grep foo1 )
294 rm -f foo2
296 test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' '
297 git config add.ignore-errors 1 &&
298 git reset --hard &&
299 date >foo1 &&
300 date >foo2 &&
301 chmod 0 foo2 &&
302 test_must_fail git add --verbose --no-ignore-errors . &&
303 ! ( git ls-files foo1 | grep foo1 ) &&
304 git config add.ignore-errors 0
306 rm -f foo2
308 test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
309 git reset --hard &&
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' '
317 git reset --hard &&
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 &&
325 git add 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' '
334 rm -fr empty &&
335 git init empty &&
337 cd empty &&
338 git add -A . &&
339 git add -A
343 test_expect_success '"git add ." in empty repo' '
344 rm -fr empty &&
345 git init empty &&
347 cd empty &&
348 git add .
352 test_expect_success '"git add" a embedded repository' '
353 rm -fr outer && git init outer &&
355 cd outer &&
356 for i in 1 2
358 name=inner$i &&
359 git init $name &&
360 git -C $name commit --allow-empty -m $name ||
361 return 1
362 done &&
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:
370 hint:
371 hint: git submodule add <url> inner1
372 hint:
373 hint: If you added this path by mistake, you can remove it from the
374 hint: index with:
375 hint:
376 hint: git rm --cached inner1
377 hint:
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' '
387 rm -fr empty &&
388 git init empty &&
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:
415 ignored-file
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
420 add 'track-this'
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' '
441 rm -f foo1 &&
442 echo foo >foo1 &&
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 &&
452 rm -f foo2 &&
453 echo foo >foo2 &&
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' '
459 rm -f foo3 xfoo3 &&
460 git reset --hard &&
461 echo foo >foo3 &&
462 git add foo3 &&
463 git add --chmod=+x foo3 &&
464 test_mode_in_index 100755 foo3 &&
465 echo foo >xfoo3 &&
466 chmod 755 xfoo3 &&
467 git add xfoo3 &&
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' '
473 echo foo >foo4 &&
474 git add foo4 &&
475 git add --chmod=+x foo4 &&
476 ! test -x foo4
479 test_expect_success 'git add --chmod fails with non regular files (but updates the other paths)' '
480 git reset --hard &&
481 test_ln_s_add foo foo3 &&
482 touch foo4 &&
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' '
490 git reset --hard &&
491 echo foo >foo4 &&
492 git add foo4 &&
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' '
498 git reset --hard &&
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' '
510 >foo5 &&
511 >foo6 &&
512 git add foo5 foo6 &&
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' '
519 mkdir -p sub &&
521 cd sub &&
522 >sub-foo1 &&
523 >sub-foo2 &&
524 git add . &&
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' '
532 git init repo &&
534 cd repo &&
535 mkdir -p sub/dir &&
536 touch x y z sub/a sub/dir/b &&
537 git add -A &&
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" &&
547 touch "$path" &&
548 downcased="$(echo "$path" | tr A-Z a-z)" &&
549 git add "$downcased"
552 test_done