View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3    * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
4    * Copyright (C) 2010, Matthias Sohn <matthias.sohn@sap.com>
5    * Copyright (C) 2012-2013, Robin Rosenberg
6    * and other copyright owners as documented in the project's IP log.
7    *
8    * This program and the accompanying materials are made available
9    * under the terms of the Eclipse Distribution License v1.0 which
10   * accompanies this distribution, is reproduced below, and is
11   * available at http://www.eclipse.org/org/documents/edl-v10.php
12   *
13   * All rights reserved.
14   *
15   * Redistribution and use in source and binary forms, with or
16   * without modification, are permitted provided that the following
17   * conditions are met:
18   *
19   * - Redistributions of source code must retain the above copyright
20   *   notice, this list of conditions and the following disclaimer.
21   *
22   * - Redistributions in binary form must reproduce the above
23   *   copyright notice, this list of conditions and the following
24   *   disclaimer in the documentation and/or other materials provided
25   *   with the distribution.
26   *
27   * - Neither the name of the Eclipse Foundation, Inc. nor the
28   *   names of its contributors may be used to endorse or promote
29   *   products derived from this software without specific prior
30   *   written permission.
31   *
32   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
44   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45   */
46  
47  package org.eclipse.jgit.treewalk;
48  
49  import static java.nio.charset.StandardCharsets.UTF_8;
50  
51  import java.io.ByteArrayInputStream;
52  import java.io.File;
53  import java.io.FileInputStream;
54  import java.io.FileNotFoundException;
55  import java.io.IOException;
56  import java.io.InputStream;
57  import java.nio.ByteBuffer;
58  import java.nio.CharBuffer;
59  import java.nio.charset.CharacterCodingException;
60  import java.nio.charset.CharsetEncoder;
61  import java.text.MessageFormat;
62  import java.time.Instant;
63  import java.util.Arrays;
64  import java.util.Collections;
65  import java.util.Comparator;
66  import java.util.HashMap;
67  import java.util.Map;
68  
69  import org.eclipse.jgit.api.errors.FilterFailedException;
70  import org.eclipse.jgit.attributes.AttributesNode;
71  import org.eclipse.jgit.attributes.AttributesRule;
72  import org.eclipse.jgit.attributes.FilterCommand;
73  import org.eclipse.jgit.attributes.FilterCommandRegistry;
74  import org.eclipse.jgit.diff.RawText;
75  import org.eclipse.jgit.dircache.DirCacheEntry;
76  import org.eclipse.jgit.dircache.DirCacheIterator;
77  import org.eclipse.jgit.errors.CorruptObjectException;
78  import org.eclipse.jgit.errors.MissingObjectException;
79  import org.eclipse.jgit.errors.NoWorkTreeException;
80  import org.eclipse.jgit.ignore.FastIgnoreRule;
81  import org.eclipse.jgit.ignore.IgnoreNode;
82  import org.eclipse.jgit.internal.JGitText;
83  import org.eclipse.jgit.lib.Constants;
84  import org.eclipse.jgit.lib.CoreConfig;
85  import org.eclipse.jgit.lib.CoreConfig.CheckStat;
86  import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
87  import org.eclipse.jgit.lib.CoreConfig.SymLinks;
88  import org.eclipse.jgit.lib.FileMode;
89  import org.eclipse.jgit.lib.ObjectId;
90  import org.eclipse.jgit.lib.ObjectLoader;
91  import org.eclipse.jgit.lib.ObjectReader;
92  import org.eclipse.jgit.lib.Repository;
93  import org.eclipse.jgit.submodule.SubmoduleWalk;
94  import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
95  import org.eclipse.jgit.util.FS;
96  import org.eclipse.jgit.util.FS.ExecutionResult;
97  import org.eclipse.jgit.util.Holder;
98  import org.eclipse.jgit.util.IO;
99  import org.eclipse.jgit.util.Paths;
100 import org.eclipse.jgit.util.RawParseUtils;
101 import org.eclipse.jgit.util.TemporaryBuffer;
102 import org.eclipse.jgit.util.TemporaryBuffer.LocalFile;
103 import org.eclipse.jgit.util.io.AutoLFInputStream;
104 import org.eclipse.jgit.util.io.EolStreamTypeUtil;
105 import org.eclipse.jgit.util.sha1.SHA1;
106 
107 /**
108  * Walks a working directory tree as part of a
109  * {@link org.eclipse.jgit.treewalk.TreeWalk}.
110  * <p>
111  * Most applications will want to use the standard implementation of this
112  * iterator, {@link org.eclipse.jgit.treewalk.FileTreeIterator}, as that does
113  * all IO through the standard <code>java.io</code> package. Plugins for a Java
114  * based IDE may however wish to create their own implementations of this class
115  * to allow traversal of the IDE's project space, as well as benefit from any
116  * caching the IDE may have.
117  *
118  * @see FileTreeIterator
119  */
120 public abstract class WorkingTreeIterator extends AbstractTreeIterator {
121 	private static final int MAX_EXCEPTION_TEXT_SIZE = 10 * 1024;
122 
123 	/** An empty entry array, suitable for {@link #init(Entry[])}. */
124 	protected static final Entry[] EOF = {};
125 
126 	/** Size we perform file IO in if we have to read and hash a file. */
127 	static final int BUFFER_SIZE = 2048;
128 
129 	/**
130 	 * Maximum size of files which may be read fully into memory for performance
131 	 * reasons.
132 	 */
133 	private static final long MAXIMUM_FILE_SIZE_TO_READ_FULLY = 65536;
134 
135 	/** Inherited state of this iterator, describing working tree, etc. */
136 	private final IteratorState state;
137 
138 	/** The {@link #idBuffer()} for the current entry. */
139 	private byte[] contentId;
140 
141 	/** Index within {@link #entries} that {@link #contentId} came from. */
142 	private int contentIdFromPtr;
143 
144 	/** List of entries obtained from the subclass. */
145 	private Entry[] entries;
146 
147 	/** Total number of entries in {@link #entries} that are valid. */
148 	private int entryCnt;
149 
150 	/** Current position within {@link #entries}. */
151 	private int ptr;
152 
153 	/** If there is a .gitignore file present, the parsed rules from it. */
154 	private IgnoreNode ignoreNode;
155 
156 	/**
157 	 * cached clean filter command. Use a Ref in order to distinguish between
158 	 * the ref not cached yet and the value null
159 	 */
160 	private Holder<String> cleanFilterCommandHolder;
161 
162 	/**
163 	 * cached eol stream type. Use a Ref in order to distinguish between the ref
164 	 * not cached yet and the value null
165 	 */
166 	private Holder<EolStreamType> eolStreamTypeHolder;
167 
168 	/** Repository that is the root level being iterated over */
169 	protected Repository repository;
170 
171 	/** Cached canonical length, initialized from {@link #idBuffer()} */
172 	private long canonLen = -1;
173 
174 	/** The offset of the content id in {@link #idBuffer()} */
175 	private int contentIdOffset;
176 
177 	/** A comparator for {@link Instant}s. */
178 	private final InstantComparator timestampComparator = new InstantComparator();
179 
180 	/**
181 	 * Create a new iterator with no parent.
182 	 *
183 	 * @param options
184 	 *            working tree options to be used
185 	 */
186 	protected WorkingTreeIterator(WorkingTreeOptions options) {
187 		super();
188 		state = new IteratorState(options);
189 	}
190 
191 	/**
192 	 * Create a new iterator with no parent and a prefix.
193 	 * <p>
194 	 * The prefix path supplied is inserted in front of all paths generated by
195 	 * this iterator. It is intended to be used when an iterator is being
196 	 * created for a subsection of an overall repository and needs to be
197 	 * combined with other iterators that are created to run over the entire
198 	 * repository namespace.
199 	 *
200 	 * @param prefix
201 	 *            position of this iterator in the repository tree. The value
202 	 *            may be null or the empty string to indicate the prefix is the
203 	 *            root of the repository. A trailing slash ('/') is
204 	 *            automatically appended if the prefix does not end in '/'.
205 	 * @param options
206 	 *            working tree options to be used
207 	 */
208 	protected WorkingTreeIterator(final String prefix,
209 			WorkingTreeOptions options) {
210 		super(prefix);
211 		state = new IteratorState(options);
212 	}
213 
214 	/**
215 	 * Create an iterator for a subtree of an existing iterator.
216 	 *
217 	 * @param p
218 	 *            parent tree iterator.
219 	 */
220 	protected WorkingTreeIterator(WorkingTreeIterator p) {
221 		super(p);
222 		state = p.state;
223 		repository = p.repository;
224 	}
225 
226 	/**
227 	 * Initialize this iterator for the root level of a repository.
228 	 * <p>
229 	 * This method should only be invoked after calling {@link #init(Entry[])},
230 	 * and only for the root iterator.
231 	 *
232 	 * @param repo
233 	 *            the repository.
234 	 */
235 	protected void initRootIterator(Repository repo) {
236 		repository = repo;
237 		Entry entry;
238 		if (ignoreNode instanceof PerDirectoryIgnoreNode)
239 			entry = ((PerDirectoryIgnoreNode) ignoreNode).entry;
240 		else
241 			entry = null;
242 		ignoreNode = new RootIgnoreNode(entry, repo);
243 	}
244 
245 	/**
246 	 * Define the matching {@link org.eclipse.jgit.dircache.DirCacheIterator},
247 	 * to optimize ObjectIds.
248 	 *
249 	 * Once the DirCacheIterator has been set this iterator must only be
250 	 * advanced by the TreeWalk that is supplied, as it assumes that itself and
251 	 * the corresponding DirCacheIterator are positioned on the same file path
252 	 * whenever {@link #idBuffer()} is invoked.
253 	 *
254 	 * @param walk
255 	 *            the walk that will be advancing this iterator.
256 	 * @param treeId
257 	 *            index of the matching
258 	 *            {@link org.eclipse.jgit.dircache.DirCacheIterator}.
259 	 */
260 	public void setDirCacheIterator(TreeWalk walk, int treeId) {
261 		state.walk = walk;
262 		state.dirCacheTree = treeId;
263 	}
264 
265 	/**
266 	 * Retrieves the {@link DirCacheIterator} at the current entry if
267 	 * {@link #setDirCacheIterator(TreeWalk, int)} was called.
268 	 *
269 	 * @return the DirCacheIterator, or {@code null} if not set or not at the
270 	 *         current entry
271 	 * @since 5.0
272 	 */
273 	protected DirCacheIterator getDirCacheIterator() {
274 		if (state.dirCacheTree >= 0 && state.walk != null) {
275 			return state.walk.getTree(state.dirCacheTree,
276 					DirCacheIterator.class);
277 		}
278 		return null;
279 	}
280 
281 	/**
282 	 * Defines whether this {@link WorkingTreeIterator} walks ignored
283 	 * directories.
284 	 *
285 	 * @param includeIgnored
286 	 *            {@code false} to skip ignored directories, if possible;
287 	 *            {@code true} to always include them in the walk
288 	 * @since 5.0
289 	 */
290 	public void setWalkIgnoredDirectories(boolean includeIgnored) {
291 		state.walkIgnored = includeIgnored;
292 	}
293 
294 	/**
295 	 * Tells whether this {@link WorkingTreeIterator} walks ignored directories.
296 	 *
297 	 * @return {@code true} if it does, {@code false} otherwise
298 	 * @since 5.0
299 	 */
300 	public boolean walksIgnoredDirectories() {
301 		return state.walkIgnored;
302 	}
303 
304 	/** {@inheritDoc} */
305 	@Override
306 	public boolean hasId() {
307 		if (contentIdFromPtr == ptr)
308 			return true;
309 		return (mode & FileMode.TYPE_MASK) == FileMode.TYPE_FILE;
310 	}
311 
312 	/** {@inheritDoc} */
313 	@Override
314 	public byte[] idBuffer() {
315 		if (contentIdFromPtr == ptr)
316 			return contentId;
317 
318 		if (state.walk != null) {
319 			// If there is a matching DirCacheIterator, we can reuse
320 			// its idBuffer, but only if we appear to be clean against
321 			// the cached index information for the path.
322 			DirCacheIterator i = state.walk.getTree(state.dirCacheTree,
323 							DirCacheIterator.class);
324 			if (i != null) {
325 				DirCacheEntry ent = i.getDirCacheEntry();
326 				if (ent != null && compareMetadata(ent) == MetadataDiff.EQUAL
327 						&& ((ent.getFileMode().getBits()
328 								& FileMode.TYPE_MASK) != FileMode.TYPE_GITLINK)) {
329 					contentIdOffset = i.idOffset();
330 					contentIdFromPtr = ptr;
331 					return contentId = i.idBuffer();
332 				}
333 				contentIdOffset = 0;
334 			} else {
335 				contentIdOffset = 0;
336 			}
337 		}
338 		switch (mode & FileMode.TYPE_MASK) {
339 		case FileMode.TYPE_SYMLINK:
340 		case FileMode.TYPE_FILE:
341 			contentIdFromPtr = ptr;
342 			return contentId = idBufferBlob(entries[ptr]);
343 		case FileMode.TYPE_GITLINK:
344 			contentIdFromPtr = ptr;
345 			return contentId = idSubmodule(entries[ptr]);
346 		}
347 		return zeroid;
348 	}
349 
350 	/** {@inheritDoc} */
351 	@Override
352 	public boolean isWorkTree() {
353 		return true;
354 	}
355 
356 	/**
357 	 * Get submodule id for given entry.
358 	 *
359 	 * @param e
360 	 *            a {@link org.eclipse.jgit.treewalk.WorkingTreeIterator.Entry}
361 	 *            object.
362 	 * @return non-null submodule id
363 	 */
364 	protected byte[] idSubmodule(Entry e) {
365 		if (repository == null)
366 			return zeroid;
367 		File directory;
368 		try {
369 			directory = repository.getWorkTree();
370 		} catch (NoWorkTreeException nwte) {
371 			return zeroid;
372 		}
373 		return idSubmodule(directory, e);
374 	}
375 
376 	/**
377 	 * Get submodule id using the repository at the location of the entry
378 	 * relative to the directory.
379 	 *
380 	 * @param directory
381 	 *            a {@link java.io.File} object.
382 	 * @param e
383 	 *            a {@link org.eclipse.jgit.treewalk.WorkingTreeIterator.Entry}
384 	 *            object.
385 	 * @return non-null submodule id
386 	 */
387 	protected byte[] idSubmodule(File directory, Entry e) {
388 		try (Repository submoduleRepo = SubmoduleWalk.getSubmoduleRepository(
389 				directory, e.getName(),
390 				repository != null ? repository.getFS() : FS.DETECTED)) {
391 			if (submoduleRepo == null) {
392 				return zeroid;
393 			}
394 			ObjectId head = submoduleRepo.resolve(Constants.HEAD);
395 			if (head == null) {
396 				return zeroid;
397 			}
398 			byte[] id = new byte[Constants.OBJECT_ID_LENGTH];
399 			head.copyRawTo(id, 0);
400 			return id;
401 		} catch (IOException exception) {
402 			return zeroid;
403 		}
404 	}
405 
406 	private static final byte[] digits = { '0', '1', '2', '3', '4', '5', '6',
407 			'7', '8', '9' };
408 
409 	private static final byte[] hblob = Constants
410 			.encodedTypeString(Constants.OBJ_BLOB);
411 
412 	private byte[] idBufferBlob(Entry e) {
413 		try {
414 			final InputStream is = e.openInputStream();
415 			if (is == null)
416 				return zeroid;
417 			try {
418 				state.initializeReadBuffer();
419 
420 				final long len = e.getLength();
421 				InputStream filteredIs = possiblyFilteredInputStream(e, is, len,
422 						OperationType.CHECKIN_OP);
423 				return computeHash(filteredIs, canonLen);
424 			} finally {
425 				safeClose(is);
426 			}
427 		} catch (IOException err) {
428 			// Can't read the file? Don't report the failure either.
429 			return zeroid;
430 		}
431 	}
432 
433 	private InputStream possiblyFilteredInputStream(final Entry e,
434 			final InputStream is, final long len) throws IOException {
435 		return possiblyFilteredInputStream(e, is, len, null);
436 
437 	}
438 
439 	private InputStream possiblyFilteredInputStream(final Entry e,
440 			final InputStream is, final long len, OperationType opType)
441 			throws IOException {
442 		if (getCleanFilterCommand() == null
443 				&& getEolStreamType(opType) == EolStreamType.DIRECT) {
444 			canonLen = len;
445 			return is;
446 		}
447 
448 		if (len <= MAXIMUM_FILE_SIZE_TO_READ_FULLY) {
449 			ByteBuffer rawbuf = IO.readWholeStream(is, (int) len);
450 			rawbuf = filterClean(rawbuf.array(), rawbuf.limit(), opType);
451 			canonLen = rawbuf.limit();
452 			return new ByteArrayInputStream(rawbuf.array(), 0, (int) canonLen);
453 		}
454 
455 		if (getCleanFilterCommand() == null && isBinary(e)) {
456 				canonLen = len;
457 				return is;
458 			}
459 
460 		final InputStream lenIs = filterClean(e.openInputStream(),
461 				opType);
462 		try {
463 			canonLen = computeLength(lenIs);
464 		} finally {
465 			safeClose(lenIs);
466 		}
467 		return filterClean(is, opType);
468 	}
469 
470 	private static void safeClose(InputStream in) {
471 		try {
472 			in.close();
473 		} catch (IOException err2) {
474 			// Suppress any error related to closing an input
475 			// stream. We don't care, we should not have any
476 			// outstanding data to flush or anything like that.
477 		}
478 	}
479 
480 	private static boolean isBinary(Entry entry) throws IOException {
481 		InputStream in = entry.openInputStream();
482 		try {
483 			return RawText.isBinary(in);
484 		} finally {
485 			safeClose(in);
486 		}
487 	}
488 
489 	private ByteBuffer filterClean(byte[] src, int n, OperationType opType)
490 			throws IOException {
491 		InputStream in = new ByteArrayInputStream(src);
492 		try {
493 			return IO.readWholeStream(filterClean(in, opType), n);
494 		} finally {
495 			safeClose(in);
496 		}
497 	}
498 
499 	private InputStream filterClean(InputStream in) throws IOException {
500 		return filterClean(in, null);
501 	}
502 
503 	private InputStream filterClean(InputStream in, OperationType opType)
504 			throws IOException {
505 		in = handleAutoCRLF(in, opType);
506 		String filterCommand = getCleanFilterCommand();
507 		if (filterCommand != null) {
508 			if (FilterCommandRegistry.isRegistered(filterCommand)) {
509 				LocalFile buffer = new TemporaryBuffer.LocalFile(null);
510 				FilterCommand command = FilterCommandRegistry
511 						.createFilterCommand(filterCommand, repository, in,
512 								buffer);
513 				while (command.run() != -1) {
514 					// loop as long as command.run() tells there is work to do
515 				}
516 				return buffer.openInputStreamWithAutoDestroy();
517 			}
518 			FS fs = repository.getFS();
519 			ProcessBuilder filterProcessBuilder = fs.runInShell(filterCommand,
520 					new String[0]);
521 			filterProcessBuilder.directory(repository.getWorkTree());
522 			filterProcessBuilder.environment().put(Constants.GIT_DIR_KEY,
523 					repository.getDirectory().getAbsolutePath());
524 			ExecutionResult result;
525 			try {
526 				result = fs.execute(filterProcessBuilder, in);
527 			} catch (IOException | InterruptedException e) {
528 				throw new IOException(new FilterFailedException(e,
529 						filterCommand, getEntryPathString()));
530 			}
531 			int rc = result.getRc();
532 			if (rc != 0) {
533 				throw new IOException(new FilterFailedException(rc,
534 						filterCommand, getEntryPathString(),
535 						result.getStdout().toByteArray(MAX_EXCEPTION_TEXT_SIZE),
536 						RawParseUtils.decode(result.getStderr()
537 								.toByteArray(MAX_EXCEPTION_TEXT_SIZE))));
538 			}
539 			return result.getStdout().openInputStreamWithAutoDestroy();
540 		}
541 		return in;
542 	}
543 
544 	private InputStream handleAutoCRLF(InputStream in, OperationType opType)
545 			throws IOException {
546 		return EolStreamTypeUtil.wrapInputStream(in, getEolStreamType(opType));
547 	}
548 
549 	/**
550 	 * Returns the working tree options used by this iterator.
551 	 *
552 	 * @return working tree options
553 	 */
554 	public WorkingTreeOptions getOptions() {
555 		return state.options;
556 	}
557 
558 	/** {@inheritDoc} */
559 	@Override
560 	public int idOffset() {
561 		return contentIdOffset;
562 	}
563 
564 	/** {@inheritDoc} */
565 	@Override
566 	public void reset() {
567 		if (!first()) {
568 			ptr = 0;
569 			if (!eof())
570 				parseEntry();
571 		}
572 	}
573 
574 	/** {@inheritDoc} */
575 	@Override
576 	public boolean first() {
577 		return ptr == 0;
578 	}
579 
580 	/** {@inheritDoc} */
581 	@Override
582 	public boolean eof() {
583 		return ptr == entryCnt;
584 	}
585 
586 	/** {@inheritDoc} */
587 	@Override
588 	public void next(int delta) throws CorruptObjectException {
589 		ptr += delta;
590 		if (!eof()) {
591 			parseEntry();
592 		}
593 	}
594 
595 	/** {@inheritDoc} */
596 	@Override
597 	public void back(int delta) throws CorruptObjectException {
598 		ptr -= delta;
599 		parseEntry();
600 	}
601 
602 	private void parseEntry() {
603 		final Entry e = entries[ptr];
604 		mode = e.getMode().getBits();
605 
606 		final int nameLen = e.encodedNameLen;
607 		ensurePathCapacity(pathOffset + nameLen, pathOffset);
608 		System.arraycopy(e.encodedName, 0, path, pathOffset, nameLen);
609 		pathLen = pathOffset + nameLen;
610 		canonLen = -1;
611 		cleanFilterCommandHolder = null;
612 		eolStreamTypeHolder = null;
613 	}
614 
615 	/**
616 	 * Get the raw byte length of this entry.
617 	 *
618 	 * @return size of this file, in bytes.
619 	 */
620 	public long getEntryLength() {
621 		return current().getLength();
622 	}
623 
624 	/**
625 	 * Get the filtered input length of this entry
626 	 *
627 	 * @return size of the content, in bytes
628 	 * @throws java.io.IOException
629 	 */
630 	public long getEntryContentLength() throws IOException {
631 		if (canonLen == -1) {
632 			long rawLen = getEntryLength();
633 			if (rawLen == 0)
634 				canonLen = 0;
635 			InputStream is = current().openInputStream();
636 			try {
637 				// canonLen gets updated here
638 				possiblyFilteredInputStream(current(), is, current()
639 						.getLength());
640 			} finally {
641 				safeClose(is);
642 			}
643 		}
644 		return canonLen;
645 	}
646 
647 	/**
648 	 * Get the last modified time of this entry.
649 	 *
650 	 * @return last modified time of this file, in milliseconds since the epoch
651 	 *         (Jan 1, 1970 UTC).
652 	 * @deprecated use {@link #getEntryLastModifiedInstant()} instead
653 	 */
654 	@Deprecated
655 	public long getEntryLastModified() {
656 		return current().getLastModified();
657 	}
658 
659 	/**
660 	 * Get the last modified time of this entry.
661 	 *
662 	 * @return last modified time of this file
663 	 * @since 5.1.9
664 	 */
665 	public Instant getEntryLastModifiedInstant() {
666 		return current().getLastModifiedInstant();
667 	}
668 
669 	/**
670 	 * Obtain an input stream to read the file content.
671 	 * <p>
672 	 * Efficient implementations are not required. The caller will usually
673 	 * obtain the stream only once per entry, if at all.
674 	 * <p>
675 	 * The input stream should not use buffering if the implementation can avoid
676 	 * it. The caller will buffer as necessary to perform efficient block IO
677 	 * operations.
678 	 * <p>
679 	 * The caller will close the stream once complete.
680 	 *
681 	 * @return a stream to read from the file.
682 	 * @throws java.io.IOException
683 	 *             the file could not be opened for reading.
684 	 */
685 	public InputStream openEntryStream() throws IOException {
686 		InputStream rawis = current().openInputStream();
687 		if (getCleanFilterCommand() == null
688 				&& getEolStreamType() == EolStreamType.DIRECT)
689 			return rawis;
690 		else
691 			return filterClean(rawis);
692 	}
693 
694 	/**
695 	 * Determine if the current entry path is ignored by an ignore rule.
696 	 *
697 	 * @return true if the entry was ignored by an ignore rule file.
698 	 * @throws java.io.IOException
699 	 *             a relevant ignore rule file exists but cannot be read.
700 	 */
701 	public boolean isEntryIgnored() throws IOException {
702 		return isEntryIgnored(pathLen);
703 	}
704 
705 	/**
706 	 * Determine if the entry path is ignored by an ignore rule.
707 	 *
708 	 * @param pLen
709 	 *            the length of the path in the path buffer.
710 	 * @return true if the entry is ignored by an ignore rule.
711 	 * @throws java.io.IOException
712 	 *             a relevant ignore rule file exists but cannot be read.
713 	 */
714 	protected boolean isEntryIgnored(int pLen) throws IOException {
715 		return isEntryIgnored(pLen, mode);
716 	}
717 
718 	/**
719 	 * Determine if the entry path is ignored by an ignore rule.
720 	 *
721 	 * @param pLen
722 	 *            the length of the path in the path buffer.
723 	 * @param fileMode
724 	 *            the original iterator file mode
725 	 * @return true if the entry is ignored by an ignore rule.
726 	 * @throws IOException
727 	 *             a relevant ignore rule file exists but cannot be read.
728 	 */
729 	private boolean isEntryIgnored(int pLen, int fileMode)
730 			throws IOException {
731 		// The ignore code wants path to start with a '/' if possible.
732 		// If we have the '/' in our path buffer because we are inside
733 		// a sub-directory include it in the range we convert to string.
734 		//
735 		final int pOff = 0 < pathOffset ? pathOffset - 1 : pathOffset;
736 		String pathRel = TreeWalk.pathOf(this.path, pOff, pLen);
737 		String parentRel = getParentPath(pathRel);
738 
739 		// CGit is processing .gitignore files by starting at the root of the
740 		// repository and then recursing into subdirectories. With this
741 		// approach, top-level ignored directories will be processed first which
742 		// allows to skip entire subtrees and further .gitignore-file processing
743 		// within these subtrees.
744 		//
745 		// We will follow the same approach by marking directories as "ignored"
746 		// here. This allows to have a simplified FastIgnore.checkIgnore()
747 		// implementation (both in terms of code and computational complexity):
748 		//
749 		// Without the "ignored" flag, we would have to apply the ignore-check
750 		// to a path and all of its parents always(!), to determine whether a
751 		// path is ignored directly or by one of its parent directories; with
752 		// the "ignored" flag, we know at this point that the parent directory
753 		// is definitely not ignored, thus the path can only become ignored if
754 		// there is a rule matching the path itself.
755 		if (isDirectoryIgnored(parentRel)) {
756 			return true;
757 		}
758 
759 		IgnoreNode rules = getIgnoreNode();
760 		final Boolean ignored = rules != null
761 				? rules.checkIgnored(pathRel, FileMode.TREE.equals(fileMode))
762 				: null;
763 		if (ignored != null) {
764 			return ignored.booleanValue();
765 		}
766 		return parent instanceof WorkingTreeIterator
767 				&& ((WorkingTreeIterator) parent).isEntryIgnored(pLen,
768 						fileMode);
769 	}
770 
771 	private IgnoreNode getIgnoreNode() throws IOException {
772 		if (ignoreNode instanceof PerDirectoryIgnoreNode)
773 			ignoreNode = ((PerDirectoryIgnoreNode) ignoreNode).load();
774 		return ignoreNode;
775 	}
776 
777 	/**
778 	 * Retrieves the {@link org.eclipse.jgit.attributes.AttributesNode} for the
779 	 * current entry.
780 	 *
781 	 * @return the {@link org.eclipse.jgit.attributes.AttributesNode} for the
782 	 *         current entry.
783 	 * @throws IOException
784 	 */
785 	public AttributesNode getEntryAttributesNode() throws IOException {
786 		if (attributesNode instanceof PerDirectoryAttributesNode)
787 			attributesNode = ((PerDirectoryAttributesNode) attributesNode)
788 					.load();
789 		return attributesNode;
790 	}
791 
792 	private static final Comparator<Entry> ENTRY_CMP = new Comparator<Entry>() {
793 		@Override
794 		public int compare(Entry a, Entry b) {
795 			return Paths.compare(
796 					a.encodedName, 0, a.encodedNameLen, a.getMode().getBits(),
797 					b.encodedName, 0, b.encodedNameLen, b.getMode().getBits());
798 		}
799 	};
800 
801 	/**
802 	 * Constructor helper.
803 	 *
804 	 * @param list
805 	 *            files in the subtree of the work tree this iterator operates
806 	 *            on
807 	 */
808 	protected void init(Entry[] list) {
809 		// Filter out nulls, . and .. as these are not valid tree entries,
810 		// also cache the encoded forms of the path names for efficient use
811 		// later on during sorting and iteration.
812 		//
813 		entries = list;
814 		int i, o;
815 
816 		final CharsetEncoder nameEncoder = state.nameEncoder;
817 		for (i = 0, o = 0; i < entries.length; i++) {
818 			final Entry e = entries[i];
819 			if (e == null)
820 				continue;
821 			final String name = e.getName();
822 			if (".".equals(name) || "..".equals(name)) //$NON-NLS-1$ //$NON-NLS-2$
823 				continue;
824 			if (Constants.DOT_GIT.equals(name))
825 				continue;
826 			if (Constants.DOT_GIT_IGNORE.equals(name))
827 				ignoreNode = new PerDirectoryIgnoreNode(e);
828 			if (Constants.DOT_GIT_ATTRIBUTES.equals(name))
829 				attributesNode = new PerDirectoryAttributesNode(e);
830 			if (i != o)
831 				entries[o] = e;
832 			e.encodeName(nameEncoder);
833 			o++;
834 		}
835 		entryCnt = o;
836 		Arrays.sort(entries, 0, entryCnt, ENTRY_CMP);
837 
838 		contentIdFromPtr = -1;
839 		ptr = 0;
840 		if (!eof())
841 			parseEntry();
842 		else if (pathLen == 0) // see bug 445363
843 			pathLen = pathOffset;
844 	}
845 
846 	/**
847 	 * Obtain the current entry from this iterator.
848 	 *
849 	 * @return the currently selected entry.
850 	 */
851 	protected Entry current() {
852 		return entries[ptr];
853 	}
854 
855 	/**
856 	 * The result of a metadata-comparison between the current entry and a
857 	 * {@link DirCacheEntry}
858 	 */
859 	public enum MetadataDiff {
860 		/**
861 		 * The entries are equal by metaData (mode, length,
862 		 * modification-timestamp) or the <code>assumeValid</code> attribute of
863 		 * the index entry is set
864 		 */
865 		EQUAL,
866 
867 		/**
868 		 * The entries are not equal by metaData (mode, length) or the
869 		 * <code>isUpdateNeeded</code> attribute of the index entry is set
870 		 */
871 		DIFFER_BY_METADATA,
872 
873 		/** index entry is smudged - can't use that entry for comparison */
874 		SMUDGED,
875 
876 		/**
877 		 * The entries are equal by metaData (mode, length) but differ by
878 		 * modification-timestamp.
879 		 */
880 		DIFFER_BY_TIMESTAMP
881 	}
882 
883 	/**
884 	 * Is the file mode of the current entry different than the given raw mode?
885 	 *
886 	 * @param rawMode
887 	 *            an int.
888 	 * @return true if different, false otherwise
889 	 */
890 	public boolean isModeDifferent(int rawMode) {
891 		// Determine difference in mode-bits of file and index-entry. In the
892 		// bitwise presentation of modeDiff we'll have a '1' when the two modes
893 		// differ at this position.
894 		int modeDiff = getEntryRawMode() ^ rawMode;
895 
896 		if (modeDiff == 0)
897 			return false;
898 
899 		// Do not rely on filemode differences in case of symbolic links
900 		if (getOptions().getSymLinks() == SymLinks.FALSE)
901 			if (FileMode.SYMLINK.equals(rawMode))
902 				return false;
903 
904 		// Ignore the executable file bits if WorkingTreeOptions tell me to
905 		// do so. Ignoring is done by setting the bits representing a
906 		// EXECUTABLE_FILE to '0' in modeDiff
907 		if (!state.options.isFileMode())
908 			modeDiff &= ~FileMode.EXECUTABLE_FILE.getBits();
909 		return modeDiff != 0;
910 	}
911 
912 	/**
913 	 * Compare the metadata (mode, length, modification-timestamp) of the
914 	 * current entry and a {@link org.eclipse.jgit.dircache.DirCacheEntry}
915 	 *
916 	 * @param entry
917 	 *            the {@link org.eclipse.jgit.dircache.DirCacheEntry} to compare
918 	 *            with
919 	 * @return a
920 	 *         {@link org.eclipse.jgit.treewalk.WorkingTreeIterator.MetadataDiff}
921 	 *         which tells whether and how the entries metadata differ
922 	 */
923 	public MetadataDiff compareMetadata(DirCacheEntry entry) {
924 		if (entry.isAssumeValid())
925 			return MetadataDiff.EQUAL;
926 
927 		if (entry.isUpdateNeeded())
928 			return MetadataDiff.DIFFER_BY_METADATA;
929 
930 		if (isModeDifferent(entry.getRawMode()))
931 			return MetadataDiff.DIFFER_BY_METADATA;
932 
933 		// Don't check for length or lastmodified on folders
934 		int type = mode & FileMode.TYPE_MASK;
935 		if (type == FileMode.TYPE_TREE || type == FileMode.TYPE_GITLINK)
936 			return MetadataDiff.EQUAL;
937 
938 		if (!entry.isSmudged() && entry.getLength() != (int) getEntryLength())
939 			return MetadataDiff.DIFFER_BY_METADATA;
940 
941 		// Cache and file timestamps may differ in resolution. Therefore don't
942 		// compare instants directly but use a comparator that compares only
943 		// up to the lower apparent resolution of either timestamp.
944 		//
945 		// If core.checkstat is set to "minimal", compare only the seconds part.
946 		Instant cacheLastModified = entry.getLastModifiedInstant();
947 		Instant fileLastModified = getEntryLastModifiedInstant();
948 		if (timestampComparator.compare(cacheLastModified, fileLastModified,
949 				getOptions().getCheckStat() == CheckStat.MINIMAL) != 0) {
950 			return MetadataDiff.DIFFER_BY_TIMESTAMP;
951 		}
952 
953 		if (entry.isSmudged()) {
954 			return MetadataDiff.SMUDGED;
955 		}
956 		// The file is clean when when comparing timestamps
957 		return MetadataDiff.EQUAL;
958 	}
959 
960 	/**
961 	 * Checks whether this entry differs from a given entry from the
962 	 * {@link org.eclipse.jgit.dircache.DirCache}.
963 	 *
964 	 * File status information is used and if status is same we consider the
965 	 * file identical to the state in the working directory. Native git uses
966 	 * more stat fields than we have accessible in Java.
967 	 *
968 	 * @param entry
969 	 *            the entry from the dircache we want to compare against
970 	 * @param forceContentCheck
971 	 *            True if the actual file content should be checked if
972 	 *            modification time differs.
973 	 * @param reader
974 	 *            access to repository objects if necessary. Should not be null.
975 	 * @return true if content is most likely different.
976 	 * @throws java.io.IOException
977 	 * @since 3.3
978 	 */
979 	public boolean isModified(DirCacheEntry entry, boolean forceContentCheck,
980 			ObjectReader reader) throws IOException {
981 		if (entry == null)
982 			return !FileMode.MISSING.equals(getEntryFileMode());
983 		MetadataDiff diff = compareMetadata(entry);
984 		switch (diff) {
985 		case DIFFER_BY_TIMESTAMP:
986 			if (forceContentCheck)
987 				// But we are told to look at content even though timestamps
988 				// tell us about modification
989 				return contentCheck(entry, reader);
990 			else
991 				// We are told to assume a modification if timestamps differs
992 				return true;
993 		case SMUDGED:
994 			// The file is clean by timestamps but the entry was smudged.
995 			// Lets do a content check
996 			return contentCheck(entry, reader);
997 		case EQUAL:
998 			if (mode == FileMode.SYMLINK.getBits()) {
999 				return contentCheck(entry, reader);
1000 			}
1001 			return false;
1002 		case DIFFER_BY_METADATA:
1003 			if (mode == FileMode.TREE.getBits()
1004 					&& entry.getFileMode().equals(FileMode.GITLINK)) {
1005 				byte[] idBuffer = idBuffer();
1006 				int idOffset = idOffset();
1007 				if (entry.getObjectId().compareTo(idBuffer, idOffset) == 0) {
1008 					return true;
1009 				} else if (ObjectId.zeroId().compareTo(idBuffer,
1010 						idOffset) == 0) {
1011 					return new File(repository.getWorkTree(),
1012 							entry.getPathString()).list().length > 0;
1013 				}
1014 				return false;
1015 			} else if (mode == FileMode.SYMLINK.getBits())
1016 				return contentCheck(entry, reader);
1017 			return true;
1018 		default:
1019 			throw new IllegalStateException(MessageFormat.format(
1020 					JGitText.get().unexpectedCompareResult, diff.name()));
1021 		}
1022 	}
1023 
1024 	/**
1025 	 * Get the file mode to use for the current entry when it is to be updated
1026 	 * in the index.
1027 	 *
1028 	 * @param indexIter
1029 	 *            {@link org.eclipse.jgit.dircache.DirCacheIterator} positioned
1030 	 *            at the same entry as this iterator or null if no
1031 	 *            {@link org.eclipse.jgit.dircache.DirCacheIterator} is
1032 	 *            available at this iterator's current entry
1033 	 * @return index file mode
1034 	 */
1035 	public FileMode getIndexFileMode(DirCacheIterator indexIter) {
1036 		final FileMode wtMode = getEntryFileMode();
1037 		if (indexIter == null) {
1038 			return wtMode;
1039 		}
1040 		final FileMode iMode = indexIter.getEntryFileMode();
1041 		if (getOptions().isFileMode() && iMode != FileMode.GITLINK && iMode != FileMode.TREE) {
1042 			return wtMode;
1043 		}
1044 		if (!getOptions().isFileMode()) {
1045 			if (FileMode.REGULAR_FILE == wtMode
1046 					&& FileMode.EXECUTABLE_FILE == iMode) {
1047 				return iMode;
1048 			}
1049 			if (FileMode.EXECUTABLE_FILE == wtMode
1050 					&& FileMode.REGULAR_FILE == iMode) {
1051 				return iMode;
1052 			}
1053 		}
1054 		if (FileMode.GITLINK == iMode
1055 				&& FileMode.TREE == wtMode) {
1056 			return iMode;
1057 		}
1058 		if (FileMode.TREE == iMode
1059 				&& FileMode.GITLINK == wtMode) {
1060 			return iMode;
1061 		}
1062 		return wtMode;
1063 	}
1064 
1065 	/**
1066 	 * Compares the entries content with the content in the filesystem.
1067 	 * Unsmudges the entry when it is detected that it is clean.
1068 	 *
1069 	 * @param entry
1070 	 *            the entry to be checked
1071 	 * @param reader
1072 	 *            acccess to repository data if necessary
1073 	 * @return <code>true</code> if the content doesn't match,
1074 	 *         <code>false</code> if it matches
1075 	 * @throws IOException
1076 	 */
1077 	private boolean contentCheck(DirCacheEntry entry, ObjectReader reader)
1078 			throws IOException {
1079 		if (getEntryObjectId().equals(entry.getObjectId())) {
1080 			// Content has not changed
1081 
1082 			// We know the entry can't be racily clean because it's still clean.
1083 			// Therefore we unsmudge the entry!
1084 			// If by any chance we now unsmudge although we are still in the
1085 			// same time-slot as the last modification to the index file the
1086 			// next index write operation will smudge again.
1087 			// Caution: we are unsmudging just by setting the length of the
1088 			// in-memory entry object. It's the callers task to detect that we
1089 			// have modified the entry and to persist the modified index.
1090 			entry.setLength((int) getEntryLength());
1091 
1092 			return false;
1093 		} else {
1094 			if (mode == FileMode.SYMLINK.getBits()) {
1095 				return !new File(readSymlinkTarget(current())).equals(
1096 						new File(readContentAsNormalizedString(entry, reader)));
1097 			}
1098 			// Content differs: that's a real change, perhaps
1099 			if (reader == null) // deprecated use, do no further checks
1100 				return true;
1101 
1102 			switch (getEolStreamType()) {
1103 			case DIRECT:
1104 				return true;
1105 			default:
1106 				try {
1107 					ObjectLoader loader = reader.open(entry.getObjectId());
1108 					if (loader == null)
1109 						return true;
1110 
1111 					// We need to compute the length, but only if it is not
1112 					// a binary stream.
1113 					long dcInLen;
1114 					try (InputStream dcIn = new AutoLFInputStream(
1115 							loader.openStream(), true,
1116 							true /* abort if binary */)) {
1117 						dcInLen = computeLength(dcIn);
1118 					} catch (AutoLFInputStream.IsBinaryException e) {
1119 						return true;
1120 					}
1121 
1122 					try (InputStream dcIn = new AutoLFInputStream(
1123 							loader.openStream(), true)) {
1124 						byte[] autoCrLfHash = computeHash(dcIn, dcInLen);
1125 						boolean changed = getEntryObjectId()
1126 								.compareTo(autoCrLfHash, 0) != 0;
1127 						return changed;
1128 					}
1129 				} catch (IOException e) {
1130 					return true;
1131 				}
1132 			}
1133 		}
1134 	}
1135 
1136 	private static String readContentAsNormalizedString(DirCacheEntry entry,
1137 			ObjectReader reader) throws MissingObjectException, IOException {
1138 		ObjectLoader open = reader.open(entry.getObjectId());
1139 		byte[] cachedBytes = open.getCachedBytes();
1140 		return FS.detect().normalize(RawParseUtils.decode(cachedBytes));
1141 	}
1142 
1143 	/**
1144 	 * Reads the target of a symlink as a string. This default implementation
1145 	 * fully reads the entry's input stream and converts it to a normalized
1146 	 * string. Subclasses may override to provide more specialized
1147 	 * implementations.
1148 	 *
1149 	 * @param entry
1150 	 *            to read
1151 	 * @return the entry's content as a normalized string
1152 	 * @throws java.io.IOException
1153 	 *             if the entry cannot be read or does not denote a symlink
1154 	 * @since 4.6
1155 	 */
1156 	protected String readSymlinkTarget(Entry entry) throws IOException {
1157 		if (!entry.getMode().equals(FileMode.SYMLINK)) {
1158 			throw new java.nio.file.NotLinkException(entry.getName());
1159 		}
1160 		long length = entry.getLength();
1161 		byte[] content = new byte[(int) length];
1162 		try (InputStream is = entry.openInputStream()) {
1163 			int bytesRead = IO.readFully(is, content, 0);
1164 			return FS.detect()
1165 					.normalize(RawParseUtils.decode(content, 0, bytesRead));
1166 		}
1167 	}
1168 
1169 	private static long computeLength(InputStream in) throws IOException {
1170 		// Since we only care about the length, use skip. The stream
1171 		// may be able to more efficiently wade through its data.
1172 		//
1173 		long length = 0;
1174 		for (;;) {
1175 			long n = in.skip(1 << 20);
1176 			if (n <= 0)
1177 				break;
1178 			length += n;
1179 		}
1180 		return length;
1181 	}
1182 
1183 	private byte[] computeHash(InputStream in, long length) throws IOException {
1184 		SHA1 contentDigest = SHA1.newInstance();
1185 		final byte[] contentReadBuffer = state.contentReadBuffer;
1186 
1187 		contentDigest.update(hblob);
1188 		contentDigest.update((byte) ' ');
1189 
1190 		long sz = length;
1191 		if (sz == 0) {
1192 			contentDigest.update((byte) '0');
1193 		} else {
1194 			final int bufn = contentReadBuffer.length;
1195 			int p = bufn;
1196 			do {
1197 				contentReadBuffer[--p] = digits[(int) (sz % 10)];
1198 				sz /= 10;
1199 			} while (sz > 0);
1200 			contentDigest.update(contentReadBuffer, p, bufn - p);
1201 		}
1202 		contentDigest.update((byte) 0);
1203 
1204 		for (;;) {
1205 			final int r = in.read(contentReadBuffer);
1206 			if (r <= 0)
1207 				break;
1208 			contentDigest.update(contentReadBuffer, 0, r);
1209 			sz += r;
1210 		}
1211 		if (sz != length)
1212 			return zeroid;
1213 		return contentDigest.digest();
1214 	}
1215 
1216 	/**
1217 	 * A single entry within a working directory tree.
1218 	 *
1219 	 * @since 5.0
1220 	 */
1221 	public static abstract class Entry {
1222 		byte[] encodedName;
1223 
1224 		int encodedNameLen;
1225 
1226 		void encodeName(CharsetEncoder enc) {
1227 			final ByteBuffer b;
1228 			try {
1229 				b = enc.encode(CharBuffer.wrap(getName()));
1230 			} catch (CharacterCodingException e) {
1231 				// This should so never happen.
1232 				throw new RuntimeException(MessageFormat.format(
1233 						JGitText.get().unencodeableFile, getName()));
1234 			}
1235 
1236 			encodedNameLen = b.limit();
1237 			if (b.hasArray() && b.arrayOffset() == 0)
1238 				encodedName = b.array();
1239 			else
1240 				b.get(encodedName = new byte[encodedNameLen]);
1241 		}
1242 
1243 		@Override
1244 		public String toString() {
1245 			return getMode().toString() + " " + getName(); //$NON-NLS-1$
1246 		}
1247 
1248 		/**
1249 		 * Get the type of this entry.
1250 		 * <p>
1251 		 * <b>Note: Efficient implementation required.</b>
1252 		 * <p>
1253 		 * The implementation of this method must be efficient. If a subclass
1254 		 * needs to compute the value they should cache the reference within an
1255 		 * instance member instead.
1256 		 *
1257 		 * @return a file mode constant from {@link FileMode}.
1258 		 */
1259 		public abstract FileMode getMode();
1260 
1261 		/**
1262 		 * Get the byte length of this entry.
1263 		 * <p>
1264 		 * <b>Note: Efficient implementation required.</b>
1265 		 * <p>
1266 		 * The implementation of this method must be efficient. If a subclass
1267 		 * needs to compute the value they should cache the reference within an
1268 		 * instance member instead.
1269 		 *
1270 		 * @return size of this file, in bytes.
1271 		 */
1272 		public abstract long getLength();
1273 
1274 		/**
1275 		 * Get the last modified time of this entry.
1276 		 * <p>
1277 		 * <b>Note: Efficient implementation required.</b>
1278 		 * <p>
1279 		 * The implementation of this method must be efficient. If a subclass
1280 		 * needs to compute the value they should cache the reference within an
1281 		 * instance member instead.
1282 		 *
1283 		 * @return time since the epoch (in ms) of the last change.
1284 		 * @deprecated use {@link #getLastModifiedInstant()} instead
1285 		 */
1286 		@Deprecated
1287 		public abstract long getLastModified();
1288 
1289 		/**
1290 		 * Get the last modified time of this entry.
1291 		 * <p>
1292 		 * <b>Note: Efficient implementation required.</b>
1293 		 * <p>
1294 		 * The implementation of this method must be efficient. If a subclass
1295 		 * needs to compute the value they should cache the reference within an
1296 		 * instance member instead.
1297 		 *
1298 		 * @return time of the last change.
1299 		 * @since 5.1.9
1300 		 */
1301 		public abstract Instant getLastModifiedInstant();
1302 
1303 		/**
1304 		 * Get the name of this entry within its directory.
1305 		 * <p>
1306 		 * Efficient implementations are not required. The caller will obtain
1307 		 * the name only once and cache it once obtained.
1308 		 *
1309 		 * @return name of the entry.
1310 		 */
1311 		public abstract String getName();
1312 
1313 		/**
1314 		 * Obtain an input stream to read the file content.
1315 		 * <p>
1316 		 * Efficient implementations are not required. The caller will usually
1317 		 * obtain the stream only once per entry, if at all.
1318 		 * <p>
1319 		 * The input stream should not use buffering if the implementation can
1320 		 * avoid it. The caller will buffer as necessary to perform efficient
1321 		 * block IO operations.
1322 		 * <p>
1323 		 * The caller will close the stream once complete.
1324 		 *
1325 		 * @return a stream to read from the file.
1326 		 * @throws IOException
1327 		 *             the file could not be opened for reading.
1328 		 */
1329 		public abstract InputStream openInputStream() throws IOException;
1330 	}
1331 
1332 	/** Magic type indicating we know rules exist, but they aren't loaded. */
1333 	private static class PerDirectoryIgnoreNode extends IgnoreNode {
1334 		final Entry entry;
1335 
1336 		PerDirectoryIgnoreNode(Entry entry) {
1337 			super(Collections.<FastIgnoreRule> emptyList());
1338 			this.entry = entry;
1339 		}
1340 
1341 		IgnoreNode load() throws IOException {
1342 			IgnoreNode r = new IgnoreNode();
1343 			try (InputStream in = entry.openInputStream()) {
1344 				r.parse(in);
1345 			}
1346 			return r.getRules().isEmpty() ? null : r;
1347 		}
1348 	}
1349 
1350 	/** Magic type indicating there may be rules for the top level. */
1351 	private static class RootIgnoreNode extends PerDirectoryIgnoreNode {
1352 		final Repository repository;
1353 
1354 		RootIgnoreNode(Entry entry, Repository repository) {
1355 			super(entry);
1356 			this.repository = repository;
1357 		}
1358 
1359 		@Override
1360 		IgnoreNode load() throws IOException {
1361 			IgnoreNode r;
1362 			if (entry != null) {
1363 				r = super.load();
1364 				if (r == null)
1365 					r = new IgnoreNode();
1366 			} else {
1367 				r = new IgnoreNode();
1368 			}
1369 
1370 			FS fs = repository.getFS();
1371 			String path = repository.getConfig().get(CoreConfig.KEY)
1372 					.getExcludesFile();
1373 			if (path != null) {
1374 				File excludesfile;
1375 				if (path.startsWith("~/")) //$NON-NLS-1$
1376 					excludesfile = fs.resolve(fs.userHome(), path.substring(2));
1377 				else
1378 					excludesfile = fs.resolve(null, path);
1379 				loadRulesFromFile(r, excludesfile);
1380 			}
1381 
1382 			File exclude = fs.resolve(repository.getDirectory(),
1383 					Constants.INFO_EXCLUDE);
1384 			loadRulesFromFile(r, exclude);
1385 
1386 			return r.getRules().isEmpty() ? null : r;
1387 		}
1388 
1389 		private static void loadRulesFromFile(IgnoreNode r, File exclude)
1390 				throws FileNotFoundException, IOException {
1391 			if (FS.DETECTED.exists(exclude)) {
1392 				try (FileInputStream in = new FileInputStream(exclude)) {
1393 					r.parse(in);
1394 				}
1395 			}
1396 		}
1397 	}
1398 
1399 	/** Magic type indicating we know rules exist, but they aren't loaded. */
1400 	private static class PerDirectoryAttributesNode extends AttributesNode {
1401 		final Entry entry;
1402 
1403 		PerDirectoryAttributesNode(Entry entry) {
1404 			super(Collections.<AttributesRule> emptyList());
1405 			this.entry = entry;
1406 		}
1407 
1408 		AttributesNode load() throws IOException {
1409 			AttributesNode r = new AttributesNode();
1410 			try (InputStream in = entry.openInputStream()) {
1411 				r.parse(in);
1412 			}
1413 			return r.getRules().isEmpty() ? null : r;
1414 		}
1415 	}
1416 
1417 
1418 	private static final class IteratorState {
1419 		/** Options used to process the working tree. */
1420 		final WorkingTreeOptions options;
1421 
1422 		/** File name character encoder. */
1423 		final CharsetEncoder nameEncoder;
1424 
1425 		/** Buffer used to perform {@link #contentId} computations. */
1426 		byte[] contentReadBuffer;
1427 
1428 		/** TreeWalk with a (supposedly) matching DirCacheIterator. */
1429 		TreeWalk walk;
1430 
1431 		/** Position of the matching {@link DirCacheIterator}. */
1432 		int dirCacheTree = -1;
1433 
1434 		/** Whether the iterator shall walk ignored directories. */
1435 		boolean walkIgnored = false;
1436 
1437 		final Map<String, Boolean> directoryToIgnored = new HashMap<>();
1438 
1439 		IteratorState(WorkingTreeOptions options) {
1440 			this.options = options;
1441 			this.nameEncoder = UTF_8.newEncoder();
1442 		}
1443 
1444 		void initializeReadBuffer() {
1445 			if (contentReadBuffer == null) {
1446 				contentReadBuffer = new byte[BUFFER_SIZE];
1447 			}
1448 		}
1449 	}
1450 
1451 	/**
1452 	 * Get the clean filter command for the current entry.
1453 	 *
1454 	 * @return the clean filter command for the current entry or
1455 	 *         <code>null</code> if no such command is defined
1456 	 * @throws java.io.IOException
1457 	 * @since 4.2
1458 	 */
1459 	public String getCleanFilterCommand() throws IOException {
1460 		if (cleanFilterCommandHolder == null) {
1461 			String cmd = null;
1462 			if (state.walk != null) {
1463 				cmd = state.walk
1464 						.getFilterCommand(Constants.ATTR_FILTER_TYPE_CLEAN);
1465 			}
1466 			cleanFilterCommandHolder = new Holder<>(cmd);
1467 		}
1468 		return cleanFilterCommandHolder.get();
1469 	}
1470 
1471 	/**
1472 	 * Get the eol stream type for the current entry.
1473 	 *
1474 	 * @return the eol stream type for the current entry or <code>null</code> if
1475 	 *         it cannot be determined. When state or state.walk is null or the
1476 	 *         {@link org.eclipse.jgit.treewalk.TreeWalk} is not based on a
1477 	 *         {@link org.eclipse.jgit.lib.Repository} then null is returned.
1478 	 * @throws java.io.IOException
1479 	 * @since 4.3
1480 	 */
1481 	public EolStreamType getEolStreamType() throws IOException {
1482 		return getEolStreamType(null);
1483 	}
1484 
1485 	/**
1486 	 * @param opType
1487 	 *            The operationtype (checkin/checkout) which should be used
1488 	 * @return the eol stream type for the current entry or <code>null</code> if
1489 	 *         it cannot be determined. When state or state.walk is null or the
1490 	 *         {@link TreeWalk} is not based on a {@link Repository} then null
1491 	 *         is returned.
1492 	 * @throws IOException
1493 	 */
1494 	private EolStreamType getEolStreamType(OperationType opType)
1495 			throws IOException {
1496 		if (eolStreamTypeHolder == null) {
1497 			EolStreamType type=null;
1498 			if (state.walk != null) {
1499 				type = state.walk.getEolStreamType(opType);
1500 			} else {
1501 				switch (getOptions().getAutoCRLF()) {
1502 				case FALSE:
1503 					type = EolStreamType.DIRECT;
1504 					break;
1505 				case TRUE:
1506 				case INPUT:
1507 					type = EolStreamType.AUTO_LF;
1508 					break;
1509 				}
1510 			}
1511 			eolStreamTypeHolder = new Holder<>(type);
1512 		}
1513 		return eolStreamTypeHolder.get();
1514 	}
1515 
1516 	private boolean isDirectoryIgnored(String pathRel) throws IOException {
1517 		final int pOff = 0 < pathOffset ? pathOffset - 1 : pathOffset;
1518 		final String base = TreeWalk.pathOf(this.path, 0, pOff);
1519 		final String pathAbs = concatPath(base, pathRel);
1520 		return isDirectoryIgnored(pathRel, pathAbs);
1521 	}
1522 
1523 	private boolean isDirectoryIgnored(String pathRel, String pathAbs)
1524 			throws IOException {
1525 		assert pathRel.length() == 0 || (pathRel.charAt(0) != '/'
1526 				&& pathRel.charAt(pathRel.length() - 1) != '/');
1527 		assert pathAbs.length() == 0 || (pathAbs.charAt(0) != '/'
1528 				&& pathAbs.charAt(pathAbs.length() - 1) != '/');
1529 		assert pathAbs.endsWith(pathRel);
1530 
1531 		Boolean ignored = state.directoryToIgnored.get(pathAbs);
1532 		if (ignored != null) {
1533 			return ignored.booleanValue();
1534 		}
1535 
1536 		final String parentRel = getParentPath(pathRel);
1537 		if (parentRel != null && isDirectoryIgnored(parentRel)) {
1538 			state.directoryToIgnored.put(pathAbs, Boolean.TRUE);
1539 			return true;
1540 		}
1541 
1542 		final IgnoreNode node = getIgnoreNode();
1543 		for (String p = pathRel; node != null
1544 				&& !"".equals(p); p = getParentPath(p)) { //$NON-NLS-1$
1545 			ignored = node.checkIgnored(p, true);
1546 			if (ignored != null) {
1547 				state.directoryToIgnored.put(pathAbs, ignored);
1548 				return ignored.booleanValue();
1549 			}
1550 		}
1551 
1552 		if (!(this.parent instanceof WorkingTreeIterator)) {
1553 			state.directoryToIgnored.put(pathAbs, Boolean.FALSE);
1554 			return false;
1555 		}
1556 
1557 		final WorkingTreeIterator wtParent = (WorkingTreeIterator) this.parent;
1558 		final String parentRelPath = concatPath(
1559 				TreeWalk.pathOf(this.path, wtParent.pathOffset, pathOffset - 1),
1560 				pathRel);
1561 		assert concatPath(TreeWalk.pathOf(wtParent.path, 0,
1562 				Math.max(0, wtParent.pathOffset - 1)), parentRelPath)
1563 						.equals(pathAbs);
1564 		return wtParent.isDirectoryIgnored(parentRelPath, pathAbs);
1565 	}
1566 
1567 	private static String getParentPath(String path) {
1568 		final int slashIndex = path.lastIndexOf('/', path.length() - 2);
1569 		if (slashIndex > 0) {
1570 			return path.substring(path.charAt(0) == '/' ? 1 : 0, slashIndex);
1571 		}
1572 		return path.length() > 0 ? "" : null; //$NON-NLS-1$
1573 	}
1574 
1575 	private static String concatPath(String p1, String p2) {
1576 		return p1 + (p1.length() > 0 && p2.length() > 0 ? "/" : "") + p2; //$NON-NLS-1$ //$NON-NLS-2$
1577 	}
1578 }