Class WorkingTreeIterator
- Direct Known Subclasses:
FileTreeIterator
TreeWalk
.
Most applications will want to use the standard implementation of this
iterator, FileTreeIterator
, as that does
all IO through the standard java.io
package. Plugins for a Java
based IDE may however wish to create their own implementations of this class
to allow traversal of the IDE's project space, as well as benefit from any
caching the IDE may have.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A single entry within a working directory tree.private static final class
static enum
The result of a metadata-comparison between the current entry and aDirCacheEntry
private static class
Magic type indicating we know rules exist, but they aren't loaded.private static class
Magic type indicating we know rules exist, but they aren't loaded.private static class
Magic type indicating there may be rules for the top level. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final int
Size we perform file IO in if we have to read and hash a file.private long
Cached canonical length, initialized fromidBuffer()
cached clean filter command.private byte[]
TheidBuffer()
for the current entry.private int
private int
The offset of the content id inidBuffer()
private static final byte[]
private WorkingTreeIterator.Entry[]
List of entries obtained from the subclass.private static final Comparator
<WorkingTreeIterator.Entry> private int
Total number of entries inentries
that are valid.protected static final WorkingTreeIterator.Entry[]
An empty entry array, suitable forinit(Entry[])
.private Holder
<CoreConfig.EolStreamType> cached eol stream type.private static final byte[]
private IgnoreNode
If there is a .gitignore file present, the parsed rules from it.private static final int
private static final long
Maximum size of files which may be read fully into memory for performance reasons.private int
Current position withinentries
.protected Repository
Repository that is the root level being iterated overprivate final WorkingTreeIterator.IteratorState
Inherited state of this iterator, describing working tree, etc.private final InstantComparator
A comparator forInstant
s.Fields inherited from class org.eclipse.jgit.treewalk.AbstractTreeIterator
attributesNode, DEFAULT_PATH_SIZE, matches, matchShift, mode, parent, path, pathLen, pathOffset, zeroid
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
WorkingTreeIterator
(String prefix, WorkingTreeOptions options) Create a new iterator with no parent and a prefix.protected
Create an iterator for a subtree of an existing iterator.protected
WorkingTreeIterator
(WorkingTreeOptions options) Create a new iterator with no parent. -
Method Summary
Modifier and TypeMethodDescriptionvoid
back
(int delta) Move to prior entry, populating this iterator with the entry data.compareMetadata
(DirCacheEntry entry) Compare the metadata (mode, length, modification-timestamp) of the current entry and aDirCacheEntry
private byte[]
computeHash
(InputStream in, long length) private static long
private static String
concatPath
(String p1, String p2) private boolean
contentCheck
(DirCacheEntry entry, ObjectReader reader) Compares the entries content with the content in the filesystem.protected WorkingTreeIterator.Entry
current()
Obtain the current entry from this iterator.boolean
eof()
Is this tree iterator at its EOF point (no more entries)?private ByteBuffer
filterClean
(byte[] src, int n, TreeWalk.OperationType opType) private InputStream
private InputStream
filterClean
(InputStream in, TreeWalk.OperationType opType) boolean
first()
Is this tree iterator positioned on its first entry?Get the clean filter command for the current entry.protected DirCacheIterator
Retrieves theDirCacheIterator
at the current entry ifsetDirCacheIterator(TreeWalk, int)
was called.Retrieves theAttributesNode
for the current entry.long
Get the filtered input length of this entrylong
Deprecated.Get the last modified time of this entry.long
Get the raw byte length of this entry.Get the eol stream type for the current entry.private CoreConfig.EolStreamType
private IgnoreNode
getIndexFileMode
(DirCacheIterator indexIter) Get the file mode to use for the current entry when it is to be updated in the index.Returns the working tree options used by this iterator.private static String
getParentPath
(String path) Retrieves theRepository
thisWorkingTreeIterator
operates on.private InputStream
handleAutoCRLF
(InputStream in, TreeWalk.OperationType opType) private boolean
hasCrLfInIndex
(DirCacheIterator dirCache) Determines whether the file was committed un-normalized.boolean
hasId()
Whether the entry has a valid ObjectId.byte[]
idBuffer()
Get the byte array buffer object IDs must be copied out of.private byte[]
int
idOffset()
Get the position withinAbstractTreeIterator.idBuffer()
of this entry's ObjectId.protected byte[]
idSubmodule
(File directory, WorkingTreeIterator.Entry e) Get submodule id using the repository at the location of the entry relative to the directory.protected byte[]
Get submodule id for given entry.protected void
init
(WorkingTreeIterator.Entry[] list) Constructor helper.protected void
initRootIterator
(Repository repo) Initialize this iterator for the root level of a repository.private static boolean
private boolean
isDirectoryIgnored
(String pathRel) private boolean
isDirectoryIgnored
(String pathRel, String pathAbs) boolean
Determine if the current entry path is ignored by an ignore rule.protected boolean
isEntryIgnored
(int pLen) Determine if the entry path is ignored by an ignore rule.private boolean
isEntryIgnored
(int pLen, int fileMode) Determine if the entry path is ignored by an ignore rule.boolean
isModeDifferent
(int rawMode) Is the file mode of the current entry different than the given raw mode?boolean
isModified
(DirCacheEntry entry, boolean forceContentCheck, ObjectReader reader) Checks whether this entry differs from a given entry from theDirCache
.boolean
Whether or not this Iterator is iterating through the working tree.void
next
(int delta) Move to next entry, populating this iterator with the entry data.Obtain an input stream to read the file content.private void
private InputStream
possiblyFilteredInputStream
(WorkingTreeIterator.Entry e, InputStream is, long len) private InputStream
possiblyFilteredInputStream
(WorkingTreeIterator.Entry e, InputStream is, long len, TreeWalk.OperationType opType) private static String
readContentAsNormalizedString
(DirCacheEntry entry, ObjectReader reader) protected String
Reads the target of a symlink as a string.void
reset()
Position this iterator on the first entry.private static void
safeClose
(InputStream in) void
setDirCacheIterator
(TreeWalk walk, int treeId) Define the matchingDirCacheIterator
, to optimize ObjectIds.void
setWalkIgnoredDirectories
(boolean includeIgnored) Defines whether thisWorkingTreeIterator
walks ignored directories.boolean
Tells whether thisWorkingTreeIterator
walks ignored directories.Methods inherited from class org.eclipse.jgit.treewalk.AbstractTreeIterator
createEmptyTreeIterator, createSubtreeIterator, createSubtreeIterator, ensurePathCapacity, findFile, findFile, getEntryFileMode, getEntryObjectId, getEntryObjectId, getEntryPathBuffer, getEntryPathHashCode, getEntryPathLength, getEntryPathString, getEntryRawMode, getName, getNameLength, getNameOffset, growPath, idEqual, needsStopWalk, pathCompare, pathCompare, pathCompare, skip, stopWalk, toString
-
Field Details
-
MAX_EXCEPTION_TEXT_SIZE
private static final int MAX_EXCEPTION_TEXT_SIZE- See Also:
-
EOF
An empty entry array, suitable forinit(Entry[])
. -
BUFFER_SIZE
static final int BUFFER_SIZESize we perform file IO in if we have to read and hash a file.- See Also:
-
MAXIMUM_FILE_SIZE_TO_READ_FULLY
private static final long MAXIMUM_FILE_SIZE_TO_READ_FULLYMaximum size of files which may be read fully into memory for performance reasons.- See Also:
-
state
Inherited state of this iterator, describing working tree, etc. -
contentId
private byte[] contentIdTheidBuffer()
for the current entry. -
contentIdFromPtr
private int contentIdFromPtr -
entries
List of entries obtained from the subclass. -
entryCnt
private int entryCntTotal number of entries inentries
that are valid. -
ptr
private int ptrCurrent position withinentries
. -
ignoreNode
If there is a .gitignore file present, the parsed rules from it. -
cleanFilterCommandHolder
cached clean filter command. Use a Ref in order to distinguish between the ref not cached yet and the value null -
eolStreamTypeHolder
cached eol stream type. Use a Ref in order to distinguish between the ref not cached yet and the value null -
repository
Repository that is the root level being iterated over -
canonLen
private long canonLenCached canonical length, initialized fromidBuffer()
-
contentIdOffset
private int contentIdOffsetThe offset of the content id inidBuffer()
-
timestampComparator
A comparator forInstant
s. -
digits
private static final byte[] digits -
hblob
private static final byte[] hblob -
ENTRY_CMP
-
-
Constructor Details
-
WorkingTreeIterator
Create a new iterator with no parent.- Parameters:
options
- working tree options to be used
-
WorkingTreeIterator
Create a new iterator with no parent and a prefix.The prefix path supplied is inserted in front of all paths generated by this iterator. It is intended to be used when an iterator is being created for a subsection of an overall repository and needs to be combined with other iterators that are created to run over the entire repository namespace.
- Parameters:
prefix
- position of this iterator in the repository tree. The value may be null or the empty string to indicate the prefix is the root of the repository. A trailing slash ('/') is automatically appended if the prefix does not end in '/'.options
- working tree options to be used
-
WorkingTreeIterator
Create an iterator for a subtree of an existing iterator.- Parameters:
p
- parent tree iterator.
-
-
Method Details
-
initRootIterator
Initialize this iterator for the root level of a repository.This method should only be invoked after calling
init(Entry[])
, and only for the root iterator.- Parameters:
repo
- the repository.
-
setDirCacheIterator
Define the matchingDirCacheIterator
, to optimize ObjectIds. Once the DirCacheIterator has been set this iterator must only be advanced by the TreeWalk that is supplied, as it assumes that itself and the corresponding DirCacheIterator are positioned on the same file path wheneveridBuffer()
is invoked.- Parameters:
walk
- the walk that will be advancing this iterator.treeId
- index of the matchingDirCacheIterator
.
-
getDirCacheIterator
Retrieves theDirCacheIterator
at the current entry ifsetDirCacheIterator(TreeWalk, int)
was called.- Returns:
- the DirCacheIterator, or
null
if not set or not at the current entry - Since:
- 5.0
-
setWalkIgnoredDirectories
public void setWalkIgnoredDirectories(boolean includeIgnored) Defines whether thisWorkingTreeIterator
walks ignored directories.- Parameters:
includeIgnored
-false
to skip ignored directories, if possible;true
to always include them in the walk- Since:
- 5.0
-
walksIgnoredDirectories
public boolean walksIgnoredDirectories()Tells whether thisWorkingTreeIterator
walks ignored directories.- Returns:
true
if it does,false
otherwise- Since:
- 5.0
-
hasId
public boolean hasId()Whether the entry has a valid ObjectId.- Specified by:
hasId
in classAbstractTreeIterator
- Returns:
true
if the entry has a valid ObjectId.
-
idBuffer
public byte[] idBuffer()Get the byte array buffer object IDs must be copied out of.The id buffer contains the bytes necessary to construct an ObjectId for the current entry of this iterator. The buffer can be the same buffer for all entries, or it can be a unique buffer per-entry. Implementations are encouraged to expose their private buffer whenever possible to reduce garbage generation and copying costs.
- Specified by:
idBuffer
in classAbstractTreeIterator
- Returns:
- byte array the implementation stores object IDs within.
- See Also:
-
isWorkTree
public boolean isWorkTree()Whether or not this Iterator is iterating through the working tree.- Overrides:
isWorkTree
in classAbstractTreeIterator
- Returns:
- whether or not this Iterator is iterating through the working tree
-
idSubmodule
Get submodule id for given entry.- Parameters:
e
- aWorkingTreeIterator.Entry
object.- Returns:
- non-null submodule id
-
idSubmodule
Get submodule id using the repository at the location of the entry relative to the directory.- Parameters:
directory
- aFile
object.e
- aWorkingTreeIterator.Entry
object.- Returns:
- non-null submodule id
-
idBufferBlob
-
possiblyFilteredInputStream
private InputStream possiblyFilteredInputStream(WorkingTreeIterator.Entry e, InputStream is, long len) throws IOException - Throws:
IOException
-
possiblyFilteredInputStream
private InputStream possiblyFilteredInputStream(WorkingTreeIterator.Entry e, InputStream is, long len, TreeWalk.OperationType opType) throws IOException - Throws:
IOException
-
safeClose
-
isBinary
- Throws:
IOException
-
filterClean
- Throws:
IOException
-
filterClean
- Throws:
IOException
-
filterClean
- Throws:
IOException
-
handleAutoCRLF
private InputStream handleAutoCRLF(InputStream in, TreeWalk.OperationType opType) throws IOException - Throws:
IOException
-
getOptions
Returns the working tree options used by this iterator.- Returns:
- working tree options
-
getRepository
Retrieves theRepository
thisWorkingTreeIterator
operates on.- Returns:
- the
Repository
- Since:
- 5.9
-
idOffset
public int idOffset()Get the position withinAbstractTreeIterator.idBuffer()
of this entry's ObjectId.- Specified by:
idOffset
in classAbstractTreeIterator
- Returns:
- offset into the array returned by
AbstractTreeIterator.idBuffer()
where the ObjectId must be copied out of.
-
reset
public void reset()Position this iterator on the first entry. The default implementation of this method usesback(1)
untilfirst()
is true. This is most likely not the most efficient method of repositioning the iterator to its first entry, so subclasses are strongly encouraged to override the method.- Overrides:
reset
in classAbstractTreeIterator
-
first
public boolean first()Is this tree iterator positioned on its first entry?An iterator is positioned on the first entry if
back(1)
would be an invalid request as there is no entry before the current one.An empty iterator (one with no entries) will be
first() && eof()
.- Specified by:
first
in classAbstractTreeIterator
- Returns:
- true if the iterator is positioned on the first entry.
-
eof
public boolean eof()Is this tree iterator at its EOF point (no more entries)?An iterator is at EOF if there is no current entry.
- Specified by:
eof
in classAbstractTreeIterator
- Returns:
- true if we have walked all entries and have none left.
-
next
Move to next entry, populating this iterator with the entry data.The delta indicates how many moves forward should occur. The most common delta is 1 to move to the next entry.
Implementations must populate the following members:
AbstractTreeIterator.mode
AbstractTreeIterator.path
(fromAbstractTreeIterator.pathOffset
toAbstractTreeIterator.pathLen
)AbstractTreeIterator.pathLen
AbstractTreeIterator.idBuffer()
andAbstractTreeIterator.idOffset()
when demanded.- Specified by:
next
in classAbstractTreeIterator
- Parameters:
delta
- number of entries to move the iterator by. Must be a positive, non-zero integer.- Throws:
CorruptObjectException
- the tree is invalid.
-
back
Move to prior entry, populating this iterator with the entry data.The delta indicates how many moves backward should occur.The most common delta is 1 to move to the prior entry.
Implementations must populate the following members:
AbstractTreeIterator.mode
AbstractTreeIterator.path
(fromAbstractTreeIterator.pathOffset
toAbstractTreeIterator.pathLen
)AbstractTreeIterator.pathLen
AbstractTreeIterator.idBuffer()
andAbstractTreeIterator.idOffset()
when demanded.- Specified by:
back
in classAbstractTreeIterator
- Parameters:
delta
- number of entries to move the iterator by. Must be a positive, non-zero integer.- Throws:
CorruptObjectException
- the tree is invalid.
-
parseEntry
private void parseEntry() -
getEntryLength
public long getEntryLength()Get the raw byte length of this entry.- Returns:
- size of this file, in bytes.
-
getEntryContentLength
Get the filtered input length of this entry- Returns:
- size of the content, in bytes
- Throws:
IOException
-
getEntryLastModified
Deprecated.usegetEntryLastModifiedInstant()
insteadGet the last modified time of this entry.- Returns:
- last modified time of this file, in milliseconds since the epoch (Jan 1, 1970 UTC).
-
getEntryLastModifiedInstant
Get the last modified time of this entry.- Returns:
- last modified time of this file
- Since:
- 5.1.9
-
openEntryStream
Obtain an input stream to read the file content.Efficient implementations are not required. The caller will usually obtain the stream only once per entry, if at all.
The input stream should not use buffering if the implementation can avoid it. The caller will buffer as necessary to perform efficient block IO operations.
The caller will close the stream once complete.
- Returns:
- a stream to read from the file.
- Throws:
IOException
- the file could not be opened for reading.
-
isEntryIgnored
Determine if the current entry path is ignored by an ignore rule.- Returns:
- true if the entry was ignored by an ignore rule file.
- Throws:
IOException
- a relevant ignore rule file exists but cannot be read.
-
isEntryIgnored
Determine if the entry path is ignored by an ignore rule.- Parameters:
pLen
- the length of the path in the path buffer.- Returns:
- true if the entry is ignored by an ignore rule.
- Throws:
IOException
- a relevant ignore rule file exists but cannot be read.
-
isEntryIgnored
Determine if the entry path is ignored by an ignore rule.- Parameters:
pLen
- the length of the path in the path buffer.fileMode
- the original iterator file mode- Returns:
- true if the entry is ignored by an ignore rule.
- Throws:
IOException
- a relevant ignore rule file exists but cannot be read.
-
getIgnoreNode
- Throws:
IOException
-
getEntryAttributesNode
Retrieves theAttributesNode
for the current entry.- Returns:
- the
AttributesNode
for the current entry. - Throws:
IOException
-
init
Constructor helper.- Parameters:
list
- files in the subtree of the work tree this iterator operates on
-
current
Obtain the current entry from this iterator.- Returns:
- the currently selected entry.
-
isModeDifferent
public boolean isModeDifferent(int rawMode) Is the file mode of the current entry different than the given raw mode?- Parameters:
rawMode
- an int.- Returns:
- true if different, false otherwise
-
compareMetadata
Compare the metadata (mode, length, modification-timestamp) of the current entry and aDirCacheEntry
- Parameters:
entry
- theDirCacheEntry
to compare with- Returns:
- a
WorkingTreeIterator.MetadataDiff
which tells whether and how the entries metadata differ
-
isModified
public boolean isModified(DirCacheEntry entry, boolean forceContentCheck, ObjectReader reader) throws IOException Checks whether this entry differs from a given entry from theDirCache
. File status information is used and if status is same we consider the file identical to the state in the working directory. Native git uses more stat fields than we have accessible in Java.- Parameters:
entry
- the entry from the dircache we want to compare againstforceContentCheck
- True if the actual file content should be checked if modification time differs.reader
- access to repository objects if necessary. Should not be null.- Returns:
- true if content is most likely different.
- Throws:
IOException
- Since:
- 3.3
-
getIndexFileMode
Get the file mode to use for the current entry when it is to be updated in the index.- Parameters:
indexIter
-DirCacheIterator
positioned at the same entry as this iterator or null if noDirCacheIterator
is available at this iterator's current entry- Returns:
- index file mode
-
contentCheck
Compares the entries content with the content in the filesystem. Unsmudges the entry when it is detected that it is clean.- Parameters:
entry
- the entry to be checkedreader
- acccess to repository data if necessary- Returns:
true
if the content doesn't match,false
if it matches- Throws:
IOException
-
readContentAsNormalizedString
private static String readContentAsNormalizedString(DirCacheEntry entry, ObjectReader reader) throws MissingObjectException, IOException - Throws:
MissingObjectException
IOException
-
readSymlinkTarget
Reads the target of a symlink as a string. This default implementation fully reads the entry's input stream and converts it to a normalized string. Subclasses may override to provide more specialized implementations.- Parameters:
entry
- to read- Returns:
- the entry's content as a normalized string
- Throws:
IOException
- if the entry cannot be read or does not denote a symlink- Since:
- 4.6
-
computeLength
- Throws:
IOException
-
computeHash
- Throws:
IOException
-
getCleanFilterCommand
Get the clean filter command for the current entry.- Returns:
- the clean filter command for the current entry or
null
if no such command is defined - Throws:
IOException
- Since:
- 4.2
-
getEolStreamType
Get the eol stream type for the current entry.- Returns:
- the eol stream type for the current entry or
null
if it cannot be determined. When state or state.walk is null or theTreeWalk
is not based on aRepository
then null is returned. - Throws:
IOException
- Since:
- 4.3
-
getEolStreamType
- Parameters:
opType
- The operationtype (checkin/checkout) which should be used- Returns:
- the eol stream type for the current entry or
null
if it cannot be determined. When state or state.walk is null or theTreeWalk
is not based on aRepository
then null is returned. - Throws:
IOException
-
hasCrLfInIndex
Determines whether the file was committed un-normalized. If the iterator points to a conflict entry, checks the "ours" version.- Parameters:
dirCache
- iterator pointing to the current entry for the file in the index- Returns:
true
if the file in the index is not binary and has CR/LF line endings,false
otherwise
-
isDirectoryIgnored
- Throws:
IOException
-
isDirectoryIgnored
- Throws:
IOException
-
getParentPath
-
concatPath
-
getEntryLastModifiedInstant()
instead