public class TreeWalk extends Object implements AutoCloseable, AttributesProvider
AbstractTreeIterator
s in
parallel.
This class can perform n-way differences across as many trees as necessary.
Each tree added must have the same root as existing trees in the walk.
A TreeWalk instance can only be used once to generate results. Running a
second time requires creating a new TreeWalk instance, or invoking
reset()
and adding new trees before starting again. Resetting an
existing instance may be faster for some applications as some internal
buffers may be recycled.
TreeWalk instances are not thread-safe. Applications must either restrict usage of a TreeWalk instance to a single thread, or implement their own synchronization at a higher level.
Multiple simultaneous TreeWalk instances per
Repository
are permitted, even from concurrent
threads.
Modifier and Type | Class and Description |
---|---|
static class |
TreeWalk.OperationType |
Constructor and Description |
---|
TreeWalk(ObjectReader or)
Create a new tree walker for a given repository.
|
TreeWalk(Repository repo)
Create a new tree walker for a given repository.
|
TreeWalk(Repository repo,
ObjectReader or)
Create a new tree walker for a given repository.
|
Modifier and Type | Method and Description |
---|---|
int |
addTree(AbstractTreeIterator p)
Add an already created tree iterator for walking.
|
int |
addTree(AnyObjectId id)
Add an already existing tree object for walking.
|
void |
close() |
void |
enterSubtree()
Enter into the current subtree.
|
static TreeWalk |
forPath(ObjectReader reader,
String path,
AnyObjectId... trees)
Open a tree walk and filter to exactly one path.
|
static TreeWalk |
forPath(Repository repo,
ObjectReader reader,
String path,
AnyObjectId... trees)
Open a tree walk and filter to exactly one path.
|
static TreeWalk |
forPath(Repository db,
String path,
AnyObjectId... trees)
Open a tree walk and filter to exactly one path.
|
static TreeWalk |
forPath(Repository db,
String path,
RevTree tree)
Open a tree walk and filter to exactly one path.
|
Attributes |
getAttributes()
Get attributes
|
AttributesNodeProvider |
getAttributesNodeProvider()
Get the attributes node provider
|
int |
getDepth()
Get the current subtree depth of this walker.
|
CoreConfig.EolStreamType |
getEolStreamType(TreeWalk.OperationType opType)
Get the EOL stream type of the current entry using the config and
getAttributes() . |
FileMode |
getFileMode()
Obtain the
FileMode for the current entry on
the currentHead tree |
FileMode |
getFileMode(int nth)
Obtain the
FileMode for the current entry. |
TreeFilter |
getFilter()
Get the currently configured filter.
|
String |
getFilterCommand(String filterCommandType)
Inspect config and attributes to return a filtercommand applicable for
the current path, but without expanding %f occurences
|
String |
getNameString()
Get the current entry's name within its parent tree.
|
ObjectId |
getObjectId(int nth)
Obtain the ObjectId for the current entry.
|
void |
getObjectId(MutableObjectId out,
int nth)
Obtain the ObjectId for the current entry.
|
ObjectReader |
getObjectReader()
Get the reader this walker is using to load objects.
|
TreeWalk.OperationType |
getOperationType()
Get the operation type
|
int |
getPathLength()
Get the path length of the current entry.
|
String |
getPathString()
Get the current entry's complete path.
|
int |
getRawMode(int nth)
Obtain the raw
FileMode bits for the current
entry. |
byte[] |
getRawPath()
Get the current entry's complete path as a UTF-8 byte array.
|
<T extends AbstractTreeIterator> |
getTree(Class<T> type)
Get the tree of that type.
|
<T extends AbstractTreeIterator> |
getTree(int nth,
Class<T> clazz)
Obtain the tree iterator for the current entry.
|
int |
getTreeCount()
Get the number of trees known to this walker.
|
boolean |
idEqual(int nthA,
int nthB)
Compare two tree's current ObjectId values for equality.
|
int |
isPathMatch(byte[] p,
int pLen)
Test if the supplied path matches the current entry's path.
|
int |
isPathPrefix(byte[] p,
int pLen)
Test if the supplied path matches the current entry's path.
|
boolean |
isPathSuffix(byte[] p,
int pLen)
Test if the supplied path matches (being suffix of) the current entry's
path.
|
boolean |
isPostChildren()
Is the current entry a subtree returned after its children?
|
boolean |
isPostOrderTraversal()
Does this walker return a tree entry after it exits the subtree?
|
boolean |
isRecursive()
Is this walker automatically entering into subtrees?
|
boolean |
isSubtree()
Is the current entry a subtree?
|
boolean |
next()
Advance this walker to the next relevant entry.
|
void |
reset()
Reset this walker so new tree iterators can be added to it.
|
void |
reset(AnyObjectId... ids)
Reset this walker to run over a set of existing trees.
|
void |
reset(AnyObjectId id)
Reset this walker to run over a single existing tree.
|
void |
setAttributesNodeProvider(AttributesNodeProvider provider)
Sets the
AttributesNodeProvider for
this TreeWalk . |
void |
setFilter(TreeFilter newFilter)
Set the tree entry filter for this walker.
|
void |
setOperationType(TreeWalk.OperationType operationType)
Set the operation type of this walk
|
void |
setPostOrderTraversal(boolean b)
Set the walker to return trees after their children.
|
void |
setRecursive(boolean b)
Set the walker to enter (or not enter) subtrees automatically.
|
public TreeWalk(Repository repo)
repo
- the repository the walker will obtain data from. An
ObjectReader will be created by the walker, and will be closed
when the walker is closed.public TreeWalk(@Nullable Repository repo, ObjectReader or)
repo
- the repository the walker will obtain data from. An
ObjectReader will be created by the walker, and will be closed
when the walker is closed.or
- the reader the walker will obtain tree data from. The reader
is not closed when the walker is closed.public TreeWalk(ObjectReader or)
or
- the reader the walker will obtain tree data from. The reader
is not closed when the walker is closed.public void setOperationType(TreeWalk.OperationType operationType)
operationType
- a TreeWalk.OperationType
object.public static TreeWalk forPath(ObjectReader reader, String path, AnyObjectId... trees) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException
The returned tree walk is already positioned on the requested path, so
the caller should not need to invoke next()
unless they are
looking for a possible directory/file name conflict.
reader
- the reader the walker will obtain tree data from.path
- single path to advance the tree walk instance into.trees
- one or more trees to walk through, all with the same root.IOException
- reading a pack file or loose object failed.CorruptObjectException
- an tree object could not be read as its data stream did not
appear to be a tree, or could not be inflated.IncorrectObjectTypeException
- an object we expected to be a tree was not a tree.MissingObjectException
- a tree object was not found.public static TreeWalk forPath(@Nullable Repository repo, ObjectReader reader, String path, AnyObjectId... trees) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException
The returned tree walk is already positioned on the requested path, so
the caller should not need to invoke next()
unless they are
looking for a possible directory/file name conflict.
repo
- repository to read config data and
AttributesNodeProvider
from.reader
- the reader the walker will obtain tree data from.path
- single path to advance the tree walk instance into.trees
- one or more trees to walk through, all with the same root.IOException
- reading a pack file or loose object failed.CorruptObjectException
- an tree object could not be read as its data stream did not
appear to be a tree, or could not be inflated.IncorrectObjectTypeException
- an object we expected to be a tree was not a tree.MissingObjectException
- a tree object was not found.public static TreeWalk forPath(Repository db, String path, AnyObjectId... trees) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException
The returned tree walk is already positioned on the requested path, so
the caller should not need to invoke next()
unless they are
looking for a possible directory/file name conflict.
db
- repository to read tree object data from.path
- single path to advance the tree walk instance into.trees
- one or more trees to walk through, all with the same root.IOException
- reading a pack file or loose object failed.CorruptObjectException
- an tree object could not be read as its data stream did not
appear to be a tree, or could not be inflated.IncorrectObjectTypeException
- an object we expected to be a tree was not a tree.MissingObjectException
- a tree object was not found.public static TreeWalk forPath(Repository db, String path, RevTree tree) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException
The returned tree walk is already positioned on the requested path, so
the caller should not need to invoke next()
unless they are
looking for a possible directory/file name conflict.
db
- repository to read tree object data from.path
- single path to advance the tree walk instance into.tree
- the single tree to walk through.IOException
- reading a pack file or loose object failed.CorruptObjectException
- an tree object could not be read as its data stream did not
appear to be a tree, or could not be inflated.IncorrectObjectTypeException
- an object we expected to be a tree was not a tree.MissingObjectException
- a tree object was not found.public ObjectReader getObjectReader()
public TreeWalk.OperationType getOperationType()
TreeWalk.OperationType
public void close()
Release any resources used by this walker's reader.
A walker that has been released can be used again, but may need to be released after the subsequent usage.
close
in interface AutoCloseable
public TreeFilter getFilter()
public void setFilter(TreeFilter newFilter)
Multiple filters may be combined by constructing an arbitrary tree of
AndTreeFilter
or OrTreeFilter
instances to
describe the boolean expression required by the application. Custom
filter implementations may also be constructed by applications.
Note that filters are not thread-safe and may not be shared by concurrent
TreeWalk instances. Every TreeWalk must be supplied its own unique
filter, unless the filter implementation specifically states it is (and
always will be) thread-safe. Callers may use
TreeFilter.clone()
to create a
unique filter tree for this TreeWalk instance.
newFilter
- the new filter. If null the special
TreeFilter.ALL
filter
will be used instead, as it matches every entry.AndTreeFilter
,
OrTreeFilter
public boolean isRecursive()
If the walker is recursive then the caller will not see a subtree node and instead will only receive file nodes in all relevant subtrees.
public void setRecursive(boolean b)
If recursive mode is enabled the walker will hide subtree nodes from the calling application and will produce only file level nodes. If a tree (directory) is deleted then all of the file level nodes will appear to be deleted, recursively, through as many levels as necessary to account for all entries.
b
- true to skip subtree nodes and only obtain files nodes.public boolean isPostOrderTraversal()
If post order traversal is enabled then the walker will return a subtree
after it has returned the last entry within that subtree. This may cause
a subtree to be seen by the application twice if isRecursive()
is false, as the application will see it once, call
enterSubtree()
, and then see it again as it leaves the subtree.
If an application does not enable isRecursive()
and it does not
call enterSubtree()
then the tree is returned only once as none
of the children were processed.
public void setPostOrderTraversal(boolean b)
b
- true to get trees after their children.isPostOrderTraversal()
public void setAttributesNodeProvider(AttributesNodeProvider provider)
AttributesNodeProvider
for
this TreeWalk
.
This is a requirement for a correct computation of the git attributes. If
this TreeWalk
has been built using
TreeWalk(Repository)
constructor, the
AttributesNodeProvider
has already
been set. Indeed,the Repository
can provide
an AttributesNodeProvider
using
Repository.createAttributesNodeProvider()
method. Otherwise you should provide one.
provider
- a AttributesNodeProvider
object.Repository.createAttributesNodeProvider()
public AttributesNodeProvider getAttributesNodeProvider()
AttributesNodeProvider
for this TreeWalk
.public Attributes getAttributes()
Retrieve the git attributes for the current entry.
AttributesNodeProvider.getInfoAttributesNode()
).AttributesNodeProvider.getGlobalAttributesNode()
)
In order to have a correct list of attributes for the current entry, this
TreeWalk
requires to have at least one
AttributesNodeProvider
and a DirCacheIterator
set up. An
AttributesNodeProvider
is used to retrieve the attributes from
the info attributes file and the global attributes file. The
DirCacheIterator
is used to retrieve the .gitattributes files
stored in the index. A WorkingTreeIterator
can also be provided
to access the local version of the .gitattributes files. If none is
provided it will fallback on the DirCacheIterator
.
getAttributes
in interface AttributesProvider
@Nullable public CoreConfig.EolStreamType getEolStreamType(TreeWalk.OperationType opType)
getAttributes()
.opType
- the operationtype (checkin/checkout) which should be usedgetAttributes()
. Note that this method may return null
if the TreeWalk
is not based on
a working treepublic void reset()
public void reset(AnyObjectId id) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException
id
- the tree we need to parse. The walker will execute over this
single tree if the reset is successful.MissingObjectException
- the given tree object does not exist in this repository.IncorrectObjectTypeException
- the given object id does not denote a tree, but instead names
some other non-tree type of object. Note that commits are not
trees, even if they are sometimes called a "tree-ish".CorruptObjectException
- the object claimed to be a tree, but its contents did not
appear to be a tree. The repository may have data corruption.IOException
- a loose object or pack file could not be read.public void reset(AnyObjectId... ids) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException
ids
- the trees we need to parse. The walker will execute over this
many parallel trees if the reset is successful.MissingObjectException
- the given tree object does not exist in this repository.IncorrectObjectTypeException
- the given object id does not denote a tree, but instead names
some other non-tree type of object. Note that commits are not
trees, even if they are sometimes called a "tree-ish".CorruptObjectException
- the object claimed to be a tree, but its contents did not
appear to be a tree. The repository may have data corruption.IOException
- a loose object or pack file could not be read.public int addTree(AnyObjectId id) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException
The position of this tree is returned to the caller, in case the caller has lost track of the order they added the trees into the walker.
The tree must have the same root as existing trees in the walk.
id
- identity of the tree object the caller wants walked.MissingObjectException
- the given tree object does not exist in this repository.IncorrectObjectTypeException
- the given object id does not denote a tree, but instead names
some other non-tree type of object. Note that commits are not
trees, even if they are sometimes called a "tree-ish".CorruptObjectException
- the object claimed to be a tree, but its contents did not
appear to be a tree. The repository may have data corruption.IOException
- a loose object or pack file could not be read.public int addTree(AbstractTreeIterator p)
The position of this tree is returned to the caller, in case the caller has lost track of the order they added the trees into the walker.
The tree which the iterator operates on must have the same root as existing trees in the walk.
p
- an iterator to walk over. The iterator should be new, with no
parent, and should still be positioned before the first entry.
The tree which the iterator operates on must have the same
root as other trees in the walk.public int getTreeCount()
public boolean next() throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException
MissingObjectException
- isRecursive()
was enabled, a subtree was found, but
the subtree object does not exist in this repository. The
repository may be missing objects.IncorrectObjectTypeException
- isRecursive()
was enabled, a subtree was found, and
the subtree id does not denote a tree, but instead names some
other non-tree type of object. The repository may have data
corruption.CorruptObjectException
- the contents of a tree did not appear to be a tree. The
repository may have data corruption.IOException
- a loose object or pack file could not be read.public <T extends AbstractTreeIterator> T getTree(int nth, Class<T> clazz)
Entering into (or exiting out of) a subtree causes the current tree iterator instance to be changed for the nth tree. This allows the tree iterators to manage only one list of items, with the diving handled by recursive trees.
nth
- tree to obtain the current iterator of.clazz
- type of the tree iterator expected by the caller.public int getRawMode(int nth)
FileMode
bits for the current
entry.
Every added tree supplies mode bits, even if the tree does not contain
the current entry. In the latter case
FileMode.MISSING
's mode bits (0) are
returned.
nth
- tree to obtain the mode bits from.FileMode.fromBits(int)
public FileMode getFileMode(int nth)
FileMode
for the current entry.
Every added tree supplies a mode, even if the tree does not contain the
current entry. In the latter case
FileMode.MISSING
is returned.
nth
- tree to obtain the mode from.public FileMode getFileMode()
FileMode
for the current entry on
the currentHead treepublic ObjectId getObjectId(int nth)
Using this method to compare ObjectId values between trees of this walker
is very inefficient. Applications should try to use
idEqual(int, int)
or getObjectId(MutableObjectId, int)
whenever possible.
Every tree supplies an object id, even if the tree does not contain the
current entry. In the latter case
ObjectId.zeroId()
is returned.
nth
- tree to obtain the object identifier from.getObjectId(MutableObjectId, int)
,
idEqual(int, int)
,
getObjectId(MutableObjectId, int)
,
idEqual(int, int)
public void getObjectId(MutableObjectId out, int nth)
Every tree supplies an object id, even if the tree does not contain the
current entry. In the latter case
ObjectId.zeroId()
is supplied.
Applications should try to use idEqual(int, int)
when possible
as it avoids conversion overheads.
out
- buffer to copy the object id into.nth
- tree to obtain the object identifier from.idEqual(int, int)
public boolean idEqual(int nthA, int nthB)
nthA
- first tree to compare the object id from.nthB
- second tree to compare the object id from.getObjectId(nthA).equals(getObjectId(nthB))
.getObjectId(int)
public String getNameString()
This method is not very efficient and is primarily meant for debugging and final output generation. Applications should try to avoid calling it, and if invoked do so only once per interesting entry, where the name is absolutely required for correct function.
public String getPathString()
This method is not very efficient and is primarily meant for debugging and final output generation. Applications should try to avoid calling it, and if invoked do so only once per interesting entry, where the name is absolutely required for correct function.
public byte[] getRawPath()
public int getPathLength()
public int isPathMatch(byte[] p, int pLen)
This method detects if the supplied path is equal to, a subtree of, or
not similar at all to the current entry. It is faster to use this
method than to use getPathString()
to first create a String
object, then test startsWith
or some other type of string
match function.
If the current entry is a subtree, then all paths within the subtree are considered to match it.
p
- path buffer to test. Callers should ensure the path does not
end with '/' prior to invocation.pLen
- number of bytes from buf
to test.public int isPathPrefix(byte[] p, int pLen)
This method tests that the supplied path is exactly equal to the current
entry or is one of its parent directories. It is faster to use this
method then to use getPathString()
to first create a String
object, then test startsWith
or some other type of string
match function.
If the current entry is a subtree, then all paths within the subtree are considered to match it.
p
- path buffer to test. Callers should ensure the path does not
end with '/' prior to invocation.pLen
- number of bytes from buf
to test.public boolean isPathSuffix(byte[] p, int pLen)
This method tests that the supplied path is exactly equal to the current
entry, or is relative to one of entry's parent directories. It is faster
to use this method then to use getPathString()
to first create
a String object, then test endsWith
or some other type of
string match function.
p
- path buffer to test.pLen
- number of bytes from buf
to test.public int getDepth()
public boolean isSubtree()
This method is faster then testing the raw mode bits of all trees to see if any of them are a subtree. If at least one is a subtree then this method will return true.
enterSubtree()
will work on the current node.public boolean isPostChildren()
isPostOrderTraversal()
public void enterSubtree() throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException
If the current entry is a subtree this method arranges for its children to be returned before the next sibling following the subtree is returned.
MissingObjectException
- a subtree was found, but the subtree object does not exist in
this repository. The repository may be missing objects.IncorrectObjectTypeException
- a subtree was found, and the subtree id does not denote a
tree, but instead names some other non-tree type of object.
The repository may have data corruption.CorruptObjectException
- the contents of a tree did not appear to be a tree. The
repository may have data corruption.IOException
- a loose object or pack file could not be read.public <T extends AbstractTreeIterator> T getTree(Class<T> type)
T
- a tree type.type
- of the tree to be queriedpublic String getFilterCommand(String filterCommandType) throws IOException
filterCommandType
- which type of filterCommand should be executed. E.g. "clean",
"smudge"IOException
Copyright © 2018 Eclipse JGit Project. All rights reserved.