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

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