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 35KB

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