Merge branch 'ab/detox-gettext-tests'
[git] / t / t3700-add.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Carl D. Worth
4 #
5
6 test_description='Test of git add, including the -- option.'
7
8 . ./test-lib.sh
9
10 # Test the file mode "$1" of the file "$2" in the index.
11 test_mode_in_index () {
12         case "$(git ls-files -s "$2")" in
13         "$1 "*" $2")
14                 echo pass
15                 ;;
16         *)
17                 echo fail
18                 git ls-files -s "$2"
19                 return 1
20                 ;;
21         esac
22 }
23
24 test_expect_success \
25     'Test of git add' \
26     'touch foo && git add foo'
27
28 test_expect_success \
29     'Post-check that foo is in the index' \
30     'git ls-files foo | grep foo'
31
32 test_expect_success \
33     'Test that "git add -- -q" works' \
34     'touch -- -q && git add -- -q'
35
36 test_expect_success \
37         'git add: Test that executable bit is not used if core.filemode=0' \
38         'git config core.filemode 0 &&
39          echo foo >xfoo1 &&
40          chmod 755 xfoo1 &&
41          git add xfoo1 &&
42          test_mode_in_index 100644 xfoo1'
43
44 test_expect_success 'git add: filemode=0 should not get confused by symlink' '
45         rm -f xfoo1 &&
46         test_ln_s_add foo xfoo1 &&
47         test_mode_in_index 120000 xfoo1
48 '
49
50 test_expect_success \
51         'git update-index --add: Test that executable bit is not used...' \
52         'git config core.filemode 0 &&
53          echo foo >xfoo2 &&
54          chmod 755 xfoo2 &&
55          git update-index --add xfoo2 &&
56          test_mode_in_index 100644 xfoo2'
57
58 test_expect_success 'git add: filemode=0 should not get confused by symlink' '
59         rm -f xfoo2 &&
60         test_ln_s_add foo xfoo2 &&
61         test_mode_in_index 120000 xfoo2
62 '
63
64 test_expect_success \
65         'git update-index --add: Test that executable bit is not used...' \
66         'git config core.filemode 0 &&
67          test_ln_s_add xfoo2 xfoo3 &&   # runs git update-index --add
68          test_mode_in_index 120000 xfoo3'
69
70 test_expect_success '.gitignore test setup' '
71         echo "*.ig" >.gitignore &&
72         mkdir c.if d.ig &&
73         >a.ig && >b.if &&
74         >c.if/c.if && >c.if/c.ig &&
75         >d.ig/d.if && >d.ig/d.ig
76 '
77
78 test_expect_success '.gitignore is honored' '
79         git add . &&
80         ! (git ls-files | grep "\\.ig")
81 '
82
83 test_expect_success 'error out when attempting to add ignored ones without -f' '
84         test_must_fail git add a.?? &&
85         ! (git ls-files | grep "\\.ig")
86 '
87
88 test_expect_success 'error out when attempting to add ignored ones without -f' '
89         test_must_fail git add d.?? &&
90         ! (git ls-files | grep "\\.ig")
91 '
92
93 test_expect_success 'error out when attempting to add ignored ones but add others' '
94         touch a.if &&
95         test_must_fail git add a.?? &&
96         ! (git ls-files | grep "\\.ig") &&
97         (git ls-files | grep a.if)
98 '
99
100 test_expect_success 'add ignored ones with -f' '
101         git add -f a.?? &&
102         git ls-files --error-unmatch a.ig
103 '
104
105 test_expect_success 'add ignored ones with -f' '
106         git add -f d.??/* &&
107         git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
108 '
109
110 test_expect_success 'add ignored ones with -f' '
111         rm -f .git/index &&
112         git add -f d.?? &&
113         git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
114 '
115
116 test_expect_success '.gitignore with subdirectory' '
117
118         rm -f .git/index &&
119         mkdir -p sub/dir &&
120         echo "!dir/a.*" >sub/.gitignore &&
121         >sub/a.ig &&
122         >sub/dir/a.ig &&
123         git add sub/dir &&
124         git ls-files --error-unmatch sub/dir/a.ig &&
125         rm -f .git/index &&
126         (
127                 cd sub/dir &&
128                 git add .
129         ) &&
130         git ls-files --error-unmatch sub/dir/a.ig
131 '
132
133 mkdir 1 1/2 1/3
134 touch 1/2/a 1/3/b 1/2/c
135 test_expect_success 'check correct prefix detection' '
136         rm -f .git/index &&
137         git add 1/2/a 1/3/b 1/2/c
138 '
139
140 test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries' '
141         for s in 1 2 3
142         do
143                 echo $s > stage$s
144                 echo "100755 $(git hash-object -w stage$s) $s   file"
145                 echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s       symlink"
146         done | git update-index --index-info &&
147         git config core.filemode 0 &&
148         git config core.symlinks 0 &&
149         echo new > file &&
150         echo new > symlink &&
151         git add file symlink &&
152         git ls-files --stage | grep "^100755 .* 0       file$" &&
153         git ls-files --stage | grep "^120000 .* 0       symlink$"
154 '
155
156 test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
157         git rm --cached -f file symlink &&
158         (
159                 echo "100644 $(git hash-object -w stage1) 1     file" &&
160                 echo "100755 $(git hash-object -w stage2) 2     file" &&
161                 echo "100644 $(printf 1 | git hash-object -w -t blob --stdin) 1 symlink" &&
162                 echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
163         ) | git update-index --index-info &&
164         git config core.filemode 0 &&
165         git config core.symlinks 0 &&
166         echo new > file &&
167         echo new > symlink &&
168         git add file symlink &&
169         git ls-files --stage | grep "^100755 .* 0       file$" &&
170         git ls-files --stage | grep "^120000 .* 0       symlink$"
171 '
172
173 test_expect_success 'git add --refresh' '
174         >foo && git add foo && git commit -a -m "commit all" &&
175         test -z "$(git diff-index HEAD -- foo)" &&
176         git read-tree HEAD &&
177         case "$(git diff-index HEAD -- foo)" in
178         :100644" "*"M   foo") echo pass;;
179         *) echo fail; (exit 1);;
180         esac &&
181         git add --refresh -- foo &&
182         test -z "$(git diff-index HEAD -- foo)"
183 '
184
185 test_expect_success 'git add --refresh with pathspec' '
186         git reset --hard &&
187         echo >foo && echo >bar && echo >baz &&
188         git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
189         echo "100644 $H 3       foo" | git update-index --index-info &&
190         test-tool chmtime -60 bar baz &&
191         git add --refresh bar >actual &&
192         test_must_be_empty actual &&
193
194         git diff-files --name-only >actual &&
195         ! grep bar actual &&
196         grep baz actual
197 '
198
199 test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unreadable file' '
200         git reset --hard &&
201         date >foo1 &&
202         date >foo2 &&
203         chmod 0 foo2 &&
204         test_must_fail git add --verbose . &&
205         ! ( git ls-files foo1 | grep foo1 )
206 '
207
208 rm -f foo2
209
210 test_expect_success POSIXPERM,SANITY 'git add --ignore-errors' '
211         git reset --hard &&
212         date >foo1 &&
213         date >foo2 &&
214         chmod 0 foo2 &&
215         test_must_fail git add --verbose --ignore-errors . &&
216         git ls-files foo1 | grep foo1
217 '
218
219 rm -f foo2
220
221 test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors)' '
222         git config add.ignore-errors 1 &&
223         git reset --hard &&
224         date >foo1 &&
225         date >foo2 &&
226         chmod 0 foo2 &&
227         test_must_fail git add --verbose . &&
228         git ls-files foo1 | grep foo1
229 '
230 rm -f foo2
231
232 test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' '
233         git config add.ignore-errors 0 &&
234         git reset --hard &&
235         date >foo1 &&
236         date >foo2 &&
237         chmod 0 foo2 &&
238         test_must_fail git add --verbose . &&
239         ! ( git ls-files foo1 | grep foo1 )
240 '
241 rm -f foo2
242
243 test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' '
244        git config add.ignore-errors 1 &&
245        git reset --hard &&
246        date >foo1 &&
247        date >foo2 &&
248        chmod 0 foo2 &&
249        test_must_fail git add --verbose --no-ignore-errors . &&
250        ! ( git ls-files foo1 | grep foo1 ) &&
251        git config add.ignore-errors 0
252 '
253 rm -f foo2
254
255 test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
256         git reset --hard &&
257         touch fo\[ou\]bar foobar &&
258         git add '\''fo\[ou\]bar'\'' &&
259         git ls-files fo\[ou\]bar | fgrep fo\[ou\]bar &&
260         ! ( git ls-files foobar | grep foobar )
261 '
262
263 test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
264         git reset --hard &&
265         H=$(git rev-parse :1/2/a) &&
266         (
267                 echo "100644 $H 1       track-this" &&
268                 echo "100644 $H 3       track-this"
269         ) | git update-index --index-info &&
270         echo track-this >>.gitignore &&
271         echo resolved >track-this &&
272         git add track-this
273 '
274
275 test_expect_success '"add non-existent" should fail' '
276         test_must_fail git add non-existent &&
277         ! (git ls-files | grep "non-existent")
278 '
279
280 test_expect_success 'git add -A on empty repo does not error out' '
281         rm -fr empty &&
282         git init empty &&
283         (
284                 cd empty &&
285                 git add -A . &&
286                 git add -A
287         )
288 '
289
290 test_expect_success '"git add ." in empty repo' '
291         rm -fr empty &&
292         git init empty &&
293         (
294                 cd empty &&
295                 git add .
296         )
297 '
298
299 test_expect_success 'error on a repository with no commits' '
300         rm -fr empty &&
301         git init empty &&
302         test_must_fail git add empty >actual 2>&1 &&
303         cat >expect <<-EOF &&
304         error: '"'empty/'"' does not have a commit checked out
305         fatal: adding files failed
306         EOF
307         test_cmp expect actual
308 '
309
310 test_expect_success 'git add --dry-run of existing changed file' "
311         echo new >>track-this &&
312         git add --dry-run track-this >actual 2>&1 &&
313         echo \"add 'track-this'\" | test_cmp - actual
314 "
315
316 test_expect_success 'git add --dry-run of non-existing file' "
317         echo ignored-file >>.gitignore &&
318         test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
319 "
320
321 test_expect_success 'git add --dry-run of an existing file output' "
322         echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
323         test_cmp expect actual
324 "
325
326 cat >expect.err <<\EOF
327 The following paths are ignored by one of your .gitignore files:
328 ignored-file
329 hint: Use -f if you really want to add them.
330 hint: Turn this message off by running
331 hint: "git config advice.addIgnoredFile false"
332 EOF
333 cat >expect.out <<\EOF
334 add 'track-this'
335 EOF
336
337 test_expect_success 'git add --dry-run --ignore-missing of non-existing file' '
338         test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
339 '
340
341 test_expect_success 'git add --dry-run --ignore-missing of non-existing file output' '
342         test_cmp expect.out actual.out &&
343         test_cmp expect.err actual.err
344 '
345
346 test_expect_success 'git add empty string should fail' '
347         test_must_fail git add ""
348 '
349
350 test_expect_success 'git add --chmod=[+-]x stages correctly' '
351         rm -f foo1 &&
352         echo foo >foo1 &&
353         git add --chmod=+x foo1 &&
354         test_mode_in_index 100755 foo1 &&
355         git add --chmod=-x foo1 &&
356         test_mode_in_index 100644 foo1
357 '
358
359 test_expect_success POSIXPERM,SYMLINKS 'git add --chmod=+x with symlinks' '
360         git config core.filemode 1 &&
361         git config core.symlinks 1 &&
362         rm -f foo2 &&
363         echo foo >foo2 &&
364         git add --chmod=+x foo2 &&
365         test_mode_in_index 100755 foo2
366 '
367
368 test_expect_success 'git add --chmod=[+-]x changes index with already added file' '
369         rm -f foo3 xfoo3 &&
370         git reset --hard &&
371         echo foo >foo3 &&
372         git add foo3 &&
373         git add --chmod=+x foo3 &&
374         test_mode_in_index 100755 foo3 &&
375         echo foo >xfoo3 &&
376         chmod 755 xfoo3 &&
377         git add xfoo3 &&
378         git add --chmod=-x xfoo3 &&
379         test_mode_in_index 100644 xfoo3
380 '
381
382 test_expect_success POSIXPERM 'git add --chmod=[+-]x does not change the working tree' '
383         echo foo >foo4 &&
384         git add foo4 &&
385         git add --chmod=+x foo4 &&
386         ! test -x foo4
387 '
388
389 test_expect_success 'git add --chmod fails with non regular files (but updates the other paths)' '
390         git reset --hard &&
391         test_ln_s_add foo foo3 &&
392         touch foo4 &&
393         test_must_fail git add --chmod=+x foo3 foo4 2>stderr &&
394         test_i18ngrep "cannot chmod +x .foo3." stderr &&
395         test_mode_in_index 120000 foo3 &&
396         test_mode_in_index 100755 foo4
397 '
398
399 test_expect_success 'git add --chmod honors --dry-run' '
400         git reset --hard &&
401         echo foo >foo4 &&
402         git add foo4 &&
403         git add --chmod=+x --dry-run foo4 &&
404         test_mode_in_index 100644 foo4
405 '
406
407 test_expect_success 'git add --chmod --dry-run reports error for non regular files' '
408         git reset --hard &&
409         test_ln_s_add foo foo4 &&
410         test_must_fail git add --chmod=+x --dry-run foo4 2>stderr &&
411         test_i18ngrep "cannot chmod +x .foo4." stderr
412 '
413
414 test_expect_success 'git add --chmod --dry-run reports error for unmatched pathspec' '
415         test_must_fail git add --chmod=+x --dry-run nonexistent 2>stderr &&
416         test_i18ngrep "pathspec .nonexistent. did not match any files" stderr
417 '
418
419 test_expect_success 'no file status change if no pathspec is given' '
420         >foo5 &&
421         >foo6 &&
422         git add foo5 foo6 &&
423         git add --chmod=+x &&
424         test_mode_in_index 100644 foo5 &&
425         test_mode_in_index 100644 foo6
426 '
427
428 test_expect_success 'no file status change if no pathspec is given in subdir' '
429         mkdir -p sub &&
430         (
431                 cd sub &&
432                 >sub-foo1 &&
433                 >sub-foo2 &&
434                 git add . &&
435                 git add --chmod=+x &&
436                 test_mode_in_index 100644 sub-foo1 &&
437                 test_mode_in_index 100644 sub-foo2
438         )
439 '
440
441 test_expect_success 'all statuses changed in folder if . is given' '
442         git init repo &&
443         (
444                 cd repo &&
445                 mkdir -p sub/dir &&
446                 touch x y z sub/a sub/dir/b &&
447                 git add -A &&
448                 git add --chmod=+x . &&
449                 test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
450                 git add --chmod=-x . &&
451                 test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
452         )
453 '
454
455 test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
456         path="$(pwd)/BLUB" &&
457         touch "$path" &&
458         downcased="$(echo "$path" | tr A-Z a-z)" &&
459         git add "$downcased"
460 '
461
462 test_done