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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. @Override
  84. public void apply(DirCacheEntry ent) {
  85. ent.setFileMode(FileMode.REGULAR_FILE);
  86. ent.setLength(f.length());
  87. ent.setObjectId(id);
  88. }
  89. };
  90. }
  91. @Test
  92. public void testAdded() throws IOException {
  93. writeTrashFile("file1", "file1");
  94. writeTrashFile("dir/subfile", "dir/subfile");
  95. ObjectId tree = insertTree(new TreeFormatter());
  96. DirCache index = db.lockDirCache();
  97. DirCacheEditor editor = index.editor();
  98. editor.add(add(db, trash, "file1"));
  99. editor.add(add(db, trash, "dir/subfile"));
  100. editor.commit();
  101. FileTreeIterator iterator = new FileTreeIterator(db);
  102. IndexDiff diff = new IndexDiff(db, tree, iterator);
  103. diff.diff();
  104. assertEquals(2, diff.getAdded().size());
  105. assertTrue(diff.getAdded().contains("file1"));
  106. assertTrue(diff.getAdded().contains("dir/subfile"));
  107. assertEquals(0, diff.getChanged().size());
  108. assertEquals(0, diff.getModified().size());
  109. assertEquals(0, diff.getRemoved().size());
  110. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  111. }
  112. @Test
  113. public void testRemoved() throws IOException {
  114. writeTrashFile("file2", "file2");
  115. writeTrashFile("dir/file3", "dir/file3");
  116. TreeFormatter dir = new TreeFormatter();
  117. dir.append("file3", FileMode.REGULAR_FILE, ObjectId.fromString("873fb8d667d05436d728c52b1d7a09528e6eb59b"));
  118. TreeFormatter tree = new TreeFormatter();
  119. tree.append("file2", FileMode.REGULAR_FILE, ObjectId.fromString("30d67d4672d5c05833b7192cc77a79eaafb5c7ad"));
  120. tree.append("dir", FileMode.TREE, insertTree(dir));
  121. ObjectId treeId = insertTree(tree);
  122. FileTreeIterator iterator = new FileTreeIterator(db);
  123. IndexDiff diff = new IndexDiff(db, treeId, iterator);
  124. diff.diff();
  125. assertEquals(2, diff.getRemoved().size());
  126. assertTrue(diff.getRemoved().contains("file2"));
  127. assertTrue(diff.getRemoved().contains("dir/file3"));
  128. assertEquals(0, diff.getChanged().size());
  129. assertEquals(0, diff.getModified().size());
  130. assertEquals(0, diff.getAdded().size());
  131. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  132. }
  133. @Test
  134. public void testModified() throws IOException, GitAPIException {
  135. writeTrashFile("file2", "file2");
  136. writeTrashFile("dir/file3", "dir/file3");
  137. try (Git git = new Git(db)) {
  138. git.add().addFilepattern("file2").addFilepattern("dir/file3").call();
  139. }
  140. writeTrashFile("dir/file3", "changed");
  141. TreeFormatter dir = new TreeFormatter();
  142. dir.append("file3", FileMode.REGULAR_FILE, ObjectId.fromString("0123456789012345678901234567890123456789"));
  143. TreeFormatter tree = new TreeFormatter();
  144. tree.append("dir", FileMode.TREE, insertTree(dir));
  145. tree.append("file2", FileMode.REGULAR_FILE, ObjectId.fromString("0123456789012345678901234567890123456789"));
  146. ObjectId treeId = insertTree(tree);
  147. FileTreeIterator iterator = new FileTreeIterator(db);
  148. IndexDiff diff = new IndexDiff(db, treeId, iterator);
  149. diff.diff();
  150. assertEquals(2, diff.getChanged().size());
  151. assertTrue(diff.getChanged().contains("file2"));
  152. assertTrue(diff.getChanged().contains("dir/file3"));
  153. assertEquals(1, diff.getModified().size());
  154. assertTrue(diff.getModified().contains("dir/file3"));
  155. assertEquals(0, diff.getAdded().size());
  156. assertEquals(0, diff.getRemoved().size());
  157. assertEquals(0, diff.getMissing().size());
  158. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  159. }
  160. @Test
  161. public void testConflicting() throws Exception {
  162. try (Git git = new Git(db)) {
  163. writeTrashFile("a", "1\na\n3\n");
  164. writeTrashFile("b", "1\nb\n3\n");
  165. git.add().addFilepattern("a").addFilepattern("b").call();
  166. RevCommit initialCommit = git.commit().setMessage("initial").call();
  167. // create side branch with two modifications
  168. createBranch(initialCommit, "refs/heads/side");
  169. checkoutBranch("refs/heads/side");
  170. writeTrashFile("a", "1\na(side)\n3\n");
  171. writeTrashFile("b", "1\nb\n3\n(side)");
  172. git.add().addFilepattern("a").addFilepattern("b").call();
  173. RevCommit secondCommit = git.commit().setMessage("side").call();
  174. // update a on master to generate conflict
  175. checkoutBranch("refs/heads/master");
  176. writeTrashFile("a", "1\na(main)\n3\n");
  177. git.add().addFilepattern("a").call();
  178. git.commit().setMessage("main").call();
  179. // merge side with master
  180. MergeResult result = git.merge().include(secondCommit.getId())
  181. .setStrategy(MergeStrategy.RESOLVE).call();
  182. assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
  183. }
  184. FileTreeIterator iterator = new FileTreeIterator(db);
  185. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  186. diff.diff();
  187. assertEquals("[b]",
  188. new TreeSet<>(diff.getChanged()).toString());
  189. assertEquals("[]", diff.getAdded().toString());
  190. assertEquals("[]", diff.getRemoved().toString());
  191. assertEquals("[]", diff.getMissing().toString());
  192. assertEquals("[]", diff.getModified().toString());
  193. assertEquals("[a]", diff.getConflicting().toString());
  194. assertEquals(StageState.BOTH_MODIFIED,
  195. diff.getConflictingStageStates().get("a"));
  196. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  197. }
  198. @Test
  199. public void testConflictingDeletedAndModified() throws Exception {
  200. try (Git git = new Git(db)) {
  201. writeTrashFile("a", "1\na\n3\n");
  202. writeTrashFile("b", "1\nb\n3\n");
  203. git.add().addFilepattern("a").addFilepattern("b").call();
  204. RevCommit initialCommit = git.commit().setMessage("initial").call();
  205. // create side branch and delete "a"
  206. createBranch(initialCommit, "refs/heads/side");
  207. checkoutBranch("refs/heads/side");
  208. git.rm().addFilepattern("a").call();
  209. RevCommit secondCommit = git.commit().setMessage("side").call();
  210. // update a on master to generate conflict
  211. checkoutBranch("refs/heads/master");
  212. writeTrashFile("a", "1\na(main)\n3\n");
  213. git.add().addFilepattern("a").call();
  214. git.commit().setMessage("main").call();
  215. // merge side with master
  216. MergeResult result = git.merge().include(secondCommit.getId())
  217. .setStrategy(MergeStrategy.RESOLVE).call();
  218. assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
  219. }
  220. FileTreeIterator iterator = new FileTreeIterator(db);
  221. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  222. diff.diff();
  223. assertEquals("[]", new TreeSet<>(diff.getChanged()).toString());
  224. assertEquals("[]", diff.getAdded().toString());
  225. assertEquals("[]", diff.getRemoved().toString());
  226. assertEquals("[]", diff.getMissing().toString());
  227. assertEquals("[]", diff.getModified().toString());
  228. assertEquals("[a]", diff.getConflicting().toString());
  229. assertEquals(StageState.DELETED_BY_THEM,
  230. diff.getConflictingStageStates().get("a"));
  231. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  232. }
  233. @Test
  234. public void testConflictingFromMultipleCreations() throws Exception {
  235. try (Git git = new Git(db)) {
  236. writeTrashFile("a", "1\na\n3\n");
  237. git.add().addFilepattern("a").call();
  238. RevCommit initialCommit = git.commit().setMessage("initial").call();
  239. createBranch(initialCommit, "refs/heads/side");
  240. checkoutBranch("refs/heads/side");
  241. writeTrashFile("b", "1\nb(side)\n3\n");
  242. git.add().addFilepattern("b").call();
  243. RevCommit secondCommit = git.commit().setMessage("side").call();
  244. checkoutBranch("refs/heads/master");
  245. writeTrashFile("b", "1\nb(main)\n3\n");
  246. git.add().addFilepattern("b").call();
  247. git.commit().setMessage("main").call();
  248. MergeResult result = git.merge().include(secondCommit.getId())
  249. .setStrategy(MergeStrategy.RESOLVE).call();
  250. assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
  251. }
  252. FileTreeIterator iterator = new FileTreeIterator(db);
  253. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  254. diff.diff();
  255. assertEquals("[]", new TreeSet<>(diff.getChanged()).toString());
  256. assertEquals("[]", diff.getAdded().toString());
  257. assertEquals("[]", diff.getRemoved().toString());
  258. assertEquals("[]", diff.getMissing().toString());
  259. assertEquals("[]", diff.getModified().toString());
  260. assertEquals("[b]", diff.getConflicting().toString());
  261. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  262. }
  263. @Test
  264. public void testUnchangedSimple() throws IOException, GitAPIException {
  265. writeTrashFile("a.b", "a.b");
  266. writeTrashFile("a.c", "a.c");
  267. writeTrashFile("a=c", "a=c");
  268. writeTrashFile("a=d", "a=d");
  269. try (Git git = new Git(db)) {
  270. git.add().addFilepattern("a.b").call();
  271. git.add().addFilepattern("a.c").call();
  272. git.add().addFilepattern("a=c").call();
  273. git.add().addFilepattern("a=d").call();
  274. }
  275. TreeFormatter tree = new TreeFormatter();
  276. // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
  277. tree.append("a.b", FileMode.REGULAR_FILE, ObjectId.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
  278. tree.append("a.c", FileMode.REGULAR_FILE, ObjectId.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
  279. tree.append("a=c", FileMode.REGULAR_FILE, ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714"));
  280. tree.append("a=d", FileMode.REGULAR_FILE, ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
  281. ObjectId treeId = insertTree(tree);
  282. FileTreeIterator iterator = new FileTreeIterator(db);
  283. IndexDiff diff = new IndexDiff(db, treeId, iterator);
  284. diff.diff();
  285. assertEquals(0, diff.getChanged().size());
  286. assertEquals(0, diff.getAdded().size());
  287. assertEquals(0, diff.getRemoved().size());
  288. assertEquals(0, diff.getMissing().size());
  289. assertEquals(0, diff.getModified().size());
  290. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  291. }
  292. /**
  293. * This test has both files and directories that involve the tricky ordering
  294. * used by Git.
  295. *
  296. * @throws IOException
  297. * @throws GitAPIException
  298. */
  299. @Test
  300. public void testUnchangedComplex() throws IOException, GitAPIException {
  301. writeTrashFile("a.b", "a.b");
  302. writeTrashFile("a.c", "a.c");
  303. writeTrashFile("a/b.b/b", "a/b.b/b");
  304. writeTrashFile("a/b", "a/b");
  305. writeTrashFile("a/c", "a/c");
  306. writeTrashFile("a=c", "a=c");
  307. writeTrashFile("a=d", "a=d");
  308. try (Git git = new Git(db)) {
  309. git.add().addFilepattern("a.b").addFilepattern("a.c")
  310. .addFilepattern("a/b.b/b").addFilepattern("a/b")
  311. .addFilepattern("a/c").addFilepattern("a=c")
  312. .addFilepattern("a=d").call();
  313. }
  314. // got the hash id'd from the data using echo -n a.b|git hash-object -t blob --stdin
  315. TreeFormatter bb = new TreeFormatter();
  316. bb.append("b", FileMode.REGULAR_FILE, ObjectId.fromString("8d840bd4e2f3a48ff417c8e927d94996849933fd"));
  317. TreeFormatter a = new TreeFormatter();
  318. a.append("b", FileMode.REGULAR_FILE, ObjectId
  319. .fromString("db89c972fc57862eae378f45b74aca228037d415"));
  320. a.append("b.b", FileMode.TREE, insertTree(bb));
  321. a.append("c", FileMode.REGULAR_FILE, ObjectId.fromString("52ad142a008aeb39694bafff8e8f1be75ed7f007"));
  322. TreeFormatter tree = new TreeFormatter();
  323. tree.append("a.b", FileMode.REGULAR_FILE, ObjectId.fromString("f6f28df96c2b40c951164286e08be7c38ec74851"));
  324. tree.append("a.c", FileMode.REGULAR_FILE, ObjectId.fromString("6bc0e647512d2a0bef4f26111e484dc87df7f5ca"));
  325. tree.append("a", FileMode.TREE, insertTree(a));
  326. tree.append("a=c", FileMode.REGULAR_FILE, ObjectId.fromString("06022365ddbd7fb126761319633bf73517770714"));
  327. tree.append("a=d", FileMode.REGULAR_FILE, ObjectId.fromString("fa6414df3da87840700e9eeb7fc261dd77ccd5c2"));
  328. ObjectId treeId = insertTree(tree);
  329. FileTreeIterator iterator = new FileTreeIterator(db);
  330. IndexDiff diff = new IndexDiff(db, treeId, iterator);
  331. diff.diff();
  332. assertEquals(0, diff.getChanged().size());
  333. assertEquals(0, diff.getAdded().size());
  334. assertEquals(0, diff.getRemoved().size());
  335. assertEquals(0, diff.getMissing().size());
  336. assertEquals(0, diff.getModified().size());
  337. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  338. }
  339. private ObjectId insertTree(TreeFormatter tree) throws IOException {
  340. try (ObjectInserter oi = db.newObjectInserter()) {
  341. ObjectId id = oi.insert(tree);
  342. oi.flush();
  343. return id;
  344. }
  345. }
  346. /**
  347. * A file is removed from the index but stays in the working directory. It
  348. * is checked if IndexDiff detects this file as removed and untracked.
  349. *
  350. * @throws Exception
  351. */
  352. @Test
  353. public void testRemovedUntracked() throws Exception{
  354. String path = "file";
  355. try (Git git = new Git(db)) {
  356. writeTrashFile(path, "content");
  357. git.add().addFilepattern(path).call();
  358. git.commit().setMessage("commit").call();
  359. }
  360. removeFromIndex(path);
  361. FileTreeIterator iterator = new FileTreeIterator(db);
  362. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  363. diff.diff();
  364. assertTrue(diff.getRemoved().contains(path));
  365. assertTrue(diff.getUntracked().contains(path));
  366. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  367. }
  368. /**
  369. *
  370. * @throws Exception
  371. */
  372. @Test
  373. public void testUntrackedFolders() throws Exception {
  374. try (Git git = new Git(db)) {
  375. IndexDiff diff = new IndexDiff(db, Constants.HEAD,
  376. new FileTreeIterator(db));
  377. diff.diff();
  378. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  379. writeTrashFile("readme", "");
  380. writeTrashFile("src/com/A.java", "");
  381. writeTrashFile("src/com/B.java", "");
  382. writeTrashFile("src/org/A.java", "");
  383. writeTrashFile("src/org/B.java", "");
  384. writeTrashFile("target/com/A.java", "");
  385. writeTrashFile("target/com/B.java", "");
  386. writeTrashFile("target/org/A.java", "");
  387. writeTrashFile("target/org/B.java", "");
  388. git.add().addFilepattern("src").addFilepattern("readme").call();
  389. git.commit().setMessage("initial").call();
  390. diff = new IndexDiff(db, Constants.HEAD,
  391. new FileTreeIterator(db));
  392. diff.diff();
  393. assertEquals(new HashSet<>(Arrays.asList("target")),
  394. diff.getUntrackedFolders());
  395. writeTrashFile("src/tst/A.java", "");
  396. writeTrashFile("src/tst/B.java", "");
  397. diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
  398. diff.diff();
  399. assertEquals(new HashSet<>(Arrays.asList("target", "src/tst")),
  400. diff.getUntrackedFolders());
  401. git.rm().addFilepattern("src/com/B.java").addFilepattern("src/org")
  402. .call();
  403. git.commit().setMessage("second").call();
  404. writeTrashFile("src/org/C.java", "");
  405. diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
  406. diff.diff();
  407. assertEquals(
  408. new HashSet<>(Arrays.asList("src/org", "src/tst",
  409. "target")),
  410. diff.getUntrackedFolders());
  411. }
  412. }
  413. /**
  414. * Test that ignored folders aren't listed as untracked
  415. *
  416. * @throws Exception
  417. */
  418. @Test
  419. public void testUntrackedNotIgnoredFolders() throws Exception {
  420. try (Git git = new Git(db)) {
  421. IndexDiff diff = new IndexDiff(db, Constants.HEAD,
  422. new FileTreeIterator(db));
  423. diff.diff();
  424. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  425. writeTrashFile("readme", "");
  426. writeTrashFile("sr/com/X.java", "");
  427. writeTrashFile("src/com/A.java", "");
  428. writeTrashFile("src/org/B.java", "");
  429. writeTrashFile("srcs/org/Y.java", "");
  430. writeTrashFile("target/com/A.java", "");
  431. writeTrashFile("target/org/B.java", "");
  432. writeTrashFile(".gitignore", "/target\n/sr");
  433. git.add().addFilepattern("readme").addFilepattern(".gitignore")
  434. .addFilepattern("srcs/").call();
  435. git.commit().setMessage("initial").call();
  436. diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
  437. diff.diff();
  438. assertEquals(new HashSet<>(Arrays.asList("src")),
  439. diff.getUntrackedFolders());
  440. git.add().addFilepattern("src").call();
  441. writeTrashFile("sr/com/X1.java", "");
  442. writeTrashFile("src/tst/A.java", "");
  443. writeTrashFile("src/tst/B.java", "");
  444. writeTrashFile("srcs/com/Y1.java", "");
  445. deleteTrashFile(".gitignore");
  446. diff = new IndexDiff(db, Constants.HEAD, new FileTreeIterator(db));
  447. diff.diff();
  448. assertEquals(
  449. new HashSet<>(Arrays.asList("srcs/com", "sr", "src/tst",
  450. "target")),
  451. diff.getUntrackedFolders());
  452. }
  453. }
  454. @Test
  455. public void testAssumeUnchanged() throws Exception {
  456. try (Git git = new Git(db)) {
  457. String path = "file";
  458. writeTrashFile(path, "content");
  459. git.add().addFilepattern(path).call();
  460. String path2 = "file2";
  461. writeTrashFile(path2, "content");
  462. String path3 = "file3";
  463. writeTrashFile(path3, "some content");
  464. git.add().addFilepattern(path2).addFilepattern(path3).call();
  465. git.commit().setMessage("commit").call();
  466. assumeUnchanged(path2);
  467. assumeUnchanged(path3);
  468. writeTrashFile(path, "more content");
  469. deleteTrashFile(path3);
  470. FileTreeIterator iterator = new FileTreeIterator(db);
  471. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  472. diff.diff();
  473. assertEquals(2, diff.getAssumeUnchanged().size());
  474. assertEquals(1, diff.getModified().size());
  475. assertEquals(0, diff.getChanged().size());
  476. assertTrue(diff.getAssumeUnchanged().contains("file2"));
  477. assertTrue(diff.getAssumeUnchanged().contains("file3"));
  478. assertTrue(diff.getModified().contains("file"));
  479. git.add().addFilepattern(".").call();
  480. iterator = new FileTreeIterator(db);
  481. diff = new IndexDiff(db, Constants.HEAD, iterator);
  482. diff.diff();
  483. assertEquals(2, diff.getAssumeUnchanged().size());
  484. assertEquals(0, diff.getModified().size());
  485. assertEquals(1, diff.getChanged().size());
  486. assertTrue(diff.getAssumeUnchanged().contains("file2"));
  487. assertTrue(diff.getAssumeUnchanged().contains("file3"));
  488. assertTrue(diff.getChanged().contains("file"));
  489. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  490. }
  491. }
  492. @Test
  493. public void testStageState() throws IOException {
  494. final int base = DirCacheEntry.STAGE_1;
  495. final int ours = DirCacheEntry.STAGE_2;
  496. final int theirs = DirCacheEntry.STAGE_3;
  497. verifyStageState(StageState.BOTH_DELETED, base);
  498. verifyStageState(StageState.DELETED_BY_THEM, ours, base);
  499. verifyStageState(StageState.DELETED_BY_US, base, theirs);
  500. verifyStageState(StageState.BOTH_MODIFIED, base, ours, theirs);
  501. verifyStageState(StageState.ADDED_BY_US, ours);
  502. verifyStageState(StageState.BOTH_ADDED, ours, theirs);
  503. verifyStageState(StageState.ADDED_BY_THEM, theirs);
  504. assertTrue(StageState.BOTH_DELETED.hasBase());
  505. assertFalse(StageState.BOTH_DELETED.hasOurs());
  506. assertFalse(StageState.BOTH_DELETED.hasTheirs());
  507. assertFalse(StageState.BOTH_ADDED.hasBase());
  508. assertTrue(StageState.BOTH_ADDED.hasOurs());
  509. assertTrue(StageState.BOTH_ADDED.hasTheirs());
  510. }
  511. @Test
  512. public void testStageState_mergeAndReset_bug() throws Exception {
  513. try (Git git = new Git(db)) {
  514. writeTrashFile("a", "content");
  515. git.add().addFilepattern("a").call();
  516. RevCommit initialCommit = git.commit().setMessage("initial commit")
  517. .call();
  518. // create branch and add a new file
  519. final String branchName = Constants.R_HEADS + "branch";
  520. createBranch(initialCommit, branchName);
  521. checkoutBranch(branchName);
  522. writeTrashFile("b", "second file content - branch");
  523. git.add().addFilepattern("b").call();
  524. RevCommit branchCommit = git.commit().setMessage("branch commit")
  525. .call();
  526. // checkout master and add the same new file
  527. checkoutBranch(Constants.R_HEADS + Constants.MASTER);
  528. writeTrashFile("b", "second file content - master");
  529. git.add().addFilepattern("b").call();
  530. git.commit().setMessage("master commit").call();
  531. // try and merge
  532. MergeResult result = git.merge().include(branchCommit).call();
  533. assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
  534. FileTreeIterator iterator = new FileTreeIterator(db);
  535. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  536. diff.diff();
  537. assertTrue(diff.getChanged().isEmpty());
  538. assertTrue(diff.getAdded().isEmpty());
  539. assertTrue(diff.getRemoved().isEmpty());
  540. assertTrue(diff.getMissing().isEmpty());
  541. assertTrue(diff.getModified().isEmpty());
  542. assertEquals(1, diff.getConflicting().size());
  543. assertTrue(diff.getConflicting().contains("b"));
  544. assertEquals(StageState.BOTH_ADDED, diff.getConflictingStageStates()
  545. .get("b"));
  546. assertTrue(diff.getUntrackedFolders().isEmpty());
  547. // reset file b to its master state without altering the index
  548. writeTrashFile("b", "second file content - master");
  549. // we should have the same result
  550. iterator = new FileTreeIterator(db);
  551. diff = new IndexDiff(db, Constants.HEAD, iterator);
  552. diff.diff();
  553. assertTrue(diff.getChanged().isEmpty());
  554. assertTrue(diff.getAdded().isEmpty());
  555. assertTrue(diff.getRemoved().isEmpty());
  556. assertTrue(diff.getMissing().isEmpty());
  557. assertTrue(diff.getModified().isEmpty());
  558. assertEquals(1, diff.getConflicting().size());
  559. assertTrue(diff.getConflicting().contains("b"));
  560. assertEquals(StageState.BOTH_ADDED, diff.getConflictingStageStates()
  561. .get("b"));
  562. assertTrue(diff.getUntrackedFolders().isEmpty());
  563. }
  564. }
  565. @Test
  566. public void testStageState_simulated_bug() throws Exception {
  567. try (Git git = new Git(db)) {
  568. writeTrashFile("a", "content");
  569. git.add().addFilepattern("a").call();
  570. RevCommit initialCommit = git.commit().setMessage("initial commit")
  571. .call();
  572. // create branch and add a new file
  573. final String branchName = Constants.R_HEADS + "branch";
  574. createBranch(initialCommit, branchName);
  575. checkoutBranch(branchName);
  576. writeTrashFile("b", "second file content - branch");
  577. git.add().addFilepattern("b").call();
  578. git.commit().setMessage("branch commit")
  579. .call();
  580. // checkout master and add the same new file
  581. checkoutBranch(Constants.R_HEADS + Constants.MASTER);
  582. writeTrashFile("b", "second file content - master");
  583. git.add().addFilepattern("b").call();
  584. git.commit().setMessage("master commit").call();
  585. // Simulate a failed merge of branch into master
  586. DirCacheBuilder builder = db.lockDirCache().builder();
  587. DirCacheEntry entry = createEntry("a", FileMode.REGULAR_FILE, 0,
  588. "content");
  589. builder.add(entry);
  590. entry = createEntry("b", FileMode.REGULAR_FILE, 2,
  591. "second file content - master");
  592. builder.add(entry);
  593. entry = createEntry("b", FileMode.REGULAR_FILE, 3,
  594. "second file content - branch");
  595. builder.add(entry);
  596. builder.commit();
  597. FileTreeIterator iterator = new FileTreeIterator(db);
  598. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  599. diff.diff();
  600. assertTrue(diff.getChanged().isEmpty());
  601. assertTrue(diff.getAdded().isEmpty());
  602. assertTrue(diff.getRemoved().isEmpty());
  603. assertTrue(diff.getMissing().isEmpty());
  604. assertTrue(diff.getModified().isEmpty());
  605. assertEquals(1, diff.getConflicting().size());
  606. assertTrue(diff.getConflicting().contains("b"));
  607. assertEquals(StageState.BOTH_ADDED, diff.getConflictingStageStates()
  608. .get("b"));
  609. assertTrue(diff.getUntrackedFolders().isEmpty());
  610. }
  611. }
  612. @Test
  613. public void testAutoCRLFInput() throws Exception {
  614. try (Git git = new Git(db)) {
  615. FileBasedConfig config = db.getConfig();
  616. // Make sure core.autocrlf is false before adding
  617. config.setEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  618. ConfigConstants.CONFIG_KEY_AUTOCRLF, AutoCRLF.FALSE);
  619. config.save();
  620. // File is already in repository with CRLF
  621. writeTrashFile("crlf.txt", "this\r\ncontains\r\ncrlf\r\n");
  622. git.add().addFilepattern("crlf.txt").call();
  623. git.commit().setMessage("Add crlf.txt").call();
  624. // Now set core.autocrlf to input
  625. config.setEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  626. ConfigConstants.CONFIG_KEY_AUTOCRLF, AutoCRLF.INPUT);
  627. config.save();
  628. FileTreeIterator iterator = new FileTreeIterator(db);
  629. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  630. diff.diff();
  631. assertTrue(
  632. "Expected no modified files, but there were: "
  633. + diff.getModified(), diff.getModified().isEmpty());
  634. }
  635. }
  636. private void verifyStageState(StageState expected, int... stages)
  637. throws IOException {
  638. DirCacheBuilder builder = db.lockDirCache().builder();
  639. for (int stage : stages) {
  640. DirCacheEntry entry = createEntry("a", FileMode.REGULAR_FILE,
  641. stage, "content");
  642. builder.add(entry);
  643. }
  644. builder.commit();
  645. IndexDiff diff = new IndexDiff(db, Constants.HEAD,
  646. new FileTreeIterator(db));
  647. diff.diff();
  648. assertEquals(
  649. "Conflict for entries in stages " + Arrays.toString(stages),
  650. expected, diff.getConflictingStageStates().get("a"));
  651. }
  652. private void removeFromIndex(String path) throws IOException {
  653. final DirCache dirc = db.lockDirCache();
  654. final DirCacheEditor edit = dirc.editor();
  655. edit.add(new DirCacheEditor.DeletePath(path));
  656. if (!edit.commit())
  657. throw new IOException("could not commit");
  658. }
  659. private void assumeUnchanged(String path) throws IOException {
  660. final DirCache dirc = db.lockDirCache();
  661. final DirCacheEntry ent = dirc.getEntry(path);
  662. if (ent != null)
  663. ent.setAssumeValid(true);
  664. dirc.write();
  665. if (!dirc.commit())
  666. throw new IOException("could not commit");
  667. }
  668. }