Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

IndexDiffTest.java 27KB

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