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

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