Is there a way to locate a Leaf without iterating over all of the leafs of a tree?

9 views
Skip to first unread message

Nick

unread,
Oct 14, 2011, 3:17:22 PM10/14/11
to GitSharp
Hello,

I am trying to gain a better understand of how to traverse the GIT
repository. Currently I am attempting to get the contents of a Leaf's
data (Leaf.Data) from a particular Tag. If I know the path of the Leaf
relative to it's repository can I use this information to access leaf
directly? Or do I have to iterate through ever leaf in a tree to find
the one I am after. Something like this:

//tag.value is a string containing the tag name

IDictionary<string, Tag> tagDictionary = _repository.Tags;

if (tagDictionary.ContainsKey(tag.Value))
{

Repository tagRepository =
tagDictionary[tag.Value].Repository;
IEnumerable<Leaf> leaves =
tagRepository.CurrentBranch.CurrentCommit.Tree.Leaves;
if (leaves.Count() > 0)
{
return leaves.Where(l => l.Name ==
leafName).Single();

}
throw new GitLeafNotFoundException(leafName,
tag.Value);

Also, I am confused why a Tag object has a reference to a repository.
How is the repository returned from:

Repository tagRepository = tagDictionary[tag.Value].Repository;

different from the repository object that created when first
connecting with the GIT repository? ie new Repository(string
pathToRep)


Thanks in advance!

-Nick

Meinrad Recheis

unread,
Oct 17, 2011, 2:51:19 AM10/17/11
to gits...@googlegroups.com
On Fri, Oct 14, 2011 at 9:17 PM, Nick <nmart...@gmail.com> wrote:
Hello,

I am trying to gain a better understand of how to traverse the GIT
repository. Currently I am attempting to get the contents of a Leaf's
data (Leaf.Data) from a particular Tag. If I know the path of the Leaf
relative to it's repository can I use this information to access leaf
directly?

Yes, tree has an accessor to directly get files by repository path, see this snippet of Tree.cs

/// <summary>
/// Find a Blob or Tree by traversing the tree along the given path. You can access not only direct children
/// of the tree but any descendant of this tree.
/// <para/>
/// The path's directory seperators may be both forward or backslash, it is converted automatically to the internal representation.
/// <para/>
/// Throws IOException.
/// </summary>
/// <param name="path">Relative path to a file or directory in the git tree. For directories a trailing slash is allowed</param>
/// <returns>A tree or blob object representing the referenced object</returns>
public AbstractObject this[string path]
{
get
{
if (path == "")
return this;
var tree_entry = _internal_tree.FindBlobMember(path);
if (tree_entry == null)
tree_entry = _internal_tree.findTreeMember(path);
if (tree_entry == null)
return null;
if (tree_entry.IsTree)
return new Tree(_repo, tree_entry as CoreTree);
else if (tree_entry.IsBlob)
return new Leaf(_repo, tree_entry as FileTreeEntry);
else // if (tree_entry.IsCommit || tree_entry.IsTag)
return AbstractObject.Wrap(_repo, tree_entry.Id);
}
}

Or do I have to iterate through ever leaf in a tree to find
the one I am after. Something like this:

//tag.value is a string containing the tag name

 IDictionary<string, Tag> tagDictionary = _repository.Tags;

           if (tagDictionary.ContainsKey(tag.Value))
           {

               Repository tagRepository =
tagDictionary[tag.Value].Repository;
               IEnumerable<Leaf> leaves =
tagRepository.CurrentBranch.CurrentCommit.Tree.Leaves;
               if (leaves.Count() > 0)
               {
                   return leaves.Where(l => l.Name ==
leafName).Single();

               }
               throw new GitLeafNotFoundException(leafName,
tag.Value);

Iterating is not necessary, the tree["path/to/file"] indexer access is surely more efficient.

Also, I am confused why a Tag object has a reference to a repository.
How is the repository returned from:

Repository tagRepository = tagDictionary[tag.Value].Repository;

different from the repository object that created when first
connecting with the GIT repository?   ie new Repository(string
pathToRep)

It is of course the same. Every git object needs to know which Repo it belongs to, though, thus the property.


Thanks in advance!
-Nick

No problem. Have fun!
-- Henon

Reply all
Reply to author
Forward
0 new messages