You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AddCommandTest.java 42KB

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