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

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