]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3700-add.sh
t3700: remove unwanted leftover files before running new tests
[thirdparty/git.git] / t / t3700-add.sh
CommitLineData
60ace879
CW
1#!/bin/sh
2#
3# Copyright (c) 2006 Carl D. Worth
4#
5
5be60078 6test_description='Test of git add, including the -- option.'
60ace879
CW
7
8. ./test-lib.sh
9
10test_expect_success \
5be60078
JH
11 'Test of git add' \
12 'touch foo && git add foo'
60ace879
CW
13
14test_expect_success \
15 'Post-check that foo is in the index' \
5be60078 16 'git ls-files foo | grep foo'
60ace879
CW
17
18test_expect_success \
5be60078
JH
19 'Test that "git add -- -q" works' \
20 'touch -- -q && git add -- -q'
60ace879 21
fd28b34a 22test_expect_success \
5be60078 23 'git add: Test that executable bit is not used if core.filemode=0' \
e0d10e1c 24 'git config core.filemode 0 &&
fd28b34a
SP
25 echo foo >xfoo1 &&
26 chmod 755 xfoo1 &&
5be60078 27 git add xfoo1 &&
ed6c2314 28 case "$(git ls-files --stage xfoo1)" in
335f8787 29 100644" "*xfoo1) echo pass;;
5be60078 30 *) echo fail; git ls-files --stage xfoo1; (exit 1);;
fd28b34a
SP
31 esac'
32
889c6f0e 33test_expect_success 'git add: filemode=0 should not get confused by symlink' '
185c975f 34 rm -f xfoo1 &&
889c6f0e 35 test_ln_s_add foo xfoo1 &&
ed6c2314 36 case "$(git ls-files --stage xfoo1)" in
335f8787 37 120000" "*xfoo1) echo pass;;
5be60078 38 *) echo fail; git ls-files --stage xfoo1; (exit 1);;
185c975f
JH
39 esac
40'
41
fd28b34a 42test_expect_success \
5be60078 43 'git update-index --add: Test that executable bit is not used...' \
e0d10e1c 44 'git config core.filemode 0 &&
fd28b34a
SP
45 echo foo >xfoo2 &&
46 chmod 755 xfoo2 &&
5be60078 47 git update-index --add xfoo2 &&
ed6c2314 48 case "$(git ls-files --stage xfoo2)" in
335f8787 49 100644" "*xfoo2) echo pass;;
5be60078 50 *) echo fail; git ls-files --stage xfoo2; (exit 1);;
2bbaaed9
JH
51 esac'
52
889c6f0e 53test_expect_success 'git add: filemode=0 should not get confused by symlink' '
185c975f 54 rm -f xfoo2 &&
889c6f0e 55 test_ln_s_add foo xfoo2 &&
ed6c2314 56 case "$(git ls-files --stage xfoo2)" in
335f8787 57 120000" "*xfoo2) echo pass;;
5be60078 58 *) echo fail; git ls-files --stage xfoo2; (exit 1);;
185c975f
JH
59 esac
60'
61
889c6f0e 62test_expect_success \
5be60078 63 'git update-index --add: Test that executable bit is not used...' \
e0d10e1c 64 'git config core.filemode 0 &&
889c6f0e 65 test_ln_s_add xfoo2 xfoo3 && # runs git update-index --add
ed6c2314 66 case "$(git ls-files --stage xfoo3)" in
335f8787 67 120000" "*xfoo3) echo pass;;
5be60078 68 *) echo fail; git ls-files --stage xfoo3; (exit 1);;
fd28b34a
SP
69 esac'
70
4d06f8ac
JH
71test_expect_success '.gitignore test setup' '
72 echo "*.ig" >.gitignore &&
73 mkdir c.if d.ig &&
74 >a.ig && >b.if &&
75 >c.if/c.if && >c.if/c.ig &&
76 >d.ig/d.if && >d.ig/d.ig
77'
78
79test_expect_success '.gitignore is honored' '
5be60078 80 git add . &&
bbf08124 81 ! (git ls-files | grep "\\.ig")
4d06f8ac
JH
82'
83
84test_expect_success 'error out when attempting to add ignored ones without -f' '
d492b31c 85 test_must_fail git add a.?? &&
bbf08124 86 ! (git ls-files | grep "\\.ig")
4d06f8ac
JH
87'
88
89test_expect_success 'error out when attempting to add ignored ones without -f' '
d492b31c 90 test_must_fail git add d.?? &&
bbf08124 91 ! (git ls-files | grep "\\.ig")
4d06f8ac
JH
92'
93
1d31e5a2
MG
94test_expect_success 'error out when attempting to add ignored ones but add others' '
95 touch a.if &&
96 test_must_fail git add a.?? &&
97 ! (git ls-files | grep "\\.ig") &&
98 (git ls-files | grep a.if)
99'
100
4d06f8ac 101test_expect_success 'add ignored ones with -f' '
5be60078
JH
102 git add -f a.?? &&
103 git ls-files --error-unmatch a.ig
4d06f8ac
JH
104'
105
106test_expect_success 'add ignored ones with -f' '
5be60078
JH
107 git add -f d.??/* &&
108 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
4d06f8ac
JH
109'
110
41a7aa58
JH
111test_expect_success 'add ignored ones with -f' '
112 rm -f .git/index &&
113 git add -f d.?? &&
114 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
115'
116
117test_expect_success '.gitignore with subdirectory' '
118
119 rm -f .git/index &&
120 mkdir -p sub/dir &&
121 echo "!dir/a.*" >sub/.gitignore &&
122 >sub/a.ig &&
123 >sub/dir/a.ig &&
124 git add sub/dir &&
125 git ls-files --error-unmatch sub/dir/a.ig &&
126 rm -f .git/index &&
127 (
128 cd sub/dir &&
129 git add .
130 ) &&
131 git ls-files --error-unmatch sub/dir/a.ig
132'
133
c7f34c18
JS
134mkdir 1 1/2 1/3
135touch 1/2/a 1/3/b 1/2/c
136test_expect_success 'check correct prefix detection' '
41a7aa58 137 rm -f .git/index &&
c7f34c18
JS
138 git add 1/2/a 1/3/b 1/2/c
139'
140
05dcd698 141test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries' '
20314271
JS
142 for s in 1 2 3
143 do
05dcd698 144 echo $s > stage$s
20314271 145 echo "100755 $(git hash-object -w stage$s) $s file"
05dcd698 146 echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink"
20314271
JS
147 done | git update-index --index-info &&
148 git config core.filemode 0 &&
05dcd698 149 git config core.symlinks 0 &&
20314271 150 echo new > file &&
05dcd698
JS
151 echo new > symlink &&
152 git add file symlink &&
153 git ls-files --stage | grep "^100755 .* 0 file$" &&
154 git ls-files --stage | grep "^120000 .* 0 symlink$"
20314271
JS
155'
156
05dcd698
JS
157test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
158 git rm --cached -f file symlink &&
20314271
JS
159 (
160 echo "100644 $(git hash-object -w stage1) 1 file"
161 echo "100755 $(git hash-object -w stage2) 2 file"
a697ec69
JH
162 echo "100644 $(printf 1 | git hash-object -w -t blob --stdin) 1 symlink"
163 echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
20314271
JS
164 ) | git update-index --index-info &&
165 git config core.filemode 0 &&
05dcd698 166 git config core.symlinks 0 &&
20314271 167 echo new > file &&
05dcd698
JS
168 echo new > symlink &&
169 git add file symlink &&
170 git ls-files --stage | grep "^100755 .* 0 file$" &&
171 git ls-files --stage | grep "^120000 .* 0 symlink$"
20314271
JS
172'
173
d616813d
AJ
174test_expect_success 'git add --refresh' '
175 >foo && git add foo && git commit -a -m "commit all" &&
ed6c2314 176 test -z "$(git diff-index HEAD -- foo)" &&
d616813d 177 git read-tree HEAD &&
ed6c2314 178 case "$(git diff-index HEAD -- foo)" in
335f8787 179 :100644" "*"M foo") echo pass;;
d616813d
AJ
180 *) echo fail; (exit 1);;
181 esac &&
182 git add --refresh -- foo &&
ed6c2314 183 test -z "$(git diff-index HEAD -- foo)"
d616813d
AJ
184'
185
3d1f148c
JH
186test_expect_success 'git add --refresh with pathspec' '
187 git reset --hard &&
188 echo >foo && echo >bar && echo >baz &&
189 git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
190 echo "100644 $H 3 foo" | git update-index --index-info &&
191 test-chmtime -60 bar baz &&
192 >expect &&
193 git add --refresh bar >actual &&
194 test_cmp expect actual &&
195
196 git diff-files --name-only >actual &&
197 ! grep bar actual&&
198 grep baz actual
199'
200
c91cfd19 201test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unreadable file' '
89597436
AR
202 git reset --hard &&
203 date >foo1 &&
204 date >foo2 &&
205 chmod 0 foo2 &&
206 test_must_fail git add --verbose . &&
207 ! ( git ls-files foo1 | grep foo1 )
208'
209
210rm -f foo2
211
c91cfd19 212test_expect_success POSIXPERM,SANITY 'git add --ignore-errors' '
89597436
AR
213 git reset --hard &&
214 date >foo1 &&
215 date >foo2 &&
216 chmod 0 foo2 &&
217 test_must_fail git add --verbose --ignore-errors . &&
218 git ls-files foo1 | grep foo1
219'
220
221rm -f foo2
222
c91cfd19 223test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors)' '
dad25e4a
AR
224 git config add.ignore-errors 1 &&
225 git reset --hard &&
226 date >foo1 &&
227 date >foo2 &&
228 chmod 0 foo2 &&
229 test_must_fail git add --verbose . &&
230 git ls-files foo1 | grep foo1
231'
232rm -f foo2
233
c91cfd19 234test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' '
dad25e4a
AR
235 git config add.ignore-errors 0 &&
236 git reset --hard &&
237 date >foo1 &&
238 date >foo2 &&
239 chmod 0 foo2 &&
240 test_must_fail git add --verbose . &&
241 ! ( git ls-files foo1 | grep foo1 )
242'
ed342fde
SB
243rm -f foo2
244
c91cfd19 245test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' '
ed342fde
SB
246 git config add.ignore-errors 1 &&
247 git reset --hard &&
248 date >foo1 &&
249 date >foo2 &&
250 chmod 0 foo2 &&
251 test_must_fail git add --verbose --no-ignore-errors . &&
252 ! ( git ls-files foo1 | grep foo1 ) &&
253 git config add.ignore-errors 0
254'
255rm -f foo2
dad25e4a 256
6fd1106a 257test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
ea335b56 258 git reset --hard &&
8134a003
AR
259 touch fo\[ou\]bar foobar &&
260 git add '\''fo\[ou\]bar'\'' &&
87539416 261 git ls-files fo\[ou\]bar | fgrep fo\[ou\]bar &&
ea335b56
KB
262 ! ( git ls-files foobar | grep foobar )
263'
264
6e4f981f
JK
265test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
266 git reset --hard &&
267 H=$(git rev-parse :1/2/a) &&
268 (
269 echo "100644 $H 1 track-this"
270 echo "100644 $H 3 track-this"
271 ) | git update-index --index-info &&
272 echo track-this >>.gitignore &&
273 echo resolved >track-this &&
274 git add track-this
275'
276
1e7ef746
CP
277test_expect_success '"add non-existent" should fail' '
278 test_must_fail git add non-existent &&
279 ! (git ls-files | grep "non-existent")
280'
281
64ed07ce
NTND
282test_expect_success 'git add -A on empty repo does not error out' '
283 rm -fr empty &&
284 git init empty &&
285 (
286 cd empty &&
287 git add -A . &&
288 git add -A
289 )
290'
291
292test_expect_success '"git add ." in empty repo' '
293 rm -fr empty &&
294 git init empty &&
295 (
296 cd empty &&
297 git add .
298 )
299'
300
108da0db
JL
301test_expect_success 'git add --dry-run of existing changed file' "
302 echo new >>track-this &&
303 git add --dry-run track-this >actual 2>&1 &&
304 echo \"add 'track-this'\" | test_cmp - actual
305"
306
307test_expect_success 'git add --dry-run of non-existing file' "
308 echo ignored-file >>.gitignore &&
48168851
ÆAB
309 test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
310"
311
68b2a005 312test_expect_success 'git add --dry-run of an existing file output' "
48168851 313 echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
68b2a005 314 test_i18ncmp expect actual
108da0db
JL
315"
316
c1e02b2b 317cat >expect.err <<\EOF
108da0db
JL
318The following paths are ignored by one of your .gitignore files:
319ignored-file
320Use -f if you really want to add them.
c1e02b2b
JS
321EOF
322cat >expect.out <<\EOF
108da0db
JL
323add 'track-this'
324EOF
325
326test_expect_success 'git add --dry-run --ignore-missing of non-existing file' '
439fb829
ÆAB
327 test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
328'
329
68b2a005
JH
330test_expect_success 'git add --dry-run --ignore-missing of non-existing file output' '
331 test_i18ncmp expect.out actual.out &&
332 test_i18ncmp expect.err actual.err
108da0db
JL
333'
334
4e55ed32 335test_expect_success 'git add --chmod=+x stages a non-executable file with +x' '
c0fa44d8 336 rm -f foo1 &&
4e55ed32
ET
337 echo foo >foo1 &&
338 git add --chmod=+x foo1 &&
339 case "$(git ls-files --stage foo1)" in
340 100755" "*foo1) echo pass;;
341 *) echo fail; git ls-files --stage foo1; (exit 1);;
342 esac
343'
344
345test_expect_success 'git add --chmod=-x stages an executable file with -x' '
c0fa44d8 346 rm -f xfoo1 &&
4e55ed32
ET
347 echo foo >xfoo1 &&
348 chmod 755 xfoo1 &&
349 git add --chmod=-x xfoo1 &&
350 case "$(git ls-files --stage xfoo1)" in
351 100644" "*xfoo1) echo pass;;
352 *) echo fail; git ls-files --stage xfoo1; (exit 1);;
353 esac
354'
355
356test_expect_success POSIXPERM,SYMLINKS 'git add --chmod=+x with symlinks' '
357 git config core.filemode 1 &&
358 git config core.symlinks 1 &&
c0fa44d8 359 rm -f foo2 &&
4e55ed32
ET
360 echo foo >foo2 &&
361 git add --chmod=+x foo2 &&
362 case "$(git ls-files --stage foo2)" in
363 100755" "*foo2) echo pass;;
364 *) echo fail; git ls-files --stage foo2; (exit 1);;
365 esac
366'
367
60ace879 368test_done