View Javadoc
1   /*
2    * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com>
3    * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  package org.eclipse.jgit.api;
45  
46  import static java.nio.charset.StandardCharsets.UTF_8;
47  import static org.eclipse.jgit.util.FileUtils.RECURSIVE;
48  import static org.junit.Assert.assertEquals;
49  import static org.junit.Assert.assertTrue;
50  import static org.junit.Assert.fail;
51  
52  import java.io.File;
53  import java.io.FileInputStream;
54  import java.io.IOException;
55  import java.io.PrintWriter;
56  import java.util.Set;
57  
58  import org.eclipse.jgit.api.errors.FilterFailedException;
59  import org.eclipse.jgit.api.errors.GitAPIException;
60  import org.eclipse.jgit.api.errors.NoFilepatternException;
61  import org.eclipse.jgit.attributes.FilterCommandRegistry;
62  import org.eclipse.jgit.dircache.DirCache;
63  import org.eclipse.jgit.dircache.DirCacheBuilder;
64  import org.eclipse.jgit.dircache.DirCacheEntry;
65  import org.eclipse.jgit.junit.JGitTestUtil;
66  import org.eclipse.jgit.junit.RepositoryTestCase;
67  import org.eclipse.jgit.lfs.BuiltinLFS;
68  import org.eclipse.jgit.lib.ConfigConstants;
69  import org.eclipse.jgit.lib.Constants;
70  import org.eclipse.jgit.lib.FileMode;
71  import org.eclipse.jgit.lib.ObjectId;
72  import org.eclipse.jgit.lib.ObjectInserter;
73  import org.eclipse.jgit.lib.Repository;
74  import org.eclipse.jgit.lib.StoredConfig;
75  import org.eclipse.jgit.revwalk.RevCommit;
76  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
77  import org.eclipse.jgit.treewalk.TreeWalk;
78  import org.eclipse.jgit.treewalk.WorkingTreeOptions;
79  import org.eclipse.jgit.util.FS;
80  import org.eclipse.jgit.util.FileUtils;
81  import org.junit.Test;
82  import org.junit.experimental.theories.DataPoints;
83  import org.junit.experimental.theories.Theories;
84  import org.junit.experimental.theories.Theory;
85  import org.junit.runner.RunWith;
86  
87  @RunWith(Theories.class)
88  public class AddCommandTest extends RepositoryTestCase {
89  	@DataPoints
90  	public static boolean[] sleepBeforeAddOptions = { true, false };
91  
92  
93  	@Override
94  	public void setUp() throws Exception {
95  		BuiltinLFS.register();
96  		super.setUp();
97  	}
98  
99  	@Test
100 	public void testAddNothing() throws GitAPIException {
101 		try (Git git = new Git(db)) {
102 			git.add().call();
103 			fail("Expected IllegalArgumentException");
104 		} catch (NoFilepatternException e) {
105 			// expected
106 		}
107 
108 	}
109 
110 	@Test
111 	public void testAddNonExistingSingleFile() throws GitAPIException {
112 		try (Git git = new Git(db)) {
113 			DirCache dc = git.add().addFilepattern("a.txt").call();
114 			assertEquals(0, dc.getEntryCount());
115 		}
116 	}
117 
118 	@Test
119 	public void testAddExistingSingleFile() throws IOException, GitAPIException {
120 		File file = new File(db.getWorkTree(), "a.txt");
121 		FileUtils.createNewFile(file);
122 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
123 			writer.print("content");
124 		}
125 
126 		try (Git git = new Git(db)) {
127 			git.add().addFilepattern("a.txt").call();
128 
129 			assertEquals(
130 					"[a.txt, mode:100644, content:content]",
131 					indexState(CONTENT));
132 		}
133 	}
134 
135 	@Test
136 	public void testCleanFilter() throws IOException, GitAPIException {
137 		writeTrashFile(".gitattributes", "*.txt filter=tstFilter");
138 		writeTrashFile("src/a.tmp", "foo");
139 		// Caution: we need a trailing '\n' since sed on mac always appends
140 		// linefeeds if missing
141 		writeTrashFile("src/a.txt", "foo\n");
142 		File script = writeTempFile("sed s/o/e/g");
143 
144 		try (Git git = new Git(db)) {
145 			StoredConfig config = git.getRepository().getConfig();
146 			config.setString("filter", "tstFilter", "clean",
147 					"sh " + slashify(script.getPath()));
148 			config.save();
149 
150 			git.add().addFilepattern("src/a.txt").addFilepattern("src/a.tmp")
151 					.call();
152 
153 			assertEquals(
154 					"[src/a.tmp, mode:100644, content:foo][src/a.txt, mode:100644, content:fee\n]",
155 					indexState(CONTENT));
156 		}
157 	}
158 
159 	@Theory
160 	public void testBuiltinFilters(boolean sleepBeforeAdd)
161 			throws IOException,
162 			GitAPIException, InterruptedException {
163 		writeTrashFile(".gitattributes", "*.txt filter=lfs");
164 		writeTrashFile("src/a.tmp", "foo");
165 		// Caution: we need a trailing '\n' since sed on mac always appends
166 		// linefeeds if missing
167 		File script = writeTempFile("sed s/o/e/g");
168 		File f = writeTrashFile("src/a.txt", "foo\n");
169 
170 		try (Git git = new Git(db)) {
171 			if (!sleepBeforeAdd) {
172 				fsTick(f);
173 			}
174 			git.add().addFilepattern(".gitattributes").call();
175 			StoredConfig config = git.getRepository().getConfig();
176 			config.setString("filter", "lfs", "clean",
177 					"sh " + slashify(script.getPath()));
178 			config.setString("filter", "lfs", "smudge",
179 					"sh " + slashify(script.getPath()));
180 			config.setBoolean("filter", "lfs", "useJGitBuiltin", true);
181 			config.save();
182 
183 			if (!sleepBeforeAdd) {
184 				fsTick(f);
185 			}
186 			git.add().addFilepattern("src/a.txt").addFilepattern("src/a.tmp")
187 					.addFilepattern(".gitattributes").call();
188 
189 			assertEquals(
190 					"[.gitattributes, mode:100644, content:*.txt filter=lfs][src/a.tmp, mode:100644, content:foo][src/a.txt, mode:100644, content:version https://git-lfs.github.com/spec/v1\noid sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c\nsize 4\n]",
191 					indexState(CONTENT));
192 
193 			RevCommit c1 = git.commit().setMessage("c1").call();
194 			assertTrue(git.status().call().isClean());
195 			f = writeTrashFile("src/a.txt", "foobar\n");
196 			if (!sleepBeforeAdd) {
197 				fsTick(f);
198 			}
199 			git.add().addFilepattern("src/a.txt").call();
200 			git.commit().setMessage("c2").call();
201 			assertTrue(git.status().call().isClean());
202 			assertEquals(
203 					"[.gitattributes, mode:100644, content:*.txt filter=lfs][src/a.tmp, mode:100644, content:foo][src/a.txt, mode:100644, content:version https://git-lfs.github.com/spec/v1\noid sha256:aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f\nsize 7\n]",
204 					indexState(CONTENT));
205 			assertEquals("foobar\n", read("src/a.txt"));
206 			git.checkout().setName(c1.getName()).call();
207 			assertEquals(
208 					"[.gitattributes, mode:100644, content:*.txt filter=lfs][src/a.tmp, mode:100644, content:foo][src/a.txt, mode:100644, content:version https://git-lfs.github.com/spec/v1\noid sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c\nsize 4\n]",
209 					indexState(CONTENT));
210 			assertEquals(
211 					"foo\n", read("src/a.txt"));
212 		}
213 	}
214 
215 	@Theory
216 	public void testBuiltinCleanFilter(boolean sleepBeforeAdd)
217 			throws IOException, GitAPIException, InterruptedException {
218 		writeTrashFile(".gitattributes", "*.txt filter=lfs");
219 		writeTrashFile("src/a.tmp", "foo");
220 		// Caution: we need a trailing '\n' since sed on mac always appends
221 		// linefeeds if missing
222 		File script = writeTempFile("sed s/o/e/g");
223 		File f = writeTrashFile("src/a.txt", "foo\n");
224 
225 		// unregister the smudge filter. Only clean filter should be builtin
226 		FilterCommandRegistry.unregister(
227 				org.eclipse.jgit.lib.Constants.BUILTIN_FILTER_PREFIX
228 						+ "lfs/smudge");
229 
230 		try (Git git = new Git(db)) {
231 			if (!sleepBeforeAdd) {
232 				fsTick(f);
233 			}
234 			git.add().addFilepattern(".gitattributes").call();
235 			StoredConfig config = git.getRepository().getConfig();
236 			config.setString("filter", "lfs", "clean",
237 					"sh " + slashify(script.getPath()));
238 			config.setString("filter", "lfs", "smudge",
239 					"sh " + slashify(script.getPath()));
240 			config.setBoolean("filter", "lfs", "useJGitBuiltin", true);
241 			config.save();
242 
243 			if (!sleepBeforeAdd) {
244 				fsTick(f);
245 			}
246 			git.add().addFilepattern("src/a.txt").addFilepattern("src/a.tmp")
247 					.addFilepattern(".gitattributes").call();
248 
249 			assertEquals(
250 					"[.gitattributes, mode:100644, content:*.txt filter=lfs][src/a.tmp, mode:100644, content:foo][src/a.txt, mode:100644, content:version https://git-lfs.github.com/spec/v1\noid sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c\nsize 4\n]",
251 					indexState(CONTENT));
252 
253 			RevCommit c1 = git.commit().setMessage("c1").call();
254 			assertTrue(git.status().call().isClean());
255 			f = writeTrashFile("src/a.txt", "foobar\n");
256 			if (!sleepBeforeAdd) {
257 				fsTick(f);
258 			}
259 			git.add().addFilepattern("src/a.txt").call();
260 			git.commit().setMessage("c2").call();
261 			assertTrue(git.status().call().isClean());
262 			assertEquals(
263 					"[.gitattributes, mode:100644, content:*.txt filter=lfs][src/a.tmp, mode:100644, content:foo][src/a.txt, mode:100644, content:version https://git-lfs.github.com/spec/v1\noid sha256:aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f\nsize 7\n]",
264 					indexState(CONTENT));
265 			assertEquals("foobar\n", read("src/a.txt"));
266 			git.checkout().setName(c1.getName()).call();
267 			assertEquals(
268 					"[.gitattributes, mode:100644, content:*.txt filter=lfs][src/a.tmp, mode:100644, content:foo][src/a.txt, mode:100644, content:version https://git-lfs.github.com/spec/v1\noid sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c\nsize 4\n]",
269 					indexState(CONTENT));
270 			// due to lfs clean filter but dummy smudge filter we expect strange
271 			// content. The smudge filter converts from real content to pointer
272 			// file content (starting with "version ") but the smudge filter
273 			// replaces 'o' by 'e' which results in a text starting with
274 			// "versien "
275 			assertEquals(
276 					"versien https://git-lfs.github.cem/spec/v1\neid sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c\nsize 4\n",
277 					read("src/a.txt"));
278 		}
279 	}
280 
281 	@Test
282 	public void testAttributesWithTreeWalkFilter()
283 			throws IOException, GitAPIException {
284 		writeTrashFile(".gitattributes", "*.txt filter=lfs");
285 		writeTrashFile("src/a.tmp", "foo");
286 		writeTrashFile("src/a.txt", "foo\n");
287 		File script = writeTempFile("sed s/o/e/g");
288 
289 		try (Git git = new Git(db)) {
290 			StoredConfig config = git.getRepository().getConfig();
291 			config.setString("filter", "lfs", "clean",
292 					"sh " + slashify(script.getPath()));
293 			config.save();
294 
295 			git.add().addFilepattern(".gitattributes").call();
296 			git.commit().setMessage("attr").call();
297 			git.add().addFilepattern("src/a.txt").addFilepattern("src/a.tmp")
298 					.addFilepattern(".gitattributes").call();
299 			git.commit().setMessage("c1").call();
300 			assertTrue(git.status().call().isClean());
301 		}
302 	}
303 
304 	@Test
305 	public void testAttributesConflictingMatch() throws Exception {
306 		writeTrashFile(".gitattributes", "foo/** crlf=input\n*.jar binary");
307 		writeTrashFile("foo/bar.jar", "\r\n");
308 		// We end up with attributes [binary -diff -merge -text crlf=input].
309 		// crlf should have no effect when -text is present.
310 		try (Git git = new Git(db)) {
311 			git.add().addFilepattern(".").call();
312 			assertEquals(
313 					"[.gitattributes, mode:100644, content:foo/** crlf=input\n*.jar binary]"
314 							+ "[foo/bar.jar, mode:100644, content:\r\n]",
315 					indexState(CONTENT));
316 		}
317 	}
318 
319 	@Test
320 	public void testCleanFilterEnvironment()
321 			throws IOException, GitAPIException {
322 		writeTrashFile(".gitattributes", "*.txt filter=tstFilter");
323 		writeTrashFile("src/a.txt", "foo");
324 		File script = writeTempFile("echo $GIT_DIR; echo 1 >xyz");
325 
326 		try (Git git = new Git(db)) {
327 			StoredConfig config = git.getRepository().getConfig();
328 			config.setString("filter", "tstFilter", "clean",
329 					"sh " + slashify(script.getPath()));
330 			config.save();
331 			git.add().addFilepattern("src/a.txt").call();
332 
333 			String gitDir = db.getDirectory().getAbsolutePath();
334 			assertEquals("[src/a.txt, mode:100644, content:" + gitDir
335 					+ "\n]", indexState(CONTENT));
336 			assertTrue(new File(db.getWorkTree(), "xyz").exists());
337 		}
338 	}
339 
340 	@Test
341 	public void testMultipleCleanFilter() throws IOException, GitAPIException {
342 		writeTrashFile(".gitattributes",
343 				"*.txt filter=tstFilter\n*.tmp filter=tstFilter2");
344 		// Caution: we need a trailing '\n' since sed on mac always appends
345 		// linefeeds if missing
346 		writeTrashFile("src/a.tmp", "foo\n");
347 		writeTrashFile("src/a.txt", "foo\n");
348 		File script = writeTempFile("sed s/o/e/g");
349 		File script2 = writeTempFile("sed s/f/x/g");
350 
351 		try (Git git = new Git(db)) {
352 			StoredConfig config = git.getRepository().getConfig();
353 			config.setString("filter", "tstFilter", "clean",
354 					"sh " + slashify(script.getPath()));
355 			config.setString("filter", "tstFilter2", "clean",
356 					"sh " + slashify(script2.getPath()));
357 			config.save();
358 
359 			git.add().addFilepattern("src/a.txt").addFilepattern("src/a.tmp")
360 					.call();
361 
362 			assertEquals(
363 					"[src/a.tmp, mode:100644, content:xoo\n][src/a.txt, mode:100644, content:fee\n]",
364 					indexState(CONTENT));
365 
366 			// TODO: multiple clean filters for one file???
367 		}
368 	}
369 
370 	/**
371 	 * The path of an added file name contains ';' and afterwards malicious
372 	 * commands. Make sure when calling filter commands to properly escape the
373 	 * filenames
374 	 *
375 	 * @throws IOException
376 	 * @throws GitAPIException
377 	 */
378 	@Test
379 	public void testCommandInjection() throws IOException, GitAPIException {
380 		// Caution: we need a trailing '\n' since sed on mac always appends
381 		// linefeeds if missing
382 		writeTrashFile("; echo virus", "foo\n");
383 		File script = writeTempFile("sed s/o/e/g");
384 
385 		try (Git git = new Git(db)) {
386 			StoredConfig config = git.getRepository().getConfig();
387 			config.setString("filter", "tstFilter", "clean",
388 					"sh " + slashify(script.getPath()) + " %f");
389 			writeTrashFile(".gitattributes", "* filter=tstFilter");
390 
391 			git.add().addFilepattern("; echo virus").call();
392 			// Without proper escaping the content would be "feovirus". The sed
393 			// command and the "echo virus" would contribute to the content
394 			assertEquals("[; echo virus, mode:100644, content:fee\n]",
395 					indexState(CONTENT));
396 		}
397 	}
398 
399 	@Test
400 	public void testBadCleanFilter() throws IOException, GitAPIException {
401 		writeTrashFile("a.txt", "foo");
402 		File script = writeTempFile("sedfoo s/o/e/g");
403 
404 		try (Git git = new Git(db)) {
405 			StoredConfig config = git.getRepository().getConfig();
406 			config.setString("filter", "tstFilter", "clean",
407 					"sh " + script.getPath());
408 			config.save();
409 			writeTrashFile(".gitattributes", "*.txt filter=tstFilter");
410 
411 			try {
412 				git.add().addFilepattern("a.txt").call();
413 				fail("Didn't received the expected exception");
414 			} catch (FilterFailedException e) {
415 				assertEquals(127, e.getReturnCode());
416 			}
417 		}
418 	}
419 
420 	@Test
421 	public void testBadCleanFilter2() throws IOException, GitAPIException {
422 		writeTrashFile("a.txt", "foo");
423 		File script = writeTempFile("sed s/o/e/g");
424 
425 		try (Git git = new Git(db)) {
426 			StoredConfig config = git.getRepository().getConfig();
427 			config.setString("filter", "tstFilter", "clean",
428 					"shfoo " + script.getPath());
429 			config.save();
430 			writeTrashFile(".gitattributes", "*.txt filter=tstFilter");
431 
432 			try {
433 				git.add().addFilepattern("a.txt").call();
434 				fail("Didn't received the expected exception");
435 			} catch (FilterFailedException e) {
436 				assertEquals(127, e.getReturnCode());
437 			}
438 		}
439 	}
440 
441 	@Test
442 	public void testCleanFilterReturning12() throws IOException,
443 			GitAPIException {
444 		writeTrashFile("a.txt", "foo");
445 		File script = writeTempFile("exit 12");
446 
447 		try (Git git = new Git(db)) {
448 			StoredConfig config = git.getRepository().getConfig();
449 			config.setString("filter", "tstFilter", "clean",
450 					"sh " + slashify(script.getPath()));
451 			config.save();
452 			writeTrashFile(".gitattributes", "*.txt filter=tstFilter");
453 
454 			try {
455 				git.add().addFilepattern("a.txt").call();
456 				fail("Didn't received the expected exception");
457 			} catch (FilterFailedException e) {
458 				assertEquals(12, e.getReturnCode());
459 			}
460 		}
461 	}
462 
463 	@Test
464 	public void testNotApplicableFilter() throws IOException, GitAPIException {
465 		writeTrashFile("a.txt", "foo");
466 		File script = writeTempFile("sed s/o/e/g");
467 
468 		try (Git git = new Git(db)) {
469 			StoredConfig config = git.getRepository().getConfig();
470 			config.setString("filter", "tstFilter", "something",
471 					"sh " + script.getPath());
472 			config.save();
473 			writeTrashFile(".gitattributes", "*.txt filter=tstFilter");
474 
475 			git.add().addFilepattern("a.txt").call();
476 
477 			assertEquals("[a.txt, mode:100644, content:foo]",
478 					indexState(CONTENT));
479 		}
480 	}
481 
482 	private File writeTempFile(String body) throws IOException {
483 		File f = File.createTempFile("AddCommandTest_", "");
484 		JGitTestUtil.write(f, body);
485 		return f;
486 	}
487 
488 	@Test
489 	public void testAddExistingSingleSmallFileWithNewLine() throws IOException,
490 			GitAPIException {
491 		File file = new File(db.getWorkTree(), "a.txt");
492 		FileUtils.createNewFile(file);
493 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
494 			writer.print("row1\r\nrow2");
495 		}
496 
497 		try (Git git = new Git(db)) {
498 			db.getConfig().setString("core", null, "autocrlf", "false");
499 			git.add().addFilepattern("a.txt").call();
500 			assertEquals("[a.txt, mode:100644, content:row1\r\nrow2]",
501 					indexState(CONTENT));
502 			db.getConfig().setString("core", null, "autocrlf", "true");
503 			git.add().addFilepattern("a.txt").call();
504 			assertEquals("[a.txt, mode:100644, content:row1\r\nrow2]",
505 					indexState(CONTENT));
506 			db.getConfig().setString("core", null, "autocrlf", "input");
507 			git.add().addFilepattern("a.txt").call();
508 			assertEquals("[a.txt, mode:100644, content:row1\r\nrow2]",
509 					indexState(CONTENT));
510 		}
511 	}
512 
513 	@Test
514 	public void testAddExistingSingleMediumSizeFileWithNewLine()
515 			throws IOException, GitAPIException {
516 		File file = new File(db.getWorkTree(), "a.txt");
517 		FileUtils.createNewFile(file);
518 		StringBuilder data = new StringBuilder();
519 		for (int i = 0; i < 1000; ++i) {
520 			data.append("row1\r\nrow2");
521 		}
522 		String crData = data.toString();
523 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
524 			writer.print(crData);
525 		}
526 		try (Git git = new Git(db)) {
527 			db.getConfig().setString("core", null, "autocrlf", "false");
528 			git.add().addFilepattern("a.txt").call();
529 			assertEquals("[a.txt, mode:100644, content:" + crData + "]",
530 					indexState(CONTENT));
531 			db.getConfig().setString("core", null, "autocrlf", "true");
532 			git.add().addFilepattern("a.txt").call();
533 			assertEquals("[a.txt, mode:100644, content:" + crData + "]",
534 					indexState(CONTENT));
535 			db.getConfig().setString("core", null, "autocrlf", "input");
536 			git.add().addFilepattern("a.txt").call();
537 			assertEquals("[a.txt, mode:100644, content:" + crData + "]",
538 					indexState(CONTENT));
539 		}
540 	}
541 
542 	@Test
543 	public void testAddExistingSingleBinaryFile() throws IOException,
544 			GitAPIException {
545 		File file = new File(db.getWorkTree(), "a.txt");
546 		FileUtils.createNewFile(file);
547 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
548 			writer.print("row1\r\nrow2\u0000");
549 		}
550 
551 		try (Git git = new Git(db)) {
552 			db.getConfig().setString("core", null, "autocrlf", "false");
553 			git.add().addFilepattern("a.txt").call();
554 			assertEquals("[a.txt, mode:100644, content:row1\r\nrow2\u0000]",
555 					indexState(CONTENT));
556 			db.getConfig().setString("core", null, "autocrlf", "true");
557 			git.add().addFilepattern("a.txt").call();
558 			assertEquals("[a.txt, mode:100644, content:row1\r\nrow2\u0000]",
559 					indexState(CONTENT));
560 			db.getConfig().setString("core", null, "autocrlf", "input");
561 			git.add().addFilepattern("a.txt").call();
562 			assertEquals("[a.txt, mode:100644, content:row1\r\nrow2\u0000]",
563 					indexState(CONTENT));
564 		}
565 	}
566 
567 	@Test
568 	public void testAddExistingSingleFileInSubDir() throws IOException,
569 			GitAPIException {
570 		FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
571 		File file = new File(db.getWorkTree(), "sub/a.txt");
572 		FileUtils.createNewFile(file);
573 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
574 			writer.print("content");
575 		}
576 
577 		try (Git git = new Git(db)) {
578 			git.add().addFilepattern("sub/a.txt").call();
579 
580 			assertEquals(
581 					"[sub/a.txt, mode:100644, content:content]",
582 					indexState(CONTENT));
583 		}
584 	}
585 
586 	@Test
587 	public void testAddExistingSingleFileTwice() throws IOException,
588 			GitAPIException {
589 		File file = new File(db.getWorkTree(), "a.txt");
590 		FileUtils.createNewFile(file);
591 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
592 			writer.print("content");
593 		}
594 
595 		try (Git git = new Git(db)) {
596 			DirCache dc = git.add().addFilepattern("a.txt").call();
597 
598 			dc.getEntry(0).getObjectId();
599 
600 			try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
601 				writer.print("other content");
602 			}
603 
604 			dc = git.add().addFilepattern("a.txt").call();
605 
606 			assertEquals(
607 					"[a.txt, mode:100644, content:other content]",
608 					indexState(CONTENT));
609 		}
610 	}
611 
612 	@Test
613 	public void testAddExistingSingleFileTwiceWithCommit() throws Exception {
614 		File file = new File(db.getWorkTree(), "a.txt");
615 		FileUtils.createNewFile(file);
616 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
617 			writer.print("content");
618 		}
619 
620 		try (Git git = new Git(db)) {
621 			DirCache dc = git.add().addFilepattern("a.txt").call();
622 
623 			dc.getEntry(0).getObjectId();
624 
625 			git.commit().setMessage("commit a.txt").call();
626 
627 			try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
628 				writer.print("other content");
629 			}
630 
631 			dc = git.add().addFilepattern("a.txt").call();
632 
633 			assertEquals(
634 					"[a.txt, mode:100644, content:other content]",
635 					indexState(CONTENT));
636 		}
637 	}
638 
639 	@Test
640 	public void testAddRemovedFile() throws Exception {
641 		File file = new File(db.getWorkTree(), "a.txt");
642 		FileUtils.createNewFile(file);
643 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
644 			writer.print("content");
645 		}
646 
647 		try (Git git = new Git(db)) {
648 			DirCache dc = git.add().addFilepattern("a.txt").call();
649 
650 			dc.getEntry(0).getObjectId();
651 			FileUtils.delete(file);
652 
653 			// is supposed to do nothing
654 			dc = git.add().addFilepattern("a.txt").call();
655 
656 			assertEquals(
657 					"[a.txt, mode:100644, content:content]",
658 					indexState(CONTENT));
659 		}
660 	}
661 
662 	@Test
663 	public void testAddRemovedCommittedFile() throws Exception {
664 		File file = new File(db.getWorkTree(), "a.txt");
665 		FileUtils.createNewFile(file);
666 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
667 			writer.print("content");
668 		}
669 
670 		try (Git git = new Git(db)) {
671 			DirCache dc = git.add().addFilepattern("a.txt").call();
672 
673 			git.commit().setMessage("commit a.txt").call();
674 
675 			dc.getEntry(0).getObjectId();
676 			FileUtils.delete(file);
677 
678 			// is supposed to do nothing
679 			dc = git.add().addFilepattern("a.txt").call();
680 
681 			assertEquals(
682 					"[a.txt, mode:100644, content:content]",
683 					indexState(CONTENT));
684 		}
685 	}
686 
687 	@Test
688 	public void testAddWithConflicts() throws Exception {
689 		// prepare conflict
690 
691 		File file = new File(db.getWorkTree(), "a.txt");
692 		FileUtils.createNewFile(file);
693 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
694 			writer.print("content");
695 		}
696 
697 		File file2 = new File(db.getWorkTree(), "b.txt");
698 		FileUtils.createNewFile(file2);
699 		try (PrintWriter writer = new PrintWriter(file2, UTF_8.name())) {
700 			writer.print("content b");
701 		}
702 
703 		DirCache dc = db.lockDirCache();
704 		try (ObjectInserter newObjectInserter = db.newObjectInserter()) {
705 			DirCacheBuilder builder = dc.builder();
706 
707 			addEntryToBuilder("b.txt", file2, newObjectInserter, builder, 0);
708 			addEntryToBuilder("a.txt", file, newObjectInserter, builder, 1);
709 
710 			try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
711 				writer.print("other content");
712 			}
713 			addEntryToBuilder("a.txt", file, newObjectInserter, builder, 3);
714 
715 			try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
716 				writer.print("our content");
717 			}
718 			addEntryToBuilder("a.txt", file, newObjectInserter, builder, 2)
719 					.getObjectId();
720 
721 			builder.commit();
722 		}
723 		assertEquals(
724 				"[a.txt, mode:100644, stage:1, content:content]" +
725 				"[a.txt, mode:100644, stage:2, content:our content]" +
726 				"[a.txt, mode:100644, stage:3, content:other content]" +
727 				"[b.txt, mode:100644, content:content b]",
728 				indexState(CONTENT));
729 
730 		// now the test begins
731 
732 		try (Git git = new Git(db)) {
733 			dc = git.add().addFilepattern("a.txt").call();
734 
735 			assertEquals(
736 					"[a.txt, mode:100644, content:our content]" +
737 					"[b.txt, mode:100644, content:content b]",
738 					indexState(CONTENT));
739 		}
740 	}
741 
742 	@Test
743 	public void testAddTwoFiles() throws Exception  {
744 		File file = new File(db.getWorkTree(), "a.txt");
745 		FileUtils.createNewFile(file);
746 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
747 			writer.print("content");
748 		}
749 
750 		File file2 = new File(db.getWorkTree(), "b.txt");
751 		FileUtils.createNewFile(file2);
752 		try (PrintWriter writer = new PrintWriter(file2, UTF_8.name())) {
753 			writer.print("content b");
754 		}
755 
756 		try (Git git = new Git(db)) {
757 			git.add().addFilepattern("a.txt").addFilepattern("b.txt").call();
758 			assertEquals(
759 					"[a.txt, mode:100644, content:content]" +
760 					"[b.txt, mode:100644, content:content b]",
761 					indexState(CONTENT));
762 		}
763 	}
764 
765 	@Test
766 	public void testAddFolder() throws Exception  {
767 		FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
768 		File file = new File(db.getWorkTree(), "sub/a.txt");
769 		FileUtils.createNewFile(file);
770 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
771 			writer.print("content");
772 		}
773 
774 		File file2 = new File(db.getWorkTree(), "sub/b.txt");
775 		FileUtils.createNewFile(file2);
776 		try (PrintWriter writer = new PrintWriter(file2, UTF_8.name())) {
777 			writer.print("content b");
778 		}
779 
780 		try (Git git = new Git(db)) {
781 			git.add().addFilepattern("sub").call();
782 			assertEquals(
783 					"[sub/a.txt, mode:100644, content:content]" +
784 					"[sub/b.txt, mode:100644, content:content b]",
785 					indexState(CONTENT));
786 		}
787 	}
788 
789 	@Test
790 	public void testAddIgnoredFile() throws Exception  {
791 		FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
792 		File file = new File(db.getWorkTree(), "sub/a.txt");
793 		FileUtils.createNewFile(file);
794 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
795 			writer.print("content");
796 		}
797 
798 		File ignoreFile = new File(db.getWorkTree(), ".gitignore");
799 		FileUtils.createNewFile(ignoreFile);
800 		try (PrintWriter writer = new PrintWriter(ignoreFile, UTF_8.name())) {
801 			writer.print("sub/b.txt");
802 		}
803 
804 		File file2 = new File(db.getWorkTree(), "sub/b.txt");
805 		FileUtils.createNewFile(file2);
806 		try (PrintWriter writer = new PrintWriter(file2, UTF_8.name())) {
807 			writer.print("content b");
808 		}
809 
810 		try (Git git = new Git(db)) {
811 			git.add().addFilepattern("sub").call();
812 
813 			assertEquals(
814 					"[sub/a.txt, mode:100644, content:content]",
815 					indexState(CONTENT));
816 		}
817 	}
818 
819 	@Test
820 	public void testAddWholeRepo() throws Exception  {
821 		FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
822 		File file = new File(db.getWorkTree(), "sub/a.txt");
823 		FileUtils.createNewFile(file);
824 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
825 			writer.print("content");
826 		}
827 
828 		File file2 = new File(db.getWorkTree(), "sub/b.txt");
829 		FileUtils.createNewFile(file2);
830 		try (PrintWriter writer = new PrintWriter(file2, UTF_8.name())) {
831 			writer.print("content b");
832 		}
833 
834 		try (Git git = new Git(db)) {
835 			git.add().addFilepattern(".").call();
836 			assertEquals(
837 					"[sub/a.txt, mode:100644, content:content]" +
838 					"[sub/b.txt, mode:100644, content:content b]",
839 					indexState(CONTENT));
840 		}
841 	}
842 
843 	// the same three cases as in testAddWithParameterUpdate
844 	// file a exists in workdir and in index -> added
845 	// file b exists not in workdir but in index -> unchanged
846 	// file c exists in workdir but not in index -> added
847 	@Test
848 	public void testAddWithoutParameterUpdate() throws Exception {
849 		FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
850 		File file = new File(db.getWorkTree(), "sub/a.txt");
851 		FileUtils.createNewFile(file);
852 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
853 			writer.print("content");
854 		}
855 
856 		File file2 = new File(db.getWorkTree(), "sub/b.txt");
857 		FileUtils.createNewFile(file2);
858 		try (PrintWriter writer = new PrintWriter(file2, UTF_8.name())) {
859 			writer.print("content b");
860 		}
861 
862 		try (Git git = new Git(db)) {
863 			git.add().addFilepattern("sub").call();
864 
865 			assertEquals(
866 					"[sub/a.txt, mode:100644, content:content]" +
867 					"[sub/b.txt, mode:100644, content:content b]",
868 					indexState(CONTENT));
869 
870 			git.commit().setMessage("commit").call();
871 
872 			// new unstaged file sub/c.txt
873 			File file3 = new File(db.getWorkTree(), "sub/c.txt");
874 			FileUtils.createNewFile(file3);
875 			try (PrintWriter writer = new PrintWriter(file3, UTF_8.name())) {
876 				writer.print("content c");
877 			}
878 
879 			// file sub/a.txt is modified
880 			try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
881 				writer.print("modified content");
882 			}
883 
884 			// file sub/b.txt is deleted
885 			FileUtils.delete(file2);
886 
887 			git.add().addFilepattern("sub").call();
888 			// change in sub/a.txt is staged
889 			// deletion of sub/b.txt is not staged
890 			// sub/c.txt is staged
891 			assertEquals(
892 					"[sub/a.txt, mode:100644, content:modified content]" +
893 					"[sub/b.txt, mode:100644, content:content b]" +
894 					"[sub/c.txt, mode:100644, content:content c]",
895 					indexState(CONTENT));
896 		}
897 	}
898 
899 	// file a exists in workdir and in index -> added
900 	// file b exists not in workdir but in index -> deleted
901 	// file c exists in workdir but not in index -> unchanged
902 	@Test
903 	public void testAddWithParameterUpdate() throws Exception {
904 		FileUtils.mkdir(new File(db.getWorkTree(), "sub"));
905 		File file = new File(db.getWorkTree(), "sub/a.txt");
906 		FileUtils.createNewFile(file);
907 		try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
908 			writer.print("content");
909 		}
910 
911 		File file2 = new File(db.getWorkTree(), "sub/b.txt");
912 		FileUtils.createNewFile(file2);
913 		try (PrintWriter writer = new PrintWriter(file2, UTF_8.name())) {
914 			writer.print("content b");
915 		}
916 
917 		try (Git git = new Git(db)) {
918 			git.add().addFilepattern("sub").call();
919 
920 			assertEquals(
921 					"[sub/a.txt, mode:100644, content:content]" +
922 					"[sub/b.txt, mode:100644, content:content b]",
923 					indexState(CONTENT));
924 
925 			git.commit().setMessage("commit").call();
926 
927 			// new unstaged file sub/c.txt
928 			File file3 = new File(db.getWorkTree(), "sub/c.txt");
929 			FileUtils.createNewFile(file3);
930 			try (PrintWriter writer = new PrintWriter(file3, UTF_8.name())) {
931 				writer.print("content c");
932 			}
933 
934 			// file sub/a.txt is modified
935 			try (PrintWriter writer = new PrintWriter(file, UTF_8.name())) {
936 				writer.print("modified content");
937 			}
938 
939 			FileUtils.delete(file2);
940 
941 			// change in sub/a.txt is staged
942 			// deletion of sub/b.txt is staged
943 			// sub/c.txt is not staged
944 			git.add().addFilepattern("sub").setUpdate(true).call();
945 			// change in sub/a.txt is staged
946 			assertEquals(
947 					"[sub/a.txt, mode:100644, content:modified content]",
948 					indexState(CONTENT));
949 		}
950 	}
951 
952 	@Test
953 	public void testAssumeUnchanged() throws Exception {
954 		try (Git git = new Git(db)) {
955 			String path = "a.txt";
956 			writeTrashFile(path, "content");
957 			git.add().addFilepattern(path).call();
958 			String path2 = "b.txt";
959 			writeTrashFile(path2, "content");
960 			git.add().addFilepattern(path2).call();
961 			git.commit().setMessage("commit").call();
962 			assertEquals("[a.txt, mode:100644, content:"
963 					+ "content, assume-unchanged:false]"
964 					+ "[b.txt, mode:100644, content:content, "
965 					+ "assume-unchanged:false]", indexState(CONTENT
966 					| ASSUME_UNCHANGED));
967 			assumeUnchanged(path2);
968 			assertEquals("[a.txt, mode:100644, content:content, "
969 					+ "assume-unchanged:false][b.txt, mode:100644, "
970 					+ "content:content, assume-unchanged:true]", indexState(CONTENT
971 					| ASSUME_UNCHANGED));
972 			writeTrashFile(path, "more content");
973 			writeTrashFile(path2, "more content");
974 
975 			git.add().addFilepattern(".").call();
976 
977 			assertEquals("[a.txt, mode:100644, content:more content,"
978 					+ " assume-unchanged:false][b.txt, mode:100644,"
979 					+ " content:content, assume-unchanged:true]",
980 					indexState(CONTENT
981 					| ASSUME_UNCHANGED));
982 		}
983 	}
984 
985 	@Test
986 	public void testReplaceFileWithDirectory()
987 			throws IOException, NoFilepatternException, GitAPIException {
988 		try (Git git = new Git(db)) {
989 			writeTrashFile("df", "before replacement");
990 			git.add().addFilepattern("df").call();
991 			assertEquals("[df, mode:100644, content:before replacement]",
992 					indexState(CONTENT));
993 			FileUtils.delete(new File(db.getWorkTree(), "df"));
994 			writeTrashFile("df/f", "after replacement");
995 			git.add().addFilepattern("df").call();
996 			assertEquals("[df/f, mode:100644, content:after replacement]",
997 					indexState(CONTENT));
998 		}
999 	}
1000 
1001 	@Test
1002 	public void testReplaceDirectoryWithFile()
1003 			throws IOException, NoFilepatternException, GitAPIException {
1004 		try (Git git = new Git(db)) {
1005 			writeTrashFile("df/f", "before replacement");
1006 			git.add().addFilepattern("df").call();
1007 			assertEquals("[df/f, mode:100644, content:before replacement]",
1008 					indexState(CONTENT));
1009 			FileUtils.delete(new File(db.getWorkTree(), "df"), RECURSIVE);
1010 			writeTrashFile("df", "after replacement");
1011 			git.add().addFilepattern("df").call();
1012 			assertEquals("[df, mode:100644, content:after replacement]",
1013 					indexState(CONTENT));
1014 		}
1015 	}
1016 
1017 	@Test
1018 	public void testReplaceFileByPartOfDirectory()
1019 			throws IOException, NoFilepatternException, GitAPIException {
1020 		try (Git git = new Git(db)) {
1021 			writeTrashFile("src/main", "df", "before replacement");
1022 			writeTrashFile("src/main", "z", "z");
1023 			writeTrashFile("z", "z2");
1024 			git.add().addFilepattern("src/main/df")
1025 				.addFilepattern("src/main/z")
1026 				.addFilepattern("z")
1027 				.call();
1028 			assertEquals(
1029 					"[src/main/df, mode:100644, content:before replacement]" +
1030 					"[src/main/z, mode:100644, content:z]" +
1031 					"[z, mode:100644, content:z2]",
1032 					indexState(CONTENT));
1033 			FileUtils.delete(new File(db.getWorkTree(), "src/main/df"));
1034 			writeTrashFile("src/main/df", "a", "after replacement");
1035 			writeTrashFile("src/main/df", "b", "unrelated file");
1036 			git.add().addFilepattern("src/main/df/a").call();
1037 			assertEquals(
1038 					"[src/main/df/a, mode:100644, content:after replacement]" +
1039 					"[src/main/z, mode:100644, content:z]" +
1040 					"[z, mode:100644, content:z2]",
1041 					indexState(CONTENT));
1042 		}
1043 	}
1044 
1045 	@Test
1046 	public void testReplaceDirectoryConflictsWithFile()
1047 			throws IOException, NoFilepatternException, GitAPIException {
1048 		DirCache dc = db.lockDirCache();
1049 		try (ObjectInserter oi = db.newObjectInserter()) {
1050 			DirCacheBuilder builder = dc.builder();
1051 			File f = writeTrashFile("a", "df", "content");
1052 			addEntryToBuilder("a", f, oi, builder, 1);
1053 
1054 			f = writeTrashFile("a", "df", "other content");
1055 			addEntryToBuilder("a/df", f, oi, builder, 3);
1056 
1057 			f = writeTrashFile("a", "df", "our content");
1058 			addEntryToBuilder("a/df", f, oi, builder, 2);
1059 
1060 			f = writeTrashFile("z", "z");
1061 			addEntryToBuilder("z", f, oi, builder, 0);
1062 			builder.commit();
1063 		}
1064 		assertEquals(
1065 				"[a, mode:100644, stage:1, content:content]" +
1066 				"[a/df, mode:100644, stage:2, content:our content]" +
1067 				"[a/df, mode:100644, stage:3, content:other content]" +
1068 				"[z, mode:100644, content:z]",
1069 				indexState(CONTENT));
1070 
1071 		try (Git git = new Git(db)) {
1072 			FileUtils.delete(new File(db.getWorkTree(), "a"), RECURSIVE);
1073 			writeTrashFile("a", "merged");
1074 			git.add().addFilepattern("a").call();
1075 			assertEquals("[a, mode:100644, content:merged]" +
1076 					"[z, mode:100644, content:z]",
1077 					indexState(CONTENT));
1078 		}
1079 	}
1080 
1081 	@Test
1082 	public void testExecutableRetention() throws Exception {
1083 		StoredConfig config = db.getConfig();
1084 		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
1085 				ConfigConstants.CONFIG_KEY_FILEMODE, true);
1086 		config.save();
1087 
1088 		FS executableFs = new FS() {
1089 
1090 			@Override
1091 			public boolean supportsExecute() {
1092 				return true;
1093 			}
1094 
1095 			@Override
1096 			public boolean setExecute(File f, boolean canExec) {
1097 				return true;
1098 			}
1099 
1100 			@Override
1101 			public ProcessBuilder runInShell(String cmd, String[] args) {
1102 				return null;
1103 			}
1104 
1105 			@Override
1106 			public boolean retryFailedLockFileCommit() {
1107 				return false;
1108 			}
1109 
1110 			@Override
1111 			public FS newInstance() {
1112 				return this;
1113 			}
1114 
1115 			@Override
1116 			protected File discoverGitExe() {
1117 				return null;
1118 			}
1119 
1120 			@Override
1121 			public boolean canExecute(File f) {
1122 				try {
1123 					return read(f).startsWith("binary:");
1124 				} catch (IOException e) {
1125 					return false;
1126 				}
1127 			}
1128 
1129 			@Override
1130 			public boolean isCaseSensitive() {
1131 				return false;
1132 			}
1133 		};
1134 
1135 		String path = "a.txt";
1136 		String path2 = "a.sh";
1137 		writeTrashFile(path, "content");
1138 		writeTrashFile(path2, "binary: content");
1139 		try (Git git = Git.open(db.getDirectory(), executableFs)) {
1140 			git.add().addFilepattern(path).addFilepattern(path2).call();
1141 			RevCommit commit1 = git.commit().setMessage("commit").call();
1142 			try (TreeWalk walk = new TreeWalk(db)) {
1143 				walk.addTree(commit1.getTree());
1144 				walk.next();
1145 				assertEquals(path2, walk.getPathString());
1146 				assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0));
1147 				walk.next();
1148 				assertEquals(path, walk.getPathString());
1149 				assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0));
1150 			}
1151 		}
1152 		config = db.getConfig();
1153 		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
1154 				ConfigConstants.CONFIG_KEY_FILEMODE, false);
1155 		config.save();
1156 
1157 		writeTrashFile(path2, "content2");
1158 		writeTrashFile(path, "binary: content2");
1159 		try (Git git2 = Git.open(db.getDirectory(), executableFs)) {
1160 			git2.add().addFilepattern(path).addFilepattern(path2).call();
1161 			RevCommit commit2 = git2.commit().setMessage("commit2").call();
1162 			try (TreeWalk walk = new TreeWalk(db)) {
1163 				walk.addTree(commit2.getTree());
1164 				walk.next();
1165 				assertEquals(path2, walk.getPathString());
1166 				assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0));
1167 				walk.next();
1168 				assertEquals(path, walk.getPathString());
1169 				assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0));
1170 			}
1171 		}
1172 	}
1173 
1174 	@Test
1175 	public void testAddGitlink() throws Exception {
1176 		createNestedRepo("git-link-dir");
1177 		try (Git git = new Git(db)) {
1178 			git.add().addFilepattern("git-link-dir").call();
1179 
1180 			assertEquals(
1181 					"[git-link-dir, mode:160000]",
1182 					indexState(0));
1183 			Set<String> untrackedFiles = git.status().call().getUntracked();
1184 			assert (untrackedFiles.isEmpty());
1185 		}
1186 
1187 	}
1188 
1189 	@Test
1190 	public void testAddSubrepoWithDirNoGitlinks() throws Exception {
1191 		createNestedRepo("nested-repo");
1192 
1193 		// Set DIR_NO_GITLINKS
1194 		StoredConfig config = db.getConfig();
1195 		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
1196 				ConfigConstants.CONFIG_KEY_DIRNOGITLINKS, true);
1197 		config.save();
1198 
1199 		assert (db.getConfig().get(WorkingTreeOptions.KEY).isDirNoGitLinks());
1200 
1201 		try (Git git = new Git(db)) {
1202 			git.add().addFilepattern("nested-repo").call();
1203 
1204 			assertEquals(
1205 					"[nested-repo/README1.md, mode:100644]" +
1206 							"[nested-repo/README2.md, mode:100644]",
1207 					indexState(0));
1208 		}
1209 
1210 		// Turn off DIR_NO_GITLINKS, ensure nested-repo is still treated as
1211 		// a normal directory
1212 		// Set DIR_NO_GITLINKS
1213 		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
1214 				ConfigConstants.CONFIG_KEY_DIRNOGITLINKS, false);
1215 		config.save();
1216 
1217 		writeTrashFile("nested-repo", "README3.md", "content");
1218 
1219 		try (Git git = new Git(db)) {
1220 			git.add().addFilepattern("nested-repo").call();
1221 
1222 			assertEquals(
1223 					"[nested-repo/README1.md, mode:100644]" +
1224 							"[nested-repo/README2.md, mode:100644]" +
1225 							"[nested-repo/README3.md, mode:100644]",
1226 					indexState(0));
1227 		}
1228 	}
1229 
1230 	@Test
1231 	public void testAddGitlinkDoesNotChange() throws Exception {
1232 		createNestedRepo("nested-repo");
1233 
1234 		try (Git git = new Git(db)) {
1235 			git.add().addFilepattern("nested-repo").call();
1236 
1237 			assertEquals(
1238 					"[nested-repo, mode:160000]",
1239 					indexState(0));
1240 		}
1241 
1242 		// Set DIR_NO_GITLINKS
1243 		StoredConfig config = db.getConfig();
1244 		config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
1245 				ConfigConstants.CONFIG_KEY_DIRNOGITLINKS, true);
1246 		config.save();
1247 
1248 		assertTrue(
1249 				db.getConfig().get(WorkingTreeOptions.KEY).isDirNoGitLinks());
1250 
1251 		try (Git git = new Git(db)) {
1252 			git.add().addFilepattern("nested-repo").call();
1253 			// with gitlinks ignored, we treat this as a normal directory
1254 			assertEquals(
1255 					"[nested-repo/README1.md, mode:100644][nested-repo/README2.md, mode:100644]",
1256 					indexState(0));
1257 		}
1258 	}
1259 
1260 	private static DirCacheEntry addEntryToBuilder(String path, File file,
1261 			ObjectInserter newObjectInserter, DirCacheBuilder builder, int stage)
1262 			throws IOException {
1263 		ObjectId id;
1264 		try (FileInputStream inputStream = new FileInputStream(file)) {
1265 			id = newObjectInserter.insert(
1266 				Constants.OBJ_BLOB, file.length(), inputStream);
1267 		}
1268 		DirCacheEntry entry = new DirCacheEntry(path, stage);
1269 		entry.setObjectId(id);
1270 		entry.setFileMode(FileMode.REGULAR_FILE);
1271 		entry.setLastModified(FS.DETECTED.lastModifiedInstant(file));
1272 		entry.setLength((int) file.length());
1273 
1274 		builder.add(entry);
1275 		return entry;
1276 	}
1277 
1278 	private void assumeUnchanged(String path) throws IOException {
1279 		final DirCache dirc = db.lockDirCache();
1280 		final DirCacheEntry ent = dirc.getEntry(path);
1281 		if (ent != null)
1282 			ent.setAssumeValid(true);
1283 		dirc.write();
1284 		if (!dirc.commit())
1285 			throw new IOException("could not commit");
1286 	}
1287 
1288 	private void createNestedRepo(String path) throws IOException {
1289 		File gitLinkDir = new File(db.getWorkTree(), path);
1290 		FileUtils.mkdir(gitLinkDir);
1291 
1292 		FileRepositoryBuilder nestedBuilder = new FileRepositoryBuilder();
1293 		nestedBuilder.setWorkTree(gitLinkDir);
1294 
1295 		try (Repository nestedRepo = nestedBuilder.build()) {
1296 			nestedRepo.create();
1297 
1298 			writeTrashFile(path, "README1.md", "content");
1299 			writeTrashFile(path, "README2.md", "content");
1300 
1301 			// Commit these changes in the subrepo
1302 			try (Git git = new Git(nestedRepo)) {
1303 				git.add().addFilepattern(".").call();
1304 				git.commit().setMessage("subrepo commit").call();
1305 			} catch (GitAPIException e) {
1306 				throw new RuntimeException(e);
1307 			}
1308 		}
1309 	}
1310 }