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.

IndexDiffFilterTest.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>,
  3. * Copyright (C) 2010, Philipp Thun <philipp.thun@sap.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.treewalk.filter;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.File;
  50. import org.eclipse.jgit.api.Git;
  51. import org.eclipse.jgit.dircache.DirCacheIterator;
  52. import org.eclipse.jgit.junit.RepositoryTestCase;
  53. import org.eclipse.jgit.revwalk.RevCommit;
  54. import org.eclipse.jgit.treewalk.FileTreeIterator;
  55. import org.eclipse.jgit.treewalk.TreeWalk;
  56. import org.eclipse.jgit.util.FileUtils;
  57. import org.junit.Before;
  58. import org.junit.Test;
  59. public class IndexDiffFilterTest extends RepositoryTestCase {
  60. private static final String FILE = "file";
  61. private static final String UNTRACKED_FILE = "untracked_file";
  62. private static final String IGNORED_FILE = "ignored_file";
  63. private static final String FILE_IN_FOLDER = "folder/file";
  64. private static final String UNTRACKED_FILE_IN_FOLDER = "folder/untracked_file";
  65. private static final String IGNORED_FILE_IN_FOLDER = "folder/ignored_file";
  66. private static final String FILE_IN_IGNORED_FOLDER = "ignored_folder/file";
  67. private static final String FOLDER = "folder";
  68. private static final String UNTRACKED_FOLDER = "untracked_folder";
  69. private static final String IGNORED_FOLDER = "ignored_folder";
  70. private static final String GITIGNORE = ".gitignore";
  71. private static final String FILE_CONTENT = "content";
  72. private static final String MODIFIED_FILE_CONTENT = "modified_content";
  73. private Git git;
  74. @Before
  75. public void setUp() throws Exception {
  76. super.setUp();
  77. git = new Git(db);
  78. }
  79. @Test
  80. public void testRecursiveTreeWalk() throws Exception {
  81. RevCommit commit = writeFileInFolderAndCommit();
  82. deleteAll();
  83. writeFileWithFolderName();
  84. TreeWalk treeWalk = createTreeWalk(commit);
  85. assertTrue(treeWalk.next());
  86. assertEquals("folder", treeWalk.getPathString());
  87. assertTrue(treeWalk.next());
  88. assertEquals("folder/file", treeWalk.getPathString());
  89. assertFalse(treeWalk.next());
  90. }
  91. @Test
  92. public void testNonRecursiveTreeWalk() throws Exception {
  93. RevCommit commit = writeFileInFolderAndCommit();
  94. deleteAll();
  95. writeFileWithFolderName();
  96. TreeWalk treeWalk = createNonRecursiveTreeWalk(commit);
  97. assertTrue(treeWalk.next());
  98. assertEquals("folder", treeWalk.getPathString());
  99. assertTrue(treeWalk.next());
  100. assertEquals("folder", treeWalk.getPathString());
  101. assertTrue(treeWalk.isSubtree());
  102. treeWalk.enterSubtree();
  103. assertTrue(treeWalk.next());
  104. assertEquals("folder/file", treeWalk.getPathString());
  105. assertFalse(treeWalk.next());
  106. }
  107. @Test
  108. public void testFileCommitted() throws Exception {
  109. RevCommit commit = writeFileAndCommit();
  110. TreeWalk treeWalk = createTreeWalk(commit);
  111. assertFalse(treeWalk.next());
  112. }
  113. @Test
  114. public void testConflicts() throws Exception {
  115. RevCommit initial = git.commit().setMessage("initial").call();
  116. writeTrashFile(FILE, "master");
  117. git.add().addFilepattern(FILE).call();
  118. RevCommit master = git.commit().setMessage("master").call();
  119. git.checkout().setName("refs/heads/side")
  120. .setCreateBranch(true).setStartPoint(initial).call();
  121. writeTrashFile(FILE, "side");
  122. git.add().addFilepattern(FILE).call();
  123. RevCommit side = git.commit().setMessage("side").call();
  124. assertFalse(git.merge().include("master", master).call()
  125. .getMergeStatus()
  126. .isSuccessful());
  127. assertEquals(read(FILE),
  128. "<<<<<<< HEAD\nside\n=======\nmaster\n>>>>>>> master\n");
  129. writeTrashFile(FILE, "master");
  130. TreeWalk treeWalk = createTreeWalk(side);
  131. int count = 0;
  132. while (treeWalk.next())
  133. count++;
  134. assertEquals(2, count);
  135. }
  136. @Test
  137. public void testFileInFolderCommitted() throws Exception {
  138. RevCommit commit = writeFileInFolderAndCommit();
  139. TreeWalk treeWalk = createTreeWalk(commit);
  140. assertFalse(treeWalk.next());
  141. }
  142. @Test
  143. public void testEmptyFolderCommitted() throws Exception {
  144. RevCommit commit = createEmptyFolderAndCommit();
  145. TreeWalk treeWalk = createTreeWalk(commit);
  146. assertFalse(treeWalk.next());
  147. }
  148. @Test
  149. public void testFileCommittedChangedNotModified() throws Exception {
  150. RevCommit commit = writeFileAndCommit();
  151. writeFile();
  152. TreeWalk treeWalk = createTreeWalk(commit);
  153. assertFalse(treeWalk.next());
  154. }
  155. @Test
  156. public void testFileInFolderCommittedChangedNotModified() throws Exception {
  157. RevCommit commit = writeFileInFolderAndCommit();
  158. writeFileInFolder();
  159. TreeWalk treeWalk = createTreeWalk(commit);
  160. assertFalse(treeWalk.next());
  161. }
  162. @Test
  163. public void testFileCommittedModified() throws Exception {
  164. RevCommit commit = writeFileAndCommit();
  165. writeFileModified();
  166. TreeWalk treeWalk = createTreeWalk(commit);
  167. assertPaths(treeWalk, FILE);
  168. }
  169. @Test
  170. public void testFileInFolderCommittedModified() throws Exception {
  171. RevCommit commit = writeFileInFolderAndCommit();
  172. writeFileInFolderModified();
  173. TreeWalk treeWalk = createTreeWalk(commit);
  174. assertPaths(treeWalk, FILE_IN_FOLDER);
  175. }
  176. @Test
  177. public void testFileCommittedDeleted() throws Exception {
  178. RevCommit commit = writeFileAndCommit();
  179. deleteFile();
  180. TreeWalk treeWalk = createTreeWalk(commit);
  181. assertPaths(treeWalk, FILE);
  182. }
  183. @Test
  184. public void testFileInFolderCommittedDeleted() throws Exception {
  185. RevCommit commit = writeFileInFolderAndCommit();
  186. deleteFileInFolder();
  187. TreeWalk treeWalk = createTreeWalk(commit);
  188. assertPaths(treeWalk, FILE_IN_FOLDER);
  189. }
  190. @Test
  191. public void testFileInFolderCommittedAllDeleted() throws Exception {
  192. RevCommit commit = writeFileInFolderAndCommit();
  193. deleteAll();
  194. TreeWalk treeWalk = createTreeWalk(commit);
  195. assertPaths(treeWalk, FILE_IN_FOLDER);
  196. }
  197. @Test
  198. public void testEmptyFolderCommittedDeleted() throws Exception {
  199. RevCommit commit = createEmptyFolderAndCommit();
  200. deleteFolder();
  201. TreeWalk treeWalk = createTreeWalk(commit);
  202. assertFalse(treeWalk.next());
  203. }
  204. @Test
  205. public void testFileCommittedModifiedCommittedComparedWithInitialCommit()
  206. throws Exception {
  207. RevCommit commit = writeFileAndCommit();
  208. writeFileModifiedAndCommit();
  209. TreeWalk treeWalk = createTreeWalk(commit);
  210. assertPaths(treeWalk, FILE);
  211. }
  212. @Test
  213. public void testFileInFolderCommittedModifiedCommittedComparedWithInitialCommit()
  214. throws Exception {
  215. RevCommit commit = writeFileInFolderAndCommit();
  216. writeFileInFolderModifiedAndCommit();
  217. TreeWalk treeWalk = createTreeWalk(commit);
  218. assertPaths(treeWalk, FILE_IN_FOLDER);
  219. }
  220. @Test
  221. public void testFileCommittedDeletedCommittedComparedWithInitialCommit()
  222. throws Exception {
  223. RevCommit commit = writeFileAndCommit();
  224. deleteFileAndCommit();
  225. TreeWalk treeWalk = createTreeWalk(commit);
  226. assertPaths(treeWalk, FILE);
  227. }
  228. @Test
  229. public void testFileInFolderCommittedDeletedCommittedComparedWithInitialCommit()
  230. throws Exception {
  231. RevCommit commit = writeFileInFolderAndCommit();
  232. deleteFileInFolderAndCommit();
  233. TreeWalk treeWalk = createTreeWalk(commit);
  234. assertPaths(treeWalk, FILE_IN_FOLDER);
  235. }
  236. @Test
  237. public void testFileInFolderCommittedAllDeletedCommittedComparedWithInitialCommit()
  238. throws Exception {
  239. RevCommit commit = writeFileInFolderAndCommit();
  240. deleteAllAndCommit();
  241. TreeWalk treeWalk = createTreeWalk(commit);
  242. assertPaths(treeWalk, FILE_IN_FOLDER);
  243. }
  244. @Test
  245. public void testEmptyFolderCommittedDeletedCommittedComparedWithInitialCommit()
  246. throws Exception {
  247. RevCommit commit = createEmptyFolderAndCommit();
  248. deleteFolderAndCommit();
  249. TreeWalk treeWalk = createTreeWalk(commit);
  250. assertFalse(treeWalk.next());
  251. }
  252. @Test
  253. public void testFileUntracked() throws Exception {
  254. RevCommit commit = writeFileAndCommit();
  255. writeFileUntracked();
  256. TreeWalk treeWalk = createTreeWalk(commit);
  257. assertPaths(treeWalk, UNTRACKED_FILE);
  258. }
  259. @Test
  260. public void testFileInFolderUntracked() throws Exception {
  261. RevCommit commit = writeFileInFolderAndCommit();
  262. writeFileInFolderUntracked();
  263. TreeWalk treeWalk = createTreeWalk(commit);
  264. assertPaths(treeWalk, UNTRACKED_FILE_IN_FOLDER);
  265. }
  266. @Test
  267. public void testEmptyFolderUntracked() throws Exception {
  268. RevCommit commit = createEmptyFolderAndCommit();
  269. createEmptyFolderUntracked();
  270. TreeWalk treeWalk = createTreeWalk(commit);
  271. assertFalse(treeWalk.next());
  272. }
  273. @Test
  274. public void testFileIgnored() throws Exception {
  275. RevCommit commit = writeFileAndCommit();
  276. writeFileIgnored();
  277. TreeWalk treeWalk = createTreeWalk(commit);
  278. assertFalse(treeWalk.next());
  279. }
  280. @Test
  281. public void testFileInFolderIgnored() throws Exception {
  282. RevCommit commit = writeFileInFolderAndCommit();
  283. writeFileInFolderIgnored();
  284. TreeWalk treeWalk = createTreeWalk(commit);
  285. assertFalse(treeWalk.next());
  286. }
  287. @Test
  288. public void testFileInFolderAllIgnored() throws Exception {
  289. RevCommit commit = writeFileInFolderAndCommit();
  290. writeFileInFolderAllIgnored();
  291. TreeWalk treeWalk = createTreeWalk(commit);
  292. assertFalse(treeWalk.next());
  293. }
  294. @Test
  295. public void testEmptyFolderIgnored() throws Exception {
  296. RevCommit commit = createEmptyFolderAndCommit();
  297. createEmptyFolderIgnored();
  298. TreeWalk treeWalk = createTreeWalk(commit);
  299. assertFalse(treeWalk.next());
  300. }
  301. @Test
  302. public void testFileIgnoredNotHonored() throws Exception {
  303. RevCommit commit = writeFileAndCommit();
  304. writeFileIgnored();
  305. TreeWalk treeWalk = createTreeWalkDishonorIgnores(commit);
  306. assertPaths(treeWalk, IGNORED_FILE, GITIGNORE);
  307. }
  308. @Test
  309. public void testFileCommittedModifiedIgnored() throws Exception {
  310. RevCommit commit = writeFileAndCommit();
  311. writeFileModifiedIgnored();
  312. TreeWalk treeWalk = createTreeWalk(commit);
  313. assertPaths(treeWalk, FILE);
  314. }
  315. @Test
  316. public void testFileInFolderCommittedModifiedIgnored() throws Exception {
  317. RevCommit commit = writeFileInFolderAndCommit();
  318. writeFileInFolderModifiedIgnored();
  319. TreeWalk treeWalk = createTreeWalk(commit);
  320. assertPaths(treeWalk, FILE_IN_FOLDER);
  321. }
  322. @Test
  323. public void testFileInFolderCommittedModifiedAllIgnored() throws Exception {
  324. RevCommit commit = writeFileInFolderAndCommit();
  325. writeFileInFolderModifiedAllIgnored();
  326. TreeWalk treeWalk = createTreeWalk(commit);
  327. assertPaths(treeWalk, FILE_IN_FOLDER);
  328. }
  329. @Test
  330. public void testFileCommittedDeletedCommittedIgnoredComparedWithInitialCommit()
  331. throws Exception {
  332. RevCommit commit = writeFileAndCommit();
  333. deleteFileAndCommit();
  334. rewriteFileIgnored();
  335. TreeWalk treeWalk = createTreeWalk(commit);
  336. assertPaths(treeWalk, FILE);
  337. }
  338. @Test
  339. public void testFileInFolderCommittedDeletedCommittedIgnoredComparedWithInitialCommit()
  340. throws Exception {
  341. RevCommit commit = writeFileInFolderAndCommit();
  342. deleteFileInFolderAndCommit();
  343. rewriteFileInFolderIgnored();
  344. TreeWalk treeWalk = createTreeWalk(commit);
  345. assertPaths(treeWalk, FILE_IN_FOLDER);
  346. }
  347. @Test
  348. public void testFileInFolderCommittedAllDeletedCommittedAllIgnoredComparedWithInitialCommit()
  349. throws Exception {
  350. RevCommit commit = writeFileInFolderAndCommit();
  351. deleteAllAndCommit();
  352. rewriteFileInFolderAllIgnored();
  353. TreeWalk treeWalk = createTreeWalk(commit);
  354. assertPaths(treeWalk, FILE_IN_FOLDER);
  355. }
  356. @Test
  357. public void testEmptyFolderCommittedDeletedCommittedIgnoredComparedWithInitialCommit()
  358. throws Exception {
  359. RevCommit commit = createEmptyFolderAndCommit();
  360. deleteFolderAndCommit();
  361. recreateEmptyFolderIgnored();
  362. TreeWalk treeWalk = createTreeWalk(commit);
  363. assertFalse(treeWalk.next());
  364. }
  365. @Test
  366. public void testFileInFolderCommittedNonRecursive() throws Exception {
  367. RevCommit commit = writeFileInFolderAndCommit();
  368. TreeWalk treeWalk = createNonRecursiveTreeWalk(commit);
  369. assertPaths(treeWalk, FOLDER);
  370. }
  371. @Test
  372. public void testFolderChangedToFile() throws Exception {
  373. RevCommit commit = writeFileInFolderAndCommit();
  374. deleteAll();
  375. writeFileWithFolderName();
  376. TreeWalk treeWalk = createTreeWalk(commit);
  377. assertPaths(treeWalk, FOLDER, FILE_IN_FOLDER);
  378. }
  379. @Test
  380. public void testFolderChangedToFileCommittedComparedWithInitialCommit()
  381. throws Exception {
  382. RevCommit commit = writeFileInFolderAndCommit();
  383. deleteAll();
  384. writeFileWithFolderNameAndCommit();
  385. TreeWalk treeWalk = createTreeWalk(commit);
  386. assertPaths(treeWalk, FOLDER, FILE_IN_FOLDER);
  387. }
  388. private void writeFile() throws Exception {
  389. writeTrashFile(FILE, FILE_CONTENT);
  390. }
  391. private RevCommit writeFileAndCommit() throws Exception {
  392. writeFile();
  393. return commitAdd();
  394. }
  395. private void writeFileModified() throws Exception {
  396. writeTrashFile(FILE, MODIFIED_FILE_CONTENT);
  397. }
  398. private void writeFileModifiedAndCommit() throws Exception {
  399. writeFileModified();
  400. commitAdd();
  401. }
  402. private void writeFileUntracked() throws Exception {
  403. writeTrashFile(UNTRACKED_FILE, FILE_CONTENT);
  404. }
  405. private void writeFileIgnored() throws Exception {
  406. writeTrashFile(IGNORED_FILE, FILE_CONTENT);
  407. writeTrashFile(GITIGNORE, GITIGNORE + "\n" + IGNORED_FILE);
  408. }
  409. private void writeFileModifiedIgnored() throws Exception {
  410. writeFileModified();
  411. writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FILE);
  412. }
  413. private void rewriteFileIgnored() throws Exception {
  414. writeFile();
  415. writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FILE);
  416. }
  417. private void writeFileWithFolderName() throws Exception {
  418. writeTrashFile(FOLDER, FILE_CONTENT);
  419. }
  420. private void writeFileWithFolderNameAndCommit() throws Exception {
  421. writeFileWithFolderName();
  422. commitAdd();
  423. }
  424. private void deleteFile() throws Exception {
  425. deleteTrashFile(FILE);
  426. }
  427. private void deleteFileAndCommit() throws Exception {
  428. deleteFile();
  429. commitRm(FILE);
  430. }
  431. private void writeFileInFolder() throws Exception {
  432. writeTrashFile(FILE_IN_FOLDER, FILE_CONTENT);
  433. }
  434. private RevCommit writeFileInFolderAndCommit() throws Exception {
  435. writeFileInFolder();
  436. return commitAdd();
  437. }
  438. private void writeFileInFolderModified() throws Exception {
  439. writeTrashFile(FILE_IN_FOLDER, MODIFIED_FILE_CONTENT);
  440. }
  441. private void writeFileInFolderModifiedAndCommit() throws Exception {
  442. writeFileInFolderModified();
  443. commitAdd();
  444. }
  445. private void writeFileInFolderUntracked() throws Exception {
  446. writeTrashFile(UNTRACKED_FILE_IN_FOLDER, FILE_CONTENT);
  447. }
  448. private void writeFileInFolderIgnored() throws Exception {
  449. writeTrashFile(IGNORED_FILE_IN_FOLDER, FILE_CONTENT);
  450. writeTrashFile(GITIGNORE, GITIGNORE + "\n" + IGNORED_FILE_IN_FOLDER);
  451. }
  452. private void writeFileInFolderAllIgnored() throws Exception {
  453. writeTrashFile(FILE_IN_IGNORED_FOLDER, FILE_CONTENT);
  454. writeTrashFile(GITIGNORE, GITIGNORE + "\n" + IGNORED_FOLDER + "/");
  455. }
  456. private void writeFileInFolderModifiedIgnored() throws Exception {
  457. writeFileInFolderModified();
  458. writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FILE_IN_FOLDER);
  459. }
  460. private void rewriteFileInFolderIgnored() throws Exception {
  461. writeFileInFolder();
  462. writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FILE_IN_FOLDER);
  463. }
  464. private void writeFileInFolderModifiedAllIgnored() throws Exception {
  465. writeFileInFolderModified();
  466. writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FOLDER + "/");
  467. }
  468. private void rewriteFileInFolderAllIgnored() throws Exception {
  469. writeFileInFolder();
  470. writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FOLDER + "/");
  471. }
  472. private void deleteFileInFolder() throws Exception {
  473. deleteTrashFile(FILE_IN_FOLDER);
  474. }
  475. private void deleteFileInFolderAndCommit() throws Exception {
  476. deleteFileInFolder();
  477. commitRm(FILE_IN_FOLDER);
  478. }
  479. private void createEmptyFolder() throws Exception {
  480. File path = new File(db.getWorkTree(), FOLDER);
  481. FileUtils.mkdir(path);
  482. }
  483. private RevCommit createEmptyFolderAndCommit() throws Exception {
  484. createEmptyFolder();
  485. return commitAdd();
  486. }
  487. private void createEmptyFolderUntracked() throws Exception {
  488. File path = new File(db.getWorkTree(), UNTRACKED_FOLDER);
  489. FileUtils.mkdir(path);
  490. }
  491. private void createEmptyFolderIgnored() throws Exception {
  492. File path = new File(db.getWorkTree(), IGNORED_FOLDER);
  493. FileUtils.mkdir(path);
  494. writeTrashFile(GITIGNORE, GITIGNORE + "\n" + IGNORED_FOLDER + "/");
  495. }
  496. private void recreateEmptyFolderIgnored() throws Exception {
  497. createEmptyFolder();
  498. writeTrashFile(GITIGNORE, GITIGNORE + "\n" + FOLDER + "/");
  499. }
  500. private void deleteFolder() throws Exception {
  501. deleteTrashFile(FOLDER);
  502. }
  503. private void deleteFolderAndCommit() throws Exception {
  504. deleteFolder();
  505. commitRm(FOLDER);
  506. }
  507. private void deleteAll() throws Exception {
  508. deleteFileInFolder();
  509. deleteFolder();
  510. }
  511. private void deleteAllAndCommit() throws Exception {
  512. deleteFileInFolderAndCommit();
  513. deleteFolderAndCommit();
  514. }
  515. private RevCommit commitAdd() throws Exception {
  516. git.add().addFilepattern(".").call();
  517. return git.commit().setMessage("commit").call();
  518. }
  519. private RevCommit commitRm(String path) throws Exception {
  520. git.rm().addFilepattern(path).call();
  521. return git.commit().setMessage("commit").call();
  522. }
  523. private TreeWalk createTreeWalk(RevCommit commit) throws Exception {
  524. return createTreeWalk(commit, true, true);
  525. }
  526. private TreeWalk createTreeWalkDishonorIgnores(RevCommit commit)
  527. throws Exception {
  528. return createTreeWalk(commit, true, false);
  529. }
  530. private TreeWalk createNonRecursiveTreeWalk(RevCommit commit)
  531. throws Exception {
  532. return createTreeWalk(commit, false, true);
  533. }
  534. private TreeWalk createTreeWalk(RevCommit commit, boolean isRecursive,
  535. boolean honorIgnores) throws Exception {
  536. TreeWalk treeWalk = new TreeWalk(db);
  537. treeWalk.setRecursive(isRecursive);
  538. treeWalk.addTree(commit.getTree());
  539. treeWalk.addTree(new DirCacheIterator(db.readDirCache()));
  540. treeWalk.addTree(new FileTreeIterator(db));
  541. if (!honorIgnores)
  542. treeWalk.setFilter(new IndexDiffFilter(1, 2, honorIgnores));
  543. else
  544. treeWalk.setFilter(new IndexDiffFilter(1, 2));
  545. return treeWalk;
  546. }
  547. private static void assertPaths(TreeWalk treeWalk, String... paths)
  548. throws Exception {
  549. for (int i = 0; i < paths.length; i++) {
  550. assertTrue(treeWalk.next());
  551. assertPath(treeWalk.getPathString(), paths);
  552. }
  553. assertFalse(treeWalk.next());
  554. }
  555. private static void assertPath(String path, String... paths) {
  556. for (String p : paths)
  557. if (p.equals(path))
  558. return;
  559. fail("Expected path '" + path + "' is not returned");
  560. }
  561. }