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.

SpaceGhost440
  • 460
  • 4
  • 17
  • `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`. – Adam Feb 14 '20 at 20:04
  • 2
    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. – SpaceGhost440 Feb 14 '20 at 20:40
  • why dont you use standard git? – Idemax Feb 14 '20 at 20:43
  • 3
    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. – SpaceGhost440 Feb 14 '20 at 20:52
  • 1
    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. – 0andriy Feb 14 '20 at 21:17

1 Answers1

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.

SpaceGhost440
  • 460
  • 4
  • 17
  • 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. – 0andriy Feb 15 '20 at 10:49