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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. ObjectInserter oi = db.newObjectInserter();
  339. try {
  340. ObjectId id = oi.insert(Constants.OBJ_TREE, tree.format());
  341. oi.flush();
  342. return id;
  343. } finally {
  344. oi.release();
  345. }
  346. }
  347. /**
  348. * A file is removed from the index but stays in the working directory. It
  349. * is checked if IndexDiff detects this file as removed and untracked.
  350. *
  351. * @throws Exception
  352. */
  353. @Test
  354. public void testRemovedUntracked() throws Exception{
  355. Git git = new Git(db);
  356. String path = "file";
  357. writeTrashFile(path, "content");
  358. git.add().addFilepattern(path).call();
  359. git.commit().setMessage("commit").call();
  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. 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<String>(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<String>(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<String>(Arrays.asList("src/org", "src/tst",
  409. "target")),
  410. diff.getUntrackedFolders());
  411. }
  412. @Test
  413. public void testAssumeUnchanged() throws Exception {
  414. Git git = new Git(db);
  415. String path = "file";
  416. writeTrashFile(path, "content");
  417. git.add().addFilepattern(path).call();
  418. String path2 = "file2";
  419. writeTrashFile(path2, "content");
  420. String path3 = "file3";
  421. writeTrashFile(path3, "some content");
  422. git.add().addFilepattern(path2).addFilepattern(path3).call();
  423. git.commit().setMessage("commit").call();
  424. assumeUnchanged(path2);
  425. assumeUnchanged(path3);
  426. writeTrashFile(path, "more content");
  427. deleteTrashFile(path3);
  428. FileTreeIterator iterator = new FileTreeIterator(db);
  429. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  430. diff.diff();
  431. assertEquals(2, diff.getAssumeUnchanged().size());
  432. assertEquals(1, diff.getModified().size());
  433. assertEquals(0, diff.getChanged().size());
  434. assertTrue(diff.getAssumeUnchanged().contains("file2"));
  435. assertTrue(diff.getAssumeUnchanged().contains("file3"));
  436. assertTrue(diff.getModified().contains("file"));
  437. git.add().addFilepattern(".").call();
  438. iterator = new FileTreeIterator(db);
  439. diff = new IndexDiff(db, Constants.HEAD, iterator);
  440. diff.diff();
  441. assertEquals(2, diff.getAssumeUnchanged().size());
  442. assertEquals(0, diff.getModified().size());
  443. assertEquals(1, diff.getChanged().size());
  444. assertTrue(diff.getAssumeUnchanged().contains("file2"));
  445. assertTrue(diff.getAssumeUnchanged().contains("file3"));
  446. assertTrue(diff.getChanged().contains("file"));
  447. assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  448. }
  449. @Test
  450. public void testStageState() throws IOException {
  451. final int base = DirCacheEntry.STAGE_1;
  452. final int ours = DirCacheEntry.STAGE_2;
  453. final int theirs = DirCacheEntry.STAGE_3;
  454. verifyStageState(StageState.BOTH_DELETED, base);
  455. verifyStageState(StageState.DELETED_BY_THEM, ours, base);
  456. verifyStageState(StageState.DELETED_BY_US, base, theirs);
  457. verifyStageState(StageState.BOTH_MODIFIED, base, ours, theirs);
  458. verifyStageState(StageState.ADDED_BY_US, ours);
  459. verifyStageState(StageState.BOTH_ADDED, ours, theirs);
  460. verifyStageState(StageState.ADDED_BY_THEM, theirs);
  461. assertTrue(StageState.BOTH_DELETED.hasBase());
  462. assertFalse(StageState.BOTH_DELETED.hasOurs());
  463. assertFalse(StageState.BOTH_DELETED.hasTheirs());
  464. assertFalse(StageState.BOTH_ADDED.hasBase());
  465. assertTrue(StageState.BOTH_ADDED.hasOurs());
  466. assertTrue(StageState.BOTH_ADDED.hasTheirs());
  467. }
  468. @Test
  469. public void testAutoCRLFInput() throws Exception {
  470. Git git = new Git(db);
  471. FileBasedConfig config = db.getConfig();
  472. // Make sure core.autocrlf is false before adding
  473. config.setEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  474. ConfigConstants.CONFIG_KEY_AUTOCRLF, AutoCRLF.FALSE);
  475. config.save();
  476. // File is already in repository with CRLF
  477. writeTrashFile("crlf.txt", "this\r\ncontains\r\ncrlf\r\n");
  478. git.add().addFilepattern("crlf.txt").call();
  479. git.commit().setMessage("Add crlf.txt").call();
  480. // Now set core.autocrlf to input
  481. config.setEnum(ConfigConstants.CONFIG_CORE_SECTION, null,
  482. ConfigConstants.CONFIG_KEY_AUTOCRLF, AutoCRLF.INPUT);
  483. config.save();
  484. FileTreeIterator iterator = new FileTreeIterator(db);
  485. IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
  486. diff.diff();
  487. assertTrue(
  488. "Expected no modified files, but there were: "
  489. + diff.getModified(), diff.getModified().isEmpty());
  490. }
  491. private void verifyStageState(StageState expected, int... stages)
  492. throws IOException {
  493. DirCacheBuilder builder = db.lockDirCache().builder();
  494. for (int stage : stages) {
  495. DirCacheEntry entry = createEntry("a", FileMode.REGULAR_FILE,
  496. stage, "content");
  497. builder.add(entry);
  498. }
  499. builder.commit();
  500. IndexDiff diff = new IndexDiff(db, Constants.HEAD,
  501. new FileTreeIterator(db));
  502. diff.diff();
  503. assertEquals(
  504. "Conflict for entries in stages " + Arrays.toString(stages),
  505. expected, diff.getConflictingStageStates().get("a"));
  506. }
  507. private void removeFromIndex(String path) throws IOException {
  508. final DirCache dirc = db.lockDirCache();
  509. final DirCacheEditor edit = dirc.editor();
  510. edit.add(new DirCacheEditor.DeletePath(path));
  511. if (!edit.commit())
  512. throw new IOException("could not commit");
  513. }
  514. private void assumeUnchanged(String path) throws IOException {
  515. final DirCache dirc = db.lockDirCache();
  516. final DirCacheEntry ent = dirc.getEntry(path);
  517. if (ent != null)
  518. ent.setAssumeValid(true);
  519. dirc.write();
  520. if (!dirc.commit())
  521. throw new IOException("could not commit");
  522. }
  523. }