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

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