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

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