Pull a specific commit using libgit2sharp
Asked Answered
A

1

7

hopefully someone call help. First off, I am pretty new to git, so forgive me if I make some mistakes in explaining my question.

I would like to pull the source code as it exists up to a specific commit using the library using libgit2sharp. So if there is a history of

  1. Commit
  2. Commit
  3. Commit
  4. Commit
  5. Commit
  6. Commit
  7. Commit
  8. Commit
  9. Commit
  10. Commit

I would like to be able to pull the source code for 5, or any other number in that list. None of the source will be tagged so I have to use the commits for the pull. Hopefully someone can help.

I have looked at https://github.com/libgit2/libgit2sharp/wiki/git-pull but it does not appear to let me pass in a sha or commit id.

Edited my question to be clear I am using the library.

Adaxial answered 14/2, 2020 at 19:59 Comment(5)
git pull is just a git fetch followed by a git merge on the given tracked branch. You're dealing with commits here and I assume you've already got the commits locally so just git merge sha-1.Tarsus
Adam, I am using the library libgit2sharp, and not the command line. Thanks, but I need how to accomplish this using the library. However, based on my research that is exactly the steps I need to accomplish.Adaxial
why dont you use standard git?Butyraceous
Please, I would rather not derail the question with a discussion about why I am not using the command line. :) Just suffice it to say I am using the library and would like to know how to accomplish the goal using the library.Adaxial
LibGit2 is quite low level library, you need to really understand Git data base (all main data structures) by heart to proceed. That said, you basically need to reimplement a big block, so called git checkout using a lot of small bricks.Marni
A
2

So another dev answered the question. All I needed to do was a clone, then a checkout against the commit I wanted to reset to. This seems to work the way I wanted.

//clone the master 
dir = Path.Combine(localPath, "Target");
Directory.CreateDirectory(dir);
Repository.Clone(tfsUri, dir);

//reset master to the base of the branch
using (var localRepo = new Repository(dir))
{
    var localCommit = localRepo.Lookup<Commit>(priorCommitId);
    Commands.Checkout(localRepo, localCommit);
}

Not sure if this is what you meant @0andriy, but your comment was a bit confusing.

Adaxial answered 15/2, 2020 at 2:19 Comment(1)
I meant that instead of git checkout call in the terminal you need to do a lot. Besides that, above is a cargo cult, that you have no clue about what it's doing internally. That said, you can easily corrupt data in your repository without knowing what happened.Marni

© 2022 - 2024 — McMap. All rights reserved.