Top Description Fields Constructors Methods
org.eclipse.jgit.treewalk

public abstract Class AbstractTreeIterator

extends Object
Class Inheritance
Known Direct Subclasses
org.eclipse.jgit.treewalk.CanonicalTreeParser, org.eclipse.jgit.treewalk.EmptyTreeIterator, org.eclipse.jgit.treewalk.WorkingTreeIterator, org.eclipse.jgit.dircache.DirCacheIterator
Static Imports
java.nio.charset.StandardCharsets.UTF_8

Walks a Git tree (directory) in Git sort order.

A new iterator instance should be positioned on the first entry, or at eof. Data for the first entry (if not at eof) should be available immediately.

Implementors must walk a tree in the Git sort order, which has the following odd sorting:

  1. A.c
  2. A/c
  3. A0c

In the second item, A is the name of a subtree and c is a file within that subtree. The other two items are files in the root level tree.

See Also
CanonicalTreeParser

Field Summary

Modifier and TypeField and Description
protected AttributesNode
attributesNode

Parsed rules of .gitattributes file if it exists.

protected static final int
DEFAULT_PATH_SIZE

Default size for the path buffer.

pack-priv AbstractTreeIterator
matches

The iterator this current entry is path equal to.

pack-priv int
matchShift

Number of entries we moved forward to force a D/F conflict match.

protected int
mode

Mode bits for the current entry.

public final AbstractTreeIterator
parent

Iterator for the parent tree; null if we are the root iterator.

protected byte[]
path

Path buffer for the current entry.

protected int
pathLen

Total length of the current entry's complete path from the root.

protected final int
pathOffset

Position within path this iterator starts writing at.

protected static final byte[]
zeroid

A dummy object id buffer that matches the zero ObjectId.

Constructor Summary

AccessConstructor and Description
protected
AbstractTreeIterator()

Create a new iterator with no parent.

protected
AbstractTreeIterator(String
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 '/'.
prefix
)

Create a new iterator with no parent and a prefix.

protected
AbstractTreeIterator(byte[]
position of this iterator in the repository tree. The value may be null or the empty array to indicate the prefix is the root of the repository. A trailing slash ('/') is automatically appended if the prefix does not end in '/'.
prefix
)

Create a new iterator with no parent and a prefix.

protected
AbstractTreeIterator(AbstractTreeIterator
parent tree iterator.
p
)

Create an iterator for a subtree of an existing iterator.

protected
AbstractTreeIterator(final AbstractTreeIterator
parent tree iterator.
p
,
final byte[]
path array to be used by the child iterator. This path must contain the path from the top of the walk to the first child and must end with a '/'.
childPath
,
final int
position within childPath where the child can insert its data. The value at childPath[childPathOffset-1] must be '/'.
childPathOffset
)

Create an iterator for a subtree of an existing iterator.

Method Summary

Modifier and TypeMethod and Description
private static int
public abstract void
back(int
number of entries to move the iterator by. Must be a positive, non-zero integer.
delta
)

Move to prior entry, populating this iterator with the entry data.

public EmptyTreeIterator

Returns:

a new empty tree iterator.
createEmptyTreeIterator
()

Create a new iterator as though the current entry were a subtree.

public abstract AbstractTreeIterator

Returns:

a new parser that walks over the current subtree.
createSubtreeIterator
(ObjectReader
reader to load the tree data from.
reader
)

Create a new iterator for the current entry's subtree.

public AbstractTreeIterator

Returns:

a new parser that walks over the current subtree.
createSubtreeIterator
(final ObjectReader
reader to load the tree data from.
reader
,
final MutableObjectId
temporary ObjectId buffer for use by this method.
idBuffer
)

Create a new iterator for the current entry's subtree.

protected void
ensurePathCapacity(int
the amount of bytes to hold
capacity
,
int
the amount of live bytes in path buffer
len
)

Ensure that path is capable to hold at least capacity bytes

public abstract boolean

Returns:

true if we have walked all entries and have none left.
eof
()

Is this tree iterator at its EOF point (no more entries)?

public boolean

Returns:

true if the file exists in this tree; false otherwise.
findFile
(String
file name to find (will not find a directory).
name
)

Seek the iterator on a file, if present.

public boolean

Returns:

true if the file exists in this tree; false otherwise.
findFile
(byte[]
file name to find (will not find a directory).
name
)

Seek the iterator on a file, if present.

public abstract boolean

Returns:

true if the iterator is positioned on the first entry.
first
()

Is this tree iterator positioned on its first entry?

public FileMode

Returns:

the file mode of the current entry.
getEntryFileMode
()

Get the file mode of the current entry.

public ObjectId

Returns:

an object id for the current entry.
getEntryObjectId
()

Get the object id of the current entry.

public void
getEntryObjectId(MutableObjectId
buffer to copy the object id into.
out
)

Obtain the ObjectId for the current entry.

public byte[]

Returns:

the internal buffer holding the current path.
getEntryPathBuffer
()

Get the current entry path buffer.

public int

Returns:

path hash code; any integer may be returned.
getEntryPathHashCode
()

Get the current entry's path hash code.

public int

Returns:

length of the path in getEntryPathBuffer().
getEntryPathLength
()

Get length of the path in getEntryPathBuffer().

public String

Returns:

path of the current entry, as a string.
getEntryPathString
()

Get path of the current entry, as a string.

public int

Returns:

the file mode of the current entry as bits.
getEntryRawMode
()

Get the file mode of the current entry as bits.

public void
getName(byte[]
the buffer to get the name into, it is assumed that buffer can hold the name
buffer
,
int
the offset of the name in the buffer
offset
)

Get the name component of the current entry path into the provided buffer.

public int

Returns:

the length of the name component of the path for the current entry.
getNameLength
()

Get the length of the name component of the path for the current entry.

public int

Returns:

start of name component part within getEntryPathBuffer()
getNameOffset
()

JGit internal API for use by org.eclipse.jgit.dircache.DirCacheCheckout

protected void
growPath(int
number of live bytes in the path buffer. This many bytes will be moved into the larger buffer.
len
)

Grow the path buffer larger.

public abstract boolean

Returns:

true if the entry has a valid ObjectId.
hasId
()

Whether the entry has a valid ObjectId.

public abstract byte[]

Returns:

byte array the implementation stores object IDs within.
idBuffer
()

Get the byte array buffer object IDs must be copied out of.

public boolean

Returns:

true if both iterators have the same object id; false otherwise.
idEqual
(AbstractTreeIterator
the other iterator to test against.
otherIterator
)

Check if the current entry of both iterators has the same id.

This method is faster than getEntryObjectId() as it does not require copying the bytes out of the buffers.

public abstract int

Returns:

offset into the array returned by idBuffer() where the ObjectId must be copied out of.
idOffset
()

Get the position within idBuffer() of this entry's ObjectId.

public boolean

Returns:

whether or not this Iterator is iterating through the working tree
isWorkTree
()

Whether or not this Iterator is iterating through the working tree.

protected boolean

Returns:

true if the iterator implements stopWalk().
needsStopWalk
()

Whether the iterator implements stopWalk().

public abstract void
next(int
number of entries to move the iterator by. Must be a positive, non-zero integer.
delta
)

Move to next entry, populating this iterator with the entry data.

public int

Returns:

-1 if this entry sorts first; 0 if the entries are equal; 1 if p's entry sorts first.
pathCompare
(AbstractTreeIterator
the other iterator to compare the path against.
p
)

Compare the path of this current entry to another iterator's entry.

pack-priv int
public int

Returns:

-1 if this entry sorts first; 0 if the entries are equal; 1 if p's entry sorts first.
pathCompare
(byte[]
the raw path buffer.
buf
,
int
position to start reading the raw buffer.
pos
,
int
one past the end of the raw buffer (length is end - pos).
end
,
int
the mode of the path.
pathMode
)

Compare the path of this current entry to a raw buffer.

private int
pathCompare(byte[] b, int bPos, int bEnd, int bMode, int aPos)

public void
reset()

Position this iterator on the first entry.

private void
setPathCapacity(int
the new size
capacity
,
int
the amount of bytes to copy
len
)

Set path buffer capacity to the specified size

public void
skip()

Advance to the next tree entry, populating this iterator with its data.

public void
stopWalk()

Indicates to the iterator that no more entries will be read.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

attributesNodeback to summary
protected AttributesNode attributesNode

Parsed rules of .gitattributes file if it exists.

Since
4.2
DEFAULT_PATH_SIZEback to summary
protected static final int DEFAULT_PATH_SIZE

Default size for the path buffer.

matchesback to summary
pack-priv AbstractTreeIterator matches

The iterator this current entry is path equal to.

matchShiftback to summary
pack-priv int matchShift

Number of entries we moved forward to force a D/F conflict match.

See Also
NameConflictTreeWalk
modeback to summary
protected int mode

Mode bits for the current entry.

A numerical value from FileMode is usually faster for an iterator to obtain from its data source so this is the preferred representation.

See Also
org.eclipse.jgit.lib.FileMode
parentback to summary
public final AbstractTreeIterator parent

Iterator for the parent tree; null if we are the root iterator.

Used by TreeWalk and AttributesHandler

Since
4.3
pathback to summary
protected byte[] path

Path buffer for the current entry.

This buffer is pre-allocated at the start of walking and is shared from parent iterators down into their subtree iterators. The sharing allows the current entry to always be a full path from the root, while each subtree only needs to populate the part that is under their control.

pathLenback to summary
protected int pathLen

Total length of the current entry's complete path from the root.

This is the number of bytes within path that pertain to the current entry. Values at this index through the end of the array are garbage and may be randomly populated from prior entries.

pathOffsetback to summary
protected final int pathOffset

Position within path this iterator starts writing at.

This is the first offset in path that this iterator must populate during next. At the root level (when parent is null) this is 0. For a subtree iterator the index before this position should have the value '/'.

zeroidback to summary
protected static final byte[] zeroid

A dummy object id buffer that matches the zero ObjectId.

Constructor Detail

AbstractTreeIteratorback to summary
protected AbstractTreeIterator()

Create a new iterator with no parent.

AbstractTreeIteratorback to summary
protected AbstractTreeIterator(String prefix)

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:String

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 '/'.

AbstractTreeIteratorback to summary
protected AbstractTreeIterator(byte[] prefix)

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:byte[]

position of this iterator in the repository tree. The value may be null or the empty array to indicate the prefix is the root of the repository. A trailing slash ('/') is automatically appended if the prefix does not end in '/'.

AbstractTreeIteratorback to summary
protected AbstractTreeIterator(AbstractTreeIterator p)

Create an iterator for a subtree of an existing iterator.

Parameters
p:AbstractTreeIterator

parent tree iterator.

AbstractTreeIteratorback to summary
protected AbstractTreeIterator(final AbstractTreeIterator p, final byte[] childPath, final int childPathOffset)

Create an iterator for a subtree of an existing iterator.

The caller is responsible for setting up the path of the child iterator.

Parameters
p:AbstractTreeIterator

parent tree iterator.

childPath:byte[]

path array to be used by the child iterator. This path must contain the path from the top of the walk to the first child and must end with a '/'.

childPathOffset:int

position within childPath where the child can insert its data. The value at childPath[childPathOffset-1] must be '/'.

Method Detail

alreadyMatchback to summary
private static int alreadyMatch(AbstractTreeIterator a, AbstractTreeIterator b)
backback to summary
public abstract void back(int delta) throws CorruptObjectException

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:

as well as any implementation dependent information necessary to accurately return data from idBuffer() and idOffset() when demanded.
Parameters
delta:int

number of entries to move the iterator by. Must be a positive, non-zero integer.

Exceptions
CorruptObjectException:
the tree is invalid.
createEmptyTreeIteratorback to summary
public EmptyTreeIterator createEmptyTreeIterator()

Create a new iterator as though the current entry were a subtree.

Returns:EmptyTreeIterator

a new empty tree iterator.

createSubtreeIteratorback to summary
public abstract AbstractTreeIterator createSubtreeIterator(ObjectReader reader) throws IncorrectObjectTypeException, IOException

Create a new iterator for the current entry's subtree.

The parent reference of the iterator must be this, otherwise the caller would not be able to exit out of the subtree iterator correctly and return to continue walking this.

Parameters
reader:ObjectReader

reader to load the tree data from.

Returns:AbstractTreeIterator

a new parser that walks over the current subtree.

Exceptions
IncorrectObjectTypeException:
the current entry is not actually a tree and cannot be parsed as though it were a tree.
IOException:
a loose object or pack file could not be read.
createSubtreeIteratorback to summary
public AbstractTreeIterator createSubtreeIterator(final ObjectReader reader, final MutableObjectId idBuffer) throws IncorrectObjectTypeException, IOException

Create a new iterator for the current entry's subtree.

The parent reference of the iterator must be this, otherwise the caller would not be able to exit out of the subtree iterator correctly and return to continue walking this.

Parameters
reader:ObjectReader

reader to load the tree data from.

idBuffer:MutableObjectId

temporary ObjectId buffer for use by this method.

Returns:AbstractTreeIterator

a new parser that walks over the current subtree.

Exceptions
IncorrectObjectTypeException:
the current entry is not actually a tree and cannot be parsed as though it were a tree.
IOException:
a loose object or pack file could not be read.
ensurePathCapacityback to summary
protected void ensurePathCapacity(int capacity, int len)

Ensure that path is capable to hold at least capacity bytes

Parameters
capacity:int

the amount of bytes to hold

len:int

the amount of live bytes in path buffer

eofback to summary
public abstract boolean eof()

Is this tree iterator at its EOF point (no more entries)?

An iterator is at EOF if there is no current entry.

Returns:boolean

true if we have walked all entries and have none left.

findFileback to summary
public boolean findFile(String name) throws CorruptObjectException

Seek the iterator on a file, if present.

Parameters
name:String

file name to find (will not find a directory).

Returns:boolean

true if the file exists in this tree; false otherwise.

Exceptions
CorruptObjectException:
tree is invalid.
Since
4.2
findFileback to summary
public boolean findFile(byte[] name) throws CorruptObjectException

Seek the iterator on a file, if present.

Parameters
name:byte[]

file name to find (will not find a directory).

Returns:boolean

true if the file exists in this tree; false otherwise.

Exceptions
CorruptObjectException:
tree is invalid.
Since
4.2
firstback to summary
public abstract 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().

Returns:boolean

true if the iterator is positioned on the first entry.

getEntryFileModeback to summary
public FileMode getEntryFileMode()

Get the file mode of the current entry.

Returns:FileMode

the file mode of the current entry.

getEntryObjectIdback to summary
public ObjectId getEntryObjectId()

Get the object id of the current entry.

Returns:ObjectId

an object id for the current entry.

getEntryObjectIdback to summary
public void getEntryObjectId(MutableObjectId out)

Obtain the ObjectId for the current entry.

Parameters
out:MutableObjectId

buffer to copy the object id into.

getEntryPathBufferback to summary
public byte[] getEntryPathBuffer()

Get the current entry path buffer.

Note that the returned byte[] has to be used together with getEntryPathLength() (only use bytes up to this length).

Returns:byte[]

the internal buffer holding the current path.

getEntryPathHashCodeback to summary
public int getEntryPathHashCode()

Get the current entry's path hash code.

This method computes a hash code on the fly for this path, the hash is suitable to cluster objects that may have similar paths together.

Returns:int

path hash code; any integer may be returned.

getEntryPathLengthback to summary
public int getEntryPathLength()

Get length of the path in getEntryPathBuffer().

Returns:int

length of the path in getEntryPathBuffer().

getEntryPathStringback to summary
public String getEntryPathString()

Get path of the current entry, as a string.

Returns:String

path of the current entry, as a string.

getEntryRawModeback to summary
public int getEntryRawMode()

Get the file mode of the current entry as bits.

Returns:int

the file mode of the current entry as bits.

getNameback to summary
public void getName(byte[] buffer, int offset)

Get the name component of the current entry path into the provided buffer.

Parameters
buffer:byte[]

the buffer to get the name into, it is assumed that buffer can hold the name

offset:int

the offset of the name in the buffer

See Also
getNameLength()
getNameLengthback to summary
public int getNameLength()

Get the length of the name component of the path for the current entry.

Returns:int

the length of the name component of the path for the current entry.

getNameOffsetback to summary
public int getNameOffset()

JGit internal API for use by org.eclipse.jgit.dircache.DirCacheCheckout

Returns:int

start of name component part within getEntryPathBuffer()

Since
2.0
growPathback to summary
protected void growPath(int len)

Grow the path buffer larger.

Parameters
len:int

number of live bytes in the path buffer. This many bytes will be moved into the larger buffer.

hasIdback to summary
public abstract boolean hasId()

Whether the entry has a valid ObjectId.

Returns:boolean

true if the entry has a valid ObjectId.

idBufferback to summary
public abstract 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.

Returns:byte[]

byte array the implementation stores object IDs within.

See Also
getEntryObjectId()
idEqualback to summary
public boolean idEqual(AbstractTreeIterator otherIterator)

Check if the current entry of both iterators has the same id.

This method is faster than getEntryObjectId() as it does not require copying the bytes out of the buffers. A direct idBuffer compare operation is performed.

Parameters
otherIterator:AbstractTreeIterator

the other iterator to test against.

Returns:boolean

true if both iterators have the same object id; false otherwise.

idOffsetback to summary
public abstract int idOffset()

Get the position within idBuffer() of this entry's ObjectId.

Returns:int

offset into the array returned by idBuffer() where the ObjectId must be copied out of.

isWorkTreeback to summary
public boolean isWorkTree()

Whether or not this Iterator is iterating through the working tree.

Returns:boolean

whether or not this Iterator is iterating through the working tree

Since
4.3
needsStopWalkback to summary
protected boolean needsStopWalk()

Whether the iterator implements stopWalk().

Returns:boolean

true if the iterator implements stopWalk().

Since
4.2
nextback to summary
public abstract void next(int delta) throws CorruptObjectException

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:

as well as any implementation dependent information necessary to accurately return data from idBuffer() and idOffset() when demanded.
Parameters
delta:int

number of entries to move the iterator by. Must be a positive, non-zero integer.

Exceptions
CorruptObjectException:
the tree is invalid.
pathCompareback to summary
public int pathCompare(AbstractTreeIterator p)

Compare the path of this current entry to another iterator's entry.

Parameters
p:AbstractTreeIterator

the other iterator to compare the path against.

Returns:int

-1 if this entry sorts first; 0 if the entries are equal; 1 if p's entry sorts first.

pathCompareback to summary
pack-priv int pathCompare(AbstractTreeIterator p, int pMode)
pathCompareback to summary
public int pathCompare(byte[] buf, int pos, int end, int pathMode)

Compare the path of this current entry to a raw buffer.

Parameters
buf:byte[]

the raw path buffer.

pos:int

position to start reading the raw buffer.

end:int

one past the end of the raw buffer (length is end - pos).

pathMode:int

the mode of the path.

Returns:int

-1 if this entry sorts first; 0 if the entries are equal; 1 if p's entry sorts first.

pathCompareback to summary
private int pathCompare(byte[] b, int bPos, int bEnd, int bMode, int aPos)
resetback to summary
public void reset() throws CorruptObjectException

Position this iterator on the first entry. The default implementation of this method uses back(1) until first() 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.

Exceptions
CorruptObjectException:
the tree is invalid.
setPathCapacityback to summary
private void setPathCapacity(int capacity, int len)

Set path buffer capacity to the specified size

Parameters
capacity:int

the new size

len:int

the amount of bytes to copy

skipback to summary
public void skip() throws CorruptObjectException

Advance to the next tree entry, populating this iterator with its data.

This method behaves like seek(1) but is called by org.eclipse.jgit.treewalk.TreeWalk only if a org.eclipse.jgit.treewalk.filter.TreeFilter was used and ruled out the current entry from the results. In such cases this tree iterator may perform special behavior.

Exceptions
CorruptObjectException:
the tree is invalid.
stopWalkback to summary
public void stopWalk()

Indicates to the iterator that no more entries will be read.

This is only invoked by TreeWalk when the iteration is aborted early due to a org.eclipse.jgit.errors.StopWalkException being thrown from within a TreeFilter.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@SuppressWarnings:nls
@Override