From 23b26fee9f239322f4b6b4dabbd6452ec332966c Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Thu, 27 Oct 2016 11:14:50 +0200 Subject: [PATCH 1/2] Fix NullReferenceException for dynamic repositories without authentication --- .../Git/DynamicRepositories.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/GitTools.Core/GitTools.Core.Shared/Git/DynamicRepositories.cs b/src/GitTools.Core/GitTools.Core.Shared/Git/DynamicRepositories.cs index c039fa6..774659e 100644 --- a/src/GitTools.Core/GitTools.Core.Shared/Git/DynamicRepositories.cs +++ b/src/GitTools.Core/GitTools.Core.Shared/Git/DynamicRepositories.cs @@ -170,15 +170,19 @@ static void CheckoutCommit(IRepository repo, string targetCommit) static void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo authentication) { Credentials credentials = null; - if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password)) - { - Log.Info(string.Format("Setting up credentials using name '{0}'", authentication.Username)); - credentials = new UsernamePasswordCredentials + if (authentication != null) + { + if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password)) { - Username = authentication.Username, - Password = authentication.Password - }; + Log.Info(string.Format("Setting up credentials using name '{0}'", authentication.Username)); + + credentials = new UsernamePasswordCredentials + { + Username = authentication.Username, + Password = authentication.Password + }; + } } Log.Info(string.Format("Retrieving git info from url '{0}'", repositoryUrl)); From 31513bdb477f5afaff36de02003ce3278b19ebf2 Mon Sep 17 00:00:00 2001 From: Geert van Horrik Date: Thu, 27 Oct 2016 11:32:48 +0200 Subject: [PATCH 2/2] Move AuthenticationInfo.ToFetchOptions to extension methods to allow usage with null instance --- .../Git/AuthenticationInfo.cs | 16 --------------- .../AuthenticationInfoExtensions.cs | 20 +++++++++++++++++++ 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/GitTools.Core/GitTools.Core.Shared/Git/AuthenticationInfo.cs b/src/GitTools.Core/GitTools.Core.Shared/Git/AuthenticationInfo.cs index 24a2b82..9a0406a 100644 --- a/src/GitTools.Core/GitTools.Core.Shared/Git/AuthenticationInfo.cs +++ b/src/GitTools.Core/GitTools.Core.Shared/Git/AuthenticationInfo.cs @@ -7,21 +7,5 @@ public class AuthenticationInfo public string Username { get; set; } public string Password { get; set; } public string Token { get; set; } - - public FetchOptions ToFetchOptions() - { - var fetchOptions = new FetchOptions(); - - if (!string.IsNullOrEmpty(Username)) - { - fetchOptions.CredentialsProvider = (url, user, types) => new UsernamePasswordCredentials - { - Username = Username, - Password = Password - }; - } - - return fetchOptions; - } } } \ No newline at end of file diff --git a/src/GitTools.Core/GitTools.Core.Shared/Git/Extensions/AuthenticationInfoExtensions.cs b/src/GitTools.Core/GitTools.Core.Shared/Git/Extensions/AuthenticationInfoExtensions.cs index 3e78830..b20818e 100644 --- a/src/GitTools.Core/GitTools.Core.Shared/Git/Extensions/AuthenticationInfoExtensions.cs +++ b/src/GitTools.Core/GitTools.Core.Shared/Git/Extensions/AuthenticationInfoExtensions.cs @@ -1,11 +1,31 @@ namespace GitTools.Git { + using LibGit2Sharp; using Logging; public static class AuthenticationInfoExtensions { private static readonly ILog Log = LogProvider.GetCurrentClassLogger(); + public static FetchOptions ToFetchOptions(this AuthenticationInfo authenticationInfo) + { + var fetchOptions = new FetchOptions(); + + if (authenticationInfo != null) + { + if (!string.IsNullOrEmpty(authenticationInfo.Username)) + { + fetchOptions.CredentialsProvider = (url, user, types) => new UsernamePasswordCredentials + { + Username = authenticationInfo.Username, + Password = authenticationInfo.Password + }; + } + } + + return fetchOptions; + } + public static bool IsEmpty(this AuthenticationInfo authenticationInfo) { if (authenticationInfo == null)