1

I want to use labgit2sharp to replace the command -- "git pull“ to pull my code from my gitlab. but it is not successful by using the following code:

using (var repo = new Repository(remotePath))
{
    LibGit2Sharp.PullOptions options = new LibGit2Sharp.PullOptions();
    options.FetchOptions = new FetchOptions();
    options.FetchOptions.CredentialsProvider = new CredentialsHandler(
        (url, usernameFromUrl, types) =>
            new UsernamePasswordCredentials()
            {
                Username = "username",
                Password = "password"
            });
    repo.Network.Pull(new LibGit2Sharp.Signature("username", emailaddress, new DateTimeOffset(DateTime.Now)), options)
}

Could you help me? I want to know all the steps from the beginning. Thanks very much!

Noam Hacker
  • 4,671
  • 7
  • 34
  • 55
崔重阳
  • 13
  • 4
  • 1
    What are you using for `remotePath`? This has to be a local file path, not your remote Git source. Also post the error/stack trace that you are getting. – SushiHangover Mar 17 '16 at 14:08
  • ok,thanks!but there has a error:Type initializer exception – 崔重阳 Mar 17 '16 at 15:36
  • how can i solve it? or i lack some steps? – 崔重阳 Mar 17 '16 at 15:38
  • Post your exception stack trace in your question – SushiHangover Mar 17 '16 at 15:50
  • - $exception {"“LibGit2Sharp.Core.NativeMethods”的类型初始值设定项引发异常。"} System.TypeInitializationException - InnerException {"无法加载 DLL“git2-785d8c4”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。"} System.Exception {System.DllNotFoundException} – 崔重阳 Mar 17 '16 at 16:28
  • The native `libgit2` library `git2-785d8c4` is not being found, have you installed the native libgit2 library (via Nuget or source & building it manually)? http://stackoverflow.com/a/35029888/4984832 https://github.com/libgit2/libgit2 – SushiHangover Mar 17 '16 at 17:32
  • yes ! i am. i have installed nuget – 崔重阳 Mar 18 '16 at 03:31
  • Do you have the `git2-785d8c4` (`.dll`/`.dylib`/`.so`) native library that the C# library is trying to load? – SushiHangover Mar 18 '16 at 04:11
  • i have git2-785d8c4.dll,and when i try to load it ,it show that unable to add reference about it,and make sure this file is accessible and is a valid assembly or COM. – 崔重阳 Mar 18 '16 at 09:16
  • It is a **native** library, not a CIL-based one, since it ends in `.dll`, I am assuming you have the correct one and you are on Windows OS? If so, during the build process is it not being copied to the output directory with the CIL-based assembies (your program files and libgit2sharp.dll)? – SushiHangover Mar 18 '16 at 15:06

3 Answers3

1

Network.Pull is no more used. Please try using LibGit2Sharp.Commands.Pull()

1

@wonko-the-sane Here is a simple example of Commands.Pull usage:

    var Username = "name";
    var Password = "pass";
    var creds = new UsernamePasswordCredentials()
    {
        Username = Username,
        Password = Password
    };

    _credentialsHandler = (_url, _user, _cred) => creds;
       
    var projectRepositoryPath = "D:\local_repo";
    var repositoryOptions = new RepositoryOptions { WorkingDirectoryPath = projectRepositoryPath};
    var fetchOptions = new FetchOptions { CredentialsProvider = _credentialsHandler, };
    var mergeOptions = new MergeOptions { FailOnConflict=true,IgnoreWhitespaceChange=true };
    var pullOptions = new PullOptions() { FetchOptions = fetchOptions, MergeOptions = mergeOptions };


    using (var repo = new Repository(projectRepositoryPath, repositoryOptions))
    {
        var signature = new Signature("guest", "guest", DateTimeOffset.Now);
        var result = Commands.Pull(repo, signature, pullOptions);
    }
Kebechet
  • 1,461
  • 15
  • 31
0

Here my working code for a pull for a public repository (LibGit2Sharp v. 0.26.2):

using LibGit2Sharp;
...
public static bool Pull(string repositoryPath)
{
    try
    {
        using Repository localRepo = new Repository(repositoryPath);
        PullOptions pullOptions = new PullOptions();
        pullOptions.FetchOptions = new FetchOptions();
        Commands.Pull(localRepo, new Signature("username", "<your email>", new DateTimeOffset(DateTime.Now)), pullOptions);
        return true;
    }
    catch (Exception ex)
    {
        StaticObjects.FireShowExceptionMessage($"Pull Error, repository= {repositoryPath}: ", ex);
        return false;
    }
}
Rogério Silva
  • 121
  • 1
  • 11