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.

FileTreeIteratorTest.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * Copyright (C) 2008, 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.treewalk;
  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.assertTrue;
  48. import java.io.File;
  49. import java.io.IOException;
  50. import java.security.MessageDigest;
  51. import org.eclipse.jgit.api.Git;
  52. import org.eclipse.jgit.dircache.DirCache;
  53. import org.eclipse.jgit.dircache.DirCacheCheckout;
  54. import org.eclipse.jgit.dircache.DirCacheEditor;
  55. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  56. import org.eclipse.jgit.dircache.DirCacheEntry;
  57. import org.eclipse.jgit.dircache.DirCacheIterator;
  58. import org.eclipse.jgit.errors.CorruptObjectException;
  59. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  60. import org.eclipse.jgit.errors.MissingObjectException;
  61. import org.eclipse.jgit.junit.JGitTestUtil;
  62. import org.eclipse.jgit.junit.RepositoryTestCase;
  63. import org.eclipse.jgit.lib.ConfigConstants;
  64. import org.eclipse.jgit.lib.Constants;
  65. import org.eclipse.jgit.lib.FileMode;
  66. import org.eclipse.jgit.lib.ObjectId;
  67. import org.eclipse.jgit.lib.ObjectReader;
  68. import org.eclipse.jgit.lib.Repository;
  69. import org.eclipse.jgit.revwalk.RevCommit;
  70. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  71. import org.eclipse.jgit.treewalk.WorkingTreeIterator.MetadataDiff;
  72. import org.eclipse.jgit.treewalk.filter.PathFilter;
  73. import org.eclipse.jgit.util.FS;
  74. import org.eclipse.jgit.util.FileUtils;
  75. import org.eclipse.jgit.util.RawParseUtils;
  76. import org.junit.Before;
  77. import org.junit.Test;
  78. public class FileTreeIteratorTest extends RepositoryTestCase {
  79. private final String[] paths = { "a,", "a,b", "a/b", "a0b" };
  80. private long[] mtime;
  81. @Before
  82. public void setUp() throws Exception {
  83. super.setUp();
  84. // We build the entries backwards so that on POSIX systems we
  85. // are likely to get the entries in the trash directory in the
  86. // opposite order of what they should be in for the iteration.
  87. // This should stress the sorting code better than doing it in
  88. // the correct order.
  89. //
  90. mtime = new long[paths.length];
  91. for (int i = paths.length - 1; i >= 0; i--) {
  92. final String s = paths[i];
  93. writeTrashFile(s, s);
  94. mtime[i] = new File(trash, s).lastModified();
  95. }
  96. }
  97. @Test
  98. public void testGetEntryContentLength() throws Exception {
  99. final FileTreeIterator fti = new FileTreeIterator(db);
  100. fti.next(1);
  101. assertEquals(3, fti.getEntryContentLength());
  102. fti.back(1);
  103. assertEquals(2, fti.getEntryContentLength());
  104. fti.next(1);
  105. assertEquals(3, fti.getEntryContentLength());
  106. fti.reset();
  107. assertEquals(2, fti.getEntryContentLength());
  108. }
  109. @Test
  110. public void testEmptyIfRootIsFile() throws Exception {
  111. final File r = new File(trash, paths[0]);
  112. assertTrue(r.isFile());
  113. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS(),
  114. db.getConfig().get(WorkingTreeOptions.KEY));
  115. assertTrue(fti.first());
  116. assertTrue(fti.eof());
  117. }
  118. @Test
  119. public void testEmptyIfRootDoesNotExist() throws Exception {
  120. final File r = new File(trash, "not-existing-file");
  121. assertFalse(r.exists());
  122. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS(),
  123. db.getConfig().get(WorkingTreeOptions.KEY));
  124. assertTrue(fti.first());
  125. assertTrue(fti.eof());
  126. }
  127. @Test
  128. public void testEmptyIfRootIsEmpty() throws Exception {
  129. final File r = new File(trash, "not-existing-file");
  130. assertFalse(r.exists());
  131. FileUtils.mkdir(r);
  132. final FileTreeIterator fti = new FileTreeIterator(r, db.getFS(),
  133. db.getConfig().get(WorkingTreeOptions.KEY));
  134. assertTrue(fti.first());
  135. assertTrue(fti.eof());
  136. }
  137. @Test
  138. public void testEmptyIteratorOnEmptyDirectory() throws Exception {
  139. String nonExistingFileName = "not-existing-file";
  140. final File r = new File(trash, nonExistingFileName);
  141. assertFalse(r.exists());
  142. FileUtils.mkdir(r);
  143. final FileTreeIterator parent = new FileTreeIterator(db);
  144. while (!parent.getEntryPathString().equals(nonExistingFileName))
  145. parent.next(1);
  146. final FileTreeIterator childIter = new FileTreeIterator(parent, r,
  147. db.getFS());
  148. assertTrue(childIter.first());
  149. assertTrue(childIter.eof());
  150. String parentPath = parent.getEntryPathString();
  151. assertEquals(nonExistingFileName, parentPath);
  152. // must be "not-existing-file/", but getEntryPathString() was broken by
  153. // 445363 too
  154. String childPath = childIter.getEntryPathString();
  155. // in bug 445363 the iterator wrote garbage to the parent "path" field
  156. EmptyTreeIterator e = childIter.createEmptyTreeIterator();
  157. assertNotNull(e);
  158. // check if parent path is not overridden by empty iterator (bug 445363)
  159. // due bug 445363 this was "/ot-existing-file" instead of
  160. // "not-existing-file"
  161. assertEquals(parentPath, parent.getEntryPathString());
  162. assertEquals(parentPath + "/", childPath);
  163. assertEquals(parentPath + "/", childIter.getEntryPathString());
  164. assertEquals(childPath + "/", e.getEntryPathString());
  165. }
  166. @Test
  167. public void testSimpleIterate() throws Exception {
  168. final FileTreeIterator top = new FileTreeIterator(trash, db.getFS(),
  169. db.getConfig().get(WorkingTreeOptions.KEY));
  170. assertTrue(top.first());
  171. assertFalse(top.eof());
  172. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  173. assertEquals(paths[0], nameOf(top));
  174. assertEquals(paths[0].length(), top.getEntryLength());
  175. assertEquals(mtime[0], top.getEntryLastModified());
  176. top.next(1);
  177. assertFalse(top.first());
  178. assertFalse(top.eof());
  179. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  180. assertEquals(paths[1], nameOf(top));
  181. assertEquals(paths[1].length(), top.getEntryLength());
  182. assertEquals(mtime[1], top.getEntryLastModified());
  183. top.next(1);
  184. assertFalse(top.first());
  185. assertFalse(top.eof());
  186. assertEquals(FileMode.TREE.getBits(), top.mode);
  187. final ObjectReader reader = db.newObjectReader();
  188. final AbstractTreeIterator sub = top.createSubtreeIterator(reader);
  189. assertTrue(sub instanceof FileTreeIterator);
  190. final FileTreeIterator subfti = (FileTreeIterator) sub;
  191. assertTrue(sub.first());
  192. assertFalse(sub.eof());
  193. assertEquals(paths[2], nameOf(sub));
  194. assertEquals(paths[2].length(), subfti.getEntryLength());
  195. assertEquals(mtime[2], subfti.getEntryLastModified());
  196. sub.next(1);
  197. assertTrue(sub.eof());
  198. top.next(1);
  199. assertFalse(top.first());
  200. assertFalse(top.eof());
  201. assertEquals(FileMode.REGULAR_FILE.getBits(), top.mode);
  202. assertEquals(paths[3], nameOf(top));
  203. assertEquals(paths[3].length(), top.getEntryLength());
  204. assertEquals(mtime[3], top.getEntryLastModified());
  205. top.next(1);
  206. assertTrue(top.eof());
  207. }
  208. @Test
  209. public void testComputeFileObjectId() throws Exception {
  210. final FileTreeIterator top = new FileTreeIterator(trash, db.getFS(),
  211. db.getConfig().get(WorkingTreeOptions.KEY));
  212. final MessageDigest md = Constants.newMessageDigest();
  213. md.update(Constants.encodeASCII(Constants.TYPE_BLOB));
  214. md.update((byte) ' ');
  215. md.update(Constants.encodeASCII(paths[0].length()));
  216. md.update((byte) 0);
  217. md.update(Constants.encode(paths[0]));
  218. final ObjectId expect = ObjectId.fromRaw(md.digest());
  219. assertEquals(expect, top.getEntryObjectId());
  220. // Verify it was cached by removing the file and getting it again.
  221. //
  222. FileUtils.delete(new File(trash, paths[0]));
  223. assertEquals(expect, top.getEntryObjectId());
  224. }
  225. @Test
  226. public void testDirCacheMatchingId() throws Exception {
  227. File f = writeTrashFile("file", "content");
  228. try (Git git = new Git(db)) {
  229. writeTrashFile("file", "content");
  230. fsTick(f);
  231. git.add().addFilepattern("file").call();
  232. }
  233. DirCacheEntry dce = db.readDirCache().getEntry("file");
  234. TreeWalk tw = new TreeWalk(db);
  235. FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
  236. .getConfig().get(WorkingTreeOptions.KEY));
  237. tw.addTree(fti);
  238. DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
  239. tw.addTree(dci);
  240. fti.setDirCacheIterator(tw, 1);
  241. while (tw.next() && !tw.getPathString().equals("file")) {
  242. //
  243. }
  244. assertEquals(MetadataDiff.EQUAL, fti.compareMetadata(dce));
  245. ObjectId fromRaw = ObjectId.fromRaw(fti.idBuffer(), fti.idOffset());
  246. assertEquals("6b584e8ece562ebffc15d38808cd6b98fc3d97ea",
  247. fromRaw.getName());
  248. try (ObjectReader objectReader = db.newObjectReader()) {
  249. assertFalse(fti.isModified(dce, false, objectReader));
  250. }
  251. }
  252. @Test
  253. public void testTreewalkEnterSubtree() throws Exception {
  254. try (Git git = new Git(db)) {
  255. writeTrashFile("b/c", "b/c");
  256. writeTrashFile("z/.git", "gitdir: /tmp/somewhere");
  257. git.add().addFilepattern(".").call();
  258. git.rm().addFilepattern("a,").addFilepattern("a,b")
  259. .addFilepattern("a0b").call();
  260. assertEquals("[a/b, mode:100644][b/c, mode:100644][z, mode:160000]",
  261. indexState(0));
  262. FileUtils.delete(new File(db.getWorkTree(), "b"),
  263. FileUtils.RECURSIVE);
  264. TreeWalk tw = new TreeWalk(db);
  265. tw.addTree(new DirCacheIterator(db.readDirCache()));
  266. tw.addTree(new FileTreeIterator(db));
  267. assertTrue(tw.next());
  268. assertEquals("a", tw.getPathString());
  269. tw.enterSubtree();
  270. tw.next();
  271. assertEquals("a/b", tw.getPathString());
  272. tw.next();
  273. assertEquals("b", tw.getPathString());
  274. tw.enterSubtree();
  275. tw.next();
  276. assertEquals("b/c", tw.getPathString());
  277. assertNotNull(tw.getTree(0, AbstractTreeIterator.class));
  278. assertNotNull(tw.getTree(EmptyTreeIterator.class));
  279. }
  280. }
  281. @Test
  282. public void testIsModifiedSymlinkAsFile() throws Exception {
  283. writeTrashFile("symlink", "content");
  284. try (Git git = new Git(db)) {
  285. db.getConfig().setString(ConfigConstants.CONFIG_CORE_SECTION, null,
  286. ConfigConstants.CONFIG_KEY_SYMLINKS, "false");
  287. git.add().addFilepattern("symlink").call();
  288. git.commit().setMessage("commit").call();
  289. }
  290. // Modify previously committed DirCacheEntry and write it back to disk
  291. DirCacheEntry dce = db.readDirCache().getEntry("symlink");
  292. dce.setFileMode(FileMode.SYMLINK);
  293. try (ObjectReader objectReader = db.newObjectReader()) {
  294. DirCacheCheckout.checkoutEntry(db, dce, objectReader);
  295. FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(),
  296. db.getConfig().get(WorkingTreeOptions.KEY));
  297. while (!fti.getEntryPathString().equals("symlink"))
  298. fti.next(1);
  299. assertFalse(fti.isModified(dce, false, objectReader));
  300. }
  301. }
  302. @Test
  303. public void testIsModifiedFileSmudged() throws Exception {
  304. File f = writeTrashFile("file", "content");
  305. try (Git git = new Git(db)) {
  306. // The idea of this test is to check the smudged handling
  307. // Hopefully fsTick will make sure our entry gets smudged
  308. fsTick(f);
  309. writeTrashFile("file", "content");
  310. long lastModified = f.lastModified();
  311. git.add().addFilepattern("file").call();
  312. writeTrashFile("file", "conten2");
  313. f.setLastModified(lastModified);
  314. // We cannot trust this to go fast enough on
  315. // a system with less than one-second lastModified
  316. // resolution, so we force the index to have the
  317. // same timestamp as the file we look at.
  318. db.getIndexFile().setLastModified(lastModified);
  319. }
  320. DirCacheEntry dce = db.readDirCache().getEntry("file");
  321. FileTreeIterator fti = new FileTreeIterator(trash, db.getFS(), db
  322. .getConfig().get(WorkingTreeOptions.KEY));
  323. while (!fti.getEntryPathString().equals("file"))
  324. fti.next(1);
  325. // If the rounding trick does not work we could skip the compareMetaData
  326. // test and hope that we are usually testing the intended code path.
  327. assertEquals(MetadataDiff.SMUDGED, fti.compareMetadata(dce));
  328. try (ObjectReader objectReader = db.newObjectReader()) {
  329. assertTrue(fti.isModified(dce, false, objectReader));
  330. }
  331. }
  332. @Test
  333. public void submoduleHeadMatchesIndex() throws Exception {
  334. try (Git git = new Git(db);
  335. TreeWalk walk = new TreeWalk(db)) {
  336. writeTrashFile("file.txt", "content");
  337. git.add().addFilepattern("file.txt").call();
  338. final RevCommit id = git.commit().setMessage("create file").call();
  339. final String path = "sub";
  340. DirCache cache = db.lockDirCache();
  341. DirCacheEditor editor = cache.editor();
  342. editor.add(new PathEdit(path) {
  343. public void apply(DirCacheEntry ent) {
  344. ent.setFileMode(FileMode.GITLINK);
  345. ent.setObjectId(id);
  346. }
  347. });
  348. editor.commit();
  349. Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
  350. .setDirectory(new File(db.getWorkTree(), path)).call()
  351. .getRepository().close();
  352. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  353. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  354. walk.addTree(indexIter);
  355. walk.addTree(workTreeIter);
  356. walk.setFilter(PathFilter.create(path));
  357. assertTrue(walk.next());
  358. assertTrue(indexIter.idEqual(workTreeIter));
  359. }
  360. }
  361. @Test
  362. public void submoduleWithNoGitDirectory() throws Exception {
  363. try (Git git = new Git(db);
  364. TreeWalk walk = new TreeWalk(db)) {
  365. writeTrashFile("file.txt", "content");
  366. git.add().addFilepattern("file.txt").call();
  367. final RevCommit id = git.commit().setMessage("create file").call();
  368. final String path = "sub";
  369. DirCache cache = db.lockDirCache();
  370. DirCacheEditor editor = cache.editor();
  371. editor.add(new PathEdit(path) {
  372. public void apply(DirCacheEntry ent) {
  373. ent.setFileMode(FileMode.GITLINK);
  374. ent.setObjectId(id);
  375. }
  376. });
  377. editor.commit();
  378. File submoduleRoot = new File(db.getWorkTree(), path);
  379. assertTrue(submoduleRoot.mkdir());
  380. assertTrue(new File(submoduleRoot, Constants.DOT_GIT).mkdir());
  381. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  382. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  383. walk.addTree(indexIter);
  384. walk.addTree(workTreeIter);
  385. walk.setFilter(PathFilter.create(path));
  386. assertTrue(walk.next());
  387. assertFalse(indexIter.idEqual(workTreeIter));
  388. assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
  389. }
  390. }
  391. @Test
  392. public void submoduleWithNoHead() throws Exception {
  393. try (Git git = new Git(db);
  394. TreeWalk walk = new TreeWalk(db)) {
  395. writeTrashFile("file.txt", "content");
  396. git.add().addFilepattern("file.txt").call();
  397. final RevCommit id = git.commit().setMessage("create file").call();
  398. final String path = "sub";
  399. DirCache cache = db.lockDirCache();
  400. DirCacheEditor editor = cache.editor();
  401. editor.add(new PathEdit(path) {
  402. public void apply(DirCacheEntry ent) {
  403. ent.setFileMode(FileMode.GITLINK);
  404. ent.setObjectId(id);
  405. }
  406. });
  407. editor.commit();
  408. assertNotNull(Git.init().setDirectory(new File(db.getWorkTree(), path))
  409. .call().getRepository());
  410. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  411. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  412. walk.addTree(indexIter);
  413. walk.addTree(workTreeIter);
  414. walk.setFilter(PathFilter.create(path));
  415. assertTrue(walk.next());
  416. assertFalse(indexIter.idEqual(workTreeIter));
  417. assertEquals(ObjectId.zeroId(), workTreeIter.getEntryObjectId());
  418. }
  419. }
  420. @Test
  421. public void submoduleDirectoryIterator() throws Exception {
  422. try (Git git = new Git(db);
  423. TreeWalk walk = new TreeWalk(db)) {
  424. writeTrashFile("file.txt", "content");
  425. git.add().addFilepattern("file.txt").call();
  426. final RevCommit id = git.commit().setMessage("create file").call();
  427. final String path = "sub";
  428. DirCache cache = db.lockDirCache();
  429. DirCacheEditor editor = cache.editor();
  430. editor.add(new PathEdit(path) {
  431. public void apply(DirCacheEntry ent) {
  432. ent.setFileMode(FileMode.GITLINK);
  433. ent.setObjectId(id);
  434. }
  435. });
  436. editor.commit();
  437. Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
  438. .setDirectory(new File(db.getWorkTree(), path)).call()
  439. .getRepository().close();
  440. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  441. FileTreeIterator workTreeIter = new FileTreeIterator(db.getWorkTree(),
  442. db.getFS(), db.getConfig().get(WorkingTreeOptions.KEY));
  443. walk.addTree(indexIter);
  444. walk.addTree(workTreeIter);
  445. walk.setFilter(PathFilter.create(path));
  446. assertTrue(walk.next());
  447. assertTrue(indexIter.idEqual(workTreeIter));
  448. }
  449. }
  450. @Test
  451. public void submoduleNestedWithHeadMatchingIndex() throws Exception {
  452. try (Git git = new Git(db);
  453. TreeWalk walk = new TreeWalk(db)) {
  454. writeTrashFile("file.txt", "content");
  455. git.add().addFilepattern("file.txt").call();
  456. final RevCommit id = git.commit().setMessage("create file").call();
  457. final String path = "sub/dir1/dir2";
  458. DirCache cache = db.lockDirCache();
  459. DirCacheEditor editor = cache.editor();
  460. editor.add(new PathEdit(path) {
  461. public void apply(DirCacheEntry ent) {
  462. ent.setFileMode(FileMode.GITLINK);
  463. ent.setObjectId(id);
  464. }
  465. });
  466. editor.commit();
  467. Git.cloneRepository().setURI(db.getDirectory().toURI().toString())
  468. .setDirectory(new File(db.getWorkTree(), path)).call()
  469. .getRepository().close();
  470. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  471. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  472. walk.addTree(indexIter);
  473. walk.addTree(workTreeIter);
  474. walk.setFilter(PathFilter.create(path));
  475. assertTrue(walk.next());
  476. assertTrue(indexIter.idEqual(workTreeIter));
  477. }
  478. }
  479. @Test
  480. public void idOffset() throws Exception {
  481. try (Git git = new Git(db);
  482. TreeWalk tw = new TreeWalk(db)) {
  483. writeTrashFile("fileAinfsonly", "A");
  484. File fileBinindex = writeTrashFile("fileBinindex", "B");
  485. fsTick(fileBinindex);
  486. git.add().addFilepattern("fileBinindex").call();
  487. writeTrashFile("fileCinfsonly", "C");
  488. DirCacheIterator indexIter = new DirCacheIterator(db.readDirCache());
  489. FileTreeIterator workTreeIter = new FileTreeIterator(db);
  490. tw.addTree(indexIter);
  491. tw.addTree(workTreeIter);
  492. workTreeIter.setDirCacheIterator(tw, 0);
  493. assertEntry("d46c305e85b630558ee19cc47e73d2e5c8c64cdc", "a,", tw);
  494. assertEntry("58ee403f98538ec02409538b3f80adf610accdec", "a,b", tw);
  495. assertEntry("0000000000000000000000000000000000000000", "a", tw);
  496. assertEntry("b8d30ff397626f0f1d3538d66067edf865e201d6", "a0b", tw);
  497. // The reason for adding this test. Check that the id is correct for
  498. // mixed
  499. assertEntry("8c7e5a667f1b771847fe88c01c3de34413a1b220",
  500. "fileAinfsonly", tw);
  501. assertEntry("7371f47a6f8bd23a8fa1a8b2a9479cdd76380e54", "fileBinindex",
  502. tw);
  503. assertEntry("96d80cd6c4e7158dbebd0849f4fb7ce513e5828c",
  504. "fileCinfsonly", tw);
  505. assertFalse(tw.next());
  506. }
  507. }
  508. private final FileTreeIterator.FileModeStrategy NO_GITLINKS_STRATEGY =
  509. new FileTreeIterator.FileModeStrategy() {
  510. @Override
  511. public FileMode getMode(File f, FS.Attributes attributes) {
  512. if (attributes.isSymbolicLink()) {
  513. return FileMode.SYMLINK;
  514. } else if (attributes.isDirectory()) {
  515. // NOTE: in the production DefaultFileModeStrategy, there is
  516. // a check here for a subdirectory called '.git', and if it
  517. // exists, we create a GITLINK instead of recursing into the
  518. // tree. In this custom strategy, we ignore nested git dirs
  519. // and treat all directories the same.
  520. return FileMode.TREE;
  521. } else if (attributes.isExecutable()) {
  522. return FileMode.EXECUTABLE_FILE;
  523. } else {
  524. return FileMode.REGULAR_FILE;
  525. }
  526. }
  527. };
  528. private Repository createNestedRepo() throws IOException {
  529. File gitdir = createUniqueTestGitDir(false);
  530. FileRepositoryBuilder builder = new FileRepositoryBuilder();
  531. builder.setGitDir(gitdir);
  532. Repository nestedRepo = builder.build();
  533. nestedRepo.create();
  534. JGitTestUtil.writeTrashFile(nestedRepo, "sub", "a.txt", "content");
  535. File nestedRepoPath = new File(nestedRepo.getWorkTree(), "sub/nested");
  536. FileRepositoryBuilder nestedBuilder = new FileRepositoryBuilder();
  537. nestedBuilder.setWorkTree(nestedRepoPath);
  538. nestedBuilder.build().create();
  539. JGitTestUtil.writeTrashFile(nestedRepo, "sub/nested", "b.txt",
  540. "content b");
  541. return nestedRepo;
  542. }
  543. @Test
  544. public void testCustomFileModeStrategy() throws Exception {
  545. Repository nestedRepo = createNestedRepo();
  546. Git git = new Git(nestedRepo);
  547. // validate that our custom strategy is honored
  548. WorkingTreeIterator customIterator =
  549. new FileTreeIterator(nestedRepo, NO_GITLINKS_STRATEGY);
  550. git.add().setWorkingTreeIterator(customIterator)
  551. .addFilepattern(".").call();
  552. assertEquals(
  553. "[sub/a.txt, mode:100644, content:content]" +
  554. "[sub/nested/b.txt, mode:100644, content:content b]",
  555. indexState(nestedRepo, CONTENT));
  556. }
  557. @Test
  558. public void testCustomFileModeStrategyFromParentIterator() throws Exception {
  559. Repository nestedRepo = createNestedRepo();
  560. Git git = new Git(nestedRepo);
  561. FileTreeIterator customIterator =
  562. new FileTreeIterator(nestedRepo, NO_GITLINKS_STRATEGY);
  563. File r = new File(nestedRepo.getWorkTree(), "sub");
  564. // here we want to validate that if we create a new iterator using the
  565. // constructor that accepts a parent iterator, that the child iterator
  566. // correctly inherits the FileModeStrategy from the parent iterator.
  567. FileTreeIterator childIterator =
  568. new FileTreeIterator(customIterator, r, nestedRepo.getFS());
  569. git.add().setWorkingTreeIterator(childIterator).addFilepattern(".").call();
  570. assertEquals(
  571. "[sub/a.txt, mode:100644, content:content]" +
  572. "[sub/nested/b.txt, mode:100644, content:content b]",
  573. indexState(nestedRepo, CONTENT));
  574. }
  575. private static void assertEntry(String sha1string, String path, TreeWalk tw)
  576. throws MissingObjectException, IncorrectObjectTypeException,
  577. CorruptObjectException, IOException {
  578. assertTrue(tw.next());
  579. assertEquals(path, tw.getPathString());
  580. assertEquals(sha1string, tw.getObjectId(1).getName() /* 1=filetree here */);
  581. }
  582. private static String nameOf(final AbstractTreeIterator i) {
  583. return RawParseUtils.decode(Constants.CHARSET, i.path, 0, i.pathLen);
  584. }
  585. }