Can GitSharp return a Leaf (blob) object directly given the leaf's hash?

11 views
Skip to first unread message

Nick

unread,
Sep 28, 2011, 3:31:12 PM9/28/11
to GitSharp
Hey all..

I noticed that I can get the contents of a particular Leaf (blob)
object from a specific branch by iterating over a Tree's children like
so:

_repository = new Repository(git_url);

IEnumerable<AbstractObject> currentBranchItems =
_repository.CurrentBranch.CurrentCommit.Tree.Children;

foreach (var currentBranchItem in currentBranchItems)
{
var currentBranchLeaf = currentBranchItem as Leaf;
_lastLeafHash = currentBranchLeaf.Hash;
Console.WriteLine("Name: " + currentBranchLeaf.Name +
" Hash: " + currentBranchLeaf.Hash);
}

However, this seems pretty inefficient if I have the hash of the leaf
that I would like to retrieve. Is there a way that I can access a Leaf
directly from the repository if I have the hash? The following does
not work:

private static void GetLeafByHash(string hash)
{
var leafAbs = _repository.Get<AbstractObject>(hash);
var leaf = leafAbs as Leaf;
Console.WriteLine("Found Leaf Named: " + leaf.Name);
Console.WriteLine("The data is this big: " +
leaf.RawData.Length);
Console.Read();

}

The Get<T> method always returns NULL. So is there a way to accomplish
the direct retrieval of a Leaf by hash? The documentation states the
following about the Get<T> method:

Access a git object by name, id or path. Use the type parameter to
tell what kind of object you like to get. Supported types are
Branches, Commits or Tags may be accessed by name or reference
expression. Currently supported are combinations of these:
Not supported is
Tree or Leaf (Blob) objects can be addressed by long hash or by their
relative repository path

It's not clear.. does this mean that Tree or Leaf objects can or
cannot be accessed via a hash?

Thanks a lot!

-Nick


Thanks!

Meinrad Recheis

unread,
Sep 29, 2011, 3:51:41 PM9/29/11
to gits...@googlegroups.com

did you try repo.Get<Blob>(sha_hash);  ?

Access a git object by name, id or path. Use the type parameter to
tell what kind of object you like to get. Supported types are
Branches, Commits or Tags may be accessed by name or reference
expression. Currently supported are combinations of these:
Not supported is
Tree or Leaf (Blob) objects can be addressed by long hash or by their
relative repository path

It's not clear.. does this mean that Tree or Leaf objects can or
cannot be accessed via a hash?

The Leaf is a special object in that it represents a blob by it's path in the repository tree. As it is not possible
to find the path of a given blob (efficiently) a Leaf instance can only be aquired by iterating down the trees by hand or by using the path accessor in Tree (tree[path_to_leaf]).
If you want to get the object directly by hash just use Get<T> and use one of the types Commit, Tree or Blob for T if you know it. If you don't know the type you can always use Blob and inspect the data, even for Commit or Tree objects.
Hth,
-- henon  

Thanks a lot!

-Nick


Thanks!

--
For infos about GitSharp visit http://www.eqqon.com/index.php/GitSharp
To join this group visit http://groups.google.com/group/gitsharp

Nick

unread,
Oct 14, 2011, 1:13:59 PM10/14/11
to GitSharp
This was very helpful. Thank you for the reply.

-Nick

On Sep 29, 1:51 pm, Meinrad Recheis <meinrad.rech...@gmail.com> wrote:
> > For infos about GitSharp visithttp://www.eqqon.com/index.php/GitSharp
Reply all
Reply to author
Forward
0 new messages