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

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