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

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