LibGit2Sharp and TFS Git repository

2019-06-11 15:59发布

问题:

I have cloned:

https://github.com/libgit2/libgit2sharp

which as I understand is the git client used in TFS 2015. I am trying to run the test:

   public void CanCloneFromBBWithCredentials(string url, string user, string pass, bool secure)

In:

https://github.com/libgit2/libgit2sharp/blob/vNext/LibGit2Sharp.Tests/CloneFixture.cs

Where I have updated it to use a Git repository I have in TFS:

    [Theory]
    //[InlineData("https://libgit2@bitbucket.org/libgit2/testgitrepository.git", "libgit3", "libgit3", true)]

    [InlineData("http://tfs/tfs/collection/project/_git/MyRepo", "myUser", "myPass", false)]
   // [InlineData("http://tfs/tfs/collection/project/_git/MyRepo", "myUser", "myPass", true)]

        public void CanCloneFromBBWithCredentials(string url, string user, string pass, bool secure)
        {
            var scd = BuildSelfCleaningDirectory();

            string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath, new CloneOptions()
            {
                CredentialsProvider = (_url, _user, _cred) => CreateUsernamePasswordCredentials (user, pass, secure)
            });

            using (var repo = new Repository(clonedRepoPath))

But when I run the test I get the following exception:

   at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) in c:\tmp\Repos\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 160
   at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result) in c:\tmp\Repos\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 178
   at LibGit2Sharp.Core.Proxy.git_clone(String url, String workdir, GitCloneOptions& opts) in c:\tmp\Repos\libgit2sharp\LibGit2Sharp\Core\Proxy.cs:line 328
   at LibGit2Sharp.Repository.Clone(String sourceUrl, String workdirPath, CloneOptions options) in c:\tmp\Repos\libgit2sharp\LibGit2Sharp\Repository.cs:line 694
   at LibGit2Sharp.Tests.CloneFixture.CanCloneFromBBWithCredentials(String url, String user, String pass, Boolean secure) in c:\tmp\Repos\libgit2sharp\LibGit2Sharp.Tests\CloneFixture.cs:line 227

I have verified that I can use the userName and password provided above to clone the repository from command line using: https://git-scm.com/

Any ideas on how to run libgit2sharp tests against a TFS 2015 Git repository?

回答1:

DefaultCredentials type is what you're after as TFS git protocol doesn't rely on username and password for authentication.

As stated by the xml documentation, it's "A credential object that will provide the "default" credentials (logged-in user information) via NTLM or SPNEGO authentication."

Settings the CloneOptions.CredentialsProvider to the following should do the trick

CredentialsProvider = (_url, _user, _cred) => new DefaultCredentials()