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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  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 java.io.ByteArrayInputStream;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.FileNotFoundException;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.nio.ByteBuffer;
  54. import java.nio.CharBuffer;
  55. import java.nio.charset.CharacterCodingException;
  56. import java.nio.charset.CharsetEncoder;
  57. import java.security.MessageDigest;
  58. import java.text.MessageFormat;
  59. import java.util.Arrays;
  60. import java.util.Collections;
  61. import java.util.Comparator;
  62. import org.eclipse.jgit.diff.RawText;
  63. import org.eclipse.jgit.dircache.DirCache;
  64. import org.eclipse.jgit.dircache.DirCacheEntry;
  65. import org.eclipse.jgit.dircache.DirCacheIterator;
  66. import org.eclipse.jgit.errors.CorruptObjectException;
  67. import org.eclipse.jgit.errors.NoWorkTreeException;
  68. import org.eclipse.jgit.ignore.IgnoreNode;
  69. import org.eclipse.jgit.ignore.IgnoreRule;
  70. import org.eclipse.jgit.internal.JGitText;
  71. import org.eclipse.jgit.lib.Constants;
  72. import org.eclipse.jgit.lib.CoreConfig;
  73. import org.eclipse.jgit.lib.CoreConfig.CheckStat;
  74. import org.eclipse.jgit.lib.FileMode;
  75. import org.eclipse.jgit.lib.ObjectId;
  76. import org.eclipse.jgit.lib.ObjectLoader;
  77. import org.eclipse.jgit.lib.ObjectReader;
  78. import org.eclipse.jgit.lib.Repository;
  79. import org.eclipse.jgit.submodule.SubmoduleWalk;
  80. import org.eclipse.jgit.util.FS;
  81. import org.eclipse.jgit.util.IO;
  82. import org.eclipse.jgit.util.io.EolCanonicalizingInputStream;
  83. /**
  84. * Walks a working directory tree as part of a {@link TreeWalk}.
  85. * <p>
  86. * Most applications will want to use the standard implementation of this
  87. * iterator, {@link FileTreeIterator}, as that does all IO through the standard
  88. * <code>java.io</code> package. Plugins for a Java based IDE may however wish
  89. * to create their own implementations of this class to allow traversal of the
  90. * IDE's project space, as well as benefit from any caching the IDE may have.
  91. *
  92. * @see FileTreeIterator
  93. */
  94. public abstract class WorkingTreeIterator extends AbstractTreeIterator {
  95. /** An empty entry array, suitable for {@link #init(Entry[])}. */
  96. protected static final Entry[] EOF = {};
  97. /** Size we perform file IO in if we have to read and hash a file. */
  98. static final int BUFFER_SIZE = 2048;
  99. /**
  100. * Maximum size of files which may be read fully into memory for performance
  101. * reasons.
  102. */
  103. private static final long MAXIMUM_FILE_SIZE_TO_READ_FULLY = 65536;
  104. /** Inherited state of this iterator, describing working tree, etc. */
  105. private final IteratorState state;
  106. /** The {@link #idBuffer()} for the current entry. */
  107. private byte[] contentId;
  108. /** Index within {@link #entries} that {@link #contentId} came from. */
  109. private int contentIdFromPtr;
  110. /** List of entries obtained from the subclass. */
  111. private Entry[] entries;
  112. /** Total number of entries in {@link #entries} that are valid. */
  113. private int entryCnt;
  114. /** Current position within {@link #entries}. */
  115. private int ptr;
  116. /** If there is a .gitignore file present, the parsed rules from it. */
  117. private IgnoreNode ignoreNode;
  118. /** Repository that is the root level being iterated over */
  119. protected Repository repository;
  120. /** Cached canonical length, initialized from {@link #idBuffer()} */
  121. private long canonLen = -1;
  122. /** The offset of the content id in {@link #idBuffer()} */
  123. private int contentIdOffset;
  124. /**
  125. * Create a new iterator with no parent.
  126. *
  127. * @param options
  128. * working tree options to be used
  129. */
  130. protected WorkingTreeIterator(WorkingTreeOptions options) {
  131. super();
  132. state = new IteratorState(options);
  133. }
  134. /**
  135. * Create a new iterator with no parent and a prefix.
  136. * <p>
  137. * The prefix path supplied is inserted in front of all paths generated by
  138. * this iterator. It is intended to be used when an iterator is being
  139. * created for a subsection of an overall repository and needs to be
  140. * combined with other iterators that are created to run over the entire
  141. * repository namespace.
  142. *
  143. * @param prefix
  144. * position of this iterator in the repository tree. The value
  145. * may be null or the empty string to indicate the prefix is the
  146. * root of the repository. A trailing slash ('/') is
  147. * automatically appended if the prefix does not end in '/'.
  148. * @param options
  149. * working tree options to be used
  150. */
  151. protected WorkingTreeIterator(final String prefix,
  152. WorkingTreeOptions options) {
  153. super(prefix);
  154. state = new IteratorState(options);
  155. }
  156. /**
  157. * Create an iterator for a subtree of an existing iterator.
  158. *
  159. * @param p
  160. * parent tree iterator.
  161. */
  162. protected WorkingTreeIterator(final WorkingTreeIterator p) {
  163. super(p);
  164. state = p.state;
  165. }
  166. /**
  167. * Initialize this iterator for the root level of a repository.
  168. * <p>
  169. * This method should only be invoked after calling {@link #init(Entry[])},
  170. * and only for the root iterator.
  171. *
  172. * @param repo
  173. * the repository.
  174. */
  175. protected void initRootIterator(Repository repo) {
  176. repository = repo;
  177. Entry entry;
  178. if (ignoreNode instanceof PerDirectoryIgnoreNode)
  179. entry = ((PerDirectoryIgnoreNode) ignoreNode).entry;
  180. else
  181. entry = null;
  182. ignoreNode = new RootIgnoreNode(entry, repo);
  183. }
  184. /**
  185. * Define the matching {@link DirCacheIterator}, to optimize ObjectIds.
  186. *
  187. * Once the DirCacheIterator has been set this iterator must only be
  188. * advanced by the TreeWalk that is supplied, as it assumes that itself and
  189. * the corresponding DirCacheIterator are positioned on the same file path
  190. * whenever {@link #idBuffer()} is invoked.
  191. *
  192. * @param walk
  193. * the walk that will be advancing this iterator.
  194. * @param treeId
  195. * index of the matching {@link DirCacheIterator}.
  196. */
  197. public void setDirCacheIterator(TreeWalk walk, int treeId) {
  198. state.walk = walk;
  199. state.dirCacheTree = treeId;
  200. }
  201. @Override
  202. public boolean hasId() {
  203. if (contentIdFromPtr == ptr)
  204. return true;
  205. return (mode & FileMode.TYPE_MASK) == FileMode.TYPE_FILE;
  206. }
  207. @Override
  208. public byte[] idBuffer() {
  209. if (contentIdFromPtr == ptr)
  210. return contentId;
  211. if (state.walk != null) {
  212. // If there is a matching DirCacheIterator, we can reuse
  213. // its idBuffer, but only if we appear to be clean against
  214. // the cached index information for the path.
  215. //
  216. DirCacheIterator i = state.walk.getTree(state.dirCacheTree,
  217. DirCacheIterator.class);
  218. if (i != null) {
  219. DirCacheEntry ent = i.getDirCacheEntry();
  220. if (ent != null && compareMetadata(ent) == MetadataDiff.EQUAL) {
  221. contentIdOffset = i.idOffset();
  222. contentIdFromPtr = ptr;
  223. return contentId = i.idBuffer();
  224. }
  225. contentIdOffset = 0;
  226. } else {
  227. contentIdOffset = 0;
  228. }
  229. }
  230. switch (mode & FileMode.TYPE_MASK) {
  231. case FileMode.TYPE_FILE:
  232. contentIdFromPtr = ptr;
  233. return contentId = idBufferBlob(entries[ptr]);
  234. case FileMode.TYPE_SYMLINK:
  235. // Java does not support symbolic links, so we should not
  236. // have reached this particular part of the walk code.
  237. //
  238. return zeroid;
  239. case FileMode.TYPE_GITLINK:
  240. contentIdFromPtr = ptr;
  241. return contentId = idSubmodule(entries[ptr]);
  242. }
  243. return zeroid;
  244. }
  245. /**
  246. * Get submodule id for given entry.
  247. *
  248. * @param e
  249. * @return non-null submodule id
  250. */
  251. protected byte[] idSubmodule(Entry e) {
  252. if (repository == null)
  253. return zeroid;
  254. File directory;
  255. try {
  256. directory = repository.getWorkTree();
  257. } catch (NoWorkTreeException nwte) {
  258. return zeroid;
  259. }
  260. return idSubmodule(directory, e);
  261. }
  262. /**
  263. * Get submodule id using the repository at the location of the entry
  264. * relative to the directory.
  265. *
  266. * @param directory
  267. * @param e
  268. * @return non-null submodule id
  269. */
  270. protected byte[] idSubmodule(File directory, Entry e) {
  271. final Repository submoduleRepo;
  272. try {
  273. submoduleRepo = SubmoduleWalk.getSubmoduleRepository(directory,
  274. e.getName());
  275. } catch (IOException exception) {
  276. return zeroid;
  277. }
  278. if (submoduleRepo == null)
  279. return zeroid;
  280. final ObjectId head;
  281. try {
  282. head = submoduleRepo.resolve(Constants.HEAD);
  283. } catch (IOException exception) {
  284. return zeroid;
  285. } finally {
  286. submoduleRepo.close();
  287. }
  288. if (head == null)
  289. return zeroid;
  290. final byte[] id = new byte[Constants.OBJECT_ID_LENGTH];
  291. head.copyRawTo(id, 0);
  292. return id;
  293. }
  294. private static final byte[] digits = { '0', '1', '2', '3', '4', '5', '6',
  295. '7', '8', '9' };
  296. private static final byte[] hblob = Constants
  297. .encodedTypeString(Constants.OBJ_BLOB);
  298. private byte[] idBufferBlob(final Entry e) {
  299. try {
  300. final InputStream is = e.openInputStream();
  301. if (is == null)
  302. return zeroid;
  303. try {
  304. state.initializeDigestAndReadBuffer();
  305. final long len = e.getLength();
  306. InputStream filteredIs = possiblyFilteredInputStream(e, is, len);
  307. return computeHash(filteredIs, canonLen);
  308. } finally {
  309. safeClose(is);
  310. }
  311. } catch (IOException err) {
  312. // Can't read the file? Don't report the failure either.
  313. return zeroid;
  314. }
  315. }
  316. private InputStream possiblyFilteredInputStream(final Entry e,
  317. final InputStream is, final long len) throws IOException {
  318. if (!mightNeedCleaning()) {
  319. canonLen = len;
  320. return is;
  321. }
  322. if (len <= MAXIMUM_FILE_SIZE_TO_READ_FULLY) {
  323. ByteBuffer rawbuf = IO.readWholeStream(is, (int) len);
  324. byte[] raw = rawbuf.array();
  325. int n = rawbuf.limit();
  326. if (!isBinary(raw, n)) {
  327. rawbuf = filterClean(raw, n);
  328. raw = rawbuf.array();
  329. n = rawbuf.limit();
  330. }
  331. canonLen = n;
  332. return new ByteArrayInputStream(raw, 0, n);
  333. }
  334. if (isBinary(e)) {
  335. canonLen = len;
  336. return is;
  337. }
  338. final InputStream lenIs = filterClean(e.openInputStream());
  339. try {
  340. canonLen = computeLength(lenIs);
  341. } finally {
  342. safeClose(lenIs);
  343. }
  344. return filterClean(is);
  345. }
  346. private static void safeClose(final InputStream in) {
  347. try {
  348. in.close();
  349. } catch (IOException err2) {
  350. // Suppress any error related to closing an input
  351. // stream. We don't care, we should not have any
  352. // outstanding data to flush or anything like that.
  353. }
  354. }
  355. private boolean mightNeedCleaning() {
  356. switch (getOptions().getAutoCRLF()) {
  357. case FALSE:
  358. default:
  359. return false;
  360. case TRUE:
  361. case INPUT:
  362. return true;
  363. }
  364. }
  365. private boolean isBinary(byte[] content, int sz) {
  366. return RawText.isBinary(content, sz);
  367. }
  368. private boolean isBinary(Entry entry) throws IOException {
  369. InputStream in = entry.openInputStream();
  370. try {
  371. return RawText.isBinary(in);
  372. } finally {
  373. safeClose(in);
  374. }
  375. }
  376. private ByteBuffer filterClean(byte[] src, int n)
  377. throws IOException {
  378. InputStream in = new ByteArrayInputStream(src);
  379. try {
  380. return IO.readWholeStream(filterClean(in), n);
  381. } finally {
  382. safeClose(in);
  383. }
  384. }
  385. private InputStream filterClean(InputStream in) {
  386. return new EolCanonicalizingInputStream(in, true);
  387. }
  388. /**
  389. * Returns the working tree options used by this iterator.
  390. *
  391. * @return working tree options
  392. */
  393. public WorkingTreeOptions getOptions() {
  394. return state.options;
  395. }
  396. @Override
  397. public int idOffset() {
  398. return contentIdOffset;
  399. }
  400. @Override
  401. public void reset() {
  402. if (!first()) {
  403. ptr = 0;
  404. if (!eof())
  405. parseEntry();
  406. }
  407. }
  408. @Override
  409. public boolean first() {
  410. return ptr == 0;
  411. }
  412. @Override
  413. public boolean eof() {
  414. return ptr == entryCnt;
  415. }
  416. @Override
  417. public void next(final int delta) throws CorruptObjectException {
  418. ptr += delta;
  419. if (!eof()) {
  420. canonLen = -1;
  421. parseEntry();
  422. }
  423. }
  424. @Override
  425. public void back(final int delta) throws CorruptObjectException {
  426. ptr -= delta;
  427. parseEntry();
  428. }
  429. private void parseEntry() {
  430. final Entry e = entries[ptr];
  431. mode = e.getMode().getBits();
  432. final int nameLen = e.encodedNameLen;
  433. ensurePathCapacity(pathOffset + nameLen, pathOffset);
  434. System.arraycopy(e.encodedName, 0, path, pathOffset, nameLen);
  435. pathLen = pathOffset + nameLen;
  436. }
  437. /**
  438. * Get the raw byte length of this entry.
  439. *
  440. * @return size of this file, in bytes.
  441. */
  442. public long getEntryLength() {
  443. return current().getLength();
  444. }
  445. /**
  446. * Get the filtered input length of this entry
  447. *
  448. * @return size of the content, in bytes
  449. * @throws IOException
  450. */
  451. public long getEntryContentLength() throws IOException {
  452. if (canonLen == -1) {
  453. long rawLen = getEntryLength();
  454. if (rawLen == 0)
  455. canonLen = 0;
  456. InputStream is = current().openInputStream();
  457. try {
  458. // canonLen gets updated here
  459. possiblyFilteredInputStream(current(), is, current()
  460. .getLength());
  461. } finally {
  462. safeClose(is);
  463. }
  464. }
  465. return canonLen;
  466. }
  467. /**
  468. * Get the last modified time of this entry.
  469. *
  470. * @return last modified time of this file, in milliseconds since the epoch
  471. * (Jan 1, 1970 UTC).
  472. */
  473. public long getEntryLastModified() {
  474. return current().getLastModified();
  475. }
  476. /**
  477. * Obtain an input stream to read the file content.
  478. * <p>
  479. * Efficient implementations are not required. The caller will usually
  480. * obtain the stream only once per entry, if at all.
  481. * <p>
  482. * The input stream should not use buffering if the implementation can avoid
  483. * it. The caller will buffer as necessary to perform efficient block IO
  484. * operations.
  485. * <p>
  486. * The caller will close the stream once complete.
  487. *
  488. * @return a stream to read from the file.
  489. * @throws IOException
  490. * the file could not be opened for reading.
  491. */
  492. public InputStream openEntryStream() throws IOException {
  493. InputStream rawis = current().openInputStream();
  494. if (mightNeedCleaning())
  495. return filterClean(rawis);
  496. else
  497. return rawis;
  498. }
  499. /**
  500. * Determine if the current entry path is ignored by an ignore rule.
  501. *
  502. * @return true if the entry was ignored by an ignore rule file.
  503. * @throws IOException
  504. * a relevant ignore rule file exists but cannot be read.
  505. */
  506. public boolean isEntryIgnored() throws IOException {
  507. return isEntryIgnored(pathLen);
  508. }
  509. /**
  510. * Determine if the entry path is ignored by an ignore rule.
  511. *
  512. * @param pLen
  513. * the length of the path in the path buffer.
  514. * @return true if the entry is ignored by an ignore rule.
  515. * @throws IOException
  516. * a relevant ignore rule file exists but cannot be read.
  517. */
  518. protected boolean isEntryIgnored(final int pLen) throws IOException {
  519. IgnoreNode rules = getIgnoreNode();
  520. if (rules != null) {
  521. // The ignore code wants path to start with a '/' if possible.
  522. // If we have the '/' in our path buffer because we are inside
  523. // a subdirectory include it in the range we convert to string.
  524. //
  525. int pOff = pathOffset;
  526. if (0 < pOff)
  527. pOff--;
  528. String p = TreeWalk.pathOf(path, pOff, pLen);
  529. switch (rules.isIgnored(p, FileMode.TREE.equals(mode))) {
  530. case IGNORED:
  531. return true;
  532. case NOT_IGNORED:
  533. return false;
  534. case CHECK_PARENT:
  535. break;
  536. }
  537. }
  538. if (parent instanceof WorkingTreeIterator)
  539. return ((WorkingTreeIterator) parent).isEntryIgnored(pLen);
  540. return false;
  541. }
  542. private IgnoreNode getIgnoreNode() throws IOException {
  543. if (ignoreNode instanceof PerDirectoryIgnoreNode)
  544. ignoreNode = ((PerDirectoryIgnoreNode) ignoreNode).load();
  545. return ignoreNode;
  546. }
  547. private static final Comparator<Entry> ENTRY_CMP = new Comparator<Entry>() {
  548. public int compare(final Entry o1, final Entry o2) {
  549. final byte[] a = o1.encodedName;
  550. final byte[] b = o2.encodedName;
  551. final int aLen = o1.encodedNameLen;
  552. final int bLen = o2.encodedNameLen;
  553. int cPos;
  554. for (cPos = 0; cPos < aLen && cPos < bLen; cPos++) {
  555. final int cmp = (a[cPos] & 0xff) - (b[cPos] & 0xff);
  556. if (cmp != 0)
  557. return cmp;
  558. }
  559. if (cPos < aLen)
  560. return (a[cPos] & 0xff) - lastPathChar(o2);
  561. if (cPos < bLen)
  562. return lastPathChar(o1) - (b[cPos] & 0xff);
  563. return lastPathChar(o1) - lastPathChar(o2);
  564. }
  565. };
  566. static int lastPathChar(final Entry e) {
  567. return e.getMode() == FileMode.TREE ? '/' : '\0';
  568. }
  569. /**
  570. * Constructor helper.
  571. *
  572. * @param list
  573. * files in the subtree of the work tree this iterator operates
  574. * on
  575. */
  576. protected void init(final Entry[] list) {
  577. // Filter out nulls, . and .. as these are not valid tree entries,
  578. // also cache the encoded forms of the path names for efficient use
  579. // later on during sorting and iteration.
  580. //
  581. entries = list;
  582. int i, o;
  583. final CharsetEncoder nameEncoder = state.nameEncoder;
  584. for (i = 0, o = 0; i < entries.length; i++) {
  585. final Entry e = entries[i];
  586. if (e == null)
  587. continue;
  588. final String name = e.getName();
  589. if (".".equals(name) || "..".equals(name)) //$NON-NLS-1$ //$NON-NLS-2$
  590. continue;
  591. if (Constants.DOT_GIT.equals(name))
  592. continue;
  593. if (Constants.DOT_GIT_IGNORE.equals(name))
  594. ignoreNode = new PerDirectoryIgnoreNode(e);
  595. if (i != o)
  596. entries[o] = e;
  597. e.encodeName(nameEncoder);
  598. o++;
  599. }
  600. entryCnt = o;
  601. Arrays.sort(entries, 0, entryCnt, ENTRY_CMP);
  602. contentIdFromPtr = -1;
  603. ptr = 0;
  604. if (!eof())
  605. parseEntry();
  606. }
  607. /**
  608. * Obtain the current entry from this iterator.
  609. *
  610. * @return the currently selected entry.
  611. */
  612. protected Entry current() {
  613. return entries[ptr];
  614. }
  615. /**
  616. * The result of a metadata-comparison between the current entry and a
  617. * {@link DirCacheEntry}
  618. */
  619. public enum MetadataDiff {
  620. /**
  621. * The entries are equal by metaData (mode, length,
  622. * modification-timestamp) or the <code>assumeValid</code> attribute of
  623. * the index entry is set
  624. */
  625. EQUAL,
  626. /**
  627. * The entries are not equal by metaData (mode, length) or the
  628. * <code>isUpdateNeeded</code> attribute of the index entry is set
  629. */
  630. DIFFER_BY_METADATA,
  631. /** index entry is smudged - can't use that entry for comparison */
  632. SMUDGED,
  633. /**
  634. * The entries are equal by metaData (mode, length) but differ by
  635. * modification-timestamp.
  636. */
  637. DIFFER_BY_TIMESTAMP
  638. }
  639. /**
  640. * Is the file mode of the current entry different than the given raw mode?
  641. *
  642. * @param rawMode
  643. * @return true if different, false otherwise
  644. */
  645. public boolean isModeDifferent(final int rawMode) {
  646. // Determine difference in mode-bits of file and index-entry. In the
  647. // bitwise presentation of modeDiff we'll have a '1' when the two modes
  648. // differ at this position.
  649. int modeDiff = getEntryRawMode() ^ rawMode;
  650. if (modeDiff == 0)
  651. return false;
  652. // Do not rely on filemode differences in case of symbolic links
  653. if (FileMode.SYMLINK.equals(rawMode))
  654. return false;
  655. // Ignore the executable file bits if WorkingTreeOptions tell me to
  656. // do so. Ignoring is done by setting the bits representing a
  657. // EXECUTABLE_FILE to '0' in modeDiff
  658. if (!state.options.isFileMode())
  659. modeDiff &= ~FileMode.EXECUTABLE_FILE.getBits();
  660. return modeDiff != 0;
  661. }
  662. /**
  663. * Compare the metadata (mode, length, modification-timestamp) of the
  664. * current entry and a {@link DirCacheEntry}
  665. *
  666. * @param entry
  667. * the {@link DirCacheEntry} to compare with
  668. * @return a {@link MetadataDiff} which tells whether and how the entries
  669. * metadata differ
  670. */
  671. public MetadataDiff compareMetadata(DirCacheEntry entry) {
  672. if (entry.isAssumeValid())
  673. return MetadataDiff.EQUAL;
  674. if (entry.isUpdateNeeded())
  675. return MetadataDiff.DIFFER_BY_METADATA;
  676. if (!entry.isSmudged() && entry.getLength() != (int) getEntryLength())
  677. return MetadataDiff.DIFFER_BY_METADATA;
  678. if (isModeDifferent(entry.getRawMode()))
  679. return MetadataDiff.DIFFER_BY_METADATA;
  680. // Git under windows only stores seconds so we round the timestamp
  681. // Java gives us if it looks like the timestamp in index is seconds
  682. // only. Otherwise we compare the timestamp at millisecond precision,
  683. // unless core.checkstat is set to "minimal", in which case we only
  684. // compare the whole second part.
  685. long cacheLastModified = entry.getLastModified();
  686. long fileLastModified = getEntryLastModified();
  687. long lastModifiedMillis = fileLastModified % 1000;
  688. long cacheMillis = cacheLastModified % 1000;
  689. if (getOptions().getCheckStat() == CheckStat.MINIMAL) {
  690. fileLastModified = fileLastModified - lastModifiedMillis;
  691. cacheLastModified = cacheLastModified - cacheMillis;
  692. } else if (cacheMillis == 0)
  693. fileLastModified = fileLastModified - lastModifiedMillis;
  694. // Some Java version on Linux return whole seconds only even when
  695. // the file systems supports more precision.
  696. else if (lastModifiedMillis == 0)
  697. cacheLastModified = cacheLastModified - cacheMillis;
  698. if (fileLastModified != cacheLastModified)
  699. return MetadataDiff.DIFFER_BY_TIMESTAMP;
  700. else if (!entry.isSmudged())
  701. // The file is clean when you look at timestamps.
  702. return MetadataDiff.EQUAL;
  703. else
  704. return MetadataDiff.SMUDGED;
  705. }
  706. /**
  707. * Checks whether this entry differs from a given entry from the
  708. * {@link DirCache}.
  709. *
  710. * File status information is used and if status is same we consider the
  711. * file identical to the state in the working directory. Native git uses
  712. * more stat fields than we have accessible in Java.
  713. *
  714. * @param entry
  715. * the entry from the dircache we want to compare against
  716. * @param forceContentCheck
  717. * True if the actual file content should be checked if
  718. * modification time differs.
  719. * @param reader
  720. * access to repository objects if necessary.
  721. * @return true if content is most likely different.
  722. * @since 3.2
  723. */
  724. public boolean isModified(DirCacheEntry entry, boolean forceContentCheck,
  725. ObjectReader reader) {
  726. MetadataDiff diff = compareMetadata(entry);
  727. switch (diff) {
  728. case DIFFER_BY_TIMESTAMP:
  729. if (forceContentCheck)
  730. // But we are told to look at content even though timestamps
  731. // tell us about modification
  732. return contentCheck(entry, reader);
  733. else
  734. // We are told to assume a modification if timestamps differs
  735. return true;
  736. case SMUDGED:
  737. // The file is clean by timestamps but the entry was smudged.
  738. // Lets do a content check
  739. return contentCheck(entry, reader);
  740. case EQUAL:
  741. return false;
  742. case DIFFER_BY_METADATA:
  743. return true;
  744. default:
  745. throw new IllegalStateException(MessageFormat.format(
  746. JGitText.get().unexpectedCompareResult, diff.name()));
  747. }
  748. }
  749. /**
  750. * Checks whether this entry differs from a given entry from the
  751. * {@link DirCache}.
  752. *
  753. * File status information is used and if status is same we consider the
  754. * file identical to the state in the working directory. Native git uses
  755. * more stat fields than we have accessible in Java.
  756. *
  757. * @param entry
  758. * the entry from the dircache we want to compare against
  759. * @param forceContentCheck
  760. * True if the actual file content should be checked if
  761. * modification time differs.
  762. * @return true if content is most likely different.
  763. * @deprecated Use {@link #isModified(DirCacheEntry, boolean, ObjectReader)}
  764. */
  765. public boolean isModified(DirCacheEntry entry, boolean forceContentCheck) {
  766. return isModified(entry, false, null);
  767. }
  768. /**
  769. * Get the file mode to use for the current entry when it is to be updated
  770. * in the index.
  771. *
  772. * @param indexIter
  773. * {@link DirCacheIterator} positioned at the same entry as this
  774. * iterator or null if no {@link DirCacheIterator} is available
  775. * at this iterator's current entry
  776. * @return index file mode
  777. */
  778. public FileMode getIndexFileMode(final DirCacheIterator indexIter) {
  779. final FileMode wtMode = getEntryFileMode();
  780. if (indexIter == null)
  781. return wtMode;
  782. if (getOptions().isFileMode())
  783. return wtMode;
  784. final FileMode iMode = indexIter.getEntryFileMode();
  785. if (FileMode.REGULAR_FILE == wtMode
  786. && FileMode.EXECUTABLE_FILE == iMode)
  787. return iMode;
  788. if (FileMode.EXECUTABLE_FILE == wtMode
  789. && FileMode.REGULAR_FILE == iMode)
  790. return iMode;
  791. return wtMode;
  792. }
  793. /**
  794. * Compares the entries content with the content in the filesystem.
  795. * Unsmudges the entry when it is detected that it is clean.
  796. *
  797. * @param entry
  798. * the entry to be checked
  799. * @param reader
  800. * acccess to repository data if necessary
  801. * @return <code>true</code> if the content doesn't match,
  802. * <code>false</code> if it matches
  803. */
  804. private boolean contentCheck(DirCacheEntry entry, ObjectReader reader) {
  805. if (getEntryObjectId().equals(entry.getObjectId())) {
  806. // Content has not changed
  807. // We know the entry can't be racily clean because it's still clean.
  808. // Therefore we unsmudge the entry!
  809. // If by any chance we now unsmudge although we are still in the
  810. // same time-slot as the last modification to the index file the
  811. // next index write operation will smudge again.
  812. // Caution: we are unsmudging just by setting the length of the
  813. // in-memory entry object. It's the callers task to detect that we
  814. // have modified the entry and to persist the modified index.
  815. entry.setLength((int) getEntryLength());
  816. return false;
  817. } else {
  818. // Content differs: that's a real change, perhaps
  819. if (reader == null) // deprecated use, do no further checks
  820. return true;
  821. switch (getOptions().getAutoCRLF()) {
  822. case INPUT:
  823. case TRUE:
  824. InputStream dcIn = null;
  825. try {
  826. ObjectLoader loader = reader.open(entry.getObjectId());
  827. if (loader == null)
  828. return true;
  829. // We need to compute the length, but only if it is not
  830. // a binary stream.
  831. dcIn = new EolCanonicalizingInputStream(
  832. loader.openStream(), true, true /* abort if binary */);
  833. long dcInLen;
  834. try {
  835. dcInLen = computeLength(dcIn);
  836. } catch (EolCanonicalizingInputStream.IsBinaryException e) {
  837. // ok, we know it's different so unsmudge the entry
  838. entry.setLength(entry.getLength());
  839. return true;
  840. } finally {
  841. dcIn.close();
  842. }
  843. dcIn = new EolCanonicalizingInputStream(
  844. loader.openStream(), true);
  845. byte[] autoCrLfHash = computeHash(dcIn, dcInLen);
  846. boolean changed = getEntryObjectId().compareTo(
  847. autoCrLfHash, 0) != 0;
  848. if (!changed) {
  849. // Update the index with the eol'ed hash, so we can
  850. // detect the no-change faster next time
  851. entry.setObjectIdFromRaw(autoCrLfHash, 0);
  852. }
  853. // Ok, we know whether it has changed, so unsmudge the
  854. // dirache entry
  855. entry.setLength(loader.getSize());
  856. return changed;
  857. } catch (IOException e) {
  858. return true;
  859. } finally {
  860. if (dcIn != null)
  861. try {
  862. dcIn.close();
  863. } catch (IOException e) {
  864. // empty
  865. }
  866. }
  867. case FALSE:
  868. // Ok, we know it's different so unsmudge the dircache entry
  869. try {
  870. ObjectLoader loader = reader.open(entry.getObjectId());
  871. if (loader != null)
  872. entry.setLength((int) loader.getSize());
  873. } catch (IOException e) {
  874. // panic, no, but don't unsmudge
  875. }
  876. break;
  877. }
  878. return true;
  879. }
  880. }
  881. private long computeLength(InputStream in) throws IOException {
  882. // Since we only care about the length, use skip. The stream
  883. // may be able to more efficiently wade through its data.
  884. //
  885. long length = 0;
  886. for (;;) {
  887. long n = in.skip(1 << 20);
  888. if (n <= 0)
  889. break;
  890. length += n;
  891. }
  892. return length;
  893. }
  894. private byte[] computeHash(InputStream in, long length) throws IOException {
  895. final MessageDigest contentDigest = state.contentDigest;
  896. final byte[] contentReadBuffer = state.contentReadBuffer;
  897. contentDigest.reset();
  898. contentDigest.update(hblob);
  899. contentDigest.update((byte) ' ');
  900. long sz = length;
  901. if (sz == 0) {
  902. contentDigest.update((byte) '0');
  903. } else {
  904. final int bufn = contentReadBuffer.length;
  905. int p = bufn;
  906. do {
  907. contentReadBuffer[--p] = digits[(int) (sz % 10)];
  908. sz /= 10;
  909. } while (sz > 0);
  910. contentDigest.update(contentReadBuffer, p, bufn - p);
  911. }
  912. contentDigest.update((byte) 0);
  913. for (;;) {
  914. final int r = in.read(contentReadBuffer);
  915. if (r <= 0)
  916. break;
  917. contentDigest.update(contentReadBuffer, 0, r);
  918. sz += r;
  919. }
  920. if (sz != length)
  921. return zeroid;
  922. return contentDigest.digest();
  923. }
  924. /** A single entry within a working directory tree. */
  925. protected static abstract class Entry {
  926. byte[] encodedName;
  927. int encodedNameLen;
  928. void encodeName(final CharsetEncoder enc) {
  929. final ByteBuffer b;
  930. try {
  931. b = enc.encode(CharBuffer.wrap(getName()));
  932. } catch (CharacterCodingException e) {
  933. // This should so never happen.
  934. throw new RuntimeException(MessageFormat.format(
  935. JGitText.get().unencodeableFile, getName()));
  936. }
  937. encodedNameLen = b.limit();
  938. if (b.hasArray() && b.arrayOffset() == 0)
  939. encodedName = b.array();
  940. else
  941. b.get(encodedName = new byte[encodedNameLen]);
  942. }
  943. public String toString() {
  944. return getMode().toString() + " " + getName(); //$NON-NLS-1$
  945. }
  946. /**
  947. * Get the type of this entry.
  948. * <p>
  949. * <b>Note: Efficient implementation required.</b>
  950. * <p>
  951. * The implementation of this method must be efficient. If a subclass
  952. * needs to compute the value they should cache the reference within an
  953. * instance member instead.
  954. *
  955. * @return a file mode constant from {@link FileMode}.
  956. */
  957. public abstract FileMode getMode();
  958. /**
  959. * Get the byte length of this entry.
  960. * <p>
  961. * <b>Note: Efficient implementation required.</b>
  962. * <p>
  963. * The implementation of this method must be efficient. If a subclass
  964. * needs to compute the value they should cache the reference within an
  965. * instance member instead.
  966. *
  967. * @return size of this file, in bytes.
  968. */
  969. public abstract long getLength();
  970. /**
  971. * Get the last modified time of this entry.
  972. * <p>
  973. * <b>Note: Efficient implementation required.</b>
  974. * <p>
  975. * The implementation of this method must be efficient. If a subclass
  976. * needs to compute the value they should cache the reference within an
  977. * instance member instead.
  978. *
  979. * @return time since the epoch (in ms) of the last change.
  980. */
  981. public abstract long getLastModified();
  982. /**
  983. * Get the name of this entry within its directory.
  984. * <p>
  985. * Efficient implementations are not required. The caller will obtain
  986. * the name only once and cache it once obtained.
  987. *
  988. * @return name of the entry.
  989. */
  990. public abstract String getName();
  991. /**
  992. * Obtain an input stream to read the file content.
  993. * <p>
  994. * Efficient implementations are not required. The caller will usually
  995. * obtain the stream only once per entry, if at all.
  996. * <p>
  997. * The input stream should not use buffering if the implementation can
  998. * avoid it. The caller will buffer as necessary to perform efficient
  999. * block IO operations.
  1000. * <p>
  1001. * The caller will close the stream once complete.
  1002. *
  1003. * @return a stream to read from the file.
  1004. * @throws IOException
  1005. * the file could not be opened for reading.
  1006. */
  1007. public abstract InputStream openInputStream() throws IOException;
  1008. }
  1009. /** Magic type indicating we know rules exist, but they aren't loaded. */
  1010. private static class PerDirectoryIgnoreNode extends IgnoreNode {
  1011. final Entry entry;
  1012. PerDirectoryIgnoreNode(Entry entry) {
  1013. super(Collections.<IgnoreRule> emptyList());
  1014. this.entry = entry;
  1015. }
  1016. IgnoreNode load() throws IOException {
  1017. IgnoreNode r = new IgnoreNode();
  1018. InputStream in = entry.openInputStream();
  1019. try {
  1020. r.parse(in);
  1021. } finally {
  1022. in.close();
  1023. }
  1024. return r.getRules().isEmpty() ? null : r;
  1025. }
  1026. }
  1027. /** Magic type indicating there may be rules for the top level. */
  1028. private static class RootIgnoreNode extends PerDirectoryIgnoreNode {
  1029. final Repository repository;
  1030. RootIgnoreNode(Entry entry, Repository repository) {
  1031. super(entry);
  1032. this.repository = repository;
  1033. }
  1034. @Override
  1035. IgnoreNode load() throws IOException {
  1036. IgnoreNode r;
  1037. if (entry != null) {
  1038. r = super.load();
  1039. if (r == null)
  1040. r = new IgnoreNode();
  1041. } else {
  1042. r = new IgnoreNode();
  1043. }
  1044. FS fs = repository.getFS();
  1045. String path = repository.getConfig().get(CoreConfig.KEY)
  1046. .getExcludesFile();
  1047. if (path != null) {
  1048. File excludesfile;
  1049. if (path.startsWith("~/")) //$NON-NLS-1$
  1050. excludesfile = fs.resolve(fs.userHome(), path.substring(2));
  1051. else
  1052. excludesfile = fs.resolve(null, path);
  1053. loadRulesFromFile(r, excludesfile);
  1054. }
  1055. File exclude = fs.resolve(repository.getDirectory(),
  1056. Constants.INFO_EXCLUDE);
  1057. loadRulesFromFile(r, exclude);
  1058. return r.getRules().isEmpty() ? null : r;
  1059. }
  1060. private void loadRulesFromFile(IgnoreNode r, File exclude)
  1061. throws FileNotFoundException, IOException {
  1062. if (exclude.exists()) {
  1063. FileInputStream in = new FileInputStream(exclude);
  1064. try {
  1065. r.parse(in);
  1066. } finally {
  1067. in.close();
  1068. }
  1069. }
  1070. }
  1071. }
  1072. private static final class IteratorState {
  1073. /** Options used to process the working tree. */
  1074. final WorkingTreeOptions options;
  1075. /** File name character encoder. */
  1076. final CharsetEncoder nameEncoder;
  1077. /** Digest computer for {@link #contentId} computations. */
  1078. MessageDigest contentDigest;
  1079. /** Buffer used to perform {@link #contentId} computations. */
  1080. byte[] contentReadBuffer;
  1081. /** TreeWalk with a (supposedly) matching DirCacheIterator. */
  1082. TreeWalk walk;
  1083. /** Position of the matching {@link DirCacheIterator}. */
  1084. int dirCacheTree;
  1085. IteratorState(WorkingTreeOptions options) {
  1086. this.options = options;
  1087. this.nameEncoder = Constants.CHARSET.newEncoder();
  1088. }
  1089. void initializeDigestAndReadBuffer() {
  1090. if (contentDigest == null) {
  1091. contentDigest = Constants.newMessageDigest();
  1092. contentReadBuffer = new byte[BUFFER_SIZE];
  1093. }
  1094. }
  1095. }
  1096. }