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.

TreeWalk.java 49KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  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;
  45. import static java.nio.charset.StandardCharsets.UTF_8;
  46. import java.io.IOException;
  47. import java.util.HashMap;
  48. import java.util.Map;
  49. import java.util.Set;
  50. import org.eclipse.jgit.annotations.Nullable;
  51. import org.eclipse.jgit.api.errors.JGitInternalException;
  52. import org.eclipse.jgit.attributes.Attribute;
  53. import org.eclipse.jgit.attributes.Attributes;
  54. import org.eclipse.jgit.attributes.AttributesHandler;
  55. import org.eclipse.jgit.attributes.AttributesNodeProvider;
  56. import org.eclipse.jgit.attributes.AttributesProvider;
  57. import org.eclipse.jgit.attributes.FilterCommandRegistry;
  58. import org.eclipse.jgit.dircache.DirCacheBuildIterator;
  59. import org.eclipse.jgit.dircache.DirCacheIterator;
  60. import org.eclipse.jgit.errors.CorruptObjectException;
  61. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  62. import org.eclipse.jgit.errors.MissingObjectException;
  63. import org.eclipse.jgit.errors.StopWalkException;
  64. import org.eclipse.jgit.lib.AnyObjectId;
  65. import org.eclipse.jgit.lib.Config;
  66. import org.eclipse.jgit.lib.ConfigConstants;
  67. import org.eclipse.jgit.lib.Constants;
  68. import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
  69. import org.eclipse.jgit.lib.FileMode;
  70. import org.eclipse.jgit.lib.MutableObjectId;
  71. import org.eclipse.jgit.lib.ObjectId;
  72. import org.eclipse.jgit.lib.ObjectReader;
  73. import org.eclipse.jgit.lib.Repository;
  74. import org.eclipse.jgit.revwalk.RevTree;
  75. import org.eclipse.jgit.treewalk.filter.PathFilter;
  76. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  77. import org.eclipse.jgit.util.QuotedString;
  78. import org.eclipse.jgit.util.RawParseUtils;
  79. import org.eclipse.jgit.util.io.EolStreamTypeUtil;
  80. /**
  81. * Walks one or more {@link org.eclipse.jgit.treewalk.AbstractTreeIterator}s in
  82. * parallel.
  83. * <p>
  84. * This class can perform n-way differences across as many trees as necessary.
  85. * <p>
  86. * Each tree added must have the same root as existing trees in the walk.
  87. * <p>
  88. * A TreeWalk instance can only be used once to generate results. Running a
  89. * second time requires creating a new TreeWalk instance, or invoking
  90. * {@link #reset()} and adding new trees before starting again. Resetting an
  91. * existing instance may be faster for some applications as some internal
  92. * buffers may be recycled.
  93. * <p>
  94. * TreeWalk instances are not thread-safe. Applications must either restrict
  95. * usage of a TreeWalk instance to a single thread, or implement their own
  96. * synchronization at a higher level.
  97. * <p>
  98. * Multiple simultaneous TreeWalk instances per
  99. * {@link org.eclipse.jgit.lib.Repository} are permitted, even from concurrent
  100. * threads.
  101. */
  102. public class TreeWalk implements AutoCloseable, AttributesProvider {
  103. private static final AbstractTreeIterator[] NO_TREES = {};
  104. /**
  105. * @since 4.2
  106. */
  107. public static enum OperationType {
  108. /**
  109. * Represents a checkout operation (for example a checkout or reset
  110. * operation).
  111. */
  112. CHECKOUT_OP,
  113. /**
  114. * Represents a checkin operation (for example an add operation)
  115. */
  116. CHECKIN_OP
  117. }
  118. /**
  119. * Type of operation you want to retrieve the git attributes for.
  120. */
  121. private OperationType operationType = OperationType.CHECKOUT_OP;
  122. /**
  123. * The filter command as defined in gitattributes. The keys are
  124. * filterName+"."+filterCommandType. E.g. "lfs.clean"
  125. */
  126. private Map<String, String> filterCommandsByNameDotType = new HashMap<>();
  127. /**
  128. * Set the operation type of this walk
  129. *
  130. * @param operationType
  131. * a {@link org.eclipse.jgit.treewalk.TreeWalk.OperationType}
  132. * object.
  133. * @since 4.2
  134. */
  135. public void setOperationType(OperationType operationType) {
  136. this.operationType = operationType;
  137. }
  138. /**
  139. * Open a tree walk and filter to exactly one path.
  140. * <p>
  141. * The returned tree walk is already positioned on the requested path, so
  142. * the caller should not need to invoke {@link #next()} unless they are
  143. * looking for a possible directory/file name conflict.
  144. *
  145. * @param reader
  146. * the reader the walker will obtain tree data from.
  147. * @param path
  148. * single path to advance the tree walk instance into.
  149. * @param trees
  150. * one or more trees to walk through, all with the same root.
  151. * @return a new tree walk configured for exactly this one path; null if no
  152. * path was found in any of the trees.
  153. * @throws java.io.IOException
  154. * reading a pack file or loose object failed.
  155. * @throws org.eclipse.jgit.errors.CorruptObjectException
  156. * an tree object could not be read as its data stream did not
  157. * appear to be a tree, or could not be inflated.
  158. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  159. * an object we expected to be a tree was not a tree.
  160. * @throws org.eclipse.jgit.errors.MissingObjectException
  161. * a tree object was not found.
  162. */
  163. public static TreeWalk forPath(final ObjectReader reader, final String path,
  164. final AnyObjectId... trees) throws MissingObjectException,
  165. IncorrectObjectTypeException, CorruptObjectException, IOException {
  166. return forPath(null, reader, path, trees);
  167. }
  168. /**
  169. * Open a tree walk and filter to exactly one path.
  170. * <p>
  171. * The returned tree walk is already positioned on the requested path, so
  172. * the caller should not need to invoke {@link #next()} unless they are
  173. * looking for a possible directory/file name conflict.
  174. *
  175. * @param repo
  176. * repository to read config data and
  177. * {@link org.eclipse.jgit.attributes.AttributesNodeProvider}
  178. * from.
  179. * @param reader
  180. * the reader the walker will obtain tree data from.
  181. * @param path
  182. * single path to advance the tree walk instance into.
  183. * @param trees
  184. * one or more trees to walk through, all with the same root.
  185. * @return a new tree walk configured for exactly this one path; null if no
  186. * path was found in any of the trees.
  187. * @throws java.io.IOException
  188. * reading a pack file or loose object failed.
  189. * @throws org.eclipse.jgit.errors.CorruptObjectException
  190. * an tree object could not be read as its data stream did not
  191. * appear to be a tree, or could not be inflated.
  192. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  193. * an object we expected to be a tree was not a tree.
  194. * @throws org.eclipse.jgit.errors.MissingObjectException
  195. * a tree object was not found.
  196. * @since 4.3
  197. */
  198. public static TreeWalk forPath(final @Nullable Repository repo,
  199. final ObjectReader reader, final String path,
  200. final AnyObjectId... trees)
  201. throws MissingObjectException, IncorrectObjectTypeException,
  202. CorruptObjectException, IOException {
  203. TreeWalk tw = new TreeWalk(repo, reader);
  204. PathFilter f = PathFilter.create(path);
  205. tw.setFilter(f);
  206. tw.reset(trees);
  207. tw.setRecursive(false);
  208. while (tw.next()) {
  209. if (f.isDone(tw)) {
  210. return tw;
  211. } else if (tw.isSubtree()) {
  212. tw.enterSubtree();
  213. }
  214. }
  215. return null;
  216. }
  217. /**
  218. * Open a tree walk and filter to exactly one path.
  219. * <p>
  220. * The returned tree walk is already positioned on the requested path, so
  221. * the caller should not need to invoke {@link #next()} unless they are
  222. * looking for a possible directory/file name conflict.
  223. *
  224. * @param db
  225. * repository to read tree object data from.
  226. * @param path
  227. * single path to advance the tree walk instance into.
  228. * @param trees
  229. * one or more trees to walk through, all with the same root.
  230. * @return a new tree walk configured for exactly this one path; null if no
  231. * path was found in any of the trees.
  232. * @throws java.io.IOException
  233. * reading a pack file or loose object failed.
  234. * @throws org.eclipse.jgit.errors.CorruptObjectException
  235. * an tree object could not be read as its data stream did not
  236. * appear to be a tree, or could not be inflated.
  237. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  238. * an object we expected to be a tree was not a tree.
  239. * @throws org.eclipse.jgit.errors.MissingObjectException
  240. * a tree object was not found.
  241. */
  242. public static TreeWalk forPath(final Repository db, final String path,
  243. final AnyObjectId... trees) throws MissingObjectException,
  244. IncorrectObjectTypeException, CorruptObjectException, IOException {
  245. try (ObjectReader reader = db.newObjectReader()) {
  246. return forPath(db, reader, path, trees);
  247. }
  248. }
  249. /**
  250. * Open a tree walk and filter to exactly one path.
  251. * <p>
  252. * The returned tree walk is already positioned on the requested path, so
  253. * the caller should not need to invoke {@link #next()} unless they are
  254. * looking for a possible directory/file name conflict.
  255. *
  256. * @param db
  257. * repository to read tree object data from.
  258. * @param path
  259. * single path to advance the tree walk instance into.
  260. * @param tree
  261. * the single tree to walk through.
  262. * @return a new tree walk configured for exactly this one path; null if no
  263. * path was found in any of the trees.
  264. * @throws java.io.IOException
  265. * reading a pack file or loose object failed.
  266. * @throws org.eclipse.jgit.errors.CorruptObjectException
  267. * an tree object could not be read as its data stream did not
  268. * appear to be a tree, or could not be inflated.
  269. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  270. * an object we expected to be a tree was not a tree.
  271. * @throws org.eclipse.jgit.errors.MissingObjectException
  272. * a tree object was not found.
  273. */
  274. public static TreeWalk forPath(final Repository db, final String path,
  275. final RevTree tree) throws MissingObjectException,
  276. IncorrectObjectTypeException, CorruptObjectException, IOException {
  277. return forPath(db, path, new ObjectId[] { tree });
  278. }
  279. private final ObjectReader reader;
  280. private final boolean closeReader;
  281. private final MutableObjectId idBuffer = new MutableObjectId();
  282. private TreeFilter filter;
  283. AbstractTreeIterator[] trees;
  284. private boolean recursive;
  285. private boolean postOrderTraversal;
  286. int depth;
  287. private boolean advance;
  288. private boolean postChildren;
  289. private AttributesNodeProvider attributesNodeProvider;
  290. AbstractTreeIterator currentHead;
  291. /** Cached attribute for the current entry */
  292. private Attributes attrs = null;
  293. /** Cached attributes handler */
  294. private AttributesHandler attributesHandler;
  295. private Config config;
  296. private Set<String> filterCommands;
  297. /**
  298. * Create a new tree walker for a given repository.
  299. *
  300. * @param repo
  301. * the repository the walker will obtain data from. An
  302. * ObjectReader will be created by the walker, and will be closed
  303. * when the walker is closed.
  304. */
  305. public TreeWalk(Repository repo) {
  306. this(repo, repo.newObjectReader(), true);
  307. }
  308. /**
  309. * Create a new tree walker for a given repository.
  310. *
  311. * @param repo
  312. * the repository the walker will obtain data from. An
  313. * ObjectReader will be created by the walker, and will be closed
  314. * when the walker is closed.
  315. * @param or
  316. * the reader the walker will obtain tree data from. The reader
  317. * is not closed when the walker is closed.
  318. * @since 4.3
  319. */
  320. public TreeWalk(@Nullable Repository repo, ObjectReader or) {
  321. this(repo, or, false);
  322. }
  323. /**
  324. * Create a new tree walker for a given repository.
  325. *
  326. * @param or
  327. * the reader the walker will obtain tree data from. The reader
  328. * is not closed when the walker is closed.
  329. */
  330. public TreeWalk(ObjectReader or) {
  331. this(null, or, false);
  332. }
  333. private TreeWalk(final @Nullable Repository repo, final ObjectReader or,
  334. final boolean closeReader) {
  335. if (repo != null) {
  336. config = repo.getConfig();
  337. attributesNodeProvider = repo.createAttributesNodeProvider();
  338. filterCommands = FilterCommandRegistry
  339. .getRegisteredFilterCommands();
  340. } else {
  341. config = null;
  342. attributesNodeProvider = null;
  343. }
  344. reader = or;
  345. filter = TreeFilter.ALL;
  346. trees = NO_TREES;
  347. this.closeReader = closeReader;
  348. }
  349. /**
  350. * Get the reader this walker is using to load objects.
  351. *
  352. * @return the reader this walker is using to load objects.
  353. */
  354. public ObjectReader getObjectReader() {
  355. return reader;
  356. }
  357. /**
  358. * Get the operation type
  359. *
  360. * @return the {@link org.eclipse.jgit.treewalk.TreeWalk.OperationType}
  361. * @since 4.3
  362. */
  363. public OperationType getOperationType() {
  364. return operationType;
  365. }
  366. /**
  367. * {@inheritDoc}
  368. * <p>
  369. * Release any resources used by this walker's reader.
  370. * <p>
  371. * A walker that has been released can be used again, but may need to be
  372. * released after the subsequent usage.
  373. *
  374. * @since 4.0
  375. */
  376. @Override
  377. public void close() {
  378. if (closeReader) {
  379. reader.close();
  380. }
  381. }
  382. /**
  383. * Get the currently configured filter.
  384. *
  385. * @return the current filter. Never null as a filter is always needed.
  386. */
  387. public TreeFilter getFilter() {
  388. return filter;
  389. }
  390. /**
  391. * Set the tree entry filter for this walker.
  392. * <p>
  393. * Multiple filters may be combined by constructing an arbitrary tree of
  394. * <code>AndTreeFilter</code> or <code>OrTreeFilter</code> instances to
  395. * describe the boolean expression required by the application. Custom
  396. * filter implementations may also be constructed by applications.
  397. * <p>
  398. * Note that filters are not thread-safe and may not be shared by concurrent
  399. * TreeWalk instances. Every TreeWalk must be supplied its own unique
  400. * filter, unless the filter implementation specifically states it is (and
  401. * always will be) thread-safe. Callers may use
  402. * {@link org.eclipse.jgit.treewalk.filter.TreeFilter#clone()} to create a
  403. * unique filter tree for this TreeWalk instance.
  404. *
  405. * @param newFilter
  406. * the new filter. If null the special
  407. * {@link org.eclipse.jgit.treewalk.filter.TreeFilter#ALL} filter
  408. * will be used instead, as it matches every entry.
  409. * @see org.eclipse.jgit.treewalk.filter.AndTreeFilter
  410. * @see org.eclipse.jgit.treewalk.filter.OrTreeFilter
  411. */
  412. public void setFilter(TreeFilter newFilter) {
  413. filter = newFilter != null ? newFilter : TreeFilter.ALL;
  414. }
  415. /**
  416. * Is this walker automatically entering into subtrees?
  417. * <p>
  418. * If the walker is recursive then the caller will not see a subtree node
  419. * and instead will only receive file nodes in all relevant subtrees.
  420. *
  421. * @return true if automatically entering subtrees is enabled.
  422. */
  423. public boolean isRecursive() {
  424. return recursive;
  425. }
  426. /**
  427. * Set the walker to enter (or not enter) subtrees automatically.
  428. * <p>
  429. * If recursive mode is enabled the walker will hide subtree nodes from the
  430. * calling application and will produce only file level nodes. If a tree
  431. * (directory) is deleted then all of the file level nodes will appear to be
  432. * deleted, recursively, through as many levels as necessary to account for
  433. * all entries.
  434. *
  435. * @param b
  436. * true to skip subtree nodes and only obtain files nodes.
  437. */
  438. public void setRecursive(boolean b) {
  439. recursive = b;
  440. }
  441. /**
  442. * Does this walker return a tree entry after it exits the subtree?
  443. * <p>
  444. * If post order traversal is enabled then the walker will return a subtree
  445. * after it has returned the last entry within that subtree. This may cause
  446. * a subtree to be seen by the application twice if {@link #isRecursive()}
  447. * is false, as the application will see it once, call
  448. * {@link #enterSubtree()}, and then see it again as it leaves the subtree.
  449. * <p>
  450. * If an application does not enable {@link #isRecursive()} and it does not
  451. * call {@link #enterSubtree()} then the tree is returned only once as none
  452. * of the children were processed.
  453. *
  454. * @return true if subtrees are returned after entries within the subtree.
  455. */
  456. public boolean isPostOrderTraversal() {
  457. return postOrderTraversal;
  458. }
  459. /**
  460. * Set the walker to return trees after their children.
  461. *
  462. * @param b
  463. * true to get trees after their children.
  464. * @see #isPostOrderTraversal()
  465. */
  466. public void setPostOrderTraversal(boolean b) {
  467. postOrderTraversal = b;
  468. }
  469. /**
  470. * Sets the {@link org.eclipse.jgit.attributes.AttributesNodeProvider} for
  471. * this {@link org.eclipse.jgit.treewalk.TreeWalk}.
  472. * <p>
  473. * This is a requirement for a correct computation of the git attributes. If
  474. * this {@link org.eclipse.jgit.treewalk.TreeWalk} has been built using
  475. * {@link #TreeWalk(Repository)} constructor, the
  476. * {@link org.eclipse.jgit.attributes.AttributesNodeProvider} has already
  477. * been set. Indeed,the {@link org.eclipse.jgit.lib.Repository} can provide
  478. * an {@link org.eclipse.jgit.attributes.AttributesNodeProvider} using
  479. * {@link org.eclipse.jgit.lib.Repository#createAttributesNodeProvider()}
  480. * method. Otherwise you should provide one.
  481. * </p>
  482. *
  483. * @see Repository#createAttributesNodeProvider()
  484. * @param provider
  485. * a {@link org.eclipse.jgit.attributes.AttributesNodeProvider}
  486. * object.
  487. * @since 4.2
  488. */
  489. public void setAttributesNodeProvider(AttributesNodeProvider provider) {
  490. attributesNodeProvider = provider;
  491. }
  492. /**
  493. * Get the attributes node provider
  494. *
  495. * @return the {@link org.eclipse.jgit.attributes.AttributesNodeProvider}
  496. * for this {@link org.eclipse.jgit.treewalk.TreeWalk}.
  497. * @since 4.3
  498. */
  499. public AttributesNodeProvider getAttributesNodeProvider() {
  500. return attributesNodeProvider;
  501. }
  502. /**
  503. * {@inheritDoc}
  504. * <p>
  505. * Retrieve the git attributes for the current entry.
  506. *
  507. * <h3>Git attribute computation</h3>
  508. *
  509. * <ul>
  510. * <li>Get the attributes matching the current path entry from the info file
  511. * (see {@link AttributesNodeProvider#getInfoAttributesNode()}).</li>
  512. * <li>Completes the list of attributes using the .gitattributes files
  513. * located on the current path (the further the directory that contains
  514. * .gitattributes is from the path in question, the lower its precedence).
  515. * For a checkin operation, it will look first on the working tree (if any).
  516. * If there is no attributes file, it will fallback on the index. For a
  517. * checkout operation, it will first use the index entry and then fallback
  518. * on the working tree if none.</li>
  519. * <li>In the end, completes the list of matching attributes using the
  520. * global attribute file define in the configuration (see
  521. * {@link AttributesNodeProvider#getGlobalAttributesNode()})</li>
  522. *
  523. * </ul>
  524. *
  525. *
  526. * <h3>Iterator constraints</h3>
  527. *
  528. * <p>
  529. * In order to have a correct list of attributes for the current entry, this
  530. * {@link TreeWalk} requires to have at least one
  531. * {@link AttributesNodeProvider} and a {@link DirCacheIterator} set up. An
  532. * {@link AttributesNodeProvider} is used to retrieve the attributes from
  533. * the info attributes file and the global attributes file. The
  534. * {@link DirCacheIterator} is used to retrieve the .gitattributes files
  535. * stored in the index. A {@link WorkingTreeIterator} can also be provided
  536. * to access the local version of the .gitattributes files. If none is
  537. * provided it will fallback on the {@link DirCacheIterator}.
  538. * </p>
  539. *
  540. * @since 4.2
  541. */
  542. @Override
  543. public Attributes getAttributes() {
  544. if (attrs != null)
  545. return attrs;
  546. if (attributesNodeProvider == null) {
  547. // The work tree should have a AttributesNodeProvider to be able to
  548. // retrieve the info and global attributes node
  549. throw new IllegalStateException(
  550. "The tree walk should have one AttributesNodeProvider set in order to compute the git attributes."); //$NON-NLS-1$
  551. }
  552. try {
  553. // Lazy create the attributesHandler on the first access of
  554. // attributes. This requires the info, global and root
  555. // attributes nodes
  556. if (attributesHandler == null) {
  557. attributesHandler = new AttributesHandler(this);
  558. }
  559. attrs = attributesHandler.getAttributes();
  560. return attrs;
  561. } catch (IOException e) {
  562. throw new JGitInternalException("Error while parsing attributes", //$NON-NLS-1$
  563. e);
  564. }
  565. }
  566. /**
  567. * Get the EOL stream type of the current entry using the config and
  568. * {@link #getAttributes()}.
  569. *
  570. * @param opType
  571. * the operationtype (checkin/checkout) which should be used
  572. * @return the EOL stream type of the current entry using the config and
  573. * {@link #getAttributes()}. Note that this method may return null
  574. * if the {@link org.eclipse.jgit.treewalk.TreeWalk} is not based on
  575. * a working tree
  576. * @since 4.10
  577. */
  578. @Nullable
  579. public EolStreamType getEolStreamType(OperationType opType) {
  580. if (attributesNodeProvider == null || config == null)
  581. return null;
  582. return EolStreamTypeUtil.detectStreamType(
  583. opType != null ? opType : operationType,
  584. config.get(WorkingTreeOptions.KEY), getAttributes());
  585. }
  586. /**
  587. * Reset this walker so new tree iterators can be added to it.
  588. */
  589. public void reset() {
  590. attrs = null;
  591. attributesHandler = null;
  592. trees = NO_TREES;
  593. advance = false;
  594. depth = 0;
  595. }
  596. /**
  597. * Reset this walker to run over a single existing tree.
  598. *
  599. * @param id
  600. * the tree we need to parse. The walker will execute over this
  601. * single tree if the reset is successful.
  602. * @throws org.eclipse.jgit.errors.MissingObjectException
  603. * the given tree object does not exist in this repository.
  604. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  605. * the given object id does not denote a tree, but instead names
  606. * some other non-tree type of object. Note that commits are not
  607. * trees, even if they are sometimes called a "tree-ish".
  608. * @throws org.eclipse.jgit.errors.CorruptObjectException
  609. * the object claimed to be a tree, but its contents did not
  610. * appear to be a tree. The repository may have data corruption.
  611. * @throws java.io.IOException
  612. * a loose object or pack file could not be read.
  613. */
  614. public void reset(AnyObjectId id) throws MissingObjectException,
  615. IncorrectObjectTypeException, CorruptObjectException, IOException {
  616. if (trees.length == 1) {
  617. AbstractTreeIterator o = trees[0];
  618. while (o.parent != null)
  619. o = o.parent;
  620. if (o instanceof CanonicalTreeParser) {
  621. o.matches = null;
  622. o.matchShift = 0;
  623. ((CanonicalTreeParser) o).reset(reader, id);
  624. trees[0] = o;
  625. } else {
  626. trees[0] = parserFor(id);
  627. }
  628. } else {
  629. trees = new AbstractTreeIterator[] { parserFor(id) };
  630. }
  631. advance = false;
  632. depth = 0;
  633. attrs = null;
  634. }
  635. /**
  636. * Reset this walker to run over a set of existing trees.
  637. *
  638. * @param ids
  639. * the trees we need to parse. The walker will execute over this
  640. * many parallel trees if the reset is successful.
  641. * @throws org.eclipse.jgit.errors.MissingObjectException
  642. * the given tree object does not exist in this repository.
  643. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  644. * the given object id does not denote a tree, but instead names
  645. * some other non-tree type of object. Note that commits are not
  646. * trees, even if they are sometimes called a "tree-ish".
  647. * @throws org.eclipse.jgit.errors.CorruptObjectException
  648. * the object claimed to be a tree, but its contents did not
  649. * appear to be a tree. The repository may have data corruption.
  650. * @throws java.io.IOException
  651. * a loose object or pack file could not be read.
  652. */
  653. public void reset(AnyObjectId... ids) throws MissingObjectException,
  654. IncorrectObjectTypeException, CorruptObjectException, IOException {
  655. final int oldLen = trees.length;
  656. final int newLen = ids.length;
  657. final AbstractTreeIterator[] r = newLen == oldLen ? trees
  658. : new AbstractTreeIterator[newLen];
  659. for (int i = 0; i < newLen; i++) {
  660. AbstractTreeIterator o;
  661. if (i < oldLen) {
  662. o = trees[i];
  663. while (o.parent != null)
  664. o = o.parent;
  665. if (o instanceof CanonicalTreeParser && o.pathOffset == 0) {
  666. o.matches = null;
  667. o.matchShift = 0;
  668. ((CanonicalTreeParser) o).reset(reader, ids[i]);
  669. r[i] = o;
  670. continue;
  671. }
  672. }
  673. o = parserFor(ids[i]);
  674. r[i] = o;
  675. }
  676. trees = r;
  677. advance = false;
  678. depth = 0;
  679. attrs = null;
  680. }
  681. /**
  682. * Add an already existing tree object for walking.
  683. * <p>
  684. * The position of this tree is returned to the caller, in case the caller
  685. * has lost track of the order they added the trees into the walker.
  686. * <p>
  687. * The tree must have the same root as existing trees in the walk.
  688. *
  689. * @param id
  690. * identity of the tree object the caller wants walked.
  691. * @return position of this tree within the walker.
  692. * @throws org.eclipse.jgit.errors.MissingObjectException
  693. * the given tree object does not exist in this repository.
  694. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  695. * the given object id does not denote a tree, but instead names
  696. * some other non-tree type of object. Note that commits are not
  697. * trees, even if they are sometimes called a "tree-ish".
  698. * @throws org.eclipse.jgit.errors.CorruptObjectException
  699. * the object claimed to be a tree, but its contents did not
  700. * appear to be a tree. The repository may have data corruption.
  701. * @throws java.io.IOException
  702. * a loose object or pack file could not be read.
  703. */
  704. public int addTree(AnyObjectId id) throws MissingObjectException,
  705. IncorrectObjectTypeException, CorruptObjectException, IOException {
  706. return addTree(parserFor(id));
  707. }
  708. /**
  709. * Add an already created tree iterator for walking.
  710. * <p>
  711. * The position of this tree is returned to the caller, in case the caller
  712. * has lost track of the order they added the trees into the walker.
  713. * <p>
  714. * The tree which the iterator operates on must have the same root as
  715. * existing trees in the walk.
  716. *
  717. * @param p
  718. * an iterator to walk over. The iterator should be new, with no
  719. * parent, and should still be positioned before the first entry.
  720. * The tree which the iterator operates on must have the same
  721. * root as other trees in the walk.
  722. * @return position of this tree within the walker.
  723. */
  724. public int addTree(AbstractTreeIterator p) {
  725. int n = trees.length;
  726. AbstractTreeIterator[] newTrees = new AbstractTreeIterator[n + 1];
  727. System.arraycopy(trees, 0, newTrees, 0, n);
  728. newTrees[n] = p;
  729. p.matches = null;
  730. p.matchShift = 0;
  731. trees = newTrees;
  732. return n;
  733. }
  734. /**
  735. * Get the number of trees known to this walker.
  736. *
  737. * @return the total number of trees this walker is iterating over.
  738. */
  739. public int getTreeCount() {
  740. return trees.length;
  741. }
  742. /**
  743. * Advance this walker to the next relevant entry.
  744. *
  745. * @return true if there is an entry available; false if all entries have
  746. * been walked and the walk of this set of tree iterators is over.
  747. * @throws org.eclipse.jgit.errors.MissingObjectException
  748. * {@link #isRecursive()} was enabled, a subtree was found, but
  749. * the subtree object does not exist in this repository. The
  750. * repository may be missing objects.
  751. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  752. * {@link #isRecursive()} was enabled, a subtree was found, and
  753. * the subtree id does not denote a tree, but instead names some
  754. * other non-tree type of object. The repository may have data
  755. * corruption.
  756. * @throws org.eclipse.jgit.errors.CorruptObjectException
  757. * the contents of a tree did not appear to be a tree. The
  758. * repository may have data corruption.
  759. * @throws java.io.IOException
  760. * a loose object or pack file could not be read.
  761. */
  762. public boolean next() throws MissingObjectException,
  763. IncorrectObjectTypeException, CorruptObjectException, IOException {
  764. try {
  765. if (advance) {
  766. advance = false;
  767. postChildren = false;
  768. popEntriesEqual();
  769. }
  770. for (;;) {
  771. attrs = null;
  772. final AbstractTreeIterator t = min();
  773. if (t.eof()) {
  774. if (depth > 0) {
  775. exitSubtree();
  776. if (postOrderTraversal) {
  777. advance = true;
  778. postChildren = true;
  779. return true;
  780. }
  781. popEntriesEqual();
  782. continue;
  783. }
  784. return false;
  785. }
  786. currentHead = t;
  787. if (filter.matchFilter(this) == 1) {
  788. skipEntriesEqual();
  789. continue;
  790. }
  791. if (recursive && FileMode.TREE.equals(t.mode)) {
  792. enterSubtree();
  793. continue;
  794. }
  795. advance = true;
  796. return true;
  797. }
  798. } catch (StopWalkException stop) {
  799. stopWalk();
  800. return false;
  801. }
  802. }
  803. /**
  804. * Notify iterators the walk is aborting.
  805. * <p>
  806. * Primarily to notify {@link DirCacheBuildIterator} the walk is aborting so
  807. * that it can copy any remaining entries.
  808. *
  809. * @throws IOException
  810. * if traversal of remaining entries throws an exception during
  811. * object access. This should never occur as remaining trees
  812. * should already be in memory, however the methods used to
  813. * finish traversal are declared to throw IOException.
  814. */
  815. void stopWalk() throws IOException {
  816. for (AbstractTreeIterator t : trees) {
  817. t.stopWalk();
  818. }
  819. }
  820. /**
  821. * Obtain the tree iterator for the current entry.
  822. * <p>
  823. * Entering into (or exiting out of) a subtree causes the current tree
  824. * iterator instance to be changed for the nth tree. This allows the tree
  825. * iterators to manage only one list of items, with the diving handled by
  826. * recursive trees.
  827. *
  828. * @param nth
  829. * tree to obtain the current iterator of.
  830. * @param clazz
  831. * type of the tree iterator expected by the caller.
  832. * @return r the current iterator of the requested type; null if the tree
  833. * has no entry to match the current path.
  834. */
  835. @SuppressWarnings("unchecked")
  836. public <T extends AbstractTreeIterator> T getTree(final int nth,
  837. final Class<T> clazz) {
  838. final AbstractTreeIterator t = trees[nth];
  839. return t.matches == currentHead ? (T) t : null;
  840. }
  841. /**
  842. * Obtain the raw {@link org.eclipse.jgit.lib.FileMode} bits for the current
  843. * entry.
  844. * <p>
  845. * Every added tree supplies mode bits, even if the tree does not contain
  846. * the current entry. In the latter case
  847. * {@link org.eclipse.jgit.lib.FileMode#MISSING}'s mode bits (0) are
  848. * returned.
  849. *
  850. * @param nth
  851. * tree to obtain the mode bits from.
  852. * @return mode bits for the current entry of the nth tree.
  853. * @see FileMode#fromBits(int)
  854. */
  855. public int getRawMode(int nth) {
  856. final AbstractTreeIterator t = trees[nth];
  857. return t.matches == currentHead ? t.mode : 0;
  858. }
  859. /**
  860. * Obtain the {@link org.eclipse.jgit.lib.FileMode} for the current entry.
  861. * <p>
  862. * Every added tree supplies a mode, even if the tree does not contain the
  863. * current entry. In the latter case
  864. * {@link org.eclipse.jgit.lib.FileMode#MISSING} is returned.
  865. *
  866. * @param nth
  867. * tree to obtain the mode from.
  868. * @return mode for the current entry of the nth tree.
  869. */
  870. public FileMode getFileMode(int nth) {
  871. return FileMode.fromBits(getRawMode(nth));
  872. }
  873. /**
  874. * Obtain the {@link org.eclipse.jgit.lib.FileMode} for the current entry on
  875. * the currentHead tree
  876. *
  877. * @return mode for the current entry of the currentHead tree.
  878. * @since 4.3
  879. */
  880. public FileMode getFileMode() {
  881. return FileMode.fromBits(currentHead.mode);
  882. }
  883. /**
  884. * Obtain the ObjectId for the current entry.
  885. * <p>
  886. * Using this method to compare ObjectId values between trees of this walker
  887. * is very inefficient. Applications should try to use
  888. * {@link #idEqual(int, int)} or {@link #getObjectId(MutableObjectId, int)}
  889. * whenever possible.
  890. * <p>
  891. * Every tree supplies an object id, even if the tree does not contain the
  892. * current entry. In the latter case
  893. * {@link org.eclipse.jgit.lib.ObjectId#zeroId()} is returned.
  894. *
  895. * @param nth
  896. * tree to obtain the object identifier from.
  897. * @return object identifier for the current tree entry.
  898. * @see #getObjectId(MutableObjectId, int)
  899. * @see #idEqual(int, int)
  900. * @see #getObjectId(MutableObjectId, int)
  901. * @see #idEqual(int, int)
  902. */
  903. public ObjectId getObjectId(int nth) {
  904. final AbstractTreeIterator t = trees[nth];
  905. return t.matches == currentHead ? t.getEntryObjectId() : ObjectId
  906. .zeroId();
  907. }
  908. /**
  909. * Obtain the ObjectId for the current entry.
  910. * <p>
  911. * Every tree supplies an object id, even if the tree does not contain the
  912. * current entry. In the latter case
  913. * {@link org.eclipse.jgit.lib.ObjectId#zeroId()} is supplied.
  914. * <p>
  915. * Applications should try to use {@link #idEqual(int, int)} when possible
  916. * as it avoids conversion overheads.
  917. *
  918. * @param out
  919. * buffer to copy the object id into.
  920. * @param nth
  921. * tree to obtain the object identifier from.
  922. * @see #idEqual(int, int)
  923. */
  924. public void getObjectId(MutableObjectId out, int nth) {
  925. final AbstractTreeIterator t = trees[nth];
  926. if (t.matches == currentHead)
  927. t.getEntryObjectId(out);
  928. else
  929. out.clear();
  930. }
  931. /**
  932. * Compare two tree's current ObjectId values for equality.
  933. *
  934. * @param nthA
  935. * first tree to compare the object id from.
  936. * @param nthB
  937. * second tree to compare the object id from.
  938. * @return result of
  939. * <code>getObjectId(nthA).equals(getObjectId(nthB))</code>.
  940. * @see #getObjectId(int)
  941. */
  942. public boolean idEqual(int nthA, int nthB) {
  943. final AbstractTreeIterator ch = currentHead;
  944. final AbstractTreeIterator a = trees[nthA];
  945. final AbstractTreeIterator b = trees[nthB];
  946. if (a.matches != ch && b.matches != ch) {
  947. // If neither tree matches the current path node then neither
  948. // tree has this entry. In such case the ObjectId is zero(),
  949. // and zero() is always equal to zero().
  950. //
  951. return true;
  952. }
  953. if (!a.hasId() || !b.hasId())
  954. return false;
  955. if (a.matches == ch && b.matches == ch)
  956. return a.idEqual(b);
  957. return false;
  958. }
  959. /**
  960. * Get the current entry's name within its parent tree.
  961. * <p>
  962. * This method is not very efficient and is primarily meant for debugging
  963. * and final output generation. Applications should try to avoid calling it,
  964. * and if invoked do so only once per interesting entry, where the name is
  965. * absolutely required for correct function.
  966. *
  967. * @return name of the current entry within the parent tree (or directory).
  968. * The name never includes a '/'.
  969. */
  970. public String getNameString() {
  971. final AbstractTreeIterator t = currentHead;
  972. final int off = t.pathOffset;
  973. final int end = t.pathLen;
  974. return RawParseUtils.decode(UTF_8, t.path, off, end);
  975. }
  976. /**
  977. * Get the current entry's complete path.
  978. * <p>
  979. * This method is not very efficient and is primarily meant for debugging
  980. * and final output generation. Applications should try to avoid calling it,
  981. * and if invoked do so only once per interesting entry, where the name is
  982. * absolutely required for correct function.
  983. *
  984. * @return complete path of the current entry, from the root of the
  985. * repository. If the current entry is in a subtree there will be at
  986. * least one '/' in the returned string.
  987. */
  988. public String getPathString() {
  989. return pathOf(currentHead);
  990. }
  991. /**
  992. * Get the current entry's complete path as a UTF-8 byte array.
  993. *
  994. * @return complete path of the current entry, from the root of the
  995. * repository. If the current entry is in a subtree there will be at
  996. * least one '/' in the returned string.
  997. */
  998. public byte[] getRawPath() {
  999. final AbstractTreeIterator t = currentHead;
  1000. final int n = t.pathLen;
  1001. final byte[] r = new byte[n];
  1002. System.arraycopy(t.path, 0, r, 0, n);
  1003. return r;
  1004. }
  1005. /**
  1006. * Get the path length of the current entry.
  1007. *
  1008. * @return The path length of the current entry.
  1009. */
  1010. public int getPathLength() {
  1011. return currentHead.pathLen;
  1012. }
  1013. /**
  1014. * Test if the supplied path matches the current entry's path.
  1015. * <p>
  1016. * This method detects if the supplied path is equal to, a subtree of, or
  1017. * not similar at all to the current entry. It is faster to use this
  1018. * method than to use {@link #getPathString()} to first create a String
  1019. * object, then test <code>startsWith</code> or some other type of string
  1020. * match function.
  1021. * <p>
  1022. * If the current entry is a subtree, then all paths within the subtree
  1023. * are considered to match it.
  1024. *
  1025. * @param p
  1026. * path buffer to test. Callers should ensure the path does not
  1027. * end with '/' prior to invocation.
  1028. * @param pLen
  1029. * number of bytes from <code>buf</code> to test.
  1030. * @return -1 if the current path is a parent to p; 0 if p matches the current
  1031. * path; 1 if the current path is different and will never match
  1032. * again on this tree walk.
  1033. * @since 4.7
  1034. */
  1035. public int isPathMatch(byte[] p, int pLen) {
  1036. final AbstractTreeIterator t = currentHead;
  1037. final byte[] c = t.path;
  1038. final int cLen = t.pathLen;
  1039. int ci;
  1040. for (ci = 0; ci < cLen && ci < pLen; ci++) {
  1041. final int c_value = (c[ci] & 0xff) - (p[ci] & 0xff);
  1042. if (c_value != 0) {
  1043. // Paths do not and will never match
  1044. return 1;
  1045. }
  1046. }
  1047. if (ci < cLen) {
  1048. // Ran out of pattern but we still had current data.
  1049. // If c[ci] == '/' then pattern matches the subtree.
  1050. // Otherwise it is a partial match == miss
  1051. return c[ci] == '/' ? 0 : 1;
  1052. }
  1053. if (ci < pLen) {
  1054. // Ran out of current, but we still have pattern data.
  1055. // If p[ci] == '/' then this subtree is a parent in the pattern,
  1056. // otherwise it's a miss.
  1057. return p[ci] == '/' && FileMode.TREE.equals(t.mode) ? -1 : 1;
  1058. }
  1059. // Both strings are identical.
  1060. return 0;
  1061. }
  1062. /**
  1063. * Test if the supplied path matches the current entry's path.
  1064. * <p>
  1065. * This method tests that the supplied path is exactly equal to the current
  1066. * entry or is one of its parent directories. It is faster to use this
  1067. * method then to use {@link #getPathString()} to first create a String
  1068. * object, then test <code>startsWith</code> or some other type of string
  1069. * match function.
  1070. * <p>
  1071. * If the current entry is a subtree, then all paths within the subtree
  1072. * are considered to match it.
  1073. *
  1074. * @param p
  1075. * path buffer to test. Callers should ensure the path does not
  1076. * end with '/' prior to invocation.
  1077. * @param pLen
  1078. * number of bytes from <code>buf</code> to test.
  1079. * @return &lt; 0 if p is before the current path; 0 if p matches the current
  1080. * path; 1 if the current path is past p and p will never match
  1081. * again on this tree walk.
  1082. */
  1083. public int isPathPrefix(byte[] p, int pLen) {
  1084. final AbstractTreeIterator t = currentHead;
  1085. final byte[] c = t.path;
  1086. final int cLen = t.pathLen;
  1087. int ci;
  1088. for (ci = 0; ci < cLen && ci < pLen; ci++) {
  1089. final int c_value = (c[ci] & 0xff) - (p[ci] & 0xff);
  1090. if (c_value != 0)
  1091. return c_value;
  1092. }
  1093. if (ci < cLen) {
  1094. // Ran out of pattern but we still had current data.
  1095. // If c[ci] == '/' then pattern matches the subtree.
  1096. // Otherwise we cannot be certain so we return -1.
  1097. //
  1098. return c[ci] == '/' ? 0 : -1;
  1099. }
  1100. if (ci < pLen) {
  1101. // Ran out of current, but we still have pattern data.
  1102. // If p[ci] == '/' then pattern matches this subtree,
  1103. // otherwise we cannot be certain so we return -1.
  1104. //
  1105. return p[ci] == '/' && FileMode.TREE.equals(t.mode) ? 0 : -1;
  1106. }
  1107. // Both strings are identical.
  1108. //
  1109. return 0;
  1110. }
  1111. /**
  1112. * Test if the supplied path matches (being suffix of) the current entry's
  1113. * path.
  1114. * <p>
  1115. * This method tests that the supplied path is exactly equal to the current
  1116. * entry, or is relative to one of entry's parent directories. It is faster
  1117. * to use this method then to use {@link #getPathString()} to first create
  1118. * a String object, then test <code>endsWith</code> or some other type of
  1119. * string match function.
  1120. *
  1121. * @param p
  1122. * path buffer to test.
  1123. * @param pLen
  1124. * number of bytes from <code>buf</code> to test.
  1125. * @return true if p is suffix of the current path;
  1126. * false if otherwise
  1127. */
  1128. public boolean isPathSuffix(byte[] p, int pLen) {
  1129. final AbstractTreeIterator t = currentHead;
  1130. final byte[] c = t.path;
  1131. final int cLen = t.pathLen;
  1132. for (int i = 1; i <= pLen; i++) {
  1133. // Pattern longer than current path
  1134. if (i > cLen)
  1135. return false;
  1136. // Current path doesn't match pattern
  1137. if (c[cLen - i] != p[pLen - i])
  1138. return false;
  1139. }
  1140. // Whole pattern tested -> matches
  1141. return true;
  1142. }
  1143. /**
  1144. * Get the current subtree depth of this walker.
  1145. *
  1146. * @return the current subtree depth of this walker.
  1147. */
  1148. public int getDepth() {
  1149. return depth;
  1150. }
  1151. /**
  1152. * Is the current entry a subtree?
  1153. * <p>
  1154. * This method is faster then testing the raw mode bits of all trees to see
  1155. * if any of them are a subtree. If at least one is a subtree then this
  1156. * method will return true.
  1157. *
  1158. * @return true if {@link #enterSubtree()} will work on the current node.
  1159. */
  1160. public boolean isSubtree() {
  1161. return FileMode.TREE.equals(currentHead.mode);
  1162. }
  1163. /**
  1164. * Is the current entry a subtree returned after its children?
  1165. *
  1166. * @return true if the current node is a tree that has been returned after
  1167. * its children were already processed.
  1168. * @see #isPostOrderTraversal()
  1169. */
  1170. public boolean isPostChildren() {
  1171. return postChildren && isSubtree();
  1172. }
  1173. /**
  1174. * Enter into the current subtree.
  1175. * <p>
  1176. * If the current entry is a subtree this method arranges for its children
  1177. * to be returned before the next sibling following the subtree is returned.
  1178. *
  1179. * @throws org.eclipse.jgit.errors.MissingObjectException
  1180. * a subtree was found, but the subtree object does not exist in
  1181. * this repository. The repository may be missing objects.
  1182. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  1183. * a subtree was found, and the subtree id does not denote a
  1184. * tree, but instead names some other non-tree type of object.
  1185. * The repository may have data corruption.
  1186. * @throws org.eclipse.jgit.errors.CorruptObjectException
  1187. * the contents of a tree did not appear to be a tree. The
  1188. * repository may have data corruption.
  1189. * @throws java.io.IOException
  1190. * a loose object or pack file could not be read.
  1191. */
  1192. public void enterSubtree() throws MissingObjectException,
  1193. IncorrectObjectTypeException, CorruptObjectException, IOException {
  1194. attrs = null;
  1195. final AbstractTreeIterator ch = currentHead;
  1196. final AbstractTreeIterator[] tmp = new AbstractTreeIterator[trees.length];
  1197. for (int i = 0; i < trees.length; i++) {
  1198. final AbstractTreeIterator t = trees[i];
  1199. final AbstractTreeIterator n;
  1200. // If we find a GITLINK when attempting to enter a subtree, then the
  1201. // GITLINK must exist as a TREE in the index, meaning the working tree
  1202. // entry should be treated as a TREE
  1203. if (t.matches == ch && !t.eof() &&
  1204. (FileMode.TREE.equals(t.mode)
  1205. || (FileMode.GITLINK.equals(t.mode) && t.isWorkTree())))
  1206. n = t.createSubtreeIterator(reader, idBuffer);
  1207. else
  1208. n = t.createEmptyTreeIterator();
  1209. tmp[i] = n;
  1210. }
  1211. depth++;
  1212. advance = false;
  1213. System.arraycopy(tmp, 0, trees, 0, trees.length);
  1214. }
  1215. @SuppressWarnings("unused")
  1216. AbstractTreeIterator min() throws CorruptObjectException {
  1217. int i = 0;
  1218. AbstractTreeIterator minRef = trees[i];
  1219. while (minRef.eof() && ++i < trees.length)
  1220. minRef = trees[i];
  1221. if (minRef.eof())
  1222. return minRef;
  1223. minRef.matches = minRef;
  1224. while (++i < trees.length) {
  1225. final AbstractTreeIterator t = trees[i];
  1226. if (t.eof())
  1227. continue;
  1228. final int cmp = t.pathCompare(minRef);
  1229. if (cmp < 0) {
  1230. t.matches = t;
  1231. minRef = t;
  1232. } else if (cmp == 0) {
  1233. t.matches = minRef;
  1234. }
  1235. }
  1236. return minRef;
  1237. }
  1238. void popEntriesEqual() throws CorruptObjectException {
  1239. final AbstractTreeIterator ch = currentHead;
  1240. for (int i = 0; i < trees.length; i++) {
  1241. final AbstractTreeIterator t = trees[i];
  1242. if (t.matches == ch) {
  1243. t.next(1);
  1244. t.matches = null;
  1245. }
  1246. }
  1247. }
  1248. void skipEntriesEqual() throws CorruptObjectException {
  1249. final AbstractTreeIterator ch = currentHead;
  1250. for (int i = 0; i < trees.length; i++) {
  1251. final AbstractTreeIterator t = trees[i];
  1252. if (t.matches == ch) {
  1253. t.skip();
  1254. t.matches = null;
  1255. }
  1256. }
  1257. }
  1258. void exitSubtree() {
  1259. depth--;
  1260. for (int i = 0; i < trees.length; i++)
  1261. trees[i] = trees[i].parent;
  1262. AbstractTreeIterator minRef = null;
  1263. for (AbstractTreeIterator t : trees) {
  1264. if (t.matches != t)
  1265. continue;
  1266. if (minRef == null || t.pathCompare(minRef) < 0)
  1267. minRef = t;
  1268. }
  1269. currentHead = minRef;
  1270. }
  1271. private CanonicalTreeParser parserFor(AnyObjectId id)
  1272. throws IncorrectObjectTypeException, IOException {
  1273. final CanonicalTreeParser p = new CanonicalTreeParser();
  1274. p.reset(reader, id);
  1275. return p;
  1276. }
  1277. static String pathOf(AbstractTreeIterator t) {
  1278. return RawParseUtils.decode(UTF_8, t.path, 0, t.pathLen);
  1279. }
  1280. static String pathOf(byte[] buf, int pos, int end) {
  1281. return RawParseUtils.decode(UTF_8, buf, pos, end);
  1282. }
  1283. /**
  1284. * Get the tree of that type.
  1285. *
  1286. * @param type
  1287. * of the tree to be queried
  1288. * @return the tree of that type or null if none is present.
  1289. * @since 4.3
  1290. * @param <T>
  1291. * a tree type.
  1292. */
  1293. public <T extends AbstractTreeIterator> T getTree(
  1294. Class<T> type) {
  1295. for (int i = 0; i < trees.length; i++) {
  1296. AbstractTreeIterator tree = trees[i];
  1297. if (type.isInstance(tree)) {
  1298. return type.cast(tree);
  1299. }
  1300. }
  1301. return null;
  1302. }
  1303. /**
  1304. * Inspect config and attributes to return a filtercommand applicable for
  1305. * the current path, but without expanding %f occurences
  1306. *
  1307. * @param filterCommandType
  1308. * which type of filterCommand should be executed. E.g. "clean",
  1309. * "smudge"
  1310. * @return a filter command
  1311. * @throws java.io.IOException
  1312. * @since 4.2
  1313. */
  1314. public String getFilterCommand(String filterCommandType)
  1315. throws IOException {
  1316. Attributes attributes = getAttributes();
  1317. Attribute f = attributes.get(Constants.ATTR_FILTER);
  1318. if (f == null) {
  1319. return null;
  1320. }
  1321. String filterValue = f.getValue();
  1322. if (filterValue == null) {
  1323. return null;
  1324. }
  1325. String filterCommand = getFilterCommandDefinition(filterValue,
  1326. filterCommandType);
  1327. if (filterCommand == null) {
  1328. return null;
  1329. }
  1330. return filterCommand.replaceAll("%f", //$NON-NLS-1$
  1331. QuotedString.BOURNE.quote((getPathString())));
  1332. }
  1333. /**
  1334. * Get the filter command how it is defined in gitconfig. The returned
  1335. * string may contain "%f" which needs to be replaced by the current path
  1336. * before executing the filter command. These filter definitions are cached
  1337. * for better performance.
  1338. *
  1339. * @param filterDriverName
  1340. * The name of the filter driver as it is referenced in the
  1341. * gitattributes file. E.g. "lfs". For each filter driver there
  1342. * may be many commands defined in the .gitconfig
  1343. * @param filterCommandType
  1344. * The type of the filter command for a specific filter driver.
  1345. * May be "clean" or "smudge".
  1346. * @return the definition of the command to be executed for this filter
  1347. * driver and filter command
  1348. */
  1349. private String getFilterCommandDefinition(String filterDriverName,
  1350. String filterCommandType) {
  1351. String key = filterDriverName + "." + filterCommandType; //$NON-NLS-1$
  1352. String filterCommand = filterCommandsByNameDotType.get(key);
  1353. if (filterCommand != null)
  1354. return filterCommand;
  1355. filterCommand = config.getString(ConfigConstants.CONFIG_FILTER_SECTION,
  1356. filterDriverName, filterCommandType);
  1357. boolean useBuiltin = config.getBoolean(
  1358. ConfigConstants.CONFIG_FILTER_SECTION,
  1359. filterDriverName, ConfigConstants.CONFIG_KEY_USEJGITBUILTIN, false);
  1360. if (useBuiltin) {
  1361. String builtinFilterCommand = Constants.BUILTIN_FILTER_PREFIX
  1362. + filterDriverName + '/' + filterCommandType;
  1363. if (filterCommands != null
  1364. && filterCommands.contains(builtinFilterCommand)) {
  1365. filterCommand = builtinFilterCommand;
  1366. }
  1367. }
  1368. if (filterCommand != null) {
  1369. filterCommandsByNameDotType.put(key, filterCommand);
  1370. }
  1371. return filterCommand;
  1372. }
  1373. }