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.

IndexDiffTest.java 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * Copyright (C) 2013, Robin Stocker <robin@nibor.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.lib;
  47. import static org.junit.Assert.assertEquals;
  48. import static org.junit.Assert.assertFalse;
  49. import static org.junit.Assert.assertTrue;
  50. import java.io.File;
  51. import java.io.FileNotFoundException;
  52. import java.io.IOException;
  53. import java.util.Arrays;
  54. import java.util.Collections;
  55. import java.util.HashSet;
  56. import java.util.TreeSet;
  57. import org.eclipse.jgit.api.Git;
  58. import org.eclipse.jgit.api.MergeResult;
  59. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  60. import org.eclipse.jgit.api.errors.GitAPIException;
  61. import org.eclipse.jgit.dircache.DirCache;
  62. import org.eclipse.jgit.dircache.DirCacheBuilder;
  63. import org.eclipse.jgit.dircache.DirCacheEditor;
  64. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  65. import org.eclipse.jgit.dircache.DirCacheEntry;
  66. import org.eclipse.jgit.junit.RepositoryTestCase;
  67. import org.eclipse.jgit.lib.CoreConfig.AutoCRLF;
  68. import org.eclipse.jgit.lib.IndexDiff.StageState;
  69. import org.eclipse.jgit.merge.MergeStrategy;
  70. import org.eclipse.jgit.revwalk.RevCommit;
  71. import org.eclipse.jgit.storage.file.FileBasedConfig;
  72. import org.eclipse.jgit.treewalk.FileTreeIterator;
  73. import org.eclipse.jgit.util.IO;
  74. import org.junit.Test;
  75. public class IndexDiffTest extends RepositoryTestCase {
  76. static PathEdit add(final Repository db, final File workdir,
  77. final String path) throws FileNotFoundException, IOException {
  78. ObjectInserter inserter = db.newObjectInserter();
  79. final File f = new File(workdir, path);
  80. final ObjectId id = inserter.insert(Constants.OBJ_BLOB,
  81. IO.readFully(f));
  82. return new PathEdit(path) {
  83. public void apply(DirCacheEntry ent) {
  84. ent.setFileMode(FileMode.REGULAR_FILE);
  85. ent.setLength(f.length());
  86. ent.setObjectId(id);
  87. }
  88. };
  89. }
  90. @Test
  91. public void testAdded() throws IOException {
  92. writeTrashFile("file1", "file1");
  93. writeTrashFile("dir/subfile", "dir/subfile");
  94. ObjectId tree = insertTree(new TreeFormatter());
  95. DirCache index = db.lockDirCache();
  96. DirCacheEditor editor = index.editor();
  97. editor.add(add(db, trash, "file1"));
  98. editor.add(add(db, trash, "dir/subfile"));
  99. editor.commit();
  100. FileTreeIterator iterator = new FileTreeIterator(db);
  101. IndexDiff diff = new IndexDiff(db, tree, iterator);
  102. diff.diff();
  103. assertEquals(2, diff.getAdded().size());
  104. assertTrue(diff.getAdded().contains("file1"));
  105. assertTrue(diff.getAdded().contains("dir/subfile"));
  106. assertEquals(0, diff.getChanged().size());
  107. assertEquals(0, diff.getModified().size());
  108. assertEquals(0, diff.getRemoved().size());
  109. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  110. }
  111. @Test
  112. public void testRemoved() throws IOException {
  113. writeTrashFile("file2", "file2");
  114. writeTrashFile("dir/file3", "dir/file3");
  115. TreeFormatter dir = new TreeFormatter();
  116. dir.append("file3", FileMode.REGULAR_FILE, ObjectId.fromString("873fb8d667d05436d728c52b1d7a09528e6eb59b"));
  117. TreeFormatter tree = new TreeFormatter();
  118. tree.append("file2", FileMode.REGULAR_FILE, ObjectId.fromString("30d67d4672d5c05833b7192cc77a79eaafb5c7ad"));
  119. tree.append("dir", FileMode.TREE, insertTree(dir));
  120. ObjectId treeId = insertTree(tree);
  121. FileTreeIterator iterator = new FileTreeIterator(db);
  122. IndexDiff diff = new IndexDiff(db, treeId, iterator);
  123. diff.diff();
  124. assertEquals(2, diff.getRemoved().size());
  125. assertTrue(diff.getRemoved().contains("file2"));
  126. assertTrue(diff.getRemoved().contains("dir/file3"));
  127. assertEquals(0, diff.getChanged().size());
  128. assertEquals(0, diff.getModified().size());
  129. assertEquals(0, diff.getAdded().size());
  130. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  131. }
  132. @Test
  133. public void testModified() throws IOException, GitAPIException {
  134. writeTrashFile("file2", "file2");
  135. writeTrashFile("dir/file3", "dir/file3");
  136. try (Git git = new Git(db)) {
  137. git.add().addFilepattern("file2").addFilepattern("dir/file3").call();
  138. }
  139. writeTrashFile("dir/file3", "changed");
  140. TreeFormatter dir = new TreeFormatter();
  141. dir.append("file3", FileMode.REGULAR_FILE, ObjectId.fromString("0123456789012345678901234567890123456789"));
  142. TreeFormatter tree = new TreeFormatter();
  143. tree.append("dir", FileMode.TREE, insertTree(dir));
  144. tree.append("file2", FileMode.REGULAR_FILE, ObjectId.fromString("0123456789012345678901234567890123456789"));
  145. ObjectId treeId = insertTree(tree);
  146. FileTreeIterator iterator = new FileTreeIterator(db);
  147. IndexDiff diff = new IndexDiff(db, treeId, iterator);
  148. diff.diff();
  149. assertEquals(2, diff.getChanged().size());
  150. assertTrue(diff.getChanged().contains("file2"));
  151. assertTrue(diff.getChanged().contains("dir/file3"));
  152. assertEquals(1, diff.getModified().size());
  153. assertTrue(diff.getModified().contains("dir/file3"));
  154. assertEquals(0, diff.getAdded().size());
  155. assertEquals(0, diff.getRemoved().size());
  156. assertEquals(0, diff.getMissing().size());
  157. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  158. }
  159. @Test
  160. public void testConflicting() throws Exception {
  161. try (Git git = new Git(db)) {
  162. writeTrashFile("a", "1\na\n3\n");
  163. writeTrashFile("b", "1\nb\n3\n");
  164. git.add().addFilepattern("a").addFilepattern("b").call();
  165. RevCommit initialCommit = git.commit().setMessage("initial").call();
  166. // create side branch with two modifications
  167. createBranch(initialCommit, "refs/heads/side");
  168. checkoutBranch("refs/heads/side");
  169. writeTrashFile("a", "1\na(side)\n3\n");
  170. writeTrashFile("b", "1\nb\n3\n(side)");
  171. git.add().addFilepattern("a").addFilepattern("b").call();
  172. RevCommit secondCommit = git.commit().setMessage("side").call();
  173. // update a on master to generate conflict
  174. checkoutBranch("refs/heads/master");
  175. writeTrashFile("a", "1\na(main)\n3\n");
  176. git.add().addFilepattern("a").call();
  177. git.commit().setMessage("main").call();
  178. // merge side with master
  179. MergeResult result = git.merge().include(secondCommit.getId())
  180. .setStrategy(MergeStrategy.RESOLVE).call();
  181. assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
  182. }
  183. FileTreeIterator iterator = new FileTreeIterator(db);
  184. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  185. diff.diff();
  186. assertEquals("[b]",
  187. new TreeSet<String>(diff.getChanged()).toString());
  188. assertEquals("[]", diff.getAdded().toString());
  189. assertEquals("[]", diff.getRemoved().toString());
  190. assertEquals("[]", diff.getMissing().toString());
  191. assertEquals("[]", diff.getModified().toString());
  192. assertEquals("[a]", diff.getConflicting().toString());
  193. assertEquals(StageState.BOTH_MODIFIED,
  194. diff.getConflictingStageStates().get("a"));
  195. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  196. }
  197. @Test
  198. public void testConflictingDeletedAndModified() throws Exception {
  199. try (Git git = new Git(db)) {
  200. writeTrashFile("a", "1\na\n3\n");
  201. writeTrashFile("b", "1\nb\n3\n");
  202. git.add().addFilepattern("a").addFilepattern("b").call();
  203. RevCommit initialCommit = git.commit().setMessage("initial").call();
  204. // create side branch and delete "a"
  205. createBranch(initialCommit, "refs/heads/side");
  206. checkoutBranch("refs/heads/side");
  207. git.rm().addFilepattern("a").call();
  208. RevCommit secondCommit = git.commit().setMessage("side").call();
  209. // update a on master to generate conflict
  210. checkoutBranch("refs/heads/master");
  211. writeTrashFile("a", "1\na(main)\n3\n");
  212. git.add().addFilepattern("a").call();
  213. git.commit().setMessage("main").call();
  214. // merge side with master
  215. MergeResult result = git.merge().include(secondCommit.getId())
  216. .setStrategy(MergeStrategy.RESOLVE).call();
  217. assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
  218. }
  219. FileTreeIterator iterator = new FileTreeIterator(db);
  220. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  221. diff.diff();
  222. assertEquals("[]", new TreeSet<String>(diff.getChanged()).toString());
  223. assertEquals("[]", diff.getAdded().toString());
  224. assertEquals("[]", diff.getRemoved().toString());
  225. assertEquals("[]", diff.getMissing().toString());
  226. assertEquals("[]", diff.getModified().toString());
  227. assertEquals("[a]", diff.getConflicting().toString());
  228. assertEquals(StageState.DELETED_BY_THEM,
  229. diff.getConflictingStageStates().get("a"));
  230. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  231. }
  232. @Test
  233. public void testConflictingFromMultipleCreations() throws Exception {
  234. try (Git git = new Git(db)) {
  235. writeTrashFile("a", "1\na\n3\n");
  236. git.add().addFilepattern("a").call();
  237. RevCommit initialCommit = git.commit().setMessage("initial").call();
  238. createBranch(initialCommit, "refs/heads/side");
  239. checkoutBranch("refs/heads/side");
  240. writeTrashFile("b", "1\nb(side)\n3\n");
  241. git.add().addFilepattern("b").call();
  242. RevCommit secondCommit = git.commit().setMessage("side").call();
  243. checkoutBranch("refs/heads/master");
  244. writeTrashFile("b", "1\nb(main)\n3\n");
  245. git.add().addFilepattern("b").call();
  246. git.commit().setMessage("main").call();
  247. MergeResult result = git.merge().include(secondCommit.getId())
  248. .setStrategy(MergeStrategy.RESOLVE).call();
  249. assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
  250. }
  251. FileTreeIterator iterator = new FileTreeIterator(db);
  252. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  253. diff.diff();
  254. assertEquals("[]", new TreeSet<String>(diff.getChanged()).toString());
  255. assertEquals("[]", diff.getAdded().toString());
  256. assertEquals("[]", diff.getRemoved().toString());
  257. assertEquals("[]", diff.getMissing().toString());
  258. assertEquals("[]", diff.getModified().toString());
  259. assertEquals("[b]", diff.getConflicting().toString());
  260. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  261. }
  262. @Test
  263. public void testUnchangedSimple() throws IOException, GitAPIException {
  264. writeTrashFile("a.b", "a.b");
  265. writeTrashFile("a.c", "a.c");
  266. writeTrashFile("a=c", "a=c");
  267. writeTrashFile("a=d", "a=d");
  268. try (Git git = new Git(db)) {
  269. git.add().addFilepattern("a.b").call();
  270. git.add().addFilepattern("a.c").call();
  271. git.add().addFilepattern("a=c").call();
  272. git.add().addFilepattern("a=d").call();
  273. }
  274. TreeFormatter tree = new TreeFormatter();
  275. // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
  276. tree.append("a.b", FileMode.REGULAR_FILE, ObjectId.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
  277. tree.append("a.c", FileMode.REGULAR_FILE, ObjectId.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
  278. tree.append("a=c", FileMode.REGULAR_FILE, ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714"));
  279. tree.append("a=d", FileMode.REGULAR_FILE, ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
  280. ObjectId treeId = insertTree(tree);
  281. FileTreeIterator iterator = new FileTreeIterator(db);
  282. IndexDiff diff = new IndexDiff(db, treeId, iterator);
  283. diff.diff();
  284. assertEquals(0, diff.getChanged().size());
  285. assertEquals(0, diff.getAdded().size());
  286. assertEquals(0, diff.getRemoved().size());
  287. assertEquals(0, diff.getMissing().size());
  288. assertEquals(0, diff.getModified().size());
  289. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  290. }
  291. /**
  292. * This test has both files and directories that involve the tricky ordering
  293. * used by Git.
  294. *
  295. * @throws IOException
  296. * @throws GitAPIException
  297. */
  298. @Test
  299. public void testUnchangedComplex() throws IOException, GitAPIException {
  300. writeTrashFile("a.b", "a.b");
  301. writeTrashFile("a.c", "a.c");
  302. writeTrashFile("a/b.b/b", "a/b.b/b");
  303. writeTrashFile("a/b", "a/b");
  304. writeTrashFile("a/c", "a/c");
  305. writeTrashFile("a=c", "a=c");
  306. writeTrashFile("a=d", "a=d");
  307. try (Git git = new Git(db)) {
  308. git.add().addFilepattern("a.b").addFilepattern("a.c")
  309. .addFilepattern("a/b.b/b").addFilepattern("a/b")
  310. .addFilepattern("a/c").addFilepattern("a=c")
  311. .addFilepattern("a=d").call();
  312. }
  313. // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
  314. TreeFormatter bb = new TreeFormatter();
  315. bb.append("b", FileMode.REGULAR_FILE, ObjectId.fromString("8d840bd4e2f3a48ff417c8e927d94996849933fd"));
  316. TreeFormatter a = new TreeFormatter();
  317. a.append("b", FileMode.REGULAR_FILE, ObjectId
  318. .fromString("db89c972fc57862eae378f45b74aca228037d415"));
  319. a.append("b.b", FileMode.TREE, insertTree(bb));
  320. a.append("c", FileMode.REGULAR_FILE, ObjectId.fromString("52ad142a008aeb39694bafff8e8f1be75ed7f007"));
  321. TreeFormatter tree = new TreeFormatter();
  322. tree.append("a.b", FileMode.REGULAR_FILE, ObjectId.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
  323. tree.append("a.c", FileMode.REGULAR_FILE, ObjectId.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
  324. tree.append("a", FileMode.TREE, insertTree(a));
  325. tree.append("a=c", FileMode.REGULAR_FILE, ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714"));
  326. tree.append("a=d", FileMode.REGULAR_FILE, ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
  327. ObjectId treeId = insertTree(tree);
  328. FileTreeIterator iterator = new FileTreeIterator(db);
  329. IndexDiff diff = new IndexDiff(db, treeId, iterator);
  330. diff.diff();
  331. assertEquals(0, diff.getChanged().size());
  332. assertEquals(0, diff.getAdded().size());
  333. assertEquals(0, diff.getRemoved().size());
  334. assertEquals(0, diff.getMissing().size());
  335. assertEquals(0, diff.getModified().size());
  336. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  337. }
  338. private ObjectId insertTree(TreeFormatter tree) throws IOException {
  339. try (ObjectInserter oi = db.newObjectInserter()) {
  340. ObjectId id = oi.insert(tree);
  341. oi.flush();
  342. return id;
  343. }
  344. }
  345. /**
  346. * A file is removed from the index but stays in the working directory. It
  347. * is checked if IndexDiff detects this file as removed and untracked.
  348. *
  349. * @throws Exception
  350. */
  351. @Test
  352. public void testRemovedUntracked() throws Exception{
  353. String path = "file";
  354. try (Git git = new Git(db)) {
  355. writeTrashFile(path, "content");
  356. git.add().addFilepattern(path).call();
  357. git.commit().setMessage("commit").call();
  358. }
  359. removeFromIndex(path);
  360. FileTreeIterator iterator = new FileTreeIterator(db);
  361. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  362. diff.diff();
  363. assertTrue(diff.getRemoved().contains(path));
  364. assertTrue(diff.getUntracked().contains(path));
  365. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  366. }
  367. /**
  368. *
  369. * @throws Exception
  370. */
  371. @Test
  372. public void testUntrackedFolders() throws Exception {
  373. try (Git git = new Git(db)) {
  374. IndexDiff diff = new IndexDiff(db, Constants.HEAD,
  375. new FileTreeIterator(db));
  376. diff.diff();
  377. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  378. writeTrashFile("readme", "");
  379. writeTrashFile("src/com/A.java", "");
  380. writeTrashFile("src/com/B.java", "");
  381. writeTrashFile("src/org/A.java", "");
  382. writeTrashFile("src/org/B.java", "");
  383. writeTrashFile("target/com/A.java", "");
  384. writeTrashFile("target/com/B.java", "");
  385. writeTrashFile("target/org/A.java", "");
  386. writeTrashFile("target/org/B.java", "");
  387. git.add().addFilepattern("src").addFilepattern("readme").call();
  388. git.commit().setMessage("initial").call();
  389. diff = new IndexDiff(db, Constants.HEAD,
  390. new FileTreeIterator(db));
  391. diff.diff();
  392. assertEquals(new HashSet<String>(Arrays.asList("target")),
  393. diff.getUntrackedFolders());
  394. writeTrashFile("src/tst/A.java", "");
  395. writeTrashFile("src/tst/B.java", "");
  396. diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
  397. diff.diff();
  398. assertEquals(new HashSet<String>(Arrays.asList("target", "src/tst")),
  399. diff.getUntrackedFolders());
  400. git.rm().addFilepattern("src/com/B.java").addFilepattern("src/org")
  401. .call();
  402. git.commit().setMessage("second").call();
  403. writeTrashFile("src/org/C.java", "");
  404. diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
  405. diff.diff();
  406. assertEquals(
  407. new HashSet<String>(Arrays.asList("src/org", "src/tst",
  408. "target")),
  409. diff.getUntrackedFolders());
  410. }
  411. }
  412. /**
  413. * Test that ignored folders aren't listed as untracked
  414. *
  415. * @throws Exception
  416. */
  417. @Test
  418. public void testUntrackedNotIgnoredFolders() throws Exception {
  419. try (Git git = new Git(db)) {
  420. IndexDiff diff = new IndexDiff(db, Constants.HEAD,
  421. new FileTreeIterator(db));
  422. diff.diff();
  423. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  424. writeTrashFile("readme", "");
  425. writeTrashFile("sr/com/X.java", "");
  426. writeTrashFile("src/com/A.java", "");
  427. writeTrashFile("src/org/B.java", "");
  428. writeTrashFile("srcs/org/Y.java", "");
  429. writeTrashFile("target/com/A.java", "");
  430. writeTrashFile("target/org/B.java", "");
  431. writeTrashFile(".gitignore", "/target\n/sr");
  432. git.add().addFilepattern("readme").addFilepattern(".gitignore")
  433. .addFilepattern("srcs/").call();
  434. git.commit().setMessage("initial").call();
  435. diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
  436. diff.diff();
  437. assertEquals(new HashSet<String>(Arrays.asList("src")),
  438. diff.getUntrackedFolders());
  439. git.add().addFilepattern("src").call();
  440. writeTrashFile("sr/com/X1.java", "");
  441. writeTrashFile("src/tst/A.java", "");
  442. writeTrashFile("src/tst/B.java", "");
  443. writeTrashFile("srcs/com/Y1.java", "");
  444. deleteTrashFile(".gitignore");
  445. diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
  446. diff.diff();
  447. assertEquals(
  448. new HashSet<String>(Arrays.asList("srcs/com", "sr", "src/tst",
  449. "target")),
  450. diff.getUntrackedFolders());
  451. }
  452. }
  453. @Test
  454. public void testAssumeUnchanged() throws Exception {
  455. try (Git git = new Git(db)) {
  456. String path = "file";
  457. writeTrashFile(path, "content");
  458. git.add().addFilepattern(path).call();
  459. String path2 = "file2";
  460. writeTrashFile(path2, "content");
  461. String path3 = "file3";
  462. writeTrashFile(path3, "some content");
  463. git.add().addFilepattern(path2).addFilepattern(path3).call();
  464. git.commit().setMessage("commit").call();
  465. assumeUnchanged(path2);
  466. assumeUnchanged(path3);
  467. writeTrashFile(path, "more content");
  468. deleteTrashFile(path3);
  469. FileTreeIterator iterator = new FileTreeIterator(db);
  470. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  471. diff.diff();
  472. assertEquals(2, diff.getAssumeUnchanged().size());
  473. assertEquals(1, diff.getModified().size());
  474. assertEquals(0, diff.getChanged().size());
  475. assertTrue(diff.getAssumeUnchanged().contains("file2"));
  476. assertTrue(diff.getAssumeUnchanged().contains("file3"));
  477. assertTrue(diff.getModified().contains("file"));
  478. git.add().addFilepattern(".").call();
  479. iterator = new FileTreeIterator(db);
  480. diff = new IndexDiff(db, Constants.HEAD, iterator);
  481. diff.diff();
  482. assertEquals(2, diff.getAssumeUnchanged().size());
  483. assertEquals(0, diff.getModified().size());
  484. assertEquals(1, diff.getChanged().size());
  485. assertTrue(diff.getAssumeUnchanged().contains("file2"));
  486. assertTrue(diff.getAssumeUnchanged().contains("file3"));
  487. assertTrue(diff.getChanged().contains("file"));
  488. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  489. }
  490. }
  491. @Test
  492. public void testStageState() throws IOException {
  493. final int base = DirCacheEntry.STAGE_1;
  494. final int ours = DirCacheEntry.STAGE_2;
  495. final int theirs = DirCacheEntry.STAGE_3;
  496. verifyStageState(StageState.BOTH_DELETED, base);
  497. verifyStageState(StageState.DELETED_BY_THEM, ours, base);
  498. verifyStageState(StageState.DELETED_BY_US, base, theirs);
  499. verifyStageState(StageState.BOTH_MODIFIED, base, ours, theirs);
  500. verifyStageState(StageState.ADDED_BY_US, ours);
  501. verifyStageState(StageState.BOTH_ADDED, ours, theirs);
  502. verifyStageState(StageState.ADDED_BY_THEM, theirs);
  503. assertTrue(StageState.BOTH_DELETED.hasBase());
  504. assertFalse(StageState.BOTH_DELETED.hasOurs());
  505. assertFalse(StageState.BOTH_DELETED.hasTheirs());
  506. assertFalse(StageState.BOTH_ADDED.hasBase());
  507. assertTrue(StageState.BOTH_ADDED.hasOurs());
  508. assertTrue(StageState.BOTH_ADDED.hasTheirs());
  509. }
  510. @Test
  511. public void testStageState_mergeAndReset_bug() throws Exception {
  512. try (Git git = new Git(db)) {
  513. writeTrashFile("a", "content");
  514. git.add().addFilepattern("a").call();
  515. RevCommit initialCommit = git.commit().setMessage("initial commit")
  516. .call();
  517. // create branch and add a new file
  518. final String branchName = Constants.R_HEADS + "branch";
  519. createBranch(initialCommit, branchName);
  520. checkoutBranch(branchName);
  521. writeTrashFile("b", "second file content - branch");
  522. git.add().addFilepattern("b").call();
  523. RevCommit branchCommit = git.commit().setMessage("branch commit")
  524. .call();
  525. // checkout master and add the same new file
  526. checkoutBranch(Constants.R_HEADS + Constants.MASTER);
  527. writeTrashFile("b", "second file content - master");
  528. git.add().addFilepattern("b").call();
  529. git.commit().setMessage("master commit").call();
  530. // try and merge
  531. MergeResult result = git.merge().include(branchCommit).call();
  532. assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
  533. FileTreeIterator iterator = new FileTreeIterator(db);
  534. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  535. diff.diff();
  536. assertTrue(diff.getChanged().isEmpty());
  537. assertTrue(diff.getAdded().isEmpty());
  538. assertTrue(diff.getRemoved().isEmpty());
  539. assertTrue(diff.getMissing().isEmpty());
  540. assertTrue(diff.getModified().isEmpty());
  541. assertEquals(1, diff.getConflicting().size());
  542. assertTrue(diff.getConflicting().contains("b"));
  543. assertEquals(StageState.BOTH_ADDED, diff.getConflictingStageStates()
  544. .get("b"));
  545. assertTrue(diff.getUntrackedFolders().isEmpty());
  546. // reset file b to its master state without altering the index
  547. writeTrashFile("b", "second file content - master");
  548. // we should have the same result
  549. iterator = new FileTreeIterator(db);
  550. diff = new IndexDiff(db, Constants.HEAD, iterator);
  551. diff.diff();
  552. assertTrue(diff.getChanged().isEmpty());
  553. assertTrue(diff.getAdded().isEmpty());
  554. assertTrue(diff.getRemoved().isEmpty());
  555. assertTrue(diff.getMissing().isEmpty());
  556. assertTrue(diff.getModified().isEmpty());
  557. assertEquals(1, diff.getConflicting().size());
  558. assertTrue(diff.getConflicting().contains("b"));
  559. assertEquals(StageState.BOTH_ADDED, diff.getConflictingStageStates()
  560. .get("b"));
  561. assertTrue(diff.getUntrackedFolders().isEmpty());
  562. }
  563. }
  564. @Test
  565. public void testStageState_simulated_bug() throws Exception {
  566. try (Git git = new Git(db)) {
  567. writeTrashFile("a", "content");
  568. git.add().addFilepattern("a").call();
  569. RevCommit initialCommit = git.commit().setMessage("initial commit")
  570. .call();
  571. // create branch and add a new file
  572. final String branchName = Constants.R_HEADS + "branch";
  573. createBranch(initialCommit, branchName);
  574. checkoutBranch(branchName);
  575. writeTrashFile("b", "second file content - branch");
  576. git.add().addFilepattern("b").call();
  577. git.commit().setMessage("branch commit")
  578. .call();
  579. // checkout master and add the same new file
  580. checkoutBranch(Constants.R_HEADS + Constants.MASTER);
  581. writeTrashFile("b", "second file content - master");
  582. git.add().addFilepattern("b").call();
  583. git.commit().setMessage("master commit").call();
  584. // Simulate a failed merge of branch into master
  585. DirCacheBuilder builder = db.lockDirCache().builder();
  586. DirCacheEntry entry = createEntry("a", FileMode.REGULAR_FILE, 0,
  587. "content");
  588. builder.add(entry);
  589. entry = createEntry("b", FileMode.REGULAR_FILE, 2,
  590. "second file content - master");
  591. builder.add(entry);
  592. entry = createEntry("b", FileMode.REGULAR_FILE, 3,
  593. "second file content - branch");
  594. builder.add(entry);
  595. builder.commit();
  596. FileTreeIterator iterator = new FileTreeIterator(db);
  597. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  598. diff.diff();
  599. assertTrue(diff.getChanged().isEmpty());
  600. assertTrue(diff.getAdded().isEmpty());
  601. assertTrue(diff.getRemoved().isEmpty());
  602. assertTrue(diff.getMissing().isEmpty());
  603. assertTrue(diff.getModified().isEmpty());
  604. assertEquals(1, diff.getConflicting().size());
  605. assertTrue(diff.getConflicting().contains("b"));
  606. assertEquals(StageState.BOTH_ADDED, diff.getConflictingStageStates()
  607. .get("b"));
  608. assertTrue(diff.getUntrackedFolders().isEmpty());
  609. }
  610. }
  611. @Test
  612. public void testAutoCRLFInput() throws Exception {
  613. try (Git git = new Git(db)) {
  614. FileBasedConfig config = db.getConfig();
  615. // Make sure core.autocrlf is false before adding
  616. config.setEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  617. ConfigConstants.CONFIG_KEY_AUTOCRLF, AutoCRLF.FALSE);
  618. config.save();
  619. // File is already in repository with CRLF
  620. writeTrashFile("crlf.txt", "this\r\ncontains\r\ncrlf\r\n");
  621. git.add().addFilepattern("crlf.txt").call();
  622. git.commit().setMessage("Add crlf.txt").call();
  623. // Now set core.autocrlf to input
  624. config.setEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  625. ConfigConstants.CONFIG_KEY_AUTOCRLF, AutoCRLF.INPUT);
  626. config.save();
  627. FileTreeIterator iterator = new FileTreeIterator(db);
  628. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  629. diff.diff();
  630. assertTrue(
  631. "Expected no modified files, but there were: "
  632. + diff.getModified(), diff.getModified().isEmpty());
  633. }
  634. }
  635. private void verifyStageState(StageState expected, int... stages)
  636. throws IOException {
  637. DirCacheBuilder builder = db.lockDirCache().builder();
  638. for (int stage : stages) {
  639. DirCacheEntry entry = createEntry("a", FileMode.REGULAR_FILE,
  640. stage, "content");
  641. builder.add(entry);
  642. }
  643. builder.commit();
  644. IndexDiff diff = new IndexDiff(db, Constants.HEAD,
  645. new FileTreeIterator(db));
  646. diff.diff();
  647. assertEquals(
  648. "Conflict for entries in stages " + Arrays.toString(stages),
  649. expected, diff.getConflictingStageStates().get("a"));
  650. }
  651. private void removeFromIndex(String path) throws IOException {
  652. final DirCache dirc = db.lockDirCache();
  653. final DirCacheEditor edit = dirc.editor();
  654. edit.add(new DirCacheEditor.DeletePath(path));
  655. if (!edit.commit())
  656. throw new IOException("could not commit");
  657. }
  658. private void assumeUnchanged(String path) throws IOException {
  659. final DirCache dirc = db.lockDirCache();
  660. final DirCacheEntry ent = dirc.getEntry(path);
  661. if (ent != null)
  662. ent.setAssumeValid(true);
  663. dirc.write();
  664. if (!dirc.commit())
  665. throw new IOException("could not commit");
  666. }
  667. }