开发者

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

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 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 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!


You can get any object in the repo as Blob like this

var blob=repo.Get<Blob>(sha_hash);

If you know the type of the object (tree, tag or commit) you can also substitute the type parameters Tree, Tag and Commit for T in Get().

Sidenote: In GitSharp a Leaf is a Blob that knows it's path in the tree of the current revision. However since a file can be in many places on different branches and revisions you can not get a Leaf via Repository.Get().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜