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 47KB

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