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.

WorkingTreeIterator.java 48KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  4. * Copyright (C) 2010, Matthias Sohn <matthias.sohn@sap.com>
  5. * Copyright (C) 2012-2013, Robin Rosenberg
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.treewalk;
  47. import static java.nio.charset.StandardCharsets.UTF_8;
  48. import java.io.ByteArrayInputStream;
  49. import java.io.File;
  50. import java.io.FileInputStream;
  51. import java.io.FileNotFoundException;
  52. import java.io.IOException;
  53. import java.io.InputStream;
  54. import java.nio.ByteBuffer;
  55. import java.nio.CharBuffer;
  56. import java.nio.charset.CharacterCodingException;
  57. import java.nio.charset.CharsetEncoder;
  58. import java.text.MessageFormat;
  59. import java.time.Instant;
  60. import java.util.Arrays;
  61. import java.util.Collections;
  62. import java.util.Comparator;
  63. import java.util.HashMap;
  64. import java.util.Map;
  65. import org.eclipse.jgit.api.errors.FilterFailedException;
  66. import org.eclipse.jgit.attributes.AttributesNode;
  67. import org.eclipse.jgit.attributes.AttributesRule;
  68. import org.eclipse.jgit.attributes.FilterCommand;
  69. import org.eclipse.jgit.attributes.FilterCommandRegistry;
  70. import org.eclipse.jgit.diff.RawText;
  71. import org.eclipse.jgit.dircache.DirCacheEntry;
  72. import org.eclipse.jgit.dircache.DirCacheIterator;
  73. import org.eclipse.jgit.errors.CorruptObjectException;
  74. import org.eclipse.jgit.errors.MissingObjectException;
  75. import org.eclipse.jgit.errors.NoWorkTreeException;
  76. import org.eclipse.jgit.ignore.FastIgnoreRule;
  77. import org.eclipse.jgit.ignore.IgnoreNode;
  78. import org.eclipse.jgit.internal.JGitText;
  79. import org.eclipse.jgit.lib.Constants;
  80. import org.eclipse.jgit.lib.CoreConfig;
  81. import org.eclipse.jgit.lib.CoreConfig.CheckStat;
  82. import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
  83. import org.eclipse.jgit.lib.CoreConfig.SymLinks;
  84. import org.eclipse.jgit.lib.FileMode;
  85. import org.eclipse.jgit.lib.ObjectId;
  86. import org.eclipse.jgit.lib.ObjectLoader;
  87. import org.eclipse.jgit.lib.ObjectReader;
  88. import org.eclipse.jgit.lib.Repository;
  89. import org.eclipse.jgit.submodule.SubmoduleWalk;
  90. import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
  91. import org.eclipse.jgit.util.FS;
  92. import org.eclipse.jgit.util.FS.ExecutionResult;
  93. import org.eclipse.jgit.util.Holder;
  94. import org.eclipse.jgit.util.IO;
  95. import org.eclipse.jgit.util.Paths;
  96. import org.eclipse.jgit.util.RawParseUtils;
  97. import org.eclipse.jgit.util.TemporaryBuffer;
  98. import org.eclipse.jgit.util.TemporaryBuffer.LocalFile;
  99. import org.eclipse.jgit.util.io.AutoLFInputStream;
  100. import org.eclipse.jgit.util.io.EolStreamTypeUtil;
  101. import org.eclipse.jgit.util.sha1.SHA1;
  102. /**
  103. * Walks a working directory tree as part of a
  104. * {@link org.eclipse.jgit.treewalk.TreeWalk}.
  105. * <p>
  106. * Most applications will want to use the standard implementation of this
  107. * iterator, {@link org.eclipse.jgit.treewalk.FileTreeIterator}, as that does
  108. * all IO through the standard <code>java.io</code> package. Plugins for a Java
  109. * based IDE may however wish to create their own implementations of this class
  110. * to allow traversal of the IDE's project space, as well as benefit from any
  111. * caching the IDE may have.
  112. *
  113. * @see FileTreeIterator
  114. */
  115. public abstract class WorkingTreeIterator extends AbstractTreeIterator {
  116. private static final int MAX_EXCEPTION_TEXT_SIZE = 10 * 1024;
  117. /** An empty entry array, suitable for {@link #init(Entry[])}. */
  118. protected static final Entry[] EOF = {};
  119. /** Size we perform file IO in if we have to read and hash a file. */
  120. static final int BUFFER_SIZE = 2048;
  121. /**
  122. * Maximum size of files which may be read fully into memory for performance
  123. * reasons.
  124. */
  125. private static final long MAXIMUM_FILE_SIZE_TO_READ_FULLY = 65536;
  126. /** Inherited state of this iterator, describing working tree, etc. */
  127. private final IteratorState state;
  128. /** The {@link #idBuffer()} for the current entry. */
  129. private byte[] contentId;
  130. /** Index within {@link #entries} that {@link #contentId} came from. */
  131. private int contentIdFromPtr;
  132. /** List of entries obtained from the subclass. */
  133. private Entry[] entries;
  134. /** Total number of entries in {@link #entries} that are valid. */
  135. private int entryCnt;
  136. /** Current position within {@link #entries}. */
  137. private int ptr;
  138. /** If there is a .gitignore file present, the parsed rules from it. */
  139. private IgnoreNode ignoreNode;
  140. /**
  141. * cached clean filter command. Use a Ref in order to distinguish between
  142. * the ref not cached yet and the value null
  143. */
  144. private Holder<String> cleanFilterCommandHolder;
  145. /**
  146. * cached eol stream type. Use a Ref in order to distinguish between the ref
  147. * not cached yet and the value null
  148. */
  149. private Holder<EolStreamType> eolStreamTypeHolder;
  150. /** Repository that is the root level being iterated over */
  151. protected Repository repository;
  152. /** Cached canonical length, initialized from {@link #idBuffer()} */
  153. private long canonLen = -1;
  154. /** The offset of the content id in {@link #idBuffer()} */
  155. private int contentIdOffset;
  156. /** A comparator for {@link Instant}s. */
  157. private final InstantComparator timestampComparator = new InstantComparator();
  158. /**
  159. * Create a new iterator with no parent.
  160. *
  161. * @param options
  162. * working tree options to be used
  163. */
  164. protected WorkingTreeIterator(WorkingTreeOptions options) {
  165. super();
  166. state = new IteratorState(options);
  167. }
  168. /**
  169. * Create a new iterator with no parent and a prefix.
  170. * <p>
  171. * The prefix path supplied is inserted in front of all paths generated by
  172. * this iterator. It is intended to be used when an iterator is being
  173. * created for a subsection of an overall repository and needs to be
  174. * combined with other iterators that are created to run over the entire
  175. * repository namespace.
  176. *
  177. * @param prefix
  178. * position of this iterator in the repository tree. The value
  179. * may be null or the empty string to indicate the prefix is the
  180. * root of the repository. A trailing slash ('/') is
  181. * automatically appended if the prefix does not end in '/'.
  182. * @param options
  183. * working tree options to be used
  184. */
  185. protected WorkingTreeIterator(final String prefix,
  186. WorkingTreeOptions options) {
  187. super(prefix);
  188. state = new IteratorState(options);
  189. }
  190. /**
  191. * Create an iterator for a subtree of an existing iterator.
  192. *
  193. * @param p
  194. * parent tree iterator.
  195. */
  196. protected WorkingTreeIterator(WorkingTreeIterator p) {
  197. super(p);
  198. state = p.state;
  199. repository = p.repository;
  200. }
  201. /**
  202. * Initialize this iterator for the root level of a repository.
  203. * <p>
  204. * This method should only be invoked after calling {@link #init(Entry[])},
  205. * and only for the root iterator.
  206. *
  207. * @param repo
  208. * the repository.
  209. */
  210. protected void initRootIterator(Repository repo) {
  211. repository = repo;
  212. Entry entry;
  213. if (ignoreNode instanceof PerDirectoryIgnoreNode)
  214. entry = ((PerDirectoryIgnoreNode) ignoreNode).entry;
  215. else
  216. entry = null;
  217. ignoreNode = new RootIgnoreNode(entry, repo);
  218. }
  219. /**
  220. * Define the matching {@link org.eclipse.jgit.dircache.DirCacheIterator},
  221. * to optimize ObjectIds.
  222. *
  223. * Once the DirCacheIterator has been set this iterator must only be
  224. * advanced by the TreeWalk that is supplied, as it assumes that itself and
  225. * the corresponding DirCacheIterator are positioned on the same file path
  226. * whenever {@link #idBuffer()} is invoked.
  227. *
  228. * @param walk
  229. * the walk that will be advancing this iterator.
  230. * @param treeId
  231. * index of the matching
  232. * {@link org.eclipse.jgit.dircache.DirCacheIterator}.
  233. */
  234. public void setDirCacheIterator(TreeWalk walk, int treeId) {
  235. state.walk = walk;
  236. state.dirCacheTree = treeId;
  237. }
  238. /**
  239. * Retrieves the {@link DirCacheIterator} at the current entry if
  240. * {@link #setDirCacheIterator(TreeWalk, int)} was called.
  241. *
  242. * @return the DirCacheIterator, or {@code null} if not set or not at the
  243. * current entry
  244. * @since 5.0
  245. */
  246. protected DirCacheIterator getDirCacheIterator() {
  247. if (state.dirCacheTree >= 0 && state.walk != null) {
  248. return state.walk.getTree(state.dirCacheTree,
  249. DirCacheIterator.class);
  250. }
  251. return null;
  252. }
  253. /**
  254. * Defines whether this {@link WorkingTreeIterator} walks ignored
  255. * directories.
  256. *
  257. * @param includeIgnored
  258. * {@code false} to skip ignored directories, if possible;
  259. * {@code true} to always include them in the walk
  260. * @since 5.0
  261. */
  262. public void setWalkIgnoredDirectories(boolean includeIgnored) {
  263. state.walkIgnored = includeIgnored;
  264. }
  265. /**
  266. * Tells whether this {@link WorkingTreeIterator} walks ignored directories.
  267. *
  268. * @return {@code true} if it does, {@code false} otherwise
  269. * @since 5.0
  270. */
  271. public boolean walksIgnoredDirectories() {
  272. return state.walkIgnored;
  273. }
  274. /** {@inheritDoc} */
  275. @Override
  276. public boolean hasId() {
  277. if (contentIdFromPtr == ptr)
  278. return true;
  279. return (mode & FileMode.TYPE_MASK) == FileMode.TYPE_FILE;
  280. }
  281. /** {@inheritDoc} */
  282. @Override
  283. public byte[] idBuffer() {
  284. if (contentIdFromPtr == ptr)
  285. return contentId;
  286. if (state.walk != null) {
  287. // If there is a matching DirCacheIterator, we can reuse
  288. // its idBuffer, but only if we appear to be clean against
  289. // the cached index information for the path.
  290. DirCacheIterator i = state.walk.getTree(state.dirCacheTree,
  291. DirCacheIterator.class);
  292. if (i != null) {
  293. DirCacheEntry ent = i.getDirCacheEntry();
  294. if (ent != null && compareMetadata(ent) == MetadataDiff.EQUAL
  295. && ((ent.getFileMode().getBits()
  296. & FileMode.TYPE_MASK) != FileMode.TYPE_GITLINK)) {
  297. contentIdOffset = i.idOffset();
  298. contentIdFromPtr = ptr;
  299. return contentId = i.idBuffer();
  300. }
  301. contentIdOffset = 0;
  302. } else {
  303. contentIdOffset = 0;
  304. }
  305. }
  306. switch (mode & FileMode.TYPE_MASK) {
  307. case FileMode.TYPE_SYMLINK:
  308. case FileMode.TYPE_FILE:
  309. contentIdFromPtr = ptr;
  310. return contentId = idBufferBlob(entries[ptr]);
  311. case FileMode.TYPE_GITLINK:
  312. contentIdFromPtr = ptr;
  313. return contentId = idSubmodule(entries[ptr]);
  314. }
  315. return zeroid;
  316. }
  317. /** {@inheritDoc} */
  318. @Override
  319. public boolean isWorkTree() {
  320. return true;
  321. }
  322. /**
  323. * Get submodule id for given entry.
  324. *
  325. * @param e
  326. * a {@link org.eclipse.jgit.treewalk.WorkingTreeIterator.Entry}
  327. * object.
  328. * @return non-null submodule id
  329. */
  330. protected byte[] idSubmodule(Entry e) {
  331. if (repository == null)
  332. return zeroid;
  333. File directory;
  334. try {
  335. directory = repository.getWorkTree();
  336. } catch (NoWorkTreeException nwte) {
  337. return zeroid;
  338. }
  339. return idSubmodule(directory, e);
  340. }
  341. /**
  342. * Get submodule id using the repository at the location of the entry
  343. * relative to the directory.
  344. *
  345. * @param directory
  346. * a {@link java.io.File} object.
  347. * @param e
  348. * a {@link org.eclipse.jgit.treewalk.WorkingTreeIterator.Entry}
  349. * object.
  350. * @return non-null submodule id
  351. */
  352. protected byte[] idSubmodule(File directory, Entry e) {
  353. try (Repository submoduleRepo = SubmoduleWalk.getSubmoduleRepository(
  354. directory, e.getName(),
  355. repository != null ? repository.getFS() : FS.DETECTED)) {
  356. if (submoduleRepo == null) {
  357. return zeroid;
  358. }
  359. ObjectId head = submoduleRepo.resolve(Constants.HEAD);
  360. if (head == null) {
  361. return zeroid;
  362. }
  363. byte[] id = new byte[Constants.OBJECT_ID_LENGTH];
  364. head.copyRawTo(id, 0);
  365. return id;
  366. } catch (IOException exception) {
  367. return zeroid;
  368. }
  369. }
  370. private static final byte[] digits = { '0', '1', '2', '3', '4', '5', '6',
  371. '7', '8', '9' };
  372. private static final byte[] hblob = Constants
  373. .encodedTypeString(Constants.OBJ_BLOB);
  374. private byte[] idBufferBlob(Entry e) {
  375. try {
  376. final InputStream is = e.openInputStream();
  377. if (is == null)
  378. return zeroid;
  379. try {
  380. state.initializeReadBuffer();
  381. final long len = e.getLength();
  382. InputStream filteredIs = possiblyFilteredInputStream(e, is, len,
  383. OperationType.CHECKIN_OP);
  384. return computeHash(filteredIs, canonLen);
  385. } finally {
  386. safeClose(is);
  387. }
  388. } catch (IOException err) {
  389. // Can't read the file? Don't report the failure either.
  390. return zeroid;
  391. }
  392. }
  393. private InputStream possiblyFilteredInputStream(final Entry e,
  394. final InputStream is, final long len) throws IOException {
  395. return possiblyFilteredInputStream(e, is, len, null);
  396. }
  397. private InputStream possiblyFilteredInputStream(final Entry e,
  398. final InputStream is, final long len, OperationType opType)
  399. throws IOException {
  400. if (getCleanFilterCommand() == null
  401. && getEolStreamType(opType) == EolStreamType.DIRECT) {
  402. canonLen = len;
  403. return is;
  404. }
  405. if (len <= MAXIMUM_FILE_SIZE_TO_READ_FULLY) {
  406. ByteBuffer rawbuf = IO.readWholeStream(is, (int) len);
  407. rawbuf = filterClean(rawbuf.array(), rawbuf.limit(), opType);
  408. canonLen = rawbuf.limit();
  409. return new ByteArrayInputStream(rawbuf.array(), 0, (int) canonLen);
  410. }
  411. if (getCleanFilterCommand() == null && isBinary(e)) {
  412. canonLen = len;
  413. return is;
  414. }
  415. final InputStream lenIs = filterClean(e.openInputStream(),
  416. opType);
  417. try {
  418. canonLen = computeLength(lenIs);
  419. } finally {
  420. safeClose(lenIs);
  421. }
  422. return filterClean(is, opType);
  423. }
  424. private static void safeClose(InputStream in) {
  425. try {
  426. in.close();
  427. } catch (IOException err2) {
  428. // Suppress any error related to closing an input
  429. // stream. We don't care, we should not have any
  430. // outstanding data to flush or anything like that.
  431. }
  432. }
  433. private static boolean isBinary(Entry entry) throws IOException {
  434. InputStream in = entry.openInputStream();
  435. try {
  436. return RawText.isBinary(in);
  437. } finally {
  438. safeClose(in);
  439. }
  440. }
  441. private ByteBuffer filterClean(byte[] src, int n, OperationType opType)
  442. throws IOException {
  443. InputStream in = new ByteArrayInputStream(src);
  444. try {
  445. return IO.readWholeStream(filterClean(in, opType), n);
  446. } finally {
  447. safeClose(in);
  448. }
  449. }
  450. private InputStream filterClean(InputStream in) throws IOException {
  451. return filterClean(in, null);
  452. }
  453. private InputStream filterClean(InputStream in, OperationType opType)
  454. throws IOException {
  455. in = handleAutoCRLF(in, opType);
  456. String filterCommand = getCleanFilterCommand();
  457. if (filterCommand != null) {
  458. if (FilterCommandRegistry.isRegistered(filterCommand)) {
  459. LocalFile buffer = new TemporaryBuffer.LocalFile(null);
  460. FilterCommand command = FilterCommandRegistry
  461. .createFilterCommand(filterCommand, repository, in,
  462. buffer);
  463. while (command.run() != -1) {
  464. // loop as long as command.run() tells there is work to do
  465. }
  466. return buffer.openInputStreamWithAutoDestroy();
  467. }
  468. FS fs = repository.getFS();
  469. ProcessBuilder filterProcessBuilder = fs.runInShell(filterCommand,
  470. new String[0]);
  471. filterProcessBuilder.directory(repository.getWorkTree());
  472. filterProcessBuilder.environment().put(Constants.GIT_DIR_KEY,
  473. repository.getDirectory().getAbsolutePath());
  474. ExecutionResult result;
  475. try {
  476. result = fs.execute(filterProcessBuilder, in);
  477. } catch (IOException | InterruptedException e) {
  478. throw new IOException(new FilterFailedException(e,
  479. filterCommand, getEntryPathString()));
  480. }
  481. int rc = result.getRc();
  482. if (rc != 0) {
  483. throw new IOException(new FilterFailedException(rc,
  484. filterCommand, getEntryPathString(),
  485. result.getStdout().toByteArray(MAX_EXCEPTION_TEXT_SIZE),
  486. RawParseUtils.decode(result.getStderr()
  487. .toByteArray(MAX_EXCEPTION_TEXT_SIZE))));
  488. }
  489. return result.getStdout().openInputStreamWithAutoDestroy();
  490. }
  491. return in;
  492. }
  493. private InputStream handleAutoCRLF(InputStream in, OperationType opType)
  494. throws IOException {
  495. return EolStreamTypeUtil.wrapInputStream(in, getEolStreamType(opType));
  496. }
  497. /**
  498. * Returns the working tree options used by this iterator.
  499. *
  500. * @return working tree options
  501. */
  502. public WorkingTreeOptions getOptions() {
  503. return state.options;
  504. }
  505. /** {@inheritDoc} */
  506. @Override
  507. public int idOffset() {
  508. return contentIdOffset;
  509. }
  510. /** {@inheritDoc} */
  511. @Override
  512. public void reset() {
  513. if (!first()) {
  514. ptr = 0;
  515. if (!eof())
  516. parseEntry();
  517. }
  518. }
  519. /** {@inheritDoc} */
  520. @Override
  521. public boolean first() {
  522. return ptr == 0;
  523. }
  524. /** {@inheritDoc} */
  525. @Override
  526. public boolean eof() {
  527. return ptr == entryCnt;
  528. }
  529. /** {@inheritDoc} */
  530. @Override
  531. public void next(int delta) throws CorruptObjectException {
  532. ptr += delta;
  533. if (!eof()) {
  534. parseEntry();
  535. }
  536. }
  537. /** {@inheritDoc} */
  538. @Override
  539. public void back(int delta) throws CorruptObjectException {
  540. ptr -= delta;
  541. parseEntry();
  542. }
  543. private void parseEntry() {
  544. final Entry e = entries[ptr];
  545. mode = e.getMode().getBits();
  546. final int nameLen = e.encodedNameLen;
  547. ensurePathCapacity(pathOffset + nameLen, pathOffset);
  548. System.arraycopy(e.encodedName, 0, path, pathOffset, nameLen);
  549. pathLen = pathOffset + nameLen;
  550. canonLen = -1;
  551. cleanFilterCommandHolder = null;
  552. eolStreamTypeHolder = null;
  553. }
  554. /**
  555. * Get the raw byte length of this entry.
  556. *
  557. * @return size of this file, in bytes.
  558. */
  559. public long getEntryLength() {
  560. return current().getLength();
  561. }
  562. /**
  563. * Get the filtered input length of this entry
  564. *
  565. * @return size of the content, in bytes
  566. * @throws java.io.IOException
  567. */
  568. public long getEntryContentLength() throws IOException {
  569. if (canonLen == -1) {
  570. long rawLen = getEntryLength();
  571. if (rawLen == 0)
  572. canonLen = 0;
  573. InputStream is = current().openInputStream();
  574. try {
  575. // canonLen gets updated here
  576. possiblyFilteredInputStream(current(), is, current()
  577. .getLength());
  578. } finally {
  579. safeClose(is);
  580. }
  581. }
  582. return canonLen;
  583. }
  584. /**
  585. * Get the last modified time of this entry.
  586. *
  587. * @return last modified time of this file, in milliseconds since the epoch
  588. * (Jan 1, 1970 UTC).
  589. * @deprecated use {@link #getEntryLastModifiedInstant()} instead
  590. */
  591. @Deprecated
  592. public long getEntryLastModified() {
  593. return current().getLastModified();
  594. }
  595. /**
  596. * Get the last modified time of this entry.
  597. *
  598. * @return last modified time of this file
  599. * @since 5.1.9
  600. */
  601. public Instant getEntryLastModifiedInstant() {
  602. return current().getLastModifiedInstant();
  603. }
  604. /**
  605. * Obtain an input stream to read the file content.
  606. * <p>
  607. * Efficient implementations are not required. The caller will usually
  608. * obtain the stream only once per entry, if at all.
  609. * <p>
  610. * The input stream should not use buffering if the implementation can avoid
  611. * it. The caller will buffer as necessary to perform efficient block IO
  612. * operations.
  613. * <p>
  614. * The caller will close the stream once complete.
  615. *
  616. * @return a stream to read from the file.
  617. * @throws java.io.IOException
  618. * the file could not be opened for reading.
  619. */
  620. public InputStream openEntryStream() throws IOException {
  621. InputStream rawis = current().openInputStream();
  622. if (getCleanFilterCommand() == null
  623. && getEolStreamType() == EolStreamType.DIRECT)
  624. return rawis;
  625. else
  626. return filterClean(rawis);
  627. }
  628. /**
  629. * Determine if the current entry path is ignored by an ignore rule.
  630. *
  631. * @return true if the entry was ignored by an ignore rule file.
  632. * @throws java.io.IOException
  633. * a relevant ignore rule file exists but cannot be read.
  634. */
  635. public boolean isEntryIgnored() throws IOException {
  636. return isEntryIgnored(pathLen);
  637. }
  638. /**
  639. * Determine if the entry path is ignored by an ignore rule.
  640. *
  641. * @param pLen
  642. * the length of the path in the path buffer.
  643. * @return true if the entry is ignored by an ignore rule.
  644. * @throws java.io.IOException
  645. * a relevant ignore rule file exists but cannot be read.
  646. */
  647. protected boolean isEntryIgnored(int pLen) throws IOException {
  648. return isEntryIgnored(pLen, mode);
  649. }
  650. /**
  651. * Determine if the entry path is ignored by an ignore rule.
  652. *
  653. * @param pLen
  654. * the length of the path in the path buffer.
  655. * @param fileMode
  656. * the original iterator file mode
  657. * @return true if the entry is ignored by an ignore rule.
  658. * @throws IOException
  659. * a relevant ignore rule file exists but cannot be read.
  660. */
  661. private boolean isEntryIgnored(int pLen, int fileMode)
  662. throws IOException {
  663. // The ignore code wants path to start with a '/' if possible.
  664. // If we have the '/' in our path buffer because we are inside
  665. // a sub-directory include it in the range we convert to string.
  666. //
  667. final int pOff = 0 < pathOffset ? pathOffset - 1 : pathOffset;
  668. String pathRel = TreeWalk.pathOf(this.path, pOff, pLen);
  669. String parentRel = getParentPath(pathRel);
  670. // CGit is processing .gitignore files by starting at the root of the
  671. // repository and then recursing into subdirectories. With this
  672. // approach, top-level ignored directories will be processed first which
  673. // allows to skip entire subtrees and further .gitignore-file processing
  674. // within these subtrees.
  675. //
  676. // We will follow the same approach by marking directories as "ignored"
  677. // here. This allows to have a simplified FastIgnore.checkIgnore()
  678. // implementation (both in terms of code and computational complexity):
  679. //
  680. // Without the "ignored" flag, we would have to apply the ignore-check
  681. // to a path and all of its parents always(!), to determine whether a
  682. // path is ignored directly or by one of its parent directories; with
  683. // the "ignored" flag, we know at this point that the parent directory
  684. // is definitely not ignored, thus the path can only become ignored if
  685. // there is a rule matching the path itself.
  686. if (isDirectoryIgnored(parentRel)) {
  687. return true;
  688. }
  689. IgnoreNode rules = getIgnoreNode();
  690. final Boolean ignored = rules != null
  691. ? rules.checkIgnored(pathRel, FileMode.TREE.equals(fileMode))
  692. : null;
  693. if (ignored != null) {
  694. return ignored.booleanValue();
  695. }
  696. return parent instanceof WorkingTreeIterator
  697. && ((WorkingTreeIterator) parent).isEntryIgnored(pLen,
  698. fileMode);
  699. }
  700. private IgnoreNode getIgnoreNode() throws IOException {
  701. if (ignoreNode instanceof PerDirectoryIgnoreNode)
  702. ignoreNode = ((PerDirectoryIgnoreNode) ignoreNode).load();
  703. return ignoreNode;
  704. }
  705. /**
  706. * Retrieves the {@link org.eclipse.jgit.attributes.AttributesNode} for the
  707. * current entry.
  708. *
  709. * @return the {@link org.eclipse.jgit.attributes.AttributesNode} for the
  710. * current entry.
  711. * @throws IOException
  712. */
  713. public AttributesNode getEntryAttributesNode() throws IOException {
  714. if (attributesNode instanceof PerDirectoryAttributesNode)
  715. attributesNode = ((PerDirectoryAttributesNode) attributesNode)
  716. .load();
  717. return attributesNode;
  718. }
  719. private static final Comparator<Entry> ENTRY_CMP = new Comparator<Entry>() {
  720. @Override
  721. public int compare(Entry a, Entry b) {
  722. return Paths.compare(
  723. a.encodedName, 0, a.encodedNameLen, a.getMode().getBits(),
  724. b.encodedName, 0, b.encodedNameLen, b.getMode().getBits());
  725. }
  726. };
  727. /**
  728. * Constructor helper.
  729. *
  730. * @param list
  731. * files in the subtree of the work tree this iterator operates
  732. * on
  733. */
  734. protected void init(Entry[] list) {
  735. // Filter out nulls, . and .. as these are not valid tree entries,
  736. // also cache the encoded forms of the path names for efficient use
  737. // later on during sorting and iteration.
  738. //
  739. entries = list;
  740. int i, o;
  741. final CharsetEncoder nameEncoder = state.nameEncoder;
  742. for (i = 0, o = 0; i < entries.length; i++) {
  743. final Entry e = entries[i];
  744. if (e == null)
  745. continue;
  746. final String name = e.getName();
  747. if (".".equals(name) || "..".equals(name)) //$NON-NLS-1$ //$NON-NLS-2$
  748. continue;
  749. if (Constants.DOT_GIT.equals(name))
  750. continue;
  751. if (Constants.DOT_GIT_IGNORE.equals(name))
  752. ignoreNode = new PerDirectoryIgnoreNode(e);
  753. if (Constants.DOT_GIT_ATTRIBUTES.equals(name))
  754. attributesNode = new PerDirectoryAttributesNode(e);
  755. if (i != o)
  756. entries[o] = e;
  757. e.encodeName(nameEncoder);
  758. o++;
  759. }
  760. entryCnt = o;
  761. Arrays.sort(entries, 0, entryCnt, ENTRY_CMP);
  762. contentIdFromPtr = -1;
  763. ptr = 0;
  764. if (!eof())
  765. parseEntry();
  766. else if (pathLen == 0) // see bug 445363
  767. pathLen = pathOffset;
  768. }
  769. /**
  770. * Obtain the current entry from this iterator.
  771. *
  772. * @return the currently selected entry.
  773. */
  774. protected Entry current() {
  775. return entries[ptr];
  776. }
  777. /**
  778. * The result of a metadata-comparison between the current entry and a
  779. * {@link DirCacheEntry}
  780. */
  781. public enum MetadataDiff {
  782. /**
  783. * The entries are equal by metaData (mode, length,
  784. * modification-timestamp) or the <code>assumeValid</code> attribute of
  785. * the index entry is set
  786. */
  787. EQUAL,
  788. /**
  789. * The entries are not equal by metaData (mode, length) or the
  790. * <code>isUpdateNeeded</code> attribute of the index entry is set
  791. */
  792. DIFFER_BY_METADATA,
  793. /** index entry is smudged - can't use that entry for comparison */
  794. SMUDGED,
  795. /**
  796. * The entries are equal by metaData (mode, length) but differ by
  797. * modification-timestamp.
  798. */
  799. DIFFER_BY_TIMESTAMP
  800. }
  801. /**
  802. * Is the file mode of the current entry different than the given raw mode?
  803. *
  804. * @param rawMode
  805. * an int.
  806. * @return true if different, false otherwise
  807. */
  808. public boolean isModeDifferent(int rawMode) {
  809. // Determine difference in mode-bits of file and index-entry. In the
  810. // bitwise presentation of modeDiff we'll have a '1' when the two modes
  811. // differ at this position.
  812. int modeDiff = getEntryRawMode() ^ rawMode;
  813. if (modeDiff == 0)
  814. return false;
  815. // Do not rely on filemode differences in case of symbolic links
  816. if (getOptions().getSymLinks() == SymLinks.FALSE)
  817. if (FileMode.SYMLINK.equals(rawMode))
  818. return false;
  819. // Ignore the executable file bits if WorkingTreeOptions tell me to
  820. // do so. Ignoring is done by setting the bits representing a
  821. // EXECUTABLE_FILE to '0' in modeDiff
  822. if (!state.options.isFileMode())
  823. modeDiff &= ~FileMode.EXECUTABLE_FILE.getBits();
  824. return modeDiff != 0;
  825. }
  826. /**
  827. * Compare the metadata (mode, length, modification-timestamp) of the
  828. * current entry and a {@link org.eclipse.jgit.dircache.DirCacheEntry}
  829. *
  830. * @param entry
  831. * the {@link org.eclipse.jgit.dircache.DirCacheEntry} to compare
  832. * with
  833. * @return a
  834. * {@link org.eclipse.jgit.treewalk.WorkingTreeIterator.MetadataDiff}
  835. * which tells whether and how the entries metadata differ
  836. */
  837. public MetadataDiff compareMetadata(DirCacheEntry entry) {
  838. if (entry.isAssumeValid())
  839. return MetadataDiff.EQUAL;
  840. if (entry.isUpdateNeeded())
  841. return MetadataDiff.DIFFER_BY_METADATA;
  842. if (isModeDifferent(entry.getRawMode()))
  843. return MetadataDiff.DIFFER_BY_METADATA;
  844. // Don't check for length or lastmodified on folders
  845. int type = mode & FileMode.TYPE_MASK;
  846. if (type == FileMode.TYPE_TREE || type == FileMode.TYPE_GITLINK)
  847. return MetadataDiff.EQUAL;
  848. if (!entry.isSmudged() && entry.getLength() != (int) getEntryLength())
  849. return MetadataDiff.DIFFER_BY_METADATA;
  850. // Cache and file timestamps may differ in resolution. Therefore don't
  851. // compare instants directly but use a comparator that compares only
  852. // up to the lower apparent resolution of either timestamp.
  853. //
  854. // If core.checkstat is set to "minimal", compare only the seconds part.
  855. Instant cacheLastModified = entry.getLastModifiedInstant();
  856. Instant fileLastModified = getEntryLastModifiedInstant();
  857. if (timestampComparator.compare(cacheLastModified, fileLastModified,
  858. getOptions().getCheckStat() == CheckStat.MINIMAL) != 0) {
  859. return MetadataDiff.DIFFER_BY_TIMESTAMP;
  860. }
  861. if (entry.isSmudged()) {
  862. return MetadataDiff.SMUDGED;
  863. }
  864. // The file is clean when when comparing timestamps
  865. return MetadataDiff.EQUAL;
  866. }
  867. /**
  868. * Checks whether this entry differs from a given entry from the
  869. * {@link org.eclipse.jgit.dircache.DirCache}.
  870. *
  871. * File status information is used and if status is same we consider the
  872. * file identical to the state in the working directory. Native git uses
  873. * more stat fields than we have accessible in Java.
  874. *
  875. * @param entry
  876. * the entry from the dircache we want to compare against
  877. * @param forceContentCheck
  878. * True if the actual file content should be checked if
  879. * modification time differs.
  880. * @param reader
  881. * access to repository objects if necessary. Should not be null.
  882. * @return true if content is most likely different.
  883. * @throws java.io.IOException
  884. * @since 3.3
  885. */
  886. public boolean isModified(DirCacheEntry entry, boolean forceContentCheck,
  887. ObjectReader reader) throws IOException {
  888. if (entry == null)
  889. return !FileMode.MISSING.equals(getEntryFileMode());
  890. MetadataDiff diff = compareMetadata(entry);
  891. switch (diff) {
  892. case DIFFER_BY_TIMESTAMP:
  893. if (forceContentCheck)
  894. // But we are told to look at content even though timestamps
  895. // tell us about modification
  896. return contentCheck(entry, reader);
  897. else
  898. // We are told to assume a modification if timestamps differs
  899. return true;
  900. case SMUDGED:
  901. // The file is clean by timestamps but the entry was smudged.
  902. // Lets do a content check
  903. return contentCheck(entry, reader);
  904. case EQUAL:
  905. if (mode == FileMode.SYMLINK.getBits()) {
  906. return contentCheck(entry, reader);
  907. }
  908. return false;
  909. case DIFFER_BY_METADATA:
  910. if (mode == FileMode.TREE.getBits()
  911. && entry.getFileMode().equals(FileMode.GITLINK)) {
  912. byte[] idBuffer = idBuffer();
  913. int idOffset = idOffset();
  914. if (entry.getObjectId().compareTo(idBuffer, idOffset) == 0) {
  915. return true;
  916. } else if (ObjectId.zeroId().compareTo(idBuffer,
  917. idOffset) == 0) {
  918. return new File(repository.getWorkTree(),
  919. entry.getPathString()).list().length > 0;
  920. }
  921. return false;
  922. } else if (mode == FileMode.SYMLINK.getBits())
  923. return contentCheck(entry, reader);
  924. return true;
  925. default:
  926. throw new IllegalStateException(MessageFormat.format(
  927. JGitText.get().unexpectedCompareResult, diff.name()));
  928. }
  929. }
  930. /**
  931. * Get the file mode to use for the current entry when it is to be updated
  932. * in the index.
  933. *
  934. * @param indexIter
  935. * {@link org.eclipse.jgit.dircache.DirCacheIterator} positioned
  936. * at the same entry as this iterator or null if no
  937. * {@link org.eclipse.jgit.dircache.DirCacheIterator} is
  938. * available at this iterator's current entry
  939. * @return index file mode
  940. */
  941. public FileMode getIndexFileMode(DirCacheIterator indexIter) {
  942. final FileMode wtMode = getEntryFileMode();
  943. if (indexIter == null) {
  944. return wtMode;
  945. }
  946. final FileMode iMode = indexIter.getEntryFileMode();
  947. if (getOptions().isFileMode() && iMode != FileMode.GITLINK && iMode != FileMode.TREE) {
  948. return wtMode;
  949. }
  950. if (!getOptions().isFileMode()) {
  951. if (FileMode.REGULAR_FILE == wtMode
  952. && FileMode.EXECUTABLE_FILE == iMode) {
  953. return iMode;
  954. }
  955. if (FileMode.EXECUTABLE_FILE == wtMode
  956. && FileMode.REGULAR_FILE == iMode) {
  957. return iMode;
  958. }
  959. }
  960. if (FileMode.GITLINK == iMode
  961. && FileMode.TREE == wtMode && !getOptions().isDirNoGitLinks()) {
  962. return iMode;
  963. }
  964. if (FileMode.TREE == iMode
  965. && FileMode.GITLINK == wtMode) {
  966. return iMode;
  967. }
  968. return wtMode;
  969. }
  970. /**
  971. * Compares the entries content with the content in the filesystem.
  972. * Unsmudges the entry when it is detected that it is clean.
  973. *
  974. * @param entry
  975. * the entry to be checked
  976. * @param reader
  977. * acccess to repository data if necessary
  978. * @return <code>true</code> if the content doesn't match,
  979. * <code>false</code> if it matches
  980. * @throws IOException
  981. */
  982. private boolean contentCheck(DirCacheEntry entry, ObjectReader reader)
  983. throws IOException {
  984. if (getEntryObjectId().equals(entry.getObjectId())) {
  985. // Content has not changed
  986. // We know the entry can't be racily clean because it's still clean.
  987. // Therefore we unsmudge the entry!
  988. // If by any chance we now unsmudge although we are still in the
  989. // same time-slot as the last modification to the index file the
  990. // next index write operation will smudge again.
  991. // Caution: we are unsmudging just by setting the length of the
  992. // in-memory entry object. It's the callers task to detect that we
  993. // have modified the entry and to persist the modified index.
  994. entry.setLength((int) getEntryLength());
  995. return false;
  996. } else {
  997. if (mode == FileMode.SYMLINK.getBits()) {
  998. return !new File(readSymlinkTarget(current())).equals(
  999. new File(readContentAsNormalizedString(entry, reader)));
  1000. }
  1001. // Content differs: that's a real change, perhaps
  1002. if (reader == null) // deprecated use, do no further checks
  1003. return true;
  1004. switch (getEolStreamType()) {
  1005. case DIRECT:
  1006. return true;
  1007. default:
  1008. try {
  1009. ObjectLoader loader = reader.open(entry.getObjectId());
  1010. if (loader == null)
  1011. return true;
  1012. // We need to compute the length, but only if it is not
  1013. // a binary stream.
  1014. long dcInLen;
  1015. try (InputStream dcIn = new AutoLFInputStream(
  1016. loader.openStream(), true,
  1017. true /* abort if binary */)) {
  1018. dcInLen = computeLength(dcIn);
  1019. } catch (AutoLFInputStream.IsBinaryException e) {
  1020. return true;
  1021. }
  1022. try (InputStream dcIn = new AutoLFInputStream(
  1023. loader.openStream(), true)) {
  1024. byte[] autoCrLfHash = computeHash(dcIn, dcInLen);
  1025. boolean changed = getEntryObjectId()
  1026. .compareTo(autoCrLfHash, 0) != 0;
  1027. return changed;
  1028. }
  1029. } catch (IOException e) {
  1030. return true;
  1031. }
  1032. }
  1033. }
  1034. }
  1035. private static String readContentAsNormalizedString(DirCacheEntry entry,
  1036. ObjectReader reader) throws MissingObjectException, IOException {
  1037. ObjectLoader open = reader.open(entry.getObjectId());
  1038. byte[] cachedBytes = open.getCachedBytes();
  1039. return FS.detect().normalize(RawParseUtils.decode(cachedBytes));
  1040. }
  1041. /**
  1042. * Reads the target of a symlink as a string. This default implementation
  1043. * fully reads the entry's input stream and converts it to a normalized
  1044. * string. Subclasses may override to provide more specialized
  1045. * implementations.
  1046. *
  1047. * @param entry
  1048. * to read
  1049. * @return the entry's content as a normalized string
  1050. * @throws java.io.IOException
  1051. * if the entry cannot be read or does not denote a symlink
  1052. * @since 4.6
  1053. */
  1054. protected String readSymlinkTarget(Entry entry) throws IOException {
  1055. if (!entry.getMode().equals(FileMode.SYMLINK)) {
  1056. throw new java.nio.file.NotLinkException(entry.getName());
  1057. }
  1058. long length = entry.getLength();
  1059. byte[] content = new byte[(int) length];
  1060. try (InputStream is = entry.openInputStream()) {
  1061. int bytesRead = IO.readFully(is, content, 0);
  1062. return FS.detect()
  1063. .normalize(RawParseUtils.decode(content, 0, bytesRead));
  1064. }
  1065. }
  1066. private static long computeLength(InputStream in) throws IOException {
  1067. // Since we only care about the length, use skip. The stream
  1068. // may be able to more efficiently wade through its data.
  1069. //
  1070. long length = 0;
  1071. for (;;) {
  1072. long n = in.skip(1 << 20);
  1073. if (n <= 0)
  1074. break;
  1075. length += n;
  1076. }
  1077. return length;
  1078. }
  1079. private byte[] computeHash(InputStream in, long length) throws IOException {
  1080. SHA1 contentDigest = SHA1.newInstance();
  1081. final byte[] contentReadBuffer = state.contentReadBuffer;
  1082. contentDigest.update(hblob);
  1083. contentDigest.update((byte) ' ');
  1084. long sz = length;
  1085. if (sz == 0) {
  1086. contentDigest.update((byte) '0');
  1087. } else {
  1088. final int bufn = contentReadBuffer.length;
  1089. int p = bufn;
  1090. do {
  1091. contentReadBuffer[--p] = digits[(int) (sz % 10)];
  1092. sz /= 10;
  1093. } while (sz > 0);
  1094. contentDigest.update(contentReadBuffer, p, bufn - p);
  1095. }
  1096. contentDigest.update((byte) 0);
  1097. for (;;) {
  1098. final int r = in.read(contentReadBuffer);
  1099. if (r <= 0)
  1100. break;
  1101. contentDigest.update(contentReadBuffer, 0, r);
  1102. sz += r;
  1103. }
  1104. if (sz != length)
  1105. return zeroid;
  1106. return contentDigest.digest();
  1107. }
  1108. /**
  1109. * A single entry within a working directory tree.
  1110. *
  1111. * @since 5.0
  1112. */
  1113. public static abstract class Entry {
  1114. byte[] encodedName;
  1115. int encodedNameLen;
  1116. void encodeName(CharsetEncoder enc) {
  1117. final ByteBuffer b;
  1118. try {
  1119. b = enc.encode(CharBuffer.wrap(getName()));
  1120. } catch (CharacterCodingException e) {
  1121. // This should so never happen.
  1122. throw new RuntimeException(MessageFormat.format(
  1123. JGitText.get().unencodeableFile, getName()));
  1124. }
  1125. encodedNameLen = b.limit();
  1126. if (b.hasArray() && b.arrayOffset() == 0)
  1127. encodedName = b.array();
  1128. else
  1129. b.get(encodedName = new byte[encodedNameLen]);
  1130. }
  1131. @Override
  1132. public String toString() {
  1133. return getMode().toString() + " " + getName(); //$NON-NLS-1$
  1134. }
  1135. /**
  1136. * Get the type of this entry.
  1137. * <p>
  1138. * <b>Note: Efficient implementation required.</b>
  1139. * <p>
  1140. * The implementation of this method must be efficient. If a subclass
  1141. * needs to compute the value they should cache the reference within an
  1142. * instance member instead.
  1143. *
  1144. * @return a file mode constant from {@link FileMode}.
  1145. */
  1146. public abstract FileMode getMode();
  1147. /**
  1148. * Get the byte length of this entry.
  1149. * <p>
  1150. * <b>Note: Efficient implementation required.</b>
  1151. * <p>
  1152. * The implementation of this method must be efficient. If a subclass
  1153. * needs to compute the value they should cache the reference within an
  1154. * instance member instead.
  1155. *
  1156. * @return size of this file, in bytes.
  1157. */
  1158. public abstract long getLength();
  1159. /**
  1160. * Get the last modified time of this entry.
  1161. * <p>
  1162. * <b>Note: Efficient implementation required.</b>
  1163. * <p>
  1164. * The implementation of this method must be efficient. If a subclass
  1165. * needs to compute the value they should cache the reference within an
  1166. * instance member instead.
  1167. *
  1168. * @return time since the epoch (in ms) of the last change.
  1169. * @deprecated use {@link #getLastModifiedInstant()} instead
  1170. */
  1171. @Deprecated
  1172. public abstract long getLastModified();
  1173. /**
  1174. * Get the last modified time of this entry.
  1175. * <p>
  1176. * <b>Note: Efficient implementation required.</b>
  1177. * <p>
  1178. * The implementation of this method must be efficient. If a subclass
  1179. * needs to compute the value they should cache the reference within an
  1180. * instance member instead.
  1181. *
  1182. * @return time of the last change.
  1183. * @since 5.1.9
  1184. */
  1185. public abstract Instant getLastModifiedInstant();
  1186. /**
  1187. * Get the name of this entry within its directory.
  1188. * <p>
  1189. * Efficient implementations are not required. The caller will obtain
  1190. * the name only once and cache it once obtained.
  1191. *
  1192. * @return name of the entry.
  1193. */
  1194. public abstract String getName();
  1195. /**
  1196. * Obtain an input stream to read the file content.
  1197. * <p>
  1198. * Efficient implementations are not required. The caller will usually
  1199. * obtain the stream only once per entry, if at all.
  1200. * <p>
  1201. * The input stream should not use buffering if the implementation can
  1202. * avoid it. The caller will buffer as necessary to perform efficient
  1203. * block IO operations.
  1204. * <p>
  1205. * The caller will close the stream once complete.
  1206. *
  1207. * @return a stream to read from the file.
  1208. * @throws IOException
  1209. * the file could not be opened for reading.
  1210. */
  1211. public abstract InputStream openInputStream() throws IOException;
  1212. }
  1213. /** Magic type indicating we know rules exist, but they aren't loaded. */
  1214. private static class PerDirectoryIgnoreNode extends IgnoreNode {
  1215. final Entry entry;
  1216. PerDirectoryIgnoreNode(Entry entry) {
  1217. super(Collections.<FastIgnoreRule> emptyList());
  1218. this.entry = entry;
  1219. }
  1220. IgnoreNode load() throws IOException {
  1221. IgnoreNode r = new IgnoreNode();
  1222. try (InputStream in = entry.openInputStream()) {
  1223. r.parse(in);
  1224. }
  1225. return r.getRules().isEmpty() ? null : r;
  1226. }
  1227. }
  1228. /** Magic type indicating there may be rules for the top level. */
  1229. private static class RootIgnoreNode extends PerDirectoryIgnoreNode {
  1230. final Repository repository;
  1231. RootIgnoreNode(Entry entry, Repository repository) {
  1232. super(entry);
  1233. this.repository = repository;
  1234. }
  1235. @Override
  1236. IgnoreNode load() throws IOException {
  1237. IgnoreNode r;
  1238. if (entry != null) {
  1239. r = super.load();
  1240. if (r == null)
  1241. r = new IgnoreNode();
  1242. } else {
  1243. r = new IgnoreNode();
  1244. }
  1245. FS fs = repository.getFS();
  1246. String path = repository.getConfig().get(CoreConfig.KEY)
  1247. .getExcludesFile();
  1248. if (path != null) {
  1249. File excludesfile;
  1250. if (path.startsWith("~/")) //$NON-NLS-1$
  1251. excludesfile = fs.resolve(fs.userHome(), path.substring(2));
  1252. else
  1253. excludesfile = fs.resolve(null, path);
  1254. loadRulesFromFile(r, excludesfile);
  1255. }
  1256. File exclude = fs.resolve(repository.getDirectory(),
  1257. Constants.INFO_EXCLUDE);
  1258. loadRulesFromFile(r, exclude);
  1259. return r.getRules().isEmpty() ? null : r;
  1260. }
  1261. private static void loadRulesFromFile(IgnoreNode r, File exclude)
  1262. throws FileNotFoundException, IOException {
  1263. if (FS.DETECTED.exists(exclude)) {
  1264. try (FileInputStream in = new FileInputStream(exclude)) {
  1265. r.parse(in);
  1266. }
  1267. }
  1268. }
  1269. }
  1270. /** Magic type indicating we know rules exist, but they aren't loaded. */
  1271. private static class PerDirectoryAttributesNode extends AttributesNode {
  1272. final Entry entry;
  1273. PerDirectoryAttributesNode(Entry entry) {
  1274. super(Collections.<AttributesRule> emptyList());
  1275. this.entry = entry;
  1276. }
  1277. AttributesNode load() throws IOException {
  1278. AttributesNode r = new AttributesNode();
  1279. try (InputStream in = entry.openInputStream()) {
  1280. r.parse(in);
  1281. }
  1282. return r.getRules().isEmpty() ? null : r;
  1283. }
  1284. }
  1285. private static final class IteratorState {
  1286. /** Options used to process the working tree. */
  1287. final WorkingTreeOptions options;
  1288. /** File name character encoder. */
  1289. final CharsetEncoder nameEncoder;
  1290. /** Buffer used to perform {@link #contentId} computations. */
  1291. byte[] contentReadBuffer;
  1292. /** TreeWalk with a (supposedly) matching DirCacheIterator. */
  1293. TreeWalk walk;
  1294. /** Position of the matching {@link DirCacheIterator}. */
  1295. int dirCacheTree = -1;
  1296. /** Whether the iterator shall walk ignored directories. */
  1297. boolean walkIgnored = false;
  1298. final Map<String, Boolean> directoryToIgnored = new HashMap<>();
  1299. IteratorState(WorkingTreeOptions options) {
  1300. this.options = options;
  1301. this.nameEncoder = UTF_8.newEncoder();
  1302. }
  1303. void initializeReadBuffer() {
  1304. if (contentReadBuffer == null) {
  1305. contentReadBuffer = new byte[BUFFER_SIZE];
  1306. }
  1307. }
  1308. }
  1309. /**
  1310. * Get the clean filter command for the current entry.
  1311. *
  1312. * @return the clean filter command for the current entry or
  1313. * <code>null</code> if no such command is defined
  1314. * @throws java.io.IOException
  1315. * @since 4.2
  1316. */
  1317. public String getCleanFilterCommand() throws IOException {
  1318. if (cleanFilterCommandHolder == null) {
  1319. String cmd = null;
  1320. if (state.walk != null) {
  1321. cmd = state.walk
  1322. .getFilterCommand(Constants.ATTR_FILTER_TYPE_CLEAN);
  1323. }
  1324. cleanFilterCommandHolder = new Holder<>(cmd);
  1325. }
  1326. return cleanFilterCommandHolder.get();
  1327. }
  1328. /**
  1329. * Get the eol stream type for the current entry.
  1330. *
  1331. * @return the eol stream type for the current entry or <code>null</code> if
  1332. * it cannot be determined. When state or state.walk is null or the
  1333. * {@link org.eclipse.jgit.treewalk.TreeWalk} is not based on a
  1334. * {@link org.eclipse.jgit.lib.Repository} then null is returned.
  1335. * @throws java.io.IOException
  1336. * @since 4.3
  1337. */
  1338. public EolStreamType getEolStreamType() throws IOException {
  1339. return getEolStreamType(null);
  1340. }
  1341. /**
  1342. * @param opType
  1343. * The operationtype (checkin/checkout) which should be used
  1344. * @return the eol stream type for the current entry or <code>null</code> if
  1345. * it cannot be determined. When state or state.walk is null or the
  1346. * {@link TreeWalk} is not based on a {@link Repository} then null
  1347. * is returned.
  1348. * @throws IOException
  1349. */
  1350. private EolStreamType getEolStreamType(OperationType opType)
  1351. throws IOException {
  1352. if (eolStreamTypeHolder == null) {
  1353. EolStreamType type=null;
  1354. if (state.walk != null) {
  1355. type = state.walk.getEolStreamType(opType);
  1356. } else {
  1357. switch (getOptions().getAutoCRLF()) {
  1358. case FALSE:
  1359. type = EolStreamType.DIRECT;
  1360. break;
  1361. case TRUE:
  1362. case INPUT:
  1363. type = EolStreamType.AUTO_LF;
  1364. break;
  1365. }
  1366. }
  1367. eolStreamTypeHolder = new Holder<>(type);
  1368. }
  1369. return eolStreamTypeHolder.get();
  1370. }
  1371. private boolean isDirectoryIgnored(String pathRel) throws IOException {
  1372. final int pOff = 0 < pathOffset ? pathOffset - 1 : pathOffset;
  1373. final String base = TreeWalk.pathOf(this.path, 0, pOff);
  1374. final String pathAbs = concatPath(base, pathRel);
  1375. return isDirectoryIgnored(pathRel, pathAbs);
  1376. }
  1377. private boolean isDirectoryIgnored(String pathRel, String pathAbs)
  1378. throws IOException {
  1379. assert pathRel.length() == 0 || (pathRel.charAt(0) != '/'
  1380. && pathRel.charAt(pathRel.length() - 1) != '/');
  1381. assert pathAbs.length() == 0 || (pathAbs.charAt(0) != '/'
  1382. && pathAbs.charAt(pathAbs.length() - 1) != '/');
  1383. assert pathAbs.endsWith(pathRel);
  1384. Boolean ignored = state.directoryToIgnored.get(pathAbs);
  1385. if (ignored != null) {
  1386. return ignored.booleanValue();
  1387. }
  1388. final String parentRel = getParentPath(pathRel);
  1389. if (parentRel != null && isDirectoryIgnored(parentRel)) {
  1390. state.directoryToIgnored.put(pathAbs, Boolean.TRUE);
  1391. return true;
  1392. }
  1393. final IgnoreNode node = getIgnoreNode();
  1394. for (String p = pathRel; node != null
  1395. && !"".equals(p); p = getParentPath(p)) { //$NON-NLS-1$
  1396. ignored = node.checkIgnored(p, true);
  1397. if (ignored != null) {
  1398. state.directoryToIgnored.put(pathAbs, ignored);
  1399. return ignored.booleanValue();
  1400. }
  1401. }
  1402. if (!(this.parent instanceof WorkingTreeIterator)) {
  1403. state.directoryToIgnored.put(pathAbs, Boolean.FALSE);
  1404. return false;
  1405. }
  1406. final WorkingTreeIterator wtParent = (WorkingTreeIterator) this.parent;
  1407. final String parentRelPath = concatPath(
  1408. TreeWalk.pathOf(this.path, wtParent.pathOffset, pathOffset - 1),
  1409. pathRel);
  1410. assert concatPath(TreeWalk.pathOf(wtParent.path, 0,
  1411. Math.max(0, wtParent.pathOffset - 1)), parentRelPath)
  1412. .equals(pathAbs);
  1413. return wtParent.isDirectoryIgnored(parentRelPath, pathAbs);
  1414. }
  1415. private static String getParentPath(String path) {
  1416. final int slashIndex = path.lastIndexOf('/', path.length() - 2);
  1417. if (slashIndex > 0) {
  1418. return path.substring(path.charAt(0) == '/' ? 1 : 0, slashIndex);
  1419. }
  1420. return path.length() > 0 ? "" : null; //$NON-NLS-1$
  1421. }
  1422. private static String concatPath(String p1, String p2) {
  1423. return p1 + (p1.length() > 0 && p2.length() > 0 ? "/" : "") + p2; //$NON-NLS-1$ //$NON-NLS-2$
  1424. }
  1425. }