]>
Commit | Line | Data |
---|---|---|
60ace879 CW |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Carl D. Worth | |
4 | # | |
5 | ||
5be60078 | 6 | test_description='Test of git add, including the -- option.' |
60ace879 CW |
7 | |
8 | . ./test-lib.sh | |
9 | ||
766cdc41 IB |
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 | ||
60ace879 | 24 | test_expect_success \ |
5be60078 JH |
25 | 'Test of git add' \ |
26 | 'touch foo && git add foo' | |
60ace879 CW |
27 | |
28 | test_expect_success \ | |
29 | 'Post-check that foo is in the index' \ | |
5be60078 | 30 | 'git ls-files foo | grep foo' |
60ace879 CW |
31 | |
32 | test_expect_success \ | |
5be60078 JH |
33 | 'Test that "git add -- -q" works' \ |
34 | 'touch -- -q && git add -- -q' | |
60ace879 | 35 | |
fd28b34a | 36 | test_expect_success \ |
5be60078 | 37 | 'git add: Test that executable bit is not used if core.filemode=0' \ |
e0d10e1c | 38 | 'git config core.filemode 0 && |
fd28b34a SP |
39 | echo foo >xfoo1 && |
40 | chmod 755 xfoo1 && | |
5be60078 | 41 | git add xfoo1 && |
766cdc41 | 42 | test_mode_in_index 100644 xfoo1' |
fd28b34a | 43 | |
889c6f0e | 44 | test_expect_success 'git add: filemode=0 should not get confused by symlink' ' |
185c975f | 45 | rm -f xfoo1 && |
889c6f0e | 46 | test_ln_s_add foo xfoo1 && |
766cdc41 | 47 | test_mode_in_index 120000 xfoo1 |
185c975f JH |
48 | ' |
49 | ||
fd28b34a | 50 | test_expect_success \ |
5be60078 | 51 | 'git update-index --add: Test that executable bit is not used...' \ |
e0d10e1c | 52 | 'git config core.filemode 0 && |
fd28b34a SP |
53 | echo foo >xfoo2 && |
54 | chmod 755 xfoo2 && | |
5be60078 | 55 | git update-index --add xfoo2 && |
766cdc41 | 56 | test_mode_in_index 100644 xfoo2' |
2bbaaed9 | 57 | |
889c6f0e | 58 | test_expect_success 'git add: filemode=0 should not get confused by symlink' ' |
185c975f | 59 | rm -f xfoo2 && |
889c6f0e | 60 | test_ln_s_add foo xfoo2 && |
766cdc41 | 61 | test_mode_in_index 120000 xfoo2 |
185c975f JH |
62 | ' |
63 | ||
889c6f0e | 64 | test_expect_success \ |
5be60078 | 65 | 'git update-index --add: Test that executable bit is not used...' \ |
e0d10e1c | 66 | 'git config core.filemode 0 && |
889c6f0e | 67 | test_ln_s_add xfoo2 xfoo3 && # runs git update-index --add |
766cdc41 | 68 | test_mode_in_index 120000 xfoo3' |
fd28b34a | 69 | |
4d06f8ac JH |
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' ' | |
5be60078 | 79 | git add . && |
bbf08124 | 80 | ! (git ls-files | grep "\\.ig") |
4d06f8ac JH |
81 | ' |
82 | ||
83 | test_expect_success 'error out when attempting to add ignored ones without -f' ' | |
d492b31c | 84 | test_must_fail git add a.?? && |
bbf08124 | 85 | ! (git ls-files | grep "\\.ig") |
4d06f8ac JH |
86 | ' |
87 | ||
88 | test_expect_success 'error out when attempting to add ignored ones without -f' ' | |
d492b31c | 89 | test_must_fail git add d.?? && |
bbf08124 | 90 | ! (git ls-files | grep "\\.ig") |
4d06f8ac JH |
91 | ' |
92 | ||
1d31e5a2 MG |
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 | ||
4d06f8ac | 100 | test_expect_success 'add ignored ones with -f' ' |
5be60078 JH |
101 | git add -f a.?? && |
102 | git ls-files --error-unmatch a.ig | |
4d06f8ac JH |
103 | ' |
104 | ||
105 | test_expect_success 'add ignored ones with -f' ' | |
5be60078 JH |
106 | git add -f d.??/* && |
107 | git ls-files --error-unmatch d.ig/d.if d.ig/d.ig | |
4d06f8ac JH |
108 | ' |
109 | ||
41a7aa58 JH |
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 | ||
c7f34c18 JS |
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' ' | |
41a7aa58 | 136 | rm -f .git/index && |
c7f34c18 JS |
137 | git add 1/2/a 1/3/b 1/2/c |
138 | ' | |
139 | ||
05dcd698 | 140 | test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries' ' |
20314271 JS |
141 | for s in 1 2 3 |
142 | do | |
05dcd698 | 143 | echo $s > stage$s |
20314271 | 144 | echo "100755 $(git hash-object -w stage$s) $s file" |
05dcd698 | 145 | echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink" |
20314271 JS |
146 | done | git update-index --index-info && |
147 | git config core.filemode 0 && | |
05dcd698 | 148 | git config core.symlinks 0 && |
20314271 | 149 | echo new > file && |
05dcd698 JS |
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$" | |
20314271 JS |
154 | ' |
155 | ||
05dcd698 JS |
156 | test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' ' |
157 | git rm --cached -f file symlink && | |
20314271 | 158 | ( |
3ea67379 ES |
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" && | |
a697ec69 | 162 | echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink" |
20314271 JS |
163 | ) | git update-index --index-info && |
164 | git config core.filemode 0 && | |
05dcd698 | 165 | git config core.symlinks 0 && |
20314271 | 166 | echo new > file && |
05dcd698 JS |
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$" | |
20314271 JS |
171 | ' |
172 | ||
d616813d AJ |
173 | test_expect_success 'git add --refresh' ' |
174 | >foo && git add foo && git commit -a -m "commit all" && | |
ed6c2314 | 175 | test -z "$(git diff-index HEAD -- foo)" && |
d616813d | 176 | git read-tree HEAD && |
ed6c2314 | 177 | case "$(git diff-index HEAD -- foo)" in |
335f8787 | 178 | :100644" "*"M foo") echo pass;; |
d616813d AJ |
179 | *) echo fail; (exit 1);; |
180 | esac && | |
181 | git add --refresh -- foo && | |
ed6c2314 | 182 | test -z "$(git diff-index HEAD -- foo)" |
d616813d AJ |
183 | ' |
184 | ||
3d1f148c JH |
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 && | |
0e496492 | 190 | test-tool chmtime -60 bar baz && |
3d1f148c | 191 | git add --refresh bar >actual && |
d3c6751b | 192 | test_must_be_empty actual && |
3d1f148c JH |
193 | |
194 | git diff-files --name-only >actual && | |
64d1022e | 195 | ! grep bar actual && |
3d1f148c JH |
196 | grep baz actual |
197 | ' | |
198 | ||
4e956983 MT |
199 | test_expect_success 'git add --refresh correctly reports no match error' " |
200 | echo \"fatal: pathspec ':(icase)nonexistent' did not match any files\" >expect && | |
201 | test_must_fail git add --refresh ':(icase)nonexistent' 2>actual && | |
202 | test_cmp expect actual | |
203 | " | |
204 | ||
c91cfd19 | 205 | test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unreadable file' ' |
89597436 AR |
206 | git reset --hard && |
207 | date >foo1 && | |
208 | date >foo2 && | |
209 | chmod 0 foo2 && | |
210 | test_must_fail git add --verbose . && | |
211 | ! ( git ls-files foo1 | grep foo1 ) | |
212 | ' | |
213 | ||
214 | rm -f foo2 | |
215 | ||
c91cfd19 | 216 | test_expect_success POSIXPERM,SANITY 'git add --ignore-errors' ' |
89597436 AR |
217 | git reset --hard && |
218 | date >foo1 && | |
219 | date >foo2 && | |
220 | chmod 0 foo2 && | |
221 | test_must_fail git add --verbose --ignore-errors . && | |
222 | git ls-files foo1 | grep foo1 | |
223 | ' | |
224 | ||
225 | rm -f foo2 | |
226 | ||
c91cfd19 | 227 | test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors)' ' |
dad25e4a AR |
228 | git config add.ignore-errors 1 && |
229 | git reset --hard && | |
230 | date >foo1 && | |
231 | date >foo2 && | |
232 | chmod 0 foo2 && | |
233 | test_must_fail git add --verbose . && | |
234 | git ls-files foo1 | grep foo1 | |
235 | ' | |
236 | rm -f foo2 | |
237 | ||
c91cfd19 | 238 | test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' ' |
dad25e4a AR |
239 | git config add.ignore-errors 0 && |
240 | git reset --hard && | |
241 | date >foo1 && | |
242 | date >foo2 && | |
243 | chmod 0 foo2 && | |
244 | test_must_fail git add --verbose . && | |
245 | ! ( git ls-files foo1 | grep foo1 ) | |
246 | ' | |
ed342fde SB |
247 | rm -f foo2 |
248 | ||
c91cfd19 | 249 | test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' ' |
ed342fde SB |
250 | git config add.ignore-errors 1 && |
251 | git reset --hard && | |
252 | date >foo1 && | |
253 | date >foo2 && | |
254 | chmod 0 foo2 && | |
255 | test_must_fail git add --verbose --no-ignore-errors . && | |
256 | ! ( git ls-files foo1 | grep foo1 ) && | |
257 | git config add.ignore-errors 0 | |
258 | ' | |
259 | rm -f foo2 | |
dad25e4a | 260 | |
6fd1106a | 261 | test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" ' |
ea335b56 | 262 | git reset --hard && |
8134a003 AR |
263 | touch fo\[ou\]bar foobar && |
264 | git add '\''fo\[ou\]bar'\'' && | |
87539416 | 265 | git ls-files fo\[ou\]bar | fgrep fo\[ou\]bar && |
ea335b56 KB |
266 | ! ( git ls-files foobar | grep foobar ) |
267 | ' | |
268 | ||
6e4f981f JK |
269 | test_expect_success 'git add to resolve conflicts on otherwise ignored path' ' |
270 | git reset --hard && | |
271 | H=$(git rev-parse :1/2/a) && | |
272 | ( | |
3ea67379 | 273 | echo "100644 $H 1 track-this" && |
6e4f981f JK |
274 | echo "100644 $H 3 track-this" |
275 | ) | git update-index --index-info && | |
276 | echo track-this >>.gitignore && | |
277 | echo resolved >track-this && | |
278 | git add track-this | |
279 | ' | |
280 | ||
1e7ef746 CP |
281 | test_expect_success '"add non-existent" should fail' ' |
282 | test_must_fail git add non-existent && | |
283 | ! (git ls-files | grep "non-existent") | |
284 | ' | |
285 | ||
64ed07ce NTND |
286 | test_expect_success 'git add -A on empty repo does not error out' ' |
287 | rm -fr empty && | |
288 | git init empty && | |
289 | ( | |
290 | cd empty && | |
291 | git add -A . && | |
292 | git add -A | |
293 | ) | |
294 | ' | |
295 | ||
296 | test_expect_success '"git add ." in empty repo' ' | |
297 | rm -fr empty && | |
298 | git init empty && | |
299 | ( | |
300 | cd empty && | |
301 | git add . | |
302 | ) | |
303 | ' | |
304 | ||
f937bc2f KM |
305 | test_expect_success 'error on a repository with no commits' ' |
306 | rm -fr empty && | |
307 | git init empty && | |
308 | test_must_fail git add empty >actual 2>&1 && | |
309 | cat >expect <<-EOF && | |
310 | error: '"'empty/'"' does not have a commit checked out | |
311 | fatal: adding files failed | |
312 | EOF | |
1108cea7 | 313 | test_cmp expect actual |
f937bc2f KM |
314 | ' |
315 | ||
108da0db JL |
316 | test_expect_success 'git add --dry-run of existing changed file' " |
317 | echo new >>track-this && | |
318 | git add --dry-run track-this >actual 2>&1 && | |
319 | echo \"add 'track-this'\" | test_cmp - actual | |
320 | " | |
321 | ||
322 | test_expect_success 'git add --dry-run of non-existing file' " | |
323 | echo ignored-file >>.gitignore && | |
48168851 ÆAB |
324 | test_must_fail git add --dry-run track-this ignored-file >actual 2>&1 |
325 | " | |
326 | ||
68b2a005 | 327 | test_expect_success 'git add --dry-run of an existing file output' " |
48168851 | 328 | echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect && |
1108cea7 | 329 | test_cmp expect actual |
108da0db JL |
330 | " |
331 | ||
c1e02b2b | 332 | cat >expect.err <<\EOF |
108da0db JL |
333 | The following paths are ignored by one of your .gitignore files: |
334 | ignored-file | |
bf66db37 | 335 | hint: Use -f if you really want to add them. |
887a0fd5 HW |
336 | hint: Turn this message off by running |
337 | hint: "git config advice.addIgnoredFile false" | |
c1e02b2b JS |
338 | EOF |
339 | cat >expect.out <<\EOF | |
108da0db JL |
340 | add 'track-this' |
341 | EOF | |
342 | ||
343 | test_expect_success 'git add --dry-run --ignore-missing of non-existing file' ' | |
439fb829 ÆAB |
344 | test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err |
345 | ' | |
346 | ||
68b2a005 | 347 | test_expect_success 'git add --dry-run --ignore-missing of non-existing file output' ' |
1108cea7 ÆAB |
348 | test_cmp expect.out actual.out && |
349 | test_cmp expect.err actual.err | |
108da0db JL |
350 | ' |
351 | ||
a1989cf7 ØW |
352 | test_expect_success 'git add --dry-run --interactive should fail' ' |
353 | test_must_fail git add --dry-run --interactive | |
354 | ' | |
355 | ||
9e4e8a64 EX |
356 | test_expect_success 'git add empty string should fail' ' |
357 | test_must_fail git add "" | |
d426430e EX |
358 | ' |
359 | ||
b38ab197 | 360 | test_expect_success 'git add --chmod=[+-]x stages correctly' ' |
c0fa44d8 | 361 | rm -f foo1 && |
4e55ed32 ET |
362 | echo foo >foo1 && |
363 | git add --chmod=+x foo1 && | |
766cdc41 | 364 | test_mode_in_index 100755 foo1 && |
b38ab197 | 365 | git add --chmod=-x foo1 && |
766cdc41 | 366 | test_mode_in_index 100644 foo1 |
4e55ed32 ET |
367 | ' |
368 | ||
369 | test_expect_success POSIXPERM,SYMLINKS 'git add --chmod=+x with symlinks' ' | |
370 | git config core.filemode 1 && | |
371 | git config core.symlinks 1 && | |
c0fa44d8 | 372 | rm -f foo2 && |
4e55ed32 ET |
373 | echo foo >foo2 && |
374 | git add --chmod=+x foo2 && | |
766cdc41 | 375 | test_mode_in_index 100755 foo2 |
4e55ed32 ET |
376 | ' |
377 | ||
610d55af | 378 | test_expect_success 'git add --chmod=[+-]x changes index with already added file' ' |
76e368c3 | 379 | rm -f foo3 xfoo3 && |
57ea241e | 380 | git reset --hard && |
610d55af TG |
381 | echo foo >foo3 && |
382 | git add foo3 && | |
383 | git add --chmod=+x foo3 && | |
384 | test_mode_in_index 100755 foo3 && | |
385 | echo foo >xfoo3 && | |
386 | chmod 755 xfoo3 && | |
387 | git add xfoo3 && | |
388 | git add --chmod=-x xfoo3 && | |
389 | test_mode_in_index 100644 xfoo3 | |
390 | ' | |
391 | ||
40e0dc17 | 392 | test_expect_success POSIXPERM 'git add --chmod=[+-]x does not change the working tree' ' |
610d55af TG |
393 | echo foo >foo4 && |
394 | git add foo4 && | |
395 | git add --chmod=+x foo4 && | |
40e0dc17 | 396 | ! test -x foo4 |
610d55af TG |
397 | ' |
398 | ||
9ebd7fe1 MT |
399 | test_expect_success 'git add --chmod fails with non regular files (but updates the other paths)' ' |
400 | git reset --hard && | |
401 | test_ln_s_add foo foo3 && | |
402 | touch foo4 && | |
403 | test_must_fail git add --chmod=+x foo3 foo4 2>stderr && | |
404 | test_i18ngrep "cannot chmod +x .foo3." stderr && | |
405 | test_mode_in_index 120000 foo3 && | |
406 | test_mode_in_index 100755 foo4 | |
407 | ' | |
408 | ||
c937d70b MT |
409 | test_expect_success 'git add --chmod honors --dry-run' ' |
410 | git reset --hard && | |
411 | echo foo >foo4 && | |
412 | git add foo4 && | |
413 | git add --chmod=+x --dry-run foo4 && | |
414 | test_mode_in_index 100644 foo4 | |
415 | ' | |
416 | ||
417 | test_expect_success 'git add --chmod --dry-run reports error for non regular files' ' | |
418 | git reset --hard && | |
419 | test_ln_s_add foo foo4 && | |
9ebd7fe1 | 420 | test_must_fail git add --chmod=+x --dry-run foo4 2>stderr && |
48960894 | 421 | test_i18ngrep "cannot chmod +x .foo4." stderr |
c937d70b MT |
422 | ' |
423 | ||
424 | test_expect_success 'git add --chmod --dry-run reports error for unmatched pathspec' ' | |
425 | test_must_fail git add --chmod=+x --dry-run nonexistent 2>stderr && | |
426 | test_i18ngrep "pathspec .nonexistent. did not match any files" stderr | |
427 | ' | |
428 | ||
610d55af TG |
429 | test_expect_success 'no file status change if no pathspec is given' ' |
430 | >foo5 && | |
431 | >foo6 && | |
432 | git add foo5 foo6 && | |
433 | git add --chmod=+x && | |
434 | test_mode_in_index 100644 foo5 && | |
435 | test_mode_in_index 100644 foo6 | |
436 | ' | |
437 | ||
438 | test_expect_success 'no file status change if no pathspec is given in subdir' ' | |
b07ad464 | 439 | mkdir -p sub && |
610d55af TG |
440 | ( |
441 | cd sub && | |
442 | >sub-foo1 && | |
443 | >sub-foo2 && | |
444 | git add . && | |
445 | git add --chmod=+x && | |
446 | test_mode_in_index 100644 sub-foo1 && | |
447 | test_mode_in_index 100644 sub-foo2 | |
448 | ) | |
449 | ' | |
450 | ||
451 | test_expect_success 'all statuses changed in folder if . is given' ' | |
9ebd7fe1 MT |
452 | git init repo && |
453 | ( | |
454 | cd repo && | |
455 | mkdir -p sub/dir && | |
456 | touch x y z sub/a sub/dir/b && | |
457 | git add -A && | |
458 | git add --chmod=+x . && | |
459 | test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 && | |
460 | git add --chmod=-x . && | |
461 | test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0 | |
462 | ) | |
610d55af TG |
463 | ' |
464 | ||
d8727b36 JS |
465 | test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' ' |
466 | path="$(pwd)/BLUB" && | |
467 | touch "$path" && | |
468 | downcased="$(echo "$path" | tr A-Z a-z)" && | |
469 | git add "$downcased" | |
470 | ' | |
471 | ||
60ace879 | 472 | test_done |