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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  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 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. /** {@inheritDoc} */
  473. @Override
  474. public int idOffset() {
  475. return contentIdOffset;
  476. }
  477. /** {@inheritDoc} */
  478. @Override
  479. public void reset() {
  480. if (!first()) {
  481. ptr = 0;
  482. if (!eof())
  483. parseEntry();
  484. }
  485. }
  486. /** {@inheritDoc} */
  487. @Override
  488. public boolean first() {
  489. return ptr == 0;
  490. }
  491. /** {@inheritDoc} */
  492. @Override
  493. public boolean eof() {
  494. return ptr == entryCnt;
  495. }
  496. /** {@inheritDoc} */
  497. @Override
  498. public void next(int delta) throws CorruptObjectException {
  499. ptr += delta;
  500. if (!eof()) {
  501. parseEntry();
  502. }
  503. }
  504. /** {@inheritDoc} */
  505. @Override
  506. public void back(int delta) throws CorruptObjectException {
  507. ptr -= delta;
  508. parseEntry();
  509. }
  510. private void parseEntry() {
  511. final Entry e = entries[ptr];
  512. mode = e.getMode().getBits();
  513. final int nameLen = e.encodedNameLen;
  514. ensurePathCapacity(pathOffset + nameLen, pathOffset);
  515. System.arraycopy(e.encodedName, 0, path, pathOffset, nameLen);
  516. pathLen = pathOffset + nameLen;
  517. canonLen = -1;
  518. cleanFilterCommandHolder = null;
  519. eolStreamTypeHolder = null;
  520. }
  521. /**
  522. * Get the raw byte length of this entry.
  523. *
  524. * @return size of this file, in bytes.
  525. */
  526. public long getEntryLength() {
  527. return current().getLength();
  528. }
  529. /**
  530. * Get the filtered input length of this entry
  531. *
  532. * @return size of the content, in bytes
  533. * @throws java.io.IOException
  534. */
  535. public long getEntryContentLength() throws IOException {
  536. if (canonLen == -1) {
  537. long rawLen = getEntryLength();
  538. if (rawLen == 0)
  539. canonLen = 0;
  540. InputStream is = current().openInputStream();
  541. try {
  542. // canonLen gets updated here
  543. possiblyFilteredInputStream(current(), is, current()
  544. .getLength());
  545. } finally {
  546. safeClose(is);
  547. }
  548. }
  549. return canonLen;
  550. }
  551. /**
  552. * Get the last modified time of this entry.
  553. *
  554. * @return last modified time of this file, in milliseconds since the epoch
  555. * (Jan 1, 1970 UTC).
  556. * @deprecated use {@link #getEntryLastModifiedInstant()} instead
  557. */
  558. @Deprecated
  559. public long getEntryLastModified() {
  560. return current().getLastModified();
  561. }
  562. /**
  563. * Get the last modified time of this entry.
  564. *
  565. * @return last modified time of this file
  566. * @since 5.1.9
  567. */
  568. public Instant getEntryLastModifiedInstant() {
  569. return current().getLastModifiedInstant();
  570. }
  571. /**
  572. * Obtain an input stream to read the file content.
  573. * <p>
  574. * Efficient implementations are not required. The caller will usually
  575. * obtain the stream only once per entry, if at all.
  576. * <p>
  577. * The input stream should not use buffering if the implementation can avoid
  578. * it. The caller will buffer as necessary to perform efficient block IO
  579. * operations.
  580. * <p>
  581. * The caller will close the stream once complete.
  582. *
  583. * @return a stream to read from the file.
  584. * @throws java.io.IOException
  585. * the file could not be opened for reading.
  586. */
  587. public InputStream openEntryStream() throws IOException {
  588. InputStream rawis = current().openInputStream();
  589. if (getCleanFilterCommand() == null
  590. && getEolStreamType() == EolStreamType.DIRECT) {
  591. return rawis;
  592. }
  593. return filterClean(rawis);
  594. }
  595. /**
  596. * Determine if the current entry path is ignored by an ignore rule.
  597. *
  598. * @return true if the entry was ignored by an ignore rule file.
  599. * @throws java.io.IOException
  600. * a relevant ignore rule file exists but cannot be read.
  601. */
  602. public boolean isEntryIgnored() throws IOException {
  603. return isEntryIgnored(pathLen);
  604. }
  605. /**
  606. * Determine if the entry path is ignored by an ignore rule.
  607. *
  608. * @param pLen
  609. * the length of the path in the path buffer.
  610. * @return true if the entry is ignored by an ignore rule.
  611. * @throws java.io.IOException
  612. * a relevant ignore rule file exists but cannot be read.
  613. */
  614. protected boolean isEntryIgnored(int pLen) throws IOException {
  615. return isEntryIgnored(pLen, mode);
  616. }
  617. /**
  618. * Determine if the entry path is ignored by an ignore rule.
  619. *
  620. * @param pLen
  621. * the length of the path in the path buffer.
  622. * @param fileMode
  623. * the original iterator file mode
  624. * @return true if the entry is ignored by an ignore rule.
  625. * @throws IOException
  626. * a relevant ignore rule file exists but cannot be read.
  627. */
  628. private boolean isEntryIgnored(int pLen, int fileMode)
  629. throws IOException {
  630. // The ignore code wants path to start with a '/' if possible.
  631. // If we have the '/' in our path buffer because we are inside
  632. // a sub-directory include it in the range we convert to string.
  633. //
  634. final int pOff = 0 < pathOffset ? pathOffset - 1 : pathOffset;
  635. String pathRel = TreeWalk.pathOf(this.path, pOff, pLen);
  636. String parentRel = getParentPath(pathRel);
  637. // CGit is processing .gitignore files by starting at the root of the
  638. // repository and then recursing into subdirectories. With this
  639. // approach, top-level ignored directories will be processed first which
  640. // allows to skip entire subtrees and further .gitignore-file processing
  641. // within these subtrees.
  642. //
  643. // We will follow the same approach by marking directories as "ignored"
  644. // here. This allows to have a simplified FastIgnore.checkIgnore()
  645. // implementation (both in terms of code and computational complexity):
  646. //
  647. // Without the "ignored" flag, we would have to apply the ignore-check
  648. // to a path and all of its parents always(!), to determine whether a
  649. // path is ignored directly or by one of its parent directories; with
  650. // the "ignored" flag, we know at this point that the parent directory
  651. // is definitely not ignored, thus the path can only become ignored if
  652. // there is a rule matching the path itself.
  653. if (isDirectoryIgnored(parentRel)) {
  654. return true;
  655. }
  656. IgnoreNode rules = getIgnoreNode();
  657. final Boolean ignored = rules != null
  658. ? rules.checkIgnored(pathRel, FileMode.TREE.equals(fileMode))
  659. : null;
  660. if (ignored != null) {
  661. return ignored.booleanValue();
  662. }
  663. return parent instanceof WorkingTreeIterator
  664. && ((WorkingTreeIterator) parent).isEntryIgnored(pLen,
  665. fileMode);
  666. }
  667. private IgnoreNode getIgnoreNode() throws IOException {
  668. if (ignoreNode instanceof PerDirectoryIgnoreNode)
  669. ignoreNode = ((PerDirectoryIgnoreNode) ignoreNode).load();
  670. return ignoreNode;
  671. }
  672. /**
  673. * Retrieves the {@link org.eclipse.jgit.attributes.AttributesNode} for the
  674. * current entry.
  675. *
  676. * @return the {@link org.eclipse.jgit.attributes.AttributesNode} for the
  677. * current entry.
  678. * @throws IOException
  679. */
  680. public AttributesNode getEntryAttributesNode() throws IOException {
  681. if (attributesNode instanceof PerDirectoryAttributesNode)
  682. attributesNode = ((PerDirectoryAttributesNode) attributesNode)
  683. .load();
  684. return attributesNode;
  685. }
  686. private static final Comparator<Entry> ENTRY_CMP = (Entry a,
  687. Entry b) -> Paths.compare(a.encodedName, 0, a.encodedNameLen,
  688. a.getMode().getBits(), b.encodedName, 0, b.encodedNameLen,
  689. b.getMode().getBits());
  690. /**
  691. * Constructor helper.
  692. *
  693. * @param list
  694. * files in the subtree of the work tree this iterator operates
  695. * on
  696. */
  697. protected void init(Entry[] list) {
  698. // Filter out nulls, . and .. as these are not valid tree entries,
  699. // also cache the encoded forms of the path names for efficient use
  700. // later on during sorting and iteration.
  701. //
  702. entries = list;
  703. int i, o;
  704. final CharsetEncoder nameEncoder = state.nameEncoder;
  705. for (i = 0, o = 0; i < entries.length; i++) {
  706. final Entry e = entries[i];
  707. if (e == null)
  708. continue;
  709. final String name = e.getName();
  710. if (".".equals(name) || "..".equals(name)) //$NON-NLS-1$ //$NON-NLS-2$
  711. continue;
  712. if (Constants.DOT_GIT.equals(name))
  713. continue;
  714. if (Constants.DOT_GIT_IGNORE.equals(name))
  715. ignoreNode = new PerDirectoryIgnoreNode(e);
  716. if (Constants.DOT_GIT_ATTRIBUTES.equals(name))
  717. attributesNode = new PerDirectoryAttributesNode(e);
  718. if (i != o)
  719. entries[o] = e;
  720. e.encodeName(nameEncoder);
  721. o++;
  722. }
  723. entryCnt = o;
  724. Arrays.sort(entries, 0, entryCnt, ENTRY_CMP);
  725. contentIdFromPtr = -1;
  726. ptr = 0;
  727. if (!eof())
  728. parseEntry();
  729. else if (pathLen == 0) // see bug 445363
  730. pathLen = pathOffset;
  731. }
  732. /**
  733. * Obtain the current entry from this iterator.
  734. *
  735. * @return the currently selected entry.
  736. */
  737. protected Entry current() {
  738. return entries[ptr];
  739. }
  740. /**
  741. * The result of a metadata-comparison between the current entry and a
  742. * {@link DirCacheEntry}
  743. */
  744. public enum MetadataDiff {
  745. /**
  746. * The entries are equal by metaData (mode, length,
  747. * modification-timestamp) or the <code>assumeValid</code> attribute of
  748. * the index entry is set
  749. */
  750. EQUAL,
  751. /**
  752. * The entries are not equal by metaData (mode, length) or the
  753. * <code>isUpdateNeeded</code> attribute of the index entry is set
  754. */
  755. DIFFER_BY_METADATA,
  756. /** index entry is smudged - can't use that entry for comparison */
  757. SMUDGED,
  758. /**
  759. * The entries are equal by metaData (mode, length) but differ by
  760. * modification-timestamp.
  761. */
  762. DIFFER_BY_TIMESTAMP
  763. }
  764. /**
  765. * Is the file mode of the current entry different than the given raw mode?
  766. *
  767. * @param rawMode
  768. * an int.
  769. * @return true if different, false otherwise
  770. */
  771. public boolean isModeDifferent(int rawMode) {
  772. // Determine difference in mode-bits of file and index-entry. In the
  773. // bitwise presentation of modeDiff we'll have a '1' when the two modes
  774. // differ at this position.
  775. int modeDiff = getEntryRawMode() ^ rawMode;
  776. if (modeDiff == 0)
  777. return false;
  778. // Do not rely on filemode differences in case of symbolic links
  779. if (getOptions().getSymLinks() == SymLinks.FALSE)
  780. if (FileMode.SYMLINK.equals(rawMode))
  781. return false;
  782. // Ignore the executable file bits if WorkingTreeOptions tell me to
  783. // do so. Ignoring is done by setting the bits representing a
  784. // EXECUTABLE_FILE to '0' in modeDiff
  785. if (!state.options.isFileMode())
  786. modeDiff &= ~FileMode.EXECUTABLE_FILE.getBits();
  787. return modeDiff != 0;
  788. }
  789. /**
  790. * Compare the metadata (mode, length, modification-timestamp) of the
  791. * current entry and a {@link org.eclipse.jgit.dircache.DirCacheEntry}
  792. *
  793. * @param entry
  794. * the {@link org.eclipse.jgit.dircache.DirCacheEntry} to compare
  795. * with
  796. * @return a
  797. * {@link org.eclipse.jgit.treewalk.WorkingTreeIterator.MetadataDiff}
  798. * which tells whether and how the entries metadata differ
  799. */
  800. public MetadataDiff compareMetadata(DirCacheEntry entry) {
  801. if (entry.isAssumeValid())
  802. return MetadataDiff.EQUAL;
  803. if (entry.isUpdateNeeded())
  804. return MetadataDiff.DIFFER_BY_METADATA;
  805. if (isModeDifferent(entry.getRawMode()))
  806. return MetadataDiff.DIFFER_BY_METADATA;
  807. // Don't check for length or lastmodified on folders
  808. int type = mode & FileMode.TYPE_MASK;
  809. if (type == FileMode.TYPE_TREE || type == FileMode.TYPE_GITLINK)
  810. return MetadataDiff.EQUAL;
  811. if (!entry.isSmudged() && entry.getLength() != (int) getEntryLength())
  812. return MetadataDiff.DIFFER_BY_METADATA;
  813. // Cache and file timestamps may differ in resolution. Therefore don't
  814. // compare instants directly but use a comparator that compares only
  815. // up to the lower apparent resolution of either timestamp.
  816. //
  817. // If core.checkstat is set to "minimal", compare only the seconds part.
  818. Instant cacheLastModified = entry.getLastModifiedInstant();
  819. Instant fileLastModified = getEntryLastModifiedInstant();
  820. if (timestampComparator.compare(cacheLastModified, fileLastModified,
  821. getOptions().getCheckStat() == CheckStat.MINIMAL) != 0) {
  822. return MetadataDiff.DIFFER_BY_TIMESTAMP;
  823. }
  824. if (entry.isSmudged()) {
  825. return MetadataDiff.SMUDGED;
  826. }
  827. // The file is clean when when comparing timestamps
  828. return MetadataDiff.EQUAL;
  829. }
  830. /**
  831. * Checks whether this entry differs from a given entry from the
  832. * {@link org.eclipse.jgit.dircache.DirCache}.
  833. *
  834. * File status information is used and if status is same we consider the
  835. * file identical to the state in the working directory. Native git uses
  836. * more stat fields than we have accessible in Java.
  837. *
  838. * @param entry
  839. * the entry from the dircache we want to compare against
  840. * @param forceContentCheck
  841. * True if the actual file content should be checked if
  842. * modification time differs.
  843. * @param reader
  844. * access to repository objects if necessary. Should not be null.
  845. * @return true if content is most likely different.
  846. * @throws java.io.IOException
  847. * @since 3.3
  848. */
  849. public boolean isModified(DirCacheEntry entry, boolean forceContentCheck,
  850. ObjectReader reader) throws IOException {
  851. if (entry == null)
  852. return !FileMode.MISSING.equals(getEntryFileMode());
  853. MetadataDiff diff = compareMetadata(entry);
  854. switch (diff) {
  855. case DIFFER_BY_TIMESTAMP:
  856. if (forceContentCheck) {
  857. // But we are told to look at content even though timestamps
  858. // tell us about modification
  859. return contentCheck(entry, reader);
  860. }
  861. // We are told to assume a modification if timestamps differs
  862. return true;
  863. case SMUDGED:
  864. // The file is clean by timestamps but the entry was smudged.
  865. // Lets do a content check
  866. return contentCheck(entry, reader);
  867. case EQUAL:
  868. if (mode == FileMode.SYMLINK.getBits()) {
  869. return contentCheck(entry, reader);
  870. }
  871. return false;
  872. case DIFFER_BY_METADATA:
  873. if (mode == FileMode.TREE.getBits()
  874. && entry.getFileMode().equals(FileMode.GITLINK)) {
  875. byte[] idBuffer = idBuffer();
  876. int idOffset = idOffset();
  877. if (entry.getObjectId().compareTo(idBuffer, idOffset) == 0) {
  878. return true;
  879. } else if (ObjectId.zeroId().compareTo(idBuffer,
  880. idOffset) == 0) {
  881. return new File(repository.getWorkTree(),
  882. entry.getPathString()).list().length > 0;
  883. }
  884. return false;
  885. } else if (mode == FileMode.SYMLINK.getBits())
  886. return contentCheck(entry, reader);
  887. return true;
  888. default:
  889. throw new IllegalStateException(MessageFormat.format(
  890. JGitText.get().unexpectedCompareResult, diff.name()));
  891. }
  892. }
  893. /**
  894. * Get the file mode to use for the current entry when it is to be updated
  895. * in the index.
  896. *
  897. * @param indexIter
  898. * {@link org.eclipse.jgit.dircache.DirCacheIterator} positioned
  899. * at the same entry as this iterator or null if no
  900. * {@link org.eclipse.jgit.dircache.DirCacheIterator} is
  901. * available at this iterator's current entry
  902. * @return index file mode
  903. */
  904. public FileMode getIndexFileMode(DirCacheIterator indexIter) {
  905. final FileMode wtMode = getEntryFileMode();
  906. if (indexIter == null) {
  907. return wtMode;
  908. }
  909. final FileMode iMode = indexIter.getEntryFileMode();
  910. if (getOptions().isFileMode() && iMode != FileMode.GITLINK && iMode != FileMode.TREE) {
  911. return wtMode;
  912. }
  913. if (!getOptions().isFileMode()) {
  914. if (FileMode.REGULAR_FILE == wtMode
  915. && FileMode.EXECUTABLE_FILE == iMode) {
  916. return iMode;
  917. }
  918. if (FileMode.EXECUTABLE_FILE == wtMode
  919. && FileMode.REGULAR_FILE == iMode) {
  920. return iMode;
  921. }
  922. }
  923. if (FileMode.GITLINK == iMode
  924. && FileMode.TREE == wtMode && !getOptions().isDirNoGitLinks()) {
  925. return iMode;
  926. }
  927. if (FileMode.TREE == iMode
  928. && FileMode.GITLINK == wtMode) {
  929. return iMode;
  930. }
  931. return wtMode;
  932. }
  933. /**
  934. * Compares the entries content with the content in the filesystem.
  935. * Unsmudges the entry when it is detected that it is clean.
  936. *
  937. * @param entry
  938. * the entry to be checked
  939. * @param reader
  940. * acccess to repository data if necessary
  941. * @return <code>true</code> if the content doesn't match,
  942. * <code>false</code> if it matches
  943. * @throws IOException
  944. */
  945. private boolean contentCheck(DirCacheEntry entry, ObjectReader reader)
  946. throws IOException {
  947. if (getEntryObjectId().equals(entry.getObjectId())) {
  948. // Content has not changed
  949. // We know the entry can't be racily clean because it's still clean.
  950. // Therefore we unsmudge the entry!
  951. // If by any chance we now unsmudge although we are still in the
  952. // same time-slot as the last modification to the index file the
  953. // next index write operation will smudge again.
  954. // Caution: we are unsmudging just by setting the length of the
  955. // in-memory entry object. It's the callers task to detect that we
  956. // have modified the entry and to persist the modified index.
  957. entry.setLength((int) getEntryLength());
  958. return false;
  959. }
  960. if (mode == FileMode.SYMLINK.getBits()) {
  961. return !new File(readSymlinkTarget(current())).equals(
  962. new File(readContentAsNormalizedString(entry, reader)));
  963. }
  964. // Content differs: that's a real change
  965. return true;
  966. }
  967. private static String readContentAsNormalizedString(DirCacheEntry entry,
  968. ObjectReader reader) throws MissingObjectException, IOException {
  969. ObjectLoader open = reader.open(entry.getObjectId());
  970. byte[] cachedBytes = open.getCachedBytes();
  971. return FS.detect().normalize(RawParseUtils.decode(cachedBytes));
  972. }
  973. /**
  974. * Reads the target of a symlink as a string. This default implementation
  975. * fully reads the entry's input stream and converts it to a normalized
  976. * string. Subclasses may override to provide more specialized
  977. * implementations.
  978. *
  979. * @param entry
  980. * to read
  981. * @return the entry's content as a normalized string
  982. * @throws java.io.IOException
  983. * if the entry cannot be read or does not denote a symlink
  984. * @since 4.6
  985. */
  986. protected String readSymlinkTarget(Entry entry) throws IOException {
  987. if (!entry.getMode().equals(FileMode.SYMLINK)) {
  988. throw new java.nio.file.NotLinkException(entry.getName());
  989. }
  990. long length = entry.getLength();
  991. byte[] content = new byte[(int) length];
  992. try (InputStream is = entry.openInputStream()) {
  993. int bytesRead = IO.readFully(is, content, 0);
  994. return FS.detect()
  995. .normalize(RawParseUtils.decode(content, 0, bytesRead));
  996. }
  997. }
  998. private static long computeLength(InputStream in) throws IOException {
  999. // Since we only care about the length, use skip. The stream
  1000. // may be able to more efficiently wade through its data.
  1001. //
  1002. long length = 0;
  1003. for (;;) {
  1004. long n = in.skip(1 << 20);
  1005. if (n <= 0)
  1006. break;
  1007. length += n;
  1008. }
  1009. return length;
  1010. }
  1011. private byte[] computeHash(InputStream in, long length) throws IOException {
  1012. SHA1 contentDigest = SHA1.newInstance();
  1013. final byte[] contentReadBuffer = state.contentReadBuffer;
  1014. contentDigest.update(hblob);
  1015. contentDigest.update((byte) ' ');
  1016. long sz = length;
  1017. if (sz == 0) {
  1018. contentDigest.update((byte) '0');
  1019. } else {
  1020. final int bufn = contentReadBuffer.length;
  1021. int p = bufn;
  1022. do {
  1023. contentReadBuffer[--p] = digits[(int) (sz % 10)];
  1024. sz /= 10;
  1025. } while (sz > 0);
  1026. contentDigest.update(contentReadBuffer, p, bufn - p);
  1027. }
  1028. contentDigest.update((byte) 0);
  1029. for (;;) {
  1030. final int r = in.read(contentReadBuffer);
  1031. if (r <= 0)
  1032. break;
  1033. contentDigest.update(contentReadBuffer, 0, r);
  1034. sz += r;
  1035. }
  1036. if (sz != length)
  1037. return zeroid;
  1038. return contentDigest.digest();
  1039. }
  1040. /**
  1041. * A single entry within a working directory tree.
  1042. *
  1043. * @since 5.0
  1044. */
  1045. public abstract static class Entry {
  1046. byte[] encodedName;
  1047. int encodedNameLen;
  1048. void encodeName(CharsetEncoder enc) {
  1049. final ByteBuffer b;
  1050. try {
  1051. b = enc.encode(CharBuffer.wrap(getName()));
  1052. } catch (CharacterCodingException e) {
  1053. // This should so never happen.
  1054. throw new RuntimeException(MessageFormat.format(
  1055. JGitText.get().unencodeableFile, getName()), e);
  1056. }
  1057. encodedNameLen = b.limit();
  1058. if (b.hasArray() && b.arrayOffset() == 0)
  1059. encodedName = b.array();
  1060. else
  1061. b.get(encodedName = new byte[encodedNameLen]);
  1062. }
  1063. @Override
  1064. public String toString() {
  1065. return getMode().toString() + " " + getName(); //$NON-NLS-1$
  1066. }
  1067. /**
  1068. * Get the type of this entry.
  1069. * <p>
  1070. * <b>Note: Efficient implementation required.</b>
  1071. * <p>
  1072. * The implementation of this method must be efficient. If a subclass
  1073. * needs to compute the value they should cache the reference within an
  1074. * instance member instead.
  1075. *
  1076. * @return a file mode constant from {@link FileMode}.
  1077. */
  1078. public abstract FileMode getMode();
  1079. /**
  1080. * Get the byte length of this entry.
  1081. * <p>
  1082. * <b>Note: Efficient implementation required.</b>
  1083. * <p>
  1084. * The implementation of this method must be efficient. If a subclass
  1085. * needs to compute the value they should cache the reference within an
  1086. * instance member instead.
  1087. *
  1088. * @return size of this file, in bytes.
  1089. */
  1090. public abstract long getLength();
  1091. /**
  1092. * Get the last modified time of this entry.
  1093. * <p>
  1094. * <b>Note: Efficient implementation required.</b>
  1095. * <p>
  1096. * The implementation of this method must be efficient. If a subclass
  1097. * needs to compute the value they should cache the reference within an
  1098. * instance member instead.
  1099. *
  1100. * @return time since the epoch (in ms) of the last change.
  1101. * @deprecated use {@link #getLastModifiedInstant()} instead
  1102. */
  1103. @Deprecated
  1104. public abstract long getLastModified();
  1105. /**
  1106. * Get the last modified time of this entry.
  1107. * <p>
  1108. * <b>Note: Efficient implementation required.</b>
  1109. * <p>
  1110. * The implementation of this method must be efficient. If a subclass
  1111. * needs to compute the value they should cache the reference within an
  1112. * instance member instead.
  1113. *
  1114. * @return time of the last change.
  1115. * @since 5.1.9
  1116. */
  1117. public abstract Instant getLastModifiedInstant();
  1118. /**
  1119. * Get the name of this entry within its directory.
  1120. * <p>
  1121. * Efficient implementations are not required. The caller will obtain
  1122. * the name only once and cache it once obtained.
  1123. *
  1124. * @return name of the entry.
  1125. */
  1126. public abstract String getName();
  1127. /**
  1128. * Obtain an input stream to read the file content.
  1129. * <p>
  1130. * Efficient implementations are not required. The caller will usually
  1131. * obtain the stream only once per entry, if at all.
  1132. * <p>
  1133. * The input stream should not use buffering if the implementation can
  1134. * avoid it. The caller will buffer as necessary to perform efficient
  1135. * block IO operations.
  1136. * <p>
  1137. * The caller will close the stream once complete.
  1138. *
  1139. * @return a stream to read from the file.
  1140. * @throws IOException
  1141. * the file could not be opened for reading.
  1142. */
  1143. public abstract InputStream openInputStream() throws IOException;
  1144. }
  1145. /** Magic type indicating we know rules exist, but they aren't loaded. */
  1146. private static class PerDirectoryIgnoreNode extends IgnoreNode {
  1147. final Entry entry;
  1148. PerDirectoryIgnoreNode(Entry entry) {
  1149. super(Collections.<FastIgnoreRule> emptyList());
  1150. this.entry = entry;
  1151. }
  1152. IgnoreNode load() throws IOException {
  1153. IgnoreNode r = new IgnoreNode();
  1154. try (InputStream in = entry.openInputStream()) {
  1155. r.parse(in);
  1156. }
  1157. return r.getRules().isEmpty() ? null : r;
  1158. }
  1159. }
  1160. /** Magic type indicating there may be rules for the top level. */
  1161. private static class RootIgnoreNode extends PerDirectoryIgnoreNode {
  1162. final Repository repository;
  1163. RootIgnoreNode(Entry entry, Repository repository) {
  1164. super(entry);
  1165. this.repository = repository;
  1166. }
  1167. @Override
  1168. IgnoreNode load() throws IOException {
  1169. IgnoreNode r;
  1170. if (entry != null) {
  1171. r = super.load();
  1172. if (r == null)
  1173. r = new IgnoreNode();
  1174. } else {
  1175. r = new IgnoreNode();
  1176. }
  1177. FS fs = repository.getFS();
  1178. String path = repository.getConfig().get(CoreConfig.KEY)
  1179. .getExcludesFile();
  1180. if (path != null) {
  1181. File excludesfile;
  1182. if (path.startsWith("~/")) //$NON-NLS-1$
  1183. excludesfile = fs.resolve(fs.userHome(), path.substring(2));
  1184. else
  1185. excludesfile = fs.resolve(null, path);
  1186. loadRulesFromFile(r, excludesfile);
  1187. }
  1188. File exclude = fs.resolve(repository.getDirectory(),
  1189. Constants.INFO_EXCLUDE);
  1190. loadRulesFromFile(r, exclude);
  1191. return r.getRules().isEmpty() ? null : r;
  1192. }
  1193. private static void loadRulesFromFile(IgnoreNode r, File exclude)
  1194. throws FileNotFoundException, IOException {
  1195. if (FS.DETECTED.exists(exclude)) {
  1196. try (FileInputStream in = new FileInputStream(exclude)) {
  1197. r.parse(in);
  1198. }
  1199. }
  1200. }
  1201. }
  1202. /** Magic type indicating we know rules exist, but they aren't loaded. */
  1203. private static class PerDirectoryAttributesNode extends AttributesNode {
  1204. final Entry entry;
  1205. PerDirectoryAttributesNode(Entry entry) {
  1206. super(Collections.<AttributesRule> emptyList());
  1207. this.entry = entry;
  1208. }
  1209. AttributesNode load() throws IOException {
  1210. AttributesNode r = new AttributesNode();
  1211. try (InputStream in = entry.openInputStream()) {
  1212. r.parse(in);
  1213. }
  1214. return r.getRules().isEmpty() ? null : r;
  1215. }
  1216. }
  1217. private static final class IteratorState {
  1218. /** Options used to process the working tree. */
  1219. final WorkingTreeOptions options;
  1220. /** File name character encoder. */
  1221. final CharsetEncoder nameEncoder;
  1222. /** Buffer used to perform {@link #contentId} computations. */
  1223. byte[] contentReadBuffer;
  1224. /** TreeWalk with a (supposedly) matching DirCacheIterator. */
  1225. TreeWalk walk;
  1226. /** Position of the matching {@link DirCacheIterator}. */
  1227. int dirCacheTree = -1;
  1228. /** Whether the iterator shall walk ignored directories. */
  1229. boolean walkIgnored = false;
  1230. final Map<String, Boolean> directoryToIgnored = new HashMap<>();
  1231. IteratorState(WorkingTreeOptions options) {
  1232. this.options = options;
  1233. this.nameEncoder = UTF_8.newEncoder();
  1234. }
  1235. void initializeReadBuffer() {
  1236. if (contentReadBuffer == null) {
  1237. contentReadBuffer = new byte[BUFFER_SIZE];
  1238. }
  1239. }
  1240. }
  1241. /**
  1242. * Get the clean filter command for the current entry.
  1243. *
  1244. * @return the clean filter command for the current entry or
  1245. * <code>null</code> if no such command is defined
  1246. * @throws java.io.IOException
  1247. * @since 4.2
  1248. */
  1249. public String getCleanFilterCommand() throws IOException {
  1250. if (cleanFilterCommandHolder == null) {
  1251. String cmd = null;
  1252. if (state.walk != null) {
  1253. cmd = state.walk
  1254. .getFilterCommand(Constants.ATTR_FILTER_TYPE_CLEAN);
  1255. }
  1256. cleanFilterCommandHolder = new Holder<>(cmd);
  1257. }
  1258. return cleanFilterCommandHolder.get();
  1259. }
  1260. /**
  1261. * Get the eol stream type for the current entry.
  1262. *
  1263. * @return the eol stream type for the current entry or <code>null</code> if
  1264. * it cannot be determined. When state or state.walk is null or the
  1265. * {@link org.eclipse.jgit.treewalk.TreeWalk} is not based on a
  1266. * {@link org.eclipse.jgit.lib.Repository} then null is returned.
  1267. * @throws java.io.IOException
  1268. * @since 4.3
  1269. */
  1270. public EolStreamType getEolStreamType() throws IOException {
  1271. return getEolStreamType(null);
  1272. }
  1273. /**
  1274. * @param opType
  1275. * The operationtype (checkin/checkout) which should be used
  1276. * @return the eol stream type for the current entry or <code>null</code> if
  1277. * it cannot be determined. When state or state.walk is null or the
  1278. * {@link TreeWalk} is not based on a {@link Repository} then null
  1279. * is returned.
  1280. * @throws IOException
  1281. */
  1282. private EolStreamType getEolStreamType(OperationType opType)
  1283. throws IOException {
  1284. if (eolStreamTypeHolder == null) {
  1285. EolStreamType type = null;
  1286. if (state.walk != null) {
  1287. type = state.walk.getEolStreamType(opType);
  1288. OperationType operationType = opType != null ? opType
  1289. : state.walk.getOperationType();
  1290. if (OperationType.CHECKIN_OP.equals(operationType)
  1291. && EolStreamType.AUTO_LF.equals(type)
  1292. && hasCrLfInIndex(getDirCacheIterator())) {
  1293. // If text=auto (or core.autocrlf=true) and the file has
  1294. // already been committed with CR/LF, then don't convert.
  1295. type = EolStreamType.DIRECT;
  1296. }
  1297. } else {
  1298. switch (getOptions().getAutoCRLF()) {
  1299. case FALSE:
  1300. type = EolStreamType.DIRECT;
  1301. break;
  1302. case TRUE:
  1303. case INPUT:
  1304. type = EolStreamType.AUTO_LF;
  1305. break;
  1306. }
  1307. }
  1308. eolStreamTypeHolder = new Holder<>(type);
  1309. }
  1310. return eolStreamTypeHolder.get();
  1311. }
  1312. /**
  1313. * Determines whether the file was committed un-normalized. If the iterator
  1314. * points to a conflict entry, checks the "ours" version.
  1315. *
  1316. * @param dirCache
  1317. * iterator pointing to the current entry for the file in the
  1318. * index
  1319. * @return {@code true} if the file in the index is not binary and has CR/LF
  1320. * line endings, {@code false} otherwise
  1321. */
  1322. private boolean hasCrLfInIndex(DirCacheIterator dirCache) {
  1323. if (dirCache == null) {
  1324. return false;
  1325. }
  1326. // Read blob from index and check for CR/LF-delimited text.
  1327. DirCacheEntry entry = dirCache.getDirCacheEntry();
  1328. if ((entry.getRawMode() & FileMode.TYPE_MASK) == FileMode.TYPE_FILE) {
  1329. ObjectId blobId = entry.getObjectId();
  1330. if (entry.getStage() > 0
  1331. && entry.getStage() != DirCacheEntry.STAGE_2) {
  1332. blobId = null;
  1333. // Merge conflict: check ours (stage 2)
  1334. byte[] name = entry.getRawPath();
  1335. int i = 0;
  1336. while (!dirCache.eof()) {
  1337. dirCache.next(1);
  1338. i++;
  1339. entry = dirCache.getDirCacheEntry();
  1340. if (entry == null
  1341. || !Arrays.equals(name, entry.getRawPath())) {
  1342. break;
  1343. }
  1344. if (entry.getStage() == DirCacheEntry.STAGE_2) {
  1345. if ((entry.getRawMode()
  1346. & FileMode.TYPE_MASK) == FileMode.TYPE_FILE) {
  1347. blobId = entry.getObjectId();
  1348. }
  1349. break;
  1350. }
  1351. }
  1352. dirCache.back(i);
  1353. }
  1354. if (blobId != null) {
  1355. try (ObjectReader reader = repository.newObjectReader()) {
  1356. ObjectLoader loader = reader.open(blobId,
  1357. Constants.OBJ_BLOB);
  1358. try {
  1359. return RawText.isCrLfText(loader.getCachedBytes());
  1360. } catch (LargeObjectException e) {
  1361. try (InputStream in = loader.openStream()) {
  1362. return RawText.isCrLfText(in);
  1363. }
  1364. }
  1365. } catch (IOException e) {
  1366. // Ignore and return false below
  1367. }
  1368. }
  1369. }
  1370. return false;
  1371. }
  1372. private boolean isDirectoryIgnored(String pathRel) throws IOException {
  1373. final int pOff = 0 < pathOffset ? pathOffset - 1 : pathOffset;
  1374. final String base = TreeWalk.pathOf(this.path, 0, pOff);
  1375. final String pathAbs = concatPath(base, pathRel);
  1376. return isDirectoryIgnored(pathRel, pathAbs);
  1377. }
  1378. private boolean isDirectoryIgnored(String pathRel, String pathAbs)
  1379. throws IOException {
  1380. assert pathRel.length() == 0 || (pathRel.charAt(0) != '/'
  1381. && pathRel.charAt(pathRel.length() - 1) != '/');
  1382. assert pathAbs.length() == 0 || (pathAbs.charAt(0) != '/'
  1383. && pathAbs.charAt(pathAbs.length() - 1) != '/');
  1384. assert pathAbs.endsWith(pathRel);
  1385. Boolean ignored = state.directoryToIgnored.get(pathAbs);
  1386. if (ignored != null) {
  1387. return ignored.booleanValue();
  1388. }
  1389. final String parentRel = getParentPath(pathRel);
  1390. if (parentRel != null && isDirectoryIgnored(parentRel)) {
  1391. state.directoryToIgnored.put(pathAbs, Boolean.TRUE);
  1392. return true;
  1393. }
  1394. final IgnoreNode node = getIgnoreNode();
  1395. for (String p = pathRel; node != null
  1396. && !"".equals(p); p = getParentPath(p)) { //$NON-NLS-1$
  1397. ignored = node.checkIgnored(p, true);
  1398. if (ignored != null) {
  1399. state.directoryToIgnored.put(pathAbs, ignored);
  1400. return ignored.booleanValue();
  1401. }
  1402. }
  1403. if (!(this.parent instanceof WorkingTreeIterator)) {
  1404. state.directoryToIgnored.put(pathAbs, Boolean.FALSE);
  1405. return false;
  1406. }
  1407. final WorkingTreeIterator wtParent = (WorkingTreeIterator) this.parent;
  1408. final String parentRelPath = concatPath(
  1409. TreeWalk.pathOf(this.path, wtParent.pathOffset, pathOffset - 1),
  1410. pathRel);
  1411. assert concatPath(TreeWalk.pathOf(wtParent.path, 0,
  1412. Math.max(0, wtParent.pathOffset - 1)), parentRelPath)
  1413. .equals(pathAbs);
  1414. return wtParent.isDirectoryIgnored(parentRelPath, pathAbs);
  1415. }
  1416. private static String getParentPath(String path) {
  1417. final int slashIndex = path.lastIndexOf('/', path.length() - 2);
  1418. if (slashIndex > 0) {
  1419. return path.substring(path.charAt(0) == '/' ? 1 : 0, slashIndex);
  1420. }
  1421. return path.length() > 0 ? "" : null; //$NON-NLS-1$
  1422. }
  1423. private static String concatPath(String p1, String p2) {
  1424. return p1 + (p1.length() > 0 && p2.length() > 0 ? "/" : "") + p2; //$NON-NLS-1$ //$NON-NLS-2$
  1425. }
  1426. }