blob: aab86e838b806f9bb2289f536f4ba9324d75da0a [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
10test_expect_success \
Junio C Hamano5be60072007-07-02 22:52:14 -070011 'Test of git add' \
12 'touch foo && git add foo'
Carl Worth60ace872006-02-21 15:33:49 -080013
14test_expect_success \
15 'Post-check that foo is in the index' \
Junio C Hamano5be60072007-07-02 22:52:14 -070016 'git ls-files foo | grep foo'
Carl Worth60ace872006-02-21 15:33:49 -080017
18test_expect_success \
Junio C Hamano5be60072007-07-02 22:52:14 -070019 'Test that "git add -- -q" works' \
20 'touch -- -q && git add -- -q'
Carl Worth60ace872006-02-21 15:33:49 -080021
Shawn Pearcefd28b342006-09-27 01:21:19 -040022test_expect_success \
Junio C Hamano5be60072007-07-02 22:52:14 -070023 'git add: Test that executable bit is not used if core.filemode=0' \
Tom Princee0d10e12007-01-28 16:16:53 -080024 'git config core.filemode 0 &&
Shawn Pearcefd28b342006-09-27 01:21:19 -040025 echo foo >xfoo1 &&
26 chmod 755 xfoo1 &&
Junio C Hamano5be60072007-07-02 22:52:14 -070027 git add xfoo1 &&
28 case "`git ls-files --stage xfoo1`" in
Ævar Arnfjörð Bjarmason335f8782010-06-24 17:44:49 +000029 100644" "*xfoo1) echo pass;;
Junio C Hamano5be60072007-07-02 22:52:14 -070030 *) echo fail; git ls-files --stage xfoo1; (exit 1);;
Shawn Pearcefd28b342006-09-27 01:21:19 -040031 esac'
32
Johannes Sixt889c6f02013-06-07 22:53:28 +020033test_expect_success 'git add: filemode=0 should not get confused by symlink' '
Junio C Hamano185c9752007-02-16 22:43:48 -080034 rm -f xfoo1 &&
Johannes Sixt889c6f02013-06-07 22:53:28 +020035 test_ln_s_add foo xfoo1 &&
Junio C Hamano5be60072007-07-02 22:52:14 -070036 case "`git ls-files --stage xfoo1`" in
Ævar Arnfjörð Bjarmason335f8782010-06-24 17:44:49 +000037 120000" "*xfoo1) echo pass;;
Junio C Hamano5be60072007-07-02 22:52:14 -070038 *) echo fail; git ls-files --stage xfoo1; (exit 1);;
Junio C Hamano185c9752007-02-16 22:43:48 -080039 esac
40'
41
Shawn Pearcefd28b342006-09-27 01:21:19 -040042test_expect_success \
Junio C Hamano5be60072007-07-02 22:52:14 -070043 'git update-index --add: Test that executable bit is not used...' \
Tom Princee0d10e12007-01-28 16:16:53 -080044 'git config core.filemode 0 &&
Shawn Pearcefd28b342006-09-27 01:21:19 -040045 echo foo >xfoo2 &&
46 chmod 755 xfoo2 &&
Junio C Hamano5be60072007-07-02 22:52:14 -070047 git update-index --add xfoo2 &&
48 case "`git ls-files --stage xfoo2`" in
Ævar Arnfjörð Bjarmason335f8782010-06-24 17:44:49 +000049 100644" "*xfoo2) echo pass;;
Junio C Hamano5be60072007-07-02 22:52:14 -070050 *) echo fail; git ls-files --stage xfoo2; (exit 1);;
Junio C Hamano2bbaaed2006-11-22 16:33:32 -080051 esac'
52
Johannes Sixt889c6f02013-06-07 22:53:28 +020053test_expect_success 'git add: filemode=0 should not get confused by symlink' '
Junio C Hamano185c9752007-02-16 22:43:48 -080054 rm -f xfoo2 &&
Johannes Sixt889c6f02013-06-07 22:53:28 +020055 test_ln_s_add foo xfoo2 &&
Junio C Hamano5be60072007-07-02 22:52:14 -070056 case "`git ls-files --stage xfoo2`" in
Ævar Arnfjörð Bjarmason335f8782010-06-24 17:44:49 +000057 120000" "*xfoo2) echo pass;;
Junio C Hamano5be60072007-07-02 22:52:14 -070058 *) echo fail; git ls-files --stage xfoo2; (exit 1);;
Junio C Hamano185c9752007-02-16 22:43:48 -080059 esac
60'
61
Johannes Sixt889c6f02013-06-07 22:53:28 +020062test_expect_success \
Junio C Hamano5be60072007-07-02 22:52:14 -070063 'git update-index --add: Test that executable bit is not used...' \
Tom Princee0d10e12007-01-28 16:16:53 -080064 'git config core.filemode 0 &&
Johannes Sixt889c6f02013-06-07 22:53:28 +020065 test_ln_s_add xfoo2 xfoo3 && # runs git update-index --add
Junio C Hamano5be60072007-07-02 22:52:14 -070066 case "`git ls-files --stage xfoo3`" in
Ævar Arnfjörð Bjarmason335f8782010-06-24 17:44:49 +000067 120000" "*xfoo3) echo pass;;
Junio C Hamano5be60072007-07-02 22:52:14 -070068 *) echo fail; git ls-files --stage xfoo3; (exit 1);;
Shawn Pearcefd28b342006-09-27 01:21:19 -040069 esac'
70
Junio C Hamano4d06f8a2006-12-29 11:01:31 -080071test_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' '
Junio C Hamano5be60072007-07-02 22:52:14 -070080 git add . &&
Jeff Kingbbf08122008-05-14 00:01:22 -040081 ! (git ls-files | grep "\\.ig")
Junio C Hamano4d06f8a2006-12-29 11:01:31 -080082'
83
84test_expect_success 'error out when attempting to add ignored ones without -f' '
Stephan Beyerd492b312008-07-12 17:47:52 +020085 test_must_fail git add a.?? &&
Jeff Kingbbf08122008-05-14 00:01:22 -040086 ! (git ls-files | grep "\\.ig")
Junio C Hamano4d06f8a2006-12-29 11:01:31 -080087'
88
89test_expect_success 'error out when attempting to add ignored ones without -f' '
Stephan Beyerd492b312008-07-12 17:47:52 +020090 test_must_fail git add d.?? &&
Jeff Kingbbf08122008-05-14 00:01:22 -040091 ! (git ls-files | grep "\\.ig")
Junio C Hamano4d06f8a2006-12-29 11:01:31 -080092'
93
94test_expect_success 'add ignored ones with -f' '
Junio C Hamano5be60072007-07-02 22:52:14 -070095 git add -f a.?? &&
96 git ls-files --error-unmatch a.ig
Junio C Hamano4d06f8a2006-12-29 11:01:31 -080097'
98
99test_expect_success 'add ignored ones with -f' '
Junio C Hamano5be60072007-07-02 22:52:14 -0700100 git add -f d.??/* &&
101 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
Junio C Hamano4d06f8a2006-12-29 11:01:31 -0800102'
103
Junio C Hamano41a7aa52007-11-16 01:15:41 -0800104test_expect_success 'add ignored ones with -f' '
105 rm -f .git/index &&
106 git add -f d.?? &&
107 git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
108'
109
110test_expect_success '.gitignore with subdirectory' '
111
112 rm -f .git/index &&
113 mkdir -p sub/dir &&
114 echo "!dir/a.*" >sub/.gitignore &&
115 >sub/a.ig &&
116 >sub/dir/a.ig &&
117 git add sub/dir &&
118 git ls-files --error-unmatch sub/dir/a.ig &&
119 rm -f .git/index &&
120 (
121 cd sub/dir &&
122 git add .
123 ) &&
124 git ls-files --error-unmatch sub/dir/a.ig
125'
126
Johannes Schindelinc7f34c12007-04-23 10:21:25 +0200127mkdir 1 1/2 1/3
128touch 1/2/a 1/3/b 1/2/c
129test_expect_success 'check correct prefix detection' '
Junio C Hamano41a7aa52007-11-16 01:15:41 -0800130 rm -f .git/index &&
Johannes Schindelinc7f34c12007-04-23 10:21:25 +0200131 git add 1/2/a 1/3/b 1/2/c
132'
133
Johannes Sixt05dcd692007-07-02 13:28:42 +0200134test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries' '
Johannes Schindelin20314272007-06-29 18:32:46 +0100135 for s in 1 2 3
136 do
Johannes Sixt05dcd692007-07-02 13:28:42 +0200137 echo $s > stage$s
Johannes Schindelin20314272007-06-29 18:32:46 +0100138 echo "100755 $(git hash-object -w stage$s) $s file"
Johannes Sixt05dcd692007-07-02 13:28:42 +0200139 echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink"
Johannes Schindelin20314272007-06-29 18:32:46 +0100140 done | git update-index --index-info &&
141 git config core.filemode 0 &&
Johannes Sixt05dcd692007-07-02 13:28:42 +0200142 git config core.symlinks 0 &&
Johannes Schindelin20314272007-06-29 18:32:46 +0100143 echo new > file &&
Johannes Sixt05dcd692007-07-02 13:28:42 +0200144 echo new > symlink &&
145 git add file symlink &&
146 git ls-files --stage | grep "^100755 .* 0 file$" &&
147 git ls-files --stage | grep "^120000 .* 0 symlink$"
Johannes Schindelin20314272007-06-29 18:32:46 +0100148'
149
Johannes Sixt05dcd692007-07-02 13:28:42 +0200150test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
151 git rm --cached -f file symlink &&
Johannes Schindelin20314272007-06-29 18:32:46 +0100152 (
153 echo "100644 $(git hash-object -w stage1) 1 file"
154 echo "100755 $(git hash-object -w stage2) 2 file"
Junio C Hamanoa697ec62007-08-03 14:31:47 -0700155 echo "100644 $(printf 1 | git hash-object -w -t blob --stdin) 1 symlink"
156 echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
Johannes Schindelin20314272007-06-29 18:32:46 +0100157 ) | git update-index --index-info &&
158 git config core.filemode 0 &&
Johannes Sixt05dcd692007-07-02 13:28:42 +0200159 git config core.symlinks 0 &&
Johannes Schindelin20314272007-06-29 18:32:46 +0100160 echo new > file &&
Johannes Sixt05dcd692007-07-02 13:28:42 +0200161 echo new > symlink &&
162 git add file symlink &&
163 git ls-files --stage | grep "^100755 .* 0 file$" &&
164 git ls-files --stage | grep "^120000 .* 0 symlink$"
Johannes Schindelin20314272007-06-29 18:32:46 +0100165'
166
Alexandre Julliardd6168132007-08-11 23:59:01 +0200167test_expect_success 'git add --refresh' '
168 >foo && git add foo && git commit -a -m "commit all" &&
169 test -z "`git diff-index HEAD -- foo`" &&
170 git read-tree HEAD &&
171 case "`git diff-index HEAD -- foo`" in
Ævar Arnfjörð Bjarmason335f8782010-06-24 17:44:49 +0000172 :100644" "*"M foo") echo pass;;
Alexandre Julliardd6168132007-08-11 23:59:01 +0200173 *) echo fail; (exit 1);;
174 esac &&
175 git add --refresh -- foo &&
176 test -z "`git diff-index HEAD -- foo`"
177'
178
Junio C Hamano3d1f1482012-02-17 10:11:05 -0800179test_expect_success 'git add --refresh with pathspec' '
180 git reset --hard &&
181 echo >foo && echo >bar && echo >baz &&
182 git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
183 echo "100644 $H 3 foo" | git update-index --index-info &&
184 test-chmtime -60 bar baz &&
185 >expect &&
186 git add --refresh bar >actual &&
187 test_cmp expect actual &&
188
189 git diff-files --name-only >actual &&
190 ! grep bar actual&&
191 grep baz actual
192'
193
Ævar Arnfjörð Bjarmasonc91cfd12010-08-06 22:09:09 +0000194test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unreadable file' '
Alex Riesen89597432008-05-12 19:58:48 +0200195 git reset --hard &&
196 date >foo1 &&
197 date >foo2 &&
198 chmod 0 foo2 &&
199 test_must_fail git add --verbose . &&
200 ! ( git ls-files foo1 | grep foo1 )
201'
202
203rm -f foo2
204
Ævar Arnfjörð Bjarmasonc91cfd12010-08-06 22:09:09 +0000205test_expect_success POSIXPERM,SANITY 'git add --ignore-errors' '
Alex Riesen89597432008-05-12 19:58:48 +0200206 git reset --hard &&
207 date >foo1 &&
208 date >foo2 &&
209 chmod 0 foo2 &&
210 test_must_fail git add --verbose --ignore-errors . &&
211 git ls-files foo1 | grep foo1
212'
213
214rm -f foo2
215
Ævar Arnfjörð Bjarmasonc91cfd12010-08-06 22:09:09 +0000216test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors)' '
Alex Riesendad25e42008-05-12 19:59:23 +0200217 git config add.ignore-errors 1 &&
218 git reset --hard &&
219 date >foo1 &&
220 date >foo2 &&
221 chmod 0 foo2 &&
222 test_must_fail git add --verbose . &&
223 git ls-files foo1 | grep foo1
224'
225rm -f foo2
226
Ævar Arnfjörð Bjarmasonc91cfd12010-08-06 22:09:09 +0000227test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' '
Alex Riesendad25e42008-05-12 19:59:23 +0200228 git config add.ignore-errors 0 &&
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'
Stephen Boyded342fd2009-06-18 02:17:54 -0700236rm -f foo2
237
Ævar Arnfjörð Bjarmasonc91cfd12010-08-06 22:09:09 +0000238test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' '
Stephen Boyded342fd2009-06-18 02:17:54 -0700239 git config add.ignore-errors 1 &&
240 git reset --hard &&
241 date >foo1 &&
242 date >foo2 &&
243 chmod 0 foo2 &&
244 test_must_fail git add --verbose --no-ignore-errors . &&
245 ! ( git ls-files foo1 | grep foo1 ) &&
246 git config add.ignore-errors 0
247'
248rm -f foo2
Alex Riesendad25e42008-05-12 19:59:23 +0200249
Johannes Sixt6fd11062009-03-13 23:00:15 +0100250test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
Kevin Ballardea335b52008-08-13 15:34:34 -0700251 git reset --hard &&
Alex Riesen8134a002008-08-15 09:32:30 +0200252 touch fo\[ou\]bar foobar &&
253 git add '\''fo\[ou\]bar'\'' &&
Jeff King87539412008-09-30 04:03:55 -0400254 git ls-files fo\[ou\]bar | fgrep fo\[ou\]bar &&
Kevin Ballardea335b52008-08-13 15:34:34 -0700255 ! ( git ls-files foobar | grep foobar )
256'
257
Jeff King6e4f9812009-05-30 17:54:18 -0400258test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
259 git reset --hard &&
260 H=$(git rev-parse :1/2/a) &&
261 (
262 echo "100644 $H 1 track-this"
263 echo "100644 $H 3 track-this"
264 ) | git update-index --index-info &&
265 echo track-this >>.gitignore &&
266 echo resolved >track-this &&
267 git add track-this
268'
269
Chris Packham1e7ef742010-02-09 17:30:48 -0500270test_expect_success '"add non-existent" should fail' '
271 test_must_fail git add non-existent &&
272 ! (git ls-files | grep "non-existent")
273'
274
Jens Lehmann108da0d2010-07-10 00:18:38 +0200275test_expect_success 'git add --dry-run of existing changed file' "
276 echo new >>track-this &&
277 git add --dry-run track-this >actual 2>&1 &&
278 echo \"add 'track-this'\" | test_cmp - actual
279"
280
281test_expect_success 'git add --dry-run of non-existing file' "
282 echo ignored-file >>.gitignore &&
Ævar Arnfjörð Bjarmason48168852011-02-22 23:41:31 +0000283 test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
284"
285
Junio C Hamano68b2a002011-04-12 16:27:11 -0700286test_expect_success 'git add --dry-run of an existing file output' "
Ævar Arnfjörð Bjarmason48168852011-02-22 23:41:31 +0000287 echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
Junio C Hamano68b2a002011-04-12 16:27:11 -0700288 test_i18ncmp expect actual
Jens Lehmann108da0d2010-07-10 00:18:38 +0200289"
290
Johannes Sixtc1e02b22010-07-22 10:13:33 +0200291cat >expect.err <<\EOF
Jens Lehmann108da0d2010-07-10 00:18:38 +0200292The following paths are ignored by one of your .gitignore files:
293ignored-file
294Use -f if you really want to add them.
295fatal: no files added
Johannes Sixtc1e02b22010-07-22 10:13:33 +0200296EOF
297cat >expect.out <<\EOF
Jens Lehmann108da0d2010-07-10 00:18:38 +0200298add 'track-this'
299EOF
300
301test_expect_success 'git add --dry-run --ignore-missing of non-existing file' '
Ævar Arnfjörð Bjarmason439fb822011-02-22 23:41:30 +0000302 test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
303'
304
Junio C Hamano68b2a002011-04-12 16:27:11 -0700305test_expect_success 'git add --dry-run --ignore-missing of non-existing file output' '
306 test_i18ncmp expect.out actual.out &&
307 test_i18ncmp expect.err actual.err
Jens Lehmann108da0d2010-07-10 00:18:38 +0200308'
309
Carl Worth60ace872006-02-21 15:33:49 -0800310test_done