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

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