blob: f3a4b4a913f344ce140344ec7b70482a6d36bcbe [file] [log] [blame]
Carl Worth60ace872006-02-21 15:33:49 -08001#!/bin/sh
2#
3# Copyright (c) 2006 Carl D. Worth
4#
5
Junio C Hamano5be60072007-07-02 22:52:14 -07006test_description='Test of git add, including the -- option.'
Carl Worth60ace872006-02-21 15:33:49 -08007
8. ./test-lib.sh
9
Ingo Brückl766cdc42016-07-30 22:13:54 +020010# Test the file mode "$1" of the file "$2" in the index.
11test_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
Carl Worth60ace872006-02-21 15:33:49 -080024test_expect_success \
Junio C Hamano5be60072007-07-02 22:52:14 -070025 'Test of git add' \
26 'touch foo && git add foo'
Carl Worth60ace872006-02-21 15:33:49 -080027
28test_expect_success \
29 'Post-check that foo is in the index' \
Junio C Hamano5be60072007-07-02 22:52:14 -070030 'git ls-files foo | grep foo'
Carl Worth60ace872006-02-21 15:33:49 -080031
32test_expect_success \
Junio C Hamano5be60072007-07-02 22:52:14 -070033 'Test that "git add -- -q" works' \
34 'touch -- -q && git add -- -q'
Carl Worth60ace872006-02-21 15:33:49 -080035
Shawn Pearcefd28b342006-09-27 01:21:19 -040036test_expect_success \
Junio C Hamano5be60072007-07-02 22:52:14 -070037 'git add: Test that executable bit is not used if core.filemode=0' \
Tom Princee0d10e12007-01-28 16:16:53 -080038 'git config core.filemode 0 &&
Shawn Pearcefd28b342006-09-27 01:21:19 -040039 echo foo >xfoo1 &&
40 chmod 755 xfoo1 &&
Junio C Hamano5be60072007-07-02 22:52:14 -070041 git add xfoo1 &&
Ingo Brückl766cdc42016-07-30 22:13:54 +020042 test_mode_in_index 100644 xfoo1'
Shawn Pearcefd28b342006-09-27 01:21:19 -040043
Johannes Sixt889c6f02013-06-07 22:53:28 +020044test_expect_success 'git add: filemode=0 should not get confused by symlink' '
Junio C Hamano185c9752007-02-16 22:43:48 -080045 rm -f xfoo1 &&
Johannes Sixt889c6f02013-06-07 22:53:28 +020046 test_ln_s_add foo xfoo1 &&
Ingo Brückl766cdc42016-07-30 22:13:54 +020047 test_mode_in_index 120000 xfoo1
Junio C Hamano185c9752007-02-16 22:43:48 -080048'
49
Shawn Pearcefd28b342006-09-27 01:21:19 -040050test_expect_success \
Junio C Hamano5be60072007-07-02 22:52:14 -070051 'git update-index --add: Test that executable bit is not used...' \
Tom Princee0d10e12007-01-28 16:16:53 -080052 'git config core.filemode 0 &&
Shawn Pearcefd28b342006-09-27 01:21:19 -040053 echo foo >xfoo2 &&
54 chmod 755 xfoo2 &&
Junio C Hamano5be60072007-07-02 22:52:14 -070055 git update-index --add xfoo2 &&
Ingo Brückl766cdc42016-07-30 22:13:54 +020056 test_mode_in_index 100644 xfoo2'
Junio C Hamano2bbaaed2006-11-22 16:33:32 -080057
Johannes Sixt889c6f02013-06-07 22:53:28 +020058test_expect_success 'git add: filemode=0 should not get confused by symlink' '
Junio C Hamano185c9752007-02-16 22:43:48 -080059 rm -f xfoo2 &&
Johannes Sixt889c6f02013-06-07 22:53:28 +020060 test_ln_s_add foo xfoo2 &&
Ingo Brückl766cdc42016-07-30 22:13:54 +020061 test_mode_in_index 120000 xfoo2
Junio C Hamano185c9752007-02-16 22:43:48 -080062'
63
Johannes Sixt889c6f02013-06-07 22:53:28 +020064test_expect_success \
Junio C Hamano5be60072007-07-02 22:52:14 -070065 'git update-index --add: Test that executable bit is not used...' \
Tom Princee0d10e12007-01-28 16:16:53 -080066 'git config core.filemode 0 &&
Johannes Sixt889c6f02013-06-07 22:53:28 +020067 test_ln_s_add xfoo2 xfoo3 && # runs git update-index --add
Ingo Brückl766cdc42016-07-30 22:13:54 +020068 test_mode_in_index 120000 xfoo3'
Shawn Pearcefd28b342006-09-27 01:21:19 -040069
Junio C Hamano4d06f8a2006-12-29 11:01:31 -080070test_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
78test_expect_success '.gitignore is honored' '
Junio C Hamano5be60072007-07-02 22:52:14 -070079 git add . &&
Jeff Kingbbf08122008-05-14 00:01:22 -040080 ! (git ls-files | grep "\\.ig")
Junio C Hamano4d06f8a2006-12-29 11:01:31 -080081'
82
83test_expect_success 'error out when attempting to add ignored ones without -f' '
Stephan Beyerd492b312008-07-12 17:47:52 +020084 test_must_fail git add a.?? &&
Jeff Kingbbf08122008-05-14 00:01:22 -040085 ! (git ls-files | grep "\\.ig")
Junio C Hamano4d06f8a2006-12-29 11:01:31 -080086'
87
88test_expect_success 'error out when attempting to add ignored ones without -f' '
Stephan Beyerd492b312008-07-12 17:47:52 +020089 test_must_fail git add d.?? &&
Jeff Kingbbf08122008-05-14 00:01:22 -040090 ! (git ls-files | grep "\\.ig")
Junio C Hamano4d06f8a2006-12-29 11:01:31 -080091'
92
Michael J Gruber1d31e5a2014-11-21 17:08:19 +010093test_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
Junio C Hamano4d06f8a2006-12-29 11:01:31 -0800100test_expect_success 'add ignored ones with -f' '
Junio C Hamano5be60072007-07-02 22:52:14 -0700101 git add -f a.?? &&
102 git ls-files --error-unmatch a.ig
Junio C Hamano4d06f8a2006-12-29 11:01:31 -0800103'
104
105test_expect_success 'add ignored ones with -f' '
Junio C Hamano5be60072007-07-02 22:52:14 -0700106 git add -f d.??/* &&
107 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
Junio C Hamano4d06f8a2006-12-29 11:01:31 -0800108'
109
Junio C Hamano41a7aa52007-11-16 01:15:41 -0800110test_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
116test_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
Johannes Schindelinc7f34c12007-04-23 10:21:25 +0200133mkdir 1 1/2 1/3
134touch 1/2/a 1/3/b 1/2/c
135test_expect_success 'check correct prefix detection' '
Junio C Hamano41a7aa52007-11-16 01:15:41 -0800136 rm -f .git/index &&
Johannes Schindelinc7f34c12007-04-23 10:21:25 +0200137 git add 1/2/a 1/3/b 1/2/c
138'
139
Johannes Sixt05dcd692007-07-02 13:28:42 +0200140test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries' '
Johannes Schindelin20314272007-06-29 18:32:46 +0100141 for s in 1 2 3
142 do
Johannes Sixt05dcd692007-07-02 13:28:42 +0200143 echo $s > stage$s
Johannes Schindelin20314272007-06-29 18:32:46 +0100144 echo "100755 $(git hash-object -w stage$s) $s file"
Johannes Sixt05dcd692007-07-02 13:28:42 +0200145 echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink"
Johannes Schindelin20314272007-06-29 18:32:46 +0100146 done | git update-index --index-info &&
147 git config core.filemode 0 &&
Johannes Sixt05dcd692007-07-02 13:28:42 +0200148 git config core.symlinks 0 &&
Johannes Schindelin20314272007-06-29 18:32:46 +0100149 echo new > file &&
Johannes Sixt05dcd692007-07-02 13:28:42 +0200150 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$"
Johannes Schindelin20314272007-06-29 18:32:46 +0100154'
155
Johannes Sixt05dcd692007-07-02 13:28:42 +0200156test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
157 git rm --cached -f file symlink &&
Johannes Schindelin20314272007-06-29 18:32:46 +0100158 (
159 echo "100644 $(git hash-object -w stage1) 1 file"
160 echo "100755 $(git hash-object -w stage2) 2 file"
Junio C Hamanoa697ec62007-08-03 14:31:47 -0700161 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"
Johannes Schindelin20314272007-06-29 18:32:46 +0100163 ) | git update-index --index-info &&
164 git config core.filemode 0 &&
Johannes Sixt05dcd692007-07-02 13:28:42 +0200165 git config core.symlinks 0 &&
Johannes Schindelin20314272007-06-29 18:32:46 +0100166 echo new > file &&
Johannes Sixt05dcd692007-07-02 13:28:42 +0200167 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$"
Johannes Schindelin20314272007-06-29 18:32:46 +0100171'
172
Alexandre Julliardd6168132007-08-11 23:59:01 +0200173test_expect_success 'git add --refresh' '
174 >foo && git add foo && git commit -a -m "commit all" &&
Elia Pintoed6c2312015-12-22 16:27:49 +0100175 test -z "$(git diff-index HEAD -- foo)" &&
Alexandre Julliardd6168132007-08-11 23:59:01 +0200176 git read-tree HEAD &&
Elia Pintoed6c2312015-12-22 16:27:49 +0100177 case "$(git diff-index HEAD -- foo)" in
Ævar Arnfjörð Bjarmason335f8782010-06-24 17:44:49 +0000178 :100644" "*"M foo") echo pass;;
Alexandre Julliardd6168132007-08-11 23:59:01 +0200179 *) echo fail; (exit 1);;
180 esac &&
181 git add --refresh -- foo &&
Elia Pintoed6c2312015-12-22 16:27:49 +0100182 test -z "$(git diff-index HEAD -- foo)"
Alexandre Julliardd6168132007-08-11 23:59:01 +0200183'
184
Junio C Hamano3d1f1482012-02-17 10:11:05 -0800185test_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-chmtime -60 bar baz &&
191 >expect &&
192 git add --refresh bar >actual &&
193 test_cmp expect actual &&
194
195 git diff-files --name-only >actual &&
196 ! grep bar actual&&
197 grep baz actual
198'
199
Ævar Arnfjörð Bjarmasonc91cfd12010-08-06 22:09:09 +0000200test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unreadable file' '
Alex Riesen89597432008-05-12 19:58:48 +0200201 git reset --hard &&
202 date >foo1 &&
203 date >foo2 &&
204 chmod 0 foo2 &&
205 test_must_fail git add --verbose . &&
206 ! ( git ls-files foo1 | grep foo1 )
207'
208
209rm -f foo2
210
Ævar Arnfjörð Bjarmasonc91cfd12010-08-06 22:09:09 +0000211test_expect_success POSIXPERM,SANITY 'git add --ignore-errors' '
Alex Riesen89597432008-05-12 19:58:48 +0200212 git reset --hard &&
213 date >foo1 &&
214 date >foo2 &&
215 chmod 0 foo2 &&
216 test_must_fail git add --verbose --ignore-errors . &&
217 git ls-files foo1 | grep foo1
218'
219
220rm -f foo2
221
Ævar Arnfjörð Bjarmasonc91cfd12010-08-06 22:09:09 +0000222test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors)' '
Alex Riesendad25e42008-05-12 19:59:23 +0200223 git config add.ignore-errors 1 &&
224 git reset --hard &&
225 date >foo1 &&
226 date >foo2 &&
227 chmod 0 foo2 &&
228 test_must_fail git add --verbose . &&
229 git ls-files foo1 | grep foo1
230'
231rm -f foo2
232
Ævar Arnfjörð Bjarmasonc91cfd12010-08-06 22:09:09 +0000233test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' '
Alex Riesendad25e42008-05-12 19:59:23 +0200234 git config add.ignore-errors 0 &&
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 )
241'
Stephen Boyded342fd2009-06-18 02:17:54 -0700242rm -f foo2
243
Ævar Arnfjörð Bjarmasonc91cfd12010-08-06 22:09:09 +0000244test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' '
Stephen Boyded342fd2009-06-18 02:17:54 -0700245 git config add.ignore-errors 1 &&
246 git reset --hard &&
247 date >foo1 &&
248 date >foo2 &&
249 chmod 0 foo2 &&
250 test_must_fail git add --verbose --no-ignore-errors . &&
251 ! ( git ls-files foo1 | grep foo1 ) &&
252 git config add.ignore-errors 0
253'
254rm -f foo2
Alex Riesendad25e42008-05-12 19:59:23 +0200255
Johannes Sixt6fd11062009-03-13 23:00:15 +0100256test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
Kevin Ballardea335b52008-08-13 15:34:34 -0700257 git reset --hard &&
Alex Riesen8134a002008-08-15 09:32:30 +0200258 touch fo\[ou\]bar foobar &&
259 git add '\''fo\[ou\]bar'\'' &&
Jeff King87539412008-09-30 04:03:55 -0400260 git ls-files fo\[ou\]bar | fgrep fo\[ou\]bar &&
Kevin Ballardea335b52008-08-13 15:34:34 -0700261 ! ( git ls-files foobar | grep foobar )
262'
263
Jeff King6e4f9812009-05-30 17:54:18 -0400264test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
265 git reset --hard &&
266 H=$(git rev-parse :1/2/a) &&
267 (
268 echo "100644 $H 1 track-this"
269 echo "100644 $H 3 track-this"
270 ) | git update-index --index-info &&
271 echo track-this >>.gitignore &&
272 echo resolved >track-this &&
273 git add track-this
274'
275
Chris Packham1e7ef742010-02-09 17:30:48 -0500276test_expect_success '"add non-existent" should fail' '
277 test_must_fail git add non-existent &&
278 ! (git ls-files | grep "non-existent")
279'
280
Nguyễn Thái Ngọc Duy64ed07c2013-12-23 16:02:41 +0700281test_expect_success 'git add -A on empty repo does not error out' '
282 rm -fr empty &&
283 git init empty &&
284 (
285 cd empty &&
286 git add -A . &&
287 git add -A
288 )
289'
290
291test_expect_success '"git add ." in empty repo' '
292 rm -fr empty &&
293 git init empty &&
294 (
295 cd empty &&
296 git add .
297 )
298'
299
Jens Lehmann108da0d2010-07-10 00:18:38 +0200300test_expect_success 'git add --dry-run of existing changed file' "
301 echo new >>track-this &&
302 git add --dry-run track-this >actual 2>&1 &&
303 echo \"add 'track-this'\" | test_cmp - actual
304"
305
306test_expect_success 'git add --dry-run of non-existing file' "
307 echo ignored-file >>.gitignore &&
Ævar Arnfjörð Bjarmason48168852011-02-22 23:41:31 +0000308 test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
309"
310
Junio C Hamano68b2a002011-04-12 16:27:11 -0700311test_expect_success 'git add --dry-run of an existing file output' "
Ævar Arnfjörð Bjarmason48168852011-02-22 23:41:31 +0000312 echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
Junio C Hamano68b2a002011-04-12 16:27:11 -0700313 test_i18ncmp expect actual
Jens Lehmann108da0d2010-07-10 00:18:38 +0200314"
315
Johannes Sixtc1e02b22010-07-22 10:13:33 +0200316cat >expect.err <<\EOF
Jens Lehmann108da0d2010-07-10 00:18:38 +0200317The following paths are ignored by one of your .gitignore files:
318ignored-file
319Use -f if you really want to add them.
Johannes Sixtc1e02b22010-07-22 10:13:33 +0200320EOF
321cat >expect.out <<\EOF
Jens Lehmann108da0d2010-07-10 00:18:38 +0200322add 'track-this'
323EOF
324
325test_expect_success 'git add --dry-run --ignore-missing of non-existing file' '
Ævar Arnfjörð Bjarmason439fb822011-02-22 23:41:30 +0000326 test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
327'
328
Junio C Hamano68b2a002011-04-12 16:27:11 -0700329test_expect_success 'git add --dry-run --ignore-missing of non-existing file output' '
330 test_i18ncmp expect.out actual.out &&
331 test_i18ncmp expect.err actual.err
Jens Lehmann108da0d2010-07-10 00:18:38 +0200332'
333
Emily Xied4264302016-06-22 19:00:24 -0400334test_expect_success 'git add empty string should invoke warning' '
335 git add "" 2>output &&
336 test_i18ngrep "warning: empty strings" output
337'
338
Ingo Brücklb38ab192016-07-30 22:13:48 +0200339test_expect_success 'git add --chmod=[+-]x stages correctly' '
Ingo Brücklc0fa44d2016-07-30 22:13:38 +0200340 rm -f foo1 &&
Edward Thomson4e55ed32016-05-31 17:08:18 -0500341 echo foo >foo1 &&
342 git add --chmod=+x foo1 &&
Ingo Brückl766cdc42016-07-30 22:13:54 +0200343 test_mode_in_index 100755 foo1 &&
Ingo Brücklb38ab192016-07-30 22:13:48 +0200344 git add --chmod=-x foo1 &&
Ingo Brückl766cdc42016-07-30 22:13:54 +0200345 test_mode_in_index 100644 foo1
Edward Thomson4e55ed32016-05-31 17:08:18 -0500346'
347
348test_expect_success POSIXPERM,SYMLINKS 'git add --chmod=+x with symlinks' '
349 git config core.filemode 1 &&
350 git config core.symlinks 1 &&
Ingo Brücklc0fa44d2016-07-30 22:13:38 +0200351 rm -f foo2 &&
Edward Thomson4e55ed32016-05-31 17:08:18 -0500352 echo foo >foo2 &&
353 git add --chmod=+x foo2 &&
Ingo Brückl766cdc42016-07-30 22:13:54 +0200354 test_mode_in_index 100755 foo2
Edward Thomson4e55ed32016-05-31 17:08:18 -0500355'
356
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100357test_expect_success 'git add --chmod=[+-]x changes index with already added file' '
Junio C Hamano76e368c2016-10-10 10:41:51 -0700358 rm -f foo3 xfoo3 &&
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100359 echo foo >foo3 &&
360 git add foo3 &&
361 git add --chmod=+x foo3 &&
362 test_mode_in_index 100755 foo3 &&
363 echo foo >xfoo3 &&
364 chmod 755 xfoo3 &&
365 git add xfoo3 &&
366 git add --chmod=-x xfoo3 &&
367 test_mode_in_index 100644 xfoo3
368'
369
Johannes Sixt40e0dc12016-09-20 08:18:25 +0200370test_expect_success POSIXPERM 'git add --chmod=[+-]x does not change the working tree' '
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100371 echo foo >foo4 &&
372 git add foo4 &&
373 git add --chmod=+x foo4 &&
Johannes Sixt40e0dc12016-09-20 08:18:25 +0200374 ! test -x foo4
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100375'
376
377test_expect_success 'no file status change if no pathspec is given' '
378 >foo5 &&
379 >foo6 &&
380 git add foo5 foo6 &&
381 git add --chmod=+x &&
382 test_mode_in_index 100644 foo5 &&
383 test_mode_in_index 100644 foo6
384'
385
386test_expect_success 'no file status change if no pathspec is given in subdir' '
Johannes Sixtb07ad462016-09-20 08:16:11 +0200387 mkdir -p sub &&
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100388 (
389 cd sub &&
390 >sub-foo1 &&
391 >sub-foo2 &&
392 git add . &&
393 git add --chmod=+x &&
394 test_mode_in_index 100644 sub-foo1 &&
395 test_mode_in_index 100644 sub-foo2
396 )
397'
398
399test_expect_success 'all statuses changed in folder if . is given' '
400 git add --chmod=+x . &&
401 test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
402 git add --chmod=-x . &&
403 test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
404'
405
Carl Worth60ace872006-02-21 15:33:49 -0800406test_done