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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.treewalk;
  46. import java.io.ByteArrayInputStream;
  47. import java.io.File;
  48. import java.io.FileInputStream;
  49. import java.io.FileNotFoundException;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.nio.ByteBuffer;
  53. import java.nio.CharBuffer;
  54. import java.nio.charset.CharacterCodingException;
  55. import java.nio.charset.CharsetEncoder;
  56. import java.security.MessageDigest;
  57. import java.text.MessageFormat;
  58. import java.util.Arrays;
  59. import java.util.Collections;
  60. import java.util.Comparator;
  61. import org.eclipse.jgit.JGitText;
  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.ignore.IgnoreNode;
  68. import org.eclipse.jgit.ignore.IgnoreRule;
  69. import org.eclipse.jgit.lib.Constants;
  70. import org.eclipse.jgit.lib.CoreConfig;
  71. import org.eclipse.jgit.lib.FileMode;
  72. import org.eclipse.jgit.lib.Repository;
  73. import org.eclipse.jgit.util.FS;
  74. import org.eclipse.jgit.util.IO;
  75. import org.eclipse.jgit.util.io.EolCanonicalizingInputStream;
  76. /**
  77. * Walks a working directory tree as part of a {@link TreeWalk}.
  78. * <p>
  79. * Most applications will want to use the standard implementation of this
  80. * iterator, {@link FileTreeIterator}, as that does all IO through the standard
  81. * <code>java.io</code> package. Plugins for a Java based IDE may however wish
  82. * to create their own implementations of this class to allow traversal of the
  83. * IDE's project space, as well as benefit from any caching the IDE may have.
  84. *
  85. * @see FileTreeIterator
  86. */
  87. public abstract class WorkingTreeIterator extends AbstractTreeIterator {
  88. /** An empty entry array, suitable for {@link #init(Entry[])}. */
  89. protected static final Entry[] EOF = {};
  90. /** Size we perform file IO in if we have to read and hash a file. */
  91. static final int BUFFER_SIZE = 2048;
  92. /**
  93. * Maximum size of files which may be read fully into memory for performance
  94. * reasons.
  95. */
  96. private static final long MAXIMUM_FILE_SIZE_TO_READ_FULLY = 65536;
  97. /** Inherited state of this iterator, describing working tree, etc. */
  98. private final IteratorState state;
  99. /** The {@link #idBuffer()} for the current entry. */
  100. private byte[] contentId;
  101. /** Index within {@link #entries} that {@link #contentId} came from. */
  102. private int contentIdFromPtr;
  103. /** List of entries obtained from the subclass. */
  104. private Entry[] entries;
  105. /** Total number of entries in {@link #entries} that are valid. */
  106. private int entryCnt;
  107. /** Current position within {@link #entries}. */
  108. private int ptr;
  109. /** If there is a .gitignore file present, the parsed rules from it. */
  110. private IgnoreNode ignoreNode;
  111. /**
  112. * Create a new iterator with no parent.
  113. *
  114. * @param options
  115. * working tree options to be used
  116. */
  117. protected WorkingTreeIterator(WorkingTreeOptions options) {
  118. super();
  119. state = new IteratorState(options);
  120. }
  121. /**
  122. * Create a new iterator with no parent and a prefix.
  123. * <p>
  124. * The prefix path supplied is inserted in front of all paths generated by
  125. * this iterator. It is intended to be used when an iterator is being
  126. * created for a subsection of an overall repository and needs to be
  127. * combined with other iterators that are created to run over the entire
  128. * repository namespace.
  129. *
  130. * @param prefix
  131. * position of this iterator in the repository tree. The value
  132. * may be null or the empty string to indicate the prefix is the
  133. * root of the repository. A trailing slash ('/') is
  134. * automatically appended if the prefix does not end in '/'.
  135. * @param options
  136. * working tree options to be used
  137. */
  138. protected WorkingTreeIterator(final String prefix,
  139. WorkingTreeOptions options) {
  140. super(prefix);
  141. state = new IteratorState(options);
  142. }
  143. /**
  144. * Create an iterator for a subtree of an existing iterator.
  145. *
  146. * @param p
  147. * parent tree iterator.
  148. */
  149. protected WorkingTreeIterator(final WorkingTreeIterator p) {
  150. super(p);
  151. state = p.state;
  152. }
  153. /**
  154. * Initialize this iterator for the root level of a repository.
  155. * <p>
  156. * This method should only be invoked after calling {@link #init(Entry[])},
  157. * and only for the root iterator.
  158. *
  159. * @param repo
  160. * the repository.
  161. */
  162. protected void initRootIterator(Repository repo) {
  163. Entry entry;
  164. if (ignoreNode instanceof PerDirectoryIgnoreNode)
  165. entry = ((PerDirectoryIgnoreNode) ignoreNode).entry;
  166. else
  167. entry = null;
  168. ignoreNode = new RootIgnoreNode(entry, repo);
  169. }
  170. /**
  171. * Define the matching {@link DirCacheIterator}, to optimize ObjectIds.
  172. *
  173. * Once the DirCacheIterator has been set this iterator must only be
  174. * advanced by the TreeWalk that is supplied, as it assumes that itself and
  175. * the corresponding DirCacheIterator are positioned on the same file path
  176. * whenever {@link #idBuffer()} is invoked.
  177. *
  178. * @param walk
  179. * the walk that will be advancing this iterator.
  180. * @param treeId
  181. * index of the matching {@link DirCacheIterator}.
  182. */
  183. public void setDirCacheIterator(TreeWalk walk, int treeId) {
  184. state.walk = walk;
  185. state.dirCacheTree = treeId;
  186. }
  187. @Override
  188. public boolean hasId() {
  189. if (contentIdFromPtr == ptr)
  190. return true;
  191. return (mode & FileMode.TYPE_MASK) == FileMode.TYPE_FILE;
  192. }
  193. @Override
  194. public byte[] idBuffer() {
  195. if (contentIdFromPtr == ptr)
  196. return contentId;
  197. if (state.walk != null) {
  198. // If there is a matching DirCacheIterator, we can reuse
  199. // its idBuffer, but only if we appear to be clean against
  200. // the cached index information for the path.
  201. //
  202. DirCacheIterator i = state.walk.getTree(state.dirCacheTree,
  203. DirCacheIterator.class);
  204. if (i != null) {
  205. DirCacheEntry ent = i.getDirCacheEntry();
  206. if (ent != null && compareMetadata(ent) == MetadataDiff.EQUAL)
  207. return i.idBuffer();
  208. }
  209. }
  210. switch (mode & FileMode.TYPE_MASK) {
  211. case FileMode.TYPE_FILE:
  212. contentIdFromPtr = ptr;
  213. return contentId = idBufferBlob(entries[ptr]);
  214. case FileMode.TYPE_SYMLINK:
  215. // Java does not support symbolic links, so we should not
  216. // have reached this particular part of the walk code.
  217. //
  218. return zeroid;
  219. case FileMode.TYPE_GITLINK:
  220. // TODO: Support obtaining current HEAD SHA-1 from nested repository
  221. //
  222. return zeroid;
  223. }
  224. return zeroid;
  225. }
  226. private static final byte[] digits = { '0', '1', '2', '3', '4', '5', '6',
  227. '7', '8', '9' };
  228. private static final byte[] hblob = Constants
  229. .encodedTypeString(Constants.OBJ_BLOB);
  230. private byte[] idBufferBlob(final Entry e) {
  231. try {
  232. final InputStream is = e.openInputStream();
  233. if (is == null)
  234. return zeroid;
  235. try {
  236. state.initializeDigestAndReadBuffer();
  237. final long len = e.getLength();
  238. if (!mightNeedCleaning())
  239. return computeHash(is, len);
  240. if (len <= MAXIMUM_FILE_SIZE_TO_READ_FULLY) {
  241. ByteBuffer rawbuf = IO.readWholeStream(is, (int) len);
  242. byte[] raw = rawbuf.array();
  243. int n = rawbuf.limit();
  244. if (!isBinary(raw, n)) {
  245. rawbuf = filterClean(raw, n);
  246. raw = rawbuf.array();
  247. n = rawbuf.limit();
  248. }
  249. return computeHash(new ByteArrayInputStream(raw, 0, n), n);
  250. }
  251. if (isBinary(e))
  252. return computeHash(is, len);
  253. final long canonLen;
  254. final InputStream lenIs = filterClean(e.openInputStream());
  255. try {
  256. canonLen = computeLength(lenIs);
  257. } finally {
  258. safeClose(lenIs);
  259. }
  260. return computeHash(filterClean(is), canonLen);
  261. } finally {
  262. safeClose(is);
  263. }
  264. } catch (IOException err) {
  265. // Can't read the file? Don't report the failure either.
  266. return zeroid;
  267. }
  268. }
  269. private static void safeClose(final InputStream in) {
  270. try {
  271. in.close();
  272. } catch (IOException err2) {
  273. // Suppress any error related to closing an input
  274. // stream. We don't care, we should not have any
  275. // outstanding data to flush or anything like that.
  276. }
  277. }
  278. private boolean mightNeedCleaning() {
  279. switch (getOptions().getAutoCRLF()) {
  280. case FALSE:
  281. default:
  282. return false;
  283. case TRUE:
  284. case INPUT:
  285. return true;
  286. }
  287. }
  288. private boolean isBinary(byte[] content, int sz) {
  289. return RawText.isBinary(content, sz);
  290. }
  291. private boolean isBinary(Entry entry) throws IOException {
  292. InputStream in = entry.openInputStream();
  293. try {
  294. return RawText.isBinary(in);
  295. } finally {
  296. safeClose(in);
  297. }
  298. }
  299. private ByteBuffer filterClean(byte[] src, int n)
  300. throws IOException {
  301. InputStream in = new ByteArrayInputStream(src);
  302. return IO.readWholeStream(filterClean(in), n);
  303. }
  304. private InputStream filterClean(InputStream in) {
  305. return new EolCanonicalizingInputStream(in);
  306. }
  307. /**
  308. * Returns the working tree options used by this iterator.
  309. *
  310. * @return working tree options
  311. */
  312. public WorkingTreeOptions getOptions() {
  313. return state.options;
  314. }
  315. @Override
  316. public int idOffset() {
  317. return 0;
  318. }
  319. @Override
  320. public void reset() {
  321. if (!first()) {
  322. ptr = 0;
  323. if (!eof())
  324. parseEntry();
  325. }
  326. }
  327. @Override
  328. public boolean first() {
  329. return ptr == 0;
  330. }
  331. @Override
  332. public boolean eof() {
  333. return ptr == entryCnt;
  334. }
  335. @Override
  336. public void next(final int delta) throws CorruptObjectException {
  337. ptr += delta;
  338. if (!eof())
  339. parseEntry();
  340. }
  341. @Override
  342. public void back(final int delta) throws CorruptObjectException {
  343. ptr -= delta;
  344. parseEntry();
  345. }
  346. private void parseEntry() {
  347. final Entry e = entries[ptr];
  348. mode = e.getMode().getBits();
  349. final int nameLen = e.encodedNameLen;
  350. ensurePathCapacity(pathOffset + nameLen, pathOffset);
  351. System.arraycopy(e.encodedName, 0, path, pathOffset, nameLen);
  352. pathLen = pathOffset + nameLen;
  353. }
  354. /**
  355. * Get the byte length of this entry.
  356. *
  357. * @return size of this file, in bytes.
  358. */
  359. public long getEntryLength() {
  360. return current().getLength();
  361. }
  362. /**
  363. * Get the last modified time of this entry.
  364. *
  365. * @return last modified time of this file, in milliseconds since the epoch
  366. * (Jan 1, 1970 UTC).
  367. */
  368. public long getEntryLastModified() {
  369. return current().getLastModified();
  370. }
  371. /**
  372. * Obtain an input stream to read the file content.
  373. * <p>
  374. * Efficient implementations are not required. The caller will usually
  375. * obtain the stream only once per entry, if at all.
  376. * <p>
  377. * The input stream should not use buffering if the implementation can avoid
  378. * it. The caller will buffer as necessary to perform efficient block IO
  379. * operations.
  380. * <p>
  381. * The caller will close the stream once complete.
  382. *
  383. * @return a stream to read from the file.
  384. * @throws IOException
  385. * the file could not be opened for reading.
  386. */
  387. public InputStream openEntryStream() throws IOException {
  388. return current().openInputStream();
  389. }
  390. /**
  391. * Determine if the current entry path is ignored by an ignore rule.
  392. *
  393. * @return true if the entry was ignored by an ignore rule file.
  394. * @throws IOException
  395. * a relevant ignore rule file exists but cannot be read.
  396. */
  397. public boolean isEntryIgnored() throws IOException {
  398. return isEntryIgnored(pathLen);
  399. }
  400. /**
  401. * Determine if the entry path is ignored by an ignore rule.
  402. *
  403. * @param pLen
  404. * the length of the path in the path buffer.
  405. * @return true if the entry is ignored by an ignore rule.
  406. * @throws IOException
  407. * a relevant ignore rule file exists but cannot be read.
  408. */
  409. protected boolean isEntryIgnored(final int pLen) throws IOException {
  410. IgnoreNode rules = getIgnoreNode();
  411. if (rules != null) {
  412. // The ignore code wants path to start with a '/' if possible.
  413. // If we have the '/' in our path buffer because we are inside
  414. // a subdirectory include it in the range we convert to string.
  415. //
  416. int pOff = pathOffset;
  417. if (0 < pOff)
  418. pOff--;
  419. String p = TreeWalk.pathOf(path, pOff, pLen);
  420. switch (rules.isIgnored(p, FileMode.TREE.equals(mode))) {
  421. case IGNORED:
  422. return true;
  423. case NOT_IGNORED:
  424. return false;
  425. case CHECK_PARENT:
  426. break;
  427. }
  428. }
  429. if (parent instanceof WorkingTreeIterator)
  430. return ((WorkingTreeIterator) parent).isEntryIgnored(pLen);
  431. return false;
  432. }
  433. private IgnoreNode getIgnoreNode() throws IOException {
  434. if (ignoreNode instanceof PerDirectoryIgnoreNode)
  435. ignoreNode = ((PerDirectoryIgnoreNode) ignoreNode).load();
  436. return ignoreNode;
  437. }
  438. private static final Comparator<Entry> ENTRY_CMP = new Comparator<Entry>() {
  439. public int compare(final Entry o1, final Entry o2) {
  440. final byte[] a = o1.encodedName;
  441. final byte[] b = o2.encodedName;
  442. final int aLen = o1.encodedNameLen;
  443. final int bLen = o2.encodedNameLen;
  444. int cPos;
  445. for (cPos = 0; cPos < aLen && cPos < bLen; cPos++) {
  446. final int cmp = (a[cPos] & 0xff) - (b[cPos] & 0xff);
  447. if (cmp != 0)
  448. return cmp;
  449. }
  450. if (cPos < aLen)
  451. return (a[cPos] & 0xff) - lastPathChar(o2);
  452. if (cPos < bLen)
  453. return lastPathChar(o1) - (b[cPos] & 0xff);
  454. return lastPathChar(o1) - lastPathChar(o2);
  455. }
  456. };
  457. static int lastPathChar(final Entry e) {
  458. return e.getMode() == FileMode.TREE ? '/' : '\0';
  459. }
  460. /**
  461. * Constructor helper.
  462. *
  463. * @param list
  464. * files in the subtree of the work tree this iterator operates
  465. * on
  466. */
  467. protected void init(final Entry[] list) {
  468. // Filter out nulls, . and .. as these are not valid tree entries,
  469. // also cache the encoded forms of the path names for efficient use
  470. // later on during sorting and iteration.
  471. //
  472. entries = list;
  473. int i, o;
  474. final CharsetEncoder nameEncoder = state.nameEncoder;
  475. for (i = 0, o = 0; i < entries.length; i++) {
  476. final Entry e = entries[i];
  477. if (e == null)
  478. continue;
  479. final String name = e.getName();
  480. if (".".equals(name) || "..".equals(name))
  481. continue;
  482. if (Constants.DOT_GIT.equals(name))
  483. continue;
  484. if (Constants.DOT_GIT_IGNORE.equals(name))
  485. ignoreNode = new PerDirectoryIgnoreNode(e);
  486. if (i != o)
  487. entries[o] = e;
  488. e.encodeName(nameEncoder);
  489. o++;
  490. }
  491. entryCnt = o;
  492. Arrays.sort(entries, 0, entryCnt, ENTRY_CMP);
  493. contentIdFromPtr = -1;
  494. ptr = 0;
  495. if (!eof())
  496. parseEntry();
  497. }
  498. /**
  499. * Obtain the current entry from this iterator.
  500. *
  501. * @return the currently selected entry.
  502. */
  503. protected Entry current() {
  504. return entries[ptr];
  505. }
  506. /**
  507. * The result of a metadata-comparison between the current entry and a
  508. * {@link DirCacheEntry}
  509. */
  510. public enum MetadataDiff {
  511. /**
  512. * The entries are equal by metaData (mode, length,
  513. * modification-timestamp) or the <code>assumeValid</code> attribute of
  514. * the index entry is set
  515. */
  516. EQUAL,
  517. /**
  518. * The entries are not equal by metaData (mode, length) or the
  519. * <code>isUpdateNeeded</code> attribute of the index entry is set
  520. */
  521. DIFFER_BY_METADATA,
  522. /** index entry is smudged - can't use that entry for comparison */
  523. SMUDGED,
  524. /**
  525. * The entries are equal by metaData (mode, length) but differ by
  526. * modification-timestamp.
  527. */
  528. DIFFER_BY_TIMESTAMP
  529. }
  530. /**
  531. * Compare the metadata (mode, length, modification-timestamp) of the
  532. * current entry and a {@link DirCacheEntry}
  533. *
  534. * @param entry
  535. * the {@link DirCacheEntry} to compare with
  536. * @return a {@link MetadataDiff} which tells whether and how the entries
  537. * metadata differ
  538. */
  539. public MetadataDiff compareMetadata(DirCacheEntry entry) {
  540. if (entry.isAssumeValid())
  541. return MetadataDiff.EQUAL;
  542. if (entry.isUpdateNeeded())
  543. return MetadataDiff.DIFFER_BY_METADATA;
  544. if (!entry.isSmudged() && (getEntryLength() != entry.getLength()))
  545. return MetadataDiff.DIFFER_BY_METADATA;
  546. // Determine difference in mode-bits of file and index-entry. In the
  547. // bitwise presentation of modeDiff we'll have a '1' when the two modes
  548. // differ at this position.
  549. int modeDiff = getEntryRawMode() ^ entry.getRawMode();
  550. // Do not rely on filemode differences in case of symbolic links
  551. if (modeDiff != 0 && !FileMode.SYMLINK.equals(entry.getRawMode())) {
  552. // Ignore the executable file bits if WorkingTreeOptions tell me to
  553. // do so. Ignoring is done by setting the bits representing a
  554. // EXECUTABLE_FILE to '0' in modeDiff
  555. if (!state.options.isFileMode())
  556. modeDiff &= ~FileMode.EXECUTABLE_FILE.getBits();
  557. if (modeDiff != 0)
  558. // Report a modification if the modes still (after potentially
  559. // ignoring EXECUTABLE_FILE bits) differ
  560. return MetadataDiff.DIFFER_BY_METADATA;
  561. }
  562. // Git under windows only stores seconds so we round the timestamp
  563. // Java gives us if it looks like the timestamp in index is seconds
  564. // only. Otherwise we compare the timestamp at millisecond precision.
  565. long cacheLastModified = entry.getLastModified();
  566. long fileLastModified = getEntryLastModified();
  567. if (cacheLastModified % 1000 == 0)
  568. fileLastModified = fileLastModified - fileLastModified % 1000;
  569. if (fileLastModified != cacheLastModified)
  570. return MetadataDiff.DIFFER_BY_TIMESTAMP;
  571. else if (!entry.isSmudged())
  572. // The file is clean when you look at timestamps.
  573. return MetadataDiff.EQUAL;
  574. else
  575. return MetadataDiff.SMUDGED;
  576. }
  577. /**
  578. * Checks whether this entry differs from a given entry from the
  579. * {@link DirCache}.
  580. *
  581. * File status information is used and if status is same we consider the
  582. * file identical to the state in the working directory. Native git uses
  583. * more stat fields than we have accessible in Java.
  584. *
  585. * @param entry
  586. * the entry from the dircache we want to compare against
  587. * @param forceContentCheck
  588. * True if the actual file content should be checked if
  589. * modification time differs.
  590. * @return true if content is most likely different.
  591. */
  592. public boolean isModified(DirCacheEntry entry, boolean forceContentCheck) {
  593. MetadataDiff diff = compareMetadata(entry);
  594. switch (diff) {
  595. case DIFFER_BY_TIMESTAMP:
  596. if (forceContentCheck)
  597. // But we are told to look at content even though timestamps
  598. // tell us about modification
  599. return contentCheck(entry);
  600. else
  601. // We are told to assume a modification if timestamps differs
  602. return true;
  603. case SMUDGED:
  604. // The file is clean by timestamps but the entry was smudged.
  605. // Lets do a content check
  606. return contentCheck(entry);
  607. case EQUAL:
  608. return false;
  609. case DIFFER_BY_METADATA:
  610. return true;
  611. default:
  612. throw new IllegalStateException(MessageFormat.format(
  613. JGitText.get().unexpectedCompareResult, diff.name()));
  614. }
  615. }
  616. /**
  617. * Compares the entries content with the content in the filesystem.
  618. * Unsmudges the entry when it is detected that it is clean.
  619. *
  620. * @param entry
  621. * the entry to be checked
  622. * @return <code>true</code> if the content matches, <code>false</code>
  623. * otherwise
  624. */
  625. private boolean contentCheck(DirCacheEntry entry) {
  626. if (getEntryObjectId().equals(entry.getObjectId())) {
  627. // Content has not changed
  628. // We know the entry can't be racily clean because it's still clean.
  629. // Therefore we unsmudge the entry!
  630. // If by any chance we now unsmudge although we are still in the
  631. // same time-slot as the last modification to the index file the
  632. // next index write operation will smudge again.
  633. // Caution: we are unsmudging just by setting the length of the
  634. // in-memory entry object. It's the callers task to detect that we
  635. // have modified the entry and to persist the modified index.
  636. entry.setLength((int) getEntryLength());
  637. return false;
  638. } else {
  639. // Content differs: that's a real change!
  640. return true;
  641. }
  642. }
  643. private long computeLength(InputStream in) throws IOException {
  644. // Since we only care about the length, use skip. The stream
  645. // may be able to more efficiently wade through its data.
  646. //
  647. long length = 0;
  648. for (;;) {
  649. long n = in.skip(1 << 20);
  650. if (n <= 0)
  651. break;
  652. length += n;
  653. }
  654. return length;
  655. }
  656. private byte[] computeHash(InputStream in, long length) throws IOException {
  657. final MessageDigest contentDigest = state.contentDigest;
  658. final byte[] contentReadBuffer = state.contentReadBuffer;
  659. contentDigest.reset();
  660. contentDigest.update(hblob);
  661. contentDigest.update((byte) ' ');
  662. long sz = length;
  663. if (sz == 0) {
  664. contentDigest.update((byte) '0');
  665. } else {
  666. final int bufn = contentReadBuffer.length;
  667. int p = bufn;
  668. do {
  669. contentReadBuffer[--p] = digits[(int) (sz % 10)];
  670. sz /= 10;
  671. } while (sz > 0);
  672. contentDigest.update(contentReadBuffer, p, bufn - p);
  673. }
  674. contentDigest.update((byte) 0);
  675. for (;;) {
  676. final int r = in.read(contentReadBuffer);
  677. if (r <= 0)
  678. break;
  679. contentDigest.update(contentReadBuffer, 0, r);
  680. sz += r;
  681. }
  682. if (sz != length)
  683. return zeroid;
  684. return contentDigest.digest();
  685. }
  686. /** A single entry within a working directory tree. */
  687. protected static abstract class Entry {
  688. byte[] encodedName;
  689. int encodedNameLen;
  690. void encodeName(final CharsetEncoder enc) {
  691. final ByteBuffer b;
  692. try {
  693. b = enc.encode(CharBuffer.wrap(getName()));
  694. } catch (CharacterCodingException e) {
  695. // This should so never happen.
  696. throw new RuntimeException(MessageFormat.format(
  697. JGitText.get().unencodeableFile, getName()));
  698. }
  699. encodedNameLen = b.limit();
  700. if (b.hasArray() && b.arrayOffset() == 0)
  701. encodedName = b.array();
  702. else
  703. b.get(encodedName = new byte[encodedNameLen]);
  704. }
  705. public String toString() {
  706. return getMode().toString() + " " + getName();
  707. }
  708. /**
  709. * Get the type of this entry.
  710. * <p>
  711. * <b>Note: Efficient implementation required.</b>
  712. * <p>
  713. * The implementation of this method must be efficient. If a subclass
  714. * needs to compute the value they should cache the reference within an
  715. * instance member instead.
  716. *
  717. * @return a file mode constant from {@link FileMode}.
  718. */
  719. public abstract FileMode getMode();
  720. /**
  721. * Get the byte length of this entry.
  722. * <p>
  723. * <b>Note: Efficient implementation required.</b>
  724. * <p>
  725. * The implementation of this method must be efficient. If a subclass
  726. * needs to compute the value they should cache the reference within an
  727. * instance member instead.
  728. *
  729. * @return size of this file, in bytes.
  730. */
  731. public abstract long getLength();
  732. /**
  733. * Get the last modified time of this entry.
  734. * <p>
  735. * <b>Note: Efficient implementation required.</b>
  736. * <p>
  737. * The implementation of this method must be efficient. If a subclass
  738. * needs to compute the value they should cache the reference within an
  739. * instance member instead.
  740. *
  741. * @return time since the epoch (in ms) of the last change.
  742. */
  743. public abstract long getLastModified();
  744. /**
  745. * Get the name of this entry within its directory.
  746. * <p>
  747. * Efficient implementations are not required. The caller will obtain
  748. * the name only once and cache it once obtained.
  749. *
  750. * @return name of the entry.
  751. */
  752. public abstract String getName();
  753. /**
  754. * Obtain an input stream to read the file content.
  755. * <p>
  756. * Efficient implementations are not required. The caller will usually
  757. * obtain the stream only once per entry, if at all.
  758. * <p>
  759. * The input stream should not use buffering if the implementation can
  760. * avoid it. The caller will buffer as necessary to perform efficient
  761. * block IO operations.
  762. * <p>
  763. * The caller will close the stream once complete.
  764. *
  765. * @return a stream to read from the file.
  766. * @throws IOException
  767. * the file could not be opened for reading.
  768. */
  769. public abstract InputStream openInputStream() throws IOException;
  770. }
  771. /** Magic type indicating we know rules exist, but they aren't loaded. */
  772. private static class PerDirectoryIgnoreNode extends IgnoreNode {
  773. final Entry entry;
  774. PerDirectoryIgnoreNode(Entry entry) {
  775. super(Collections.<IgnoreRule> emptyList());
  776. this.entry = entry;
  777. }
  778. IgnoreNode load() throws IOException {
  779. IgnoreNode r = new IgnoreNode();
  780. InputStream in = entry.openInputStream();
  781. try {
  782. r.parse(in);
  783. } finally {
  784. in.close();
  785. }
  786. return r.getRules().isEmpty() ? null : r;
  787. }
  788. }
  789. /** Magic type indicating there may be rules for the top level. */
  790. private static class RootIgnoreNode extends PerDirectoryIgnoreNode {
  791. final Repository repository;
  792. RootIgnoreNode(Entry entry, Repository repository) {
  793. super(entry);
  794. this.repository = repository;
  795. }
  796. @Override
  797. IgnoreNode load() throws IOException {
  798. IgnoreNode r;
  799. if (entry != null) {
  800. r = super.load();
  801. if (r == null)
  802. r = new IgnoreNode();
  803. } else {
  804. r = new IgnoreNode();
  805. }
  806. FS fs = repository.getFS();
  807. String path = repository.getConfig().get(CoreConfig.KEY)
  808. .getExcludesFile();
  809. if (path != null) {
  810. File excludesfile;
  811. if (path.startsWith("~/"))
  812. excludesfile = fs.resolve(fs.userHome(), path.substring(2));
  813. else
  814. excludesfile = fs.resolve(null, path);
  815. loadRulesFromFile(r, excludesfile);
  816. }
  817. File exclude = fs
  818. .resolve(repository.getDirectory(), "info/exclude");
  819. loadRulesFromFile(r, exclude);
  820. return r.getRules().isEmpty() ? null : r;
  821. }
  822. private void loadRulesFromFile(IgnoreNode r, File exclude)
  823. throws FileNotFoundException, IOException {
  824. if (exclude.exists()) {
  825. FileInputStream in = new FileInputStream(exclude);
  826. try {
  827. r.parse(in);
  828. } finally {
  829. in.close();
  830. }
  831. }
  832. }
  833. }
  834. private static final class IteratorState {
  835. /** Options used to process the working tree. */
  836. final WorkingTreeOptions options;
  837. /** File name character encoder. */
  838. final CharsetEncoder nameEncoder;
  839. /** Digest computer for {@link #contentId} computations. */
  840. MessageDigest contentDigest;
  841. /** Buffer used to perform {@link #contentId} computations. */
  842. byte[] contentReadBuffer;
  843. /** TreeWalk with a (supposedly) matching DirCacheIterator. */
  844. TreeWalk walk;
  845. /** Position of the matching {@link DirCacheIterator}. */
  846. int dirCacheTree;
  847. IteratorState(WorkingTreeOptions options) {
  848. this.options = options;
  849. this.nameEncoder = Constants.CHARSET.newEncoder();
  850. }
  851. void initializeDigestAndReadBuffer() {
  852. if (contentDigest == null) {
  853. contentDigest = Constants.newMessageDigest();
  854. contentReadBuffer = new byte[BUFFER_SIZE];
  855. }
  856. }
  857. }
  858. }