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.

DirCacheIteratorTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.dircache;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertSame;
  48. import static org.junit.Assert.assertTrue;
  49. import java.io.File;
  50. import java.util.Collections;
  51. import org.eclipse.jgit.junit.JGitTestUtil;
  52. import org.eclipse.jgit.junit.RepositoryTestCase;
  53. import org.eclipse.jgit.lib.FileMode;
  54. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  55. import org.eclipse.jgit.treewalk.TreeWalk;
  56. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  57. import org.eclipse.jgit.util.FS;
  58. import org.junit.Test;
  59. public class DirCacheIteratorTest extends RepositoryTestCase {
  60. @Test
  61. public void testEmptyTree_NoTreeWalk() throws Exception {
  62. final DirCache dc = DirCache.newInCore();
  63. assertEquals(0, dc.getEntryCount());
  64. final DirCacheIterator i = new DirCacheIterator(dc);
  65. assertTrue(i.eof());
  66. }
  67. @Test
  68. public void testEmptyTree_WithTreeWalk() throws Exception {
  69. final DirCache dc = DirCache.newInCore();
  70. assertEquals(0, dc.getEntryCount());
  71. try (TreeWalk tw = new TreeWalk(db)) {
  72. tw.addTree(new DirCacheIterator(dc));
  73. assertFalse(tw.next());
  74. }
  75. }
  76. @Test
  77. public void testNoSubtree_NoTreeWalk() throws Exception {
  78. final DirCache dc = DirCache.newInCore();
  79. final String[] paths = { "a-", "a0b" };
  80. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  81. for (int i = 0; i < paths.length; i++) {
  82. ents[i] = new DirCacheEntry(paths[i]);
  83. ents[i].setFileMode(FileMode.REGULAR_FILE);
  84. }
  85. final DirCacheBuilder b = dc.builder();
  86. for (DirCacheEntry ent : ents) {
  87. b.add(ent);
  88. }
  89. b.finish();
  90. final DirCacheIterator i = new DirCacheIterator(dc);
  91. int pathIdx = 0;
  92. for (; !i.eof(); i.next(1)) {
  93. assertEquals(pathIdx, i.ptr);
  94. assertSame(ents[pathIdx], i.getDirCacheEntry());
  95. pathIdx++;
  96. }
  97. assertEquals(paths.length, pathIdx);
  98. }
  99. @Test
  100. public void testNoSubtree_WithTreeWalk() throws Exception {
  101. final DirCache dc = DirCache.newInCore();
  102. final String[] paths = { "a-", "a0b" };
  103. final FileMode[] modes = { FileMode.EXECUTABLE_FILE, FileMode.GITLINK };
  104. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  105. for (int i = 0; i < paths.length; i++) {
  106. ents[i] = new DirCacheEntry(paths[i]);
  107. ents[i].setFileMode(modes[i]);
  108. }
  109. final DirCacheBuilder b = dc.builder();
  110. for (DirCacheEntry ent : ents) {
  111. b.add(ent);
  112. }
  113. b.finish();
  114. final DirCacheIterator i = new DirCacheIterator(dc);
  115. try (TreeWalk tw = new TreeWalk(db)) {
  116. tw.addTree(i);
  117. int pathIdx = 0;
  118. while (tw.next()) {
  119. assertSame(i, tw.getTree(0, DirCacheIterator.class));
  120. assertEquals(pathIdx, i.ptr);
  121. assertSame(ents[pathIdx], i.getDirCacheEntry());
  122. assertEquals(paths[pathIdx], tw.getPathString());
  123. assertEquals(modes[pathIdx].getBits(), tw.getRawMode(0));
  124. assertSame(modes[pathIdx], tw.getFileMode(0));
  125. pathIdx++;
  126. }
  127. assertEquals(paths.length, pathIdx);
  128. }
  129. }
  130. @Test
  131. public void testSingleSubtree_NoRecursion() throws Exception {
  132. final DirCache dc = DirCache.newInCore();
  133. final String[] paths = { "a-", "a/b", "a/c", "a/d", "a0b" };
  134. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  135. for (int i = 0; i < paths.length; i++) {
  136. ents[i] = new DirCacheEntry(paths[i]);
  137. ents[i].setFileMode(FileMode.REGULAR_FILE);
  138. }
  139. final DirCacheBuilder b = dc.builder();
  140. for (DirCacheEntry ent : ents) {
  141. b.add(ent);
  142. }
  143. b.finish();
  144. final String[] expPaths = { "a-", "a", "a0b" };
  145. final FileMode[] expModes = { FileMode.REGULAR_FILE, FileMode.TREE,
  146. FileMode.REGULAR_FILE };
  147. final int expPos[] = { 0, -1, 4 };
  148. final DirCacheIterator i = new DirCacheIterator(dc);
  149. try (TreeWalk tw = new TreeWalk(db)) {
  150. tw.addTree(i);
  151. tw.setRecursive(false);
  152. int pathIdx = 0;
  153. while (tw.next()) {
  154. assertSame(i, tw.getTree(0, DirCacheIterator.class));
  155. assertEquals(expModes[pathIdx].getBits(), tw.getRawMode(0));
  156. assertSame(expModes[pathIdx], tw.getFileMode(0));
  157. assertEquals(expPaths[pathIdx], tw.getPathString());
  158. if (expPos[pathIdx] >= 0) {
  159. assertEquals(expPos[pathIdx], i.ptr);
  160. assertSame(ents[expPos[pathIdx]], i.getDirCacheEntry());
  161. } else {
  162. assertSame(FileMode.TREE, tw.getFileMode(0));
  163. }
  164. pathIdx++;
  165. }
  166. assertEquals(expPaths.length, pathIdx);
  167. }
  168. }
  169. @Test
  170. public void testSingleSubtree_Recursive() throws Exception {
  171. final DirCache dc = DirCache.newInCore();
  172. final FileMode mode = FileMode.REGULAR_FILE;
  173. final String[] paths = { "a-", "a/b", "a/c", "a/d", "a0b" };
  174. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  175. for (int i = 0; i < paths.length; i++) {
  176. ents[i] = new DirCacheEntry(paths[i]);
  177. ents[i].setFileMode(mode);
  178. }
  179. final DirCacheBuilder b = dc.builder();
  180. for (DirCacheEntry ent : ents) {
  181. b.add(ent);
  182. }
  183. b.finish();
  184. final DirCacheIterator i = new DirCacheIterator(dc);
  185. try (TreeWalk tw = new TreeWalk(db)) {
  186. tw.addTree(i);
  187. tw.setRecursive(true);
  188. int pathIdx = 0;
  189. while (tw.next()) {
  190. final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
  191. assertNotNull(c);
  192. assertEquals(pathIdx, c.ptr);
  193. assertSame(ents[pathIdx], c.getDirCacheEntry());
  194. assertEquals(paths[pathIdx], tw.getPathString());
  195. assertEquals(mode.getBits(), tw.getRawMode(0));
  196. assertSame(mode, tw.getFileMode(0));
  197. pathIdx++;
  198. }
  199. assertEquals(paths.length, pathIdx);
  200. }
  201. }
  202. @Test
  203. public void testTwoLevelSubtree_Recursive() throws Exception {
  204. final DirCache dc = DirCache.newInCore();
  205. final FileMode mode = FileMode.REGULAR_FILE;
  206. final String[] paths = { "a-", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
  207. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  208. for (int i = 0; i < paths.length; i++) {
  209. ents[i] = new DirCacheEntry(paths[i]);
  210. ents[i].setFileMode(mode);
  211. }
  212. final DirCacheBuilder b = dc.builder();
  213. for (DirCacheEntry ent : ents) {
  214. b.add(ent);
  215. }
  216. b.finish();
  217. try (TreeWalk tw = new TreeWalk(db)) {
  218. tw.addTree(new DirCacheIterator(dc));
  219. tw.setRecursive(true);
  220. int pathIdx = 0;
  221. while (tw.next()) {
  222. final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
  223. assertNotNull(c);
  224. assertEquals(pathIdx, c.ptr);
  225. assertSame(ents[pathIdx], c.getDirCacheEntry());
  226. assertEquals(paths[pathIdx], tw.getPathString());
  227. assertEquals(mode.getBits(), tw.getRawMode(0));
  228. assertSame(mode, tw.getFileMode(0));
  229. pathIdx++;
  230. }
  231. assertEquals(paths.length, pathIdx);
  232. }
  233. }
  234. @Test
  235. public void testReset() throws Exception {
  236. final DirCache dc = DirCache.newInCore();
  237. final FileMode mode = FileMode.REGULAR_FILE;
  238. final String[] paths = { "a-", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
  239. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  240. for (int i = 0; i < paths.length; i++) {
  241. ents[i] = new DirCacheEntry(paths[i]);
  242. ents[i].setFileMode(mode);
  243. }
  244. final DirCacheBuilder b = dc.builder();
  245. for (DirCacheEntry ent : ents) {
  246. b.add(ent);
  247. }
  248. b.finish();
  249. DirCacheIterator dci = new DirCacheIterator(dc);
  250. assertFalse(dci.eof());
  251. assertEquals("a-", dci.getEntryPathString());
  252. dci.next(1);
  253. assertFalse(dci.eof());
  254. assertEquals("a", dci.getEntryPathString());
  255. dci.next(1);
  256. assertFalse(dci.eof());
  257. assertEquals("a0b", dci.getEntryPathString());
  258. dci.next(1);
  259. assertTrue(dci.eof());
  260. // same entries the second time
  261. dci.reset();
  262. assertFalse(dci.eof());
  263. assertEquals("a-", dci.getEntryPathString());
  264. dci.next(1);
  265. assertFalse(dci.eof());
  266. assertEquals("a", dci.getEntryPathString());
  267. dci.next(1);
  268. assertFalse(dci.eof());
  269. assertEquals("a0b", dci.getEntryPathString());
  270. dci.next(1);
  271. assertTrue(dci.eof());
  272. // Step backwards
  273. dci.back(1);
  274. assertFalse(dci.eof());
  275. assertEquals("a0b", dci.getEntryPathString());
  276. dci.back(1);
  277. assertFalse(dci.eof());
  278. assertEquals("a", dci.getEntryPathString());
  279. dci.back(1);
  280. assertFalse(dci.eof());
  281. assertEquals("a-", dci.getEntryPathString());
  282. assertTrue(dci.first());
  283. // forward
  284. assertFalse(dci.eof());
  285. assertEquals("a-", dci.getEntryPathString());
  286. dci.next(1);
  287. assertFalse(dci.eof());
  288. assertEquals("a", dci.getEntryPathString());
  289. dci.next(1);
  290. assertFalse(dci.eof());
  291. assertEquals("a0b", dci.getEntryPathString());
  292. dci.next(1);
  293. assertTrue(dci.eof());
  294. // backwqrd halways, and forward again
  295. dci.back(1);
  296. assertFalse(dci.eof());
  297. assertEquals("a0b", dci.getEntryPathString());
  298. dci.back(1);
  299. assertFalse(dci.eof());
  300. assertEquals("a", dci.getEntryPathString());
  301. dci.next(1);
  302. assertFalse(dci.eof());
  303. assertEquals("a0b", dci.getEntryPathString());
  304. dci.next(1);
  305. assertTrue(dci.eof());
  306. dci.reset(); // a.
  307. dci.next(1); // a
  308. AbstractTreeIterator sti = dci.createSubtreeIterator(null);
  309. assertEquals("a/b", sti.getEntryPathString());
  310. sti.next(1);
  311. assertEquals("a/c", sti.getEntryPathString());
  312. sti.next(1);
  313. assertEquals("a/d", sti.getEntryPathString());
  314. sti.back(2);
  315. assertEquals("a/b", sti.getEntryPathString());
  316. }
  317. @Test
  318. public void testBackBug396127() throws Exception {
  319. final DirCache dc = DirCache.newInCore();
  320. final FileMode mode = FileMode.REGULAR_FILE;
  321. final String[] paths = { "git-gui/po/fr.po",
  322. "git_remote_helpers/git/repo.py" };
  323. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  324. for (int i = 0; i < paths.length; i++) {
  325. ents[i] = new DirCacheEntry(paths[i]);
  326. ents[i].setFileMode(mode);
  327. }
  328. final DirCacheBuilder b = dc.builder();
  329. for (DirCacheEntry ent : ents) {
  330. b.add(ent);
  331. }
  332. b.finish();
  333. DirCacheIterator dci = new DirCacheIterator(dc);
  334. assertFalse(dci.eof());
  335. assertEquals("git-gui", dci.getEntryPathString());
  336. dci.next(1);
  337. assertFalse(dci.eof());
  338. assertEquals("git_remote_helpers", dci.getEntryPathString());
  339. dci.back(1);
  340. assertFalse(dci.eof());
  341. assertEquals("git-gui", dci.getEntryPathString());
  342. dci.next(1);
  343. assertEquals("git_remote_helpers", dci.getEntryPathString());
  344. dci.next(1);
  345. assertTrue(dci.eof());
  346. }
  347. @Test
  348. public void testTwoLevelSubtree_FilterPath() throws Exception {
  349. final DirCache dc = DirCache.newInCore();
  350. final FileMode mode = FileMode.REGULAR_FILE;
  351. final String[] paths = { "a-", "a/b", "a/c/e", "a/c/f", "a/d", "a0b" };
  352. final DirCacheEntry[] ents = new DirCacheEntry[paths.length];
  353. for (int i = 0; i < paths.length; i++) {
  354. ents[i] = new DirCacheEntry(paths[i]);
  355. ents[i].setFileMode(mode);
  356. }
  357. final DirCacheBuilder b = dc.builder();
  358. for (DirCacheEntry ent : ents) {
  359. b.add(ent);
  360. }
  361. b.finish();
  362. try (TreeWalk tw = new TreeWalk(db)) {
  363. for (int victimIdx = 0; victimIdx < paths.length; victimIdx++) {
  364. tw.reset();
  365. tw.addTree(new DirCacheIterator(dc));
  366. tw.setFilter(PathFilterGroup.createFromStrings(Collections
  367. .singleton(paths[victimIdx])));
  368. tw.setRecursive(tw.getFilter().shouldBeRecursive());
  369. assertTrue(tw.next());
  370. final DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
  371. assertNotNull(c);
  372. assertEquals(victimIdx, c.ptr);
  373. assertSame(ents[victimIdx], c.getDirCacheEntry());
  374. assertEquals(paths[victimIdx], tw.getPathString());
  375. assertEquals(mode.getBits(), tw.getRawMode(0));
  376. assertSame(mode, tw.getFileMode(0));
  377. assertFalse(tw.next());
  378. }
  379. }
  380. }
  381. @Test
  382. public void testRemovedSubtree() throws Exception {
  383. final File path = JGitTestUtil
  384. .getTestResourceFile("dircache.testRemovedSubtree");
  385. final DirCache dc = DirCache.read(path, FS.DETECTED);
  386. assertEquals(2, dc.getEntryCount());
  387. try (TreeWalk tw = new TreeWalk(db)) {
  388. tw.setRecursive(true);
  389. tw.addTree(new DirCacheIterator(dc));
  390. assertTrue(tw.next());
  391. assertEquals("a/a", tw.getPathString());
  392. assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0));
  393. assertTrue(tw.next());
  394. assertEquals("q", tw.getPathString());
  395. assertSame(FileMode.REGULAR_FILE, tw.getFileMode(0));
  396. assertFalse(tw.next());
  397. }
  398. }
  399. }