svick
svick

Reputation: 244928

Using parent references

I'm using libgit2sharp and I'd like to get a Commit object representing something like HEAD~10. I tried repo.Lookup("HEAD~10"), but that doesn't work:

LibGit2Sharp.LibGit2Exception: An error was raised by libgit2. Class = GITERR_REFERENCE (-1).
The given reference name is not valid
  at LibGit2Sharp.Core.Ensure.Success(Int32 result, Boolean allowPositiveResult)
  at LibGit2Sharp.ReferenceCollection.Resolve[T](String name)
  at LibGit2Sharp.Repository.Lookup(String shaOrReferenceName, GitObjectType type, LookUpOptions lookUpOptions)
  at LibGit2Sharp.Repository.Lookup(String shaOrReferenceName, GitObjectType type)
  at libgit_tmp.Program.Main(String[] args)

I realize I could do the same by something like the following code, but I'd still prefer it if I could specify the reference this way. Is there some way to do it? If not, is it a limitation of libgit2sharp or of libgit2?

var commit = repo.Head.Tip;
for (int i = 0; i < 10; i++)
    commit = commit.Parents.First();

Upvotes: 1

Views: 346

Answers (1)

nulltoken
nulltoken

Reputation: 67639

Unfortunately, LibGit2Sharp isn't able to accept parameters following the rev-parse revision-specification syntax.

The proposed workaround is currently the best possible implementation. It's fully compliant with the documentation which states

A suffix ~<n> to a revision parameter means the commit object that is the <n>th generation ancestor of the named commit object, following only the first parents.

However, a feature has been recently merged in the development branch of libgit2 which may cover "retrieving parent references" need and beyond.

A nice API is now available and makes possible to retrieve a concrete git object from a revparse textual specification. In order to get a quick peek at its usage, the tests are available here.

Binding this API and making it widely available to LibGit2Sharp is yet to be done.

Upvotes: 1

Related Questions