Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit 61b3044

Browse files
authored
Merge pull request #37 from GitTools/pr/fix-authentication-nullreference
Fix NullReferenceException for dynamic repositories without authentication
2 parents 82e648d + 31513bd commit 61b3044

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

src/GitTools.Core/GitTools.Core.Shared/Git/AuthenticationInfo.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,5 @@ public class AuthenticationInfo
77
public string Username { get; set; }
88
public string Password { get; set; }
99
public string Token { get; set; }
10-
11-
public FetchOptions ToFetchOptions()
12-
{
13-
var fetchOptions = new FetchOptions();
14-
15-
if (!string.IsNullOrEmpty(Username))
16-
{
17-
fetchOptions.CredentialsProvider = (url, user, types) => new UsernamePasswordCredentials
18-
{
19-
Username = Username,
20-
Password = Password
21-
};
22-
}
23-
24-
return fetchOptions;
25-
}
2610
}
2711
}

src/GitTools.Core/GitTools.Core.Shared/Git/DynamicRepositories.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,19 @@ static void CheckoutCommit(IRepository repo, string targetCommit)
170170
static void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo authentication)
171171
{
172172
Credentials credentials = null;
173-
if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password))
174-
{
175-
Log.Info(string.Format("Setting up credentials using name '{0}'", authentication.Username));
176173

177-
credentials = new UsernamePasswordCredentials
174+
if (authentication != null)
175+
{
176+
if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password))
178177
{
179-
Username = authentication.Username,
180-
Password = authentication.Password
181-
};
178+
Log.Info(string.Format("Setting up credentials using name '{0}'", authentication.Username));
179+
180+
credentials = new UsernamePasswordCredentials
181+
{
182+
Username = authentication.Username,
183+
Password = authentication.Password
184+
};
185+
}
182186
}
183187

184188
Log.Info(string.Format("Retrieving git info from url '{0}'", repositoryUrl));

src/GitTools.Core/GitTools.Core.Shared/Git/Extensions/AuthenticationInfoExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
namespace GitTools.Git
22
{
3+
using LibGit2Sharp;
34
using Logging;
45

56
public static class AuthenticationInfoExtensions
67
{
78
private static readonly ILog Log = LogProvider.GetCurrentClassLogger();
89

10+
public static FetchOptions ToFetchOptions(this AuthenticationInfo authenticationInfo)
11+
{
12+
var fetchOptions = new FetchOptions();
13+
14+
if (authenticationInfo != null)
15+
{
16+
if (!string.IsNullOrEmpty(authenticationInfo.Username))
17+
{
18+
fetchOptions.CredentialsProvider = (url, user, types) => new UsernamePasswordCredentials
19+
{
20+
Username = authenticationInfo.Username,
21+
Password = authenticationInfo.Password
22+
};
23+
}
24+
}
25+
26+
return fetchOptions;
27+
}
28+
929
public static bool IsEmpty(this AuthenticationInfo authenticationInfo)
1030
{
1131
if (authenticationInfo == null)

0 commit comments

Comments
 (0)