libgit2sharp: replace the "git pull” command
Asked Answered
B

3

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!

Bubonocele answered 17/3, 2016 at 13:48 Comment(10)
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.Kilbride
ok,thanks!but there has a error:Type initializer exceptionAday
how can i solve it? or i lack some steps?Aday
Post your exception stack trace in your questionKilbride
- $exception {"“LibGit2Sharp.Core.NativeMethods”的类型初始值设定项引发异常。"} System.TypeInitializationException - InnerException {"无法加载 DLL“git2-785d8c4”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。"} System.Exception {System.DllNotFoundException}Aday
The native libgit2 library git2-785d8c4 is not being found, have you installed the native libgit2 library (via Nuget or source & building it manually)? https://mcmap.net/q/1175825/-not-able-to-run-libgit2sharp-on-mono-on-ubuntu github.com/libgit2/libgit2Kilbride
yes ! i am. i have installed nugetAday
Do you have the git2-785d8c4 (.dll/.dylib/.so) native library that the C# library is trying to load?Kilbride
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.Aday
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)?Kilbride
M
1

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

Minivet answered 11/8, 2017 at 13:52 Comment(1)
An example of how to use this syntax would make this answer a lot better.Mafaldamafeking
F
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);
    }
Fates answered 14/6, 2022 at 13:8 Comment(0)
U
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;
    }
}
Uninterrupted answered 31/1, 2023 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.