Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Feature: Optional Git cmd support (#800)
Browse files Browse the repository at this point in the history
* Fix uri issue with subdomain addresses like
https://yourserver/git/YourOrga/Repo.git

* removed System.IO using

* Starting GitCmdDriver implementation.
Works with gitea.

additional input parameter is still needed

* Starting GitCmdDriver implementation.
Works with gitea.

additional input parameter is still needed

* added --gitclipath, -git as input parameter to specificy the path to git.exe. when setting this value GitCmdDriver will be initialied instead of Lib2SharpDriver.

Added credentials handling into git clone

* fix nullreference and integrate gitpath setting injection

* disable warning for all type of configuration and plattform

* cleanup

* Replace executing external process with nukeeper implementation.
Fixed the issue that remote add might have bad credentials

* implemented the async approach in IGitDriver

* implemented async approach in IGitDiscoveryDriver and IGitDriver

Added GitCmdDiscovery
Added Nukeeper.Git.Tests assembly for general git unit tests

* fixed some unittests

* fix build

* try catch issue in linux

* fixed CloneRepo for Linux

* finish gitcmddiscoverydriver implementation

* add documentation
  • Loading branch information
sharpSteff authored and MarcBruins committed Jun 28, 2019
1 parent c36eb7d commit 59e9207
Show file tree
Hide file tree
Showing 52 changed files with 908 additions and 276 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Threading.Tasks;
using NuKeeper.Abstractions.Configuration;

namespace NuKeeper.Abstractions.CollaborationPlatform
{
public interface ICollaborationFactory
{
ValidationResult Initialise(Uri apiUri, string token,
Task<ValidationResult> Initialise(Uri apiUri, string token,
ForkMode? forkModeFromSettings, Platform? platformFromSettings);

ICommitWorder CommitWorder { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using NuKeeper.Abstractions.Configuration;

namespace NuKeeper.Abstractions.CollaborationPlatform
Expand All @@ -7,9 +8,9 @@ public interface ISettingsReader
{
Platform Platform { get; }

bool CanRead(Uri repositoryUri);
Task<bool> CanRead(Uri repositoryUri);

RepositorySettings RepositorySettings(Uri repositoryUri, string targetBranch = null);
Task<RepositorySettings> RepositorySettings(Uri repositoryUri, string targetBranch = null);

void UpdateCollaborationPlatformSettings(CollaborationPlatformSettings settings);
}
Expand Down
2 changes: 2 additions & 0 deletions NuKeeper.Abstractions/Configuration/FileSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class FileSettings
public string BranchNamePrefix { get; set; }
public bool? DeleteBranchAfterMerge { get; set; }

public string GitCliPath { get; set; }

public static FileSettings Empty()
{
return new FileSettings();
Expand Down
2 changes: 2 additions & 0 deletions NuKeeper.Abstractions/Configuration/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ public class UserSettings
public string OutputFileName { get; set; }

public string Directory { get; set; }

public string GitPath { get; set; }
}
}
7 changes: 4 additions & 3 deletions NuKeeper.Abstractions/Formats/UriFormats.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading.Tasks;
using NuKeeper.Abstractions.Git;

namespace NuKeeper.Abstractions.Formats
Expand All @@ -23,12 +24,12 @@ public static Uri EnsureTrailingSlash(Uri uri)
return new Uri(path + "/");
}

public static Uri GetRemoteUriFromLocalRepo(this Uri repositoryUri, IGitDiscoveryDriver discoveryDriver, string shouldMatchTo)
public static async Task<Uri> GetRemoteUriFromLocalRepo(this Uri repositoryUri, IGitDiscoveryDriver discoveryDriver, string shouldMatchTo)
{
if (discoveryDriver.IsGitRepo(repositoryUri))
if (await discoveryDriver.IsGitRepo(repositoryUri))
{
// Check the origin remotes
var origin = discoveryDriver.GetRemoteForPlatform(repositoryUri, shouldMatchTo);
var origin = await discoveryDriver.GetRemoteForPlatform(repositoryUri, shouldMatchTo);

if (origin != null)
{
Expand Down
11 changes: 6 additions & 5 deletions NuKeeper.Abstractions/Git/IGitDiscoveryDriver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace NuKeeper.Abstractions.Git
{
Expand All @@ -10,18 +11,18 @@ public interface IGitDiscoveryDriver
/// </summary>
/// <param name="repositoryUri"></param>
/// <returns></returns>
bool IsGitRepo(Uri repositoryUri);
Task<bool> IsGitRepo(Uri repositoryUri);

/// <summary>
/// Get all the configured remotes
/// </summary>
/// <param name="repositoryUri"></param>
IEnumerable<GitRemote> GetRemotes(Uri repositoryUri);
Task<IEnumerable<GitRemote>> GetRemotes(Uri repositoryUri);

Uri DiscoverRepo(Uri repositoryUri);
Task<Uri> DiscoverRepo(Uri repositoryUri);

string GetCurrentHead(Uri repositoryUri);
Task<string> GetCurrentHead(Uri repositoryUri);

GitRemote GetRemoteForPlatform(Uri repositoryUri, string platformHost);
Task<GitRemote> GetRemoteForPlatform(Uri repositoryUri, string platformHost);
}
}
17 changes: 9 additions & 8 deletions NuKeeper.Abstractions/Git/IGitDriver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using NuKeeper.Abstractions.Inspections.Files;

namespace NuKeeper.Abstractions.Git
Expand All @@ -7,21 +8,21 @@ public interface IGitDriver
{
IFolder WorkingFolder { get; }

void Clone(Uri pullEndpoint);
Task Clone(Uri pullEndpoint);

void Clone(Uri pullEndpoint, string branchName);
Task Clone(Uri pullEndpoint, string branchName);

void AddRemote(string name, Uri endpoint);
Task AddRemote(string name, Uri endpoint);

void Checkout(string branchName);
Task Checkout(string branchName);

void CheckoutNewBranch(string branchName);
Task CheckoutNewBranch(string branchName);

void Commit(string message);
Task Commit(string message);

void Push(string remoteName, string branchName);
Task Push(string remoteName, string branchName);

string GetCurrentHead();
Task<string> GetCurrentHead();

}
}
23 changes: 23 additions & 0 deletions NuKeeper.Abstractions/LinqAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,28 @@ public static async Task<IEnumerable<T>> WhereAsync<T>(this IEnumerable<T> items
.Where(x => x.PredTask.Result)
.Select(x => x.Item);
}

/// <summary>
/// Async first or default implementation
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="items"></param>
/// <param name="predicate"></param>
/// <returns></returns>
public static async Task<T> FirstOrDefaultAsync<T>(this IEnumerable<T> items, Func<T, Task<bool>> predicate)
{
var itemTaskList = items
.Select(item => new { Item = item, PredTask = predicate.Invoke(item) })
.ToList();

await Task.WhenAll(itemTaskList.Select(x => x.PredTask));
var firstOrDefault = itemTaskList.FirstOrDefault(x => x.PredTask.Result);
if (firstOrDefault == null)
{
return await Task.FromResult(default(T));
}

return firstOrDefault.Item;
}
}
}
19 changes: 10 additions & 9 deletions NuKeeper.AzureDevOps/AzureDevOpsSettingsReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using NuKeeper.Abstractions;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.Formats;
Expand All @@ -20,7 +21,7 @@ public AzureDevOpsSettingsReader(IGitDiscoveryDriver gitDriver, IEnvironmentVari
_gitDriver = gitDriver;
}

public override bool CanRead(Uri repositoryUri)
public override async Task<bool> CanRead(Uri repositoryUri)
{
if (repositoryUri == null)
{
Expand All @@ -30,22 +31,22 @@ public override bool CanRead(Uri repositoryUri)
// Is the specified folder already a git repository?
if (repositoryUri.IsFile)
{
repositoryUri = repositoryUri.GetRemoteUriFromLocalRepo(_gitDriver, PlatformHost);
repositoryUri = await repositoryUri.GetRemoteUriFromLocalRepo(_gitDriver, PlatformHost);
}

// Did we specify a Azure DevOps url?
return repositoryUri?.Host.Contains(PlatformHost, StringComparison.OrdinalIgnoreCase) == true;
}

public override RepositorySettings RepositorySettings(Uri repositoryUri, string targetBranch)
public override async Task<RepositorySettings> RepositorySettings(Uri repositoryUri, string targetBranch)
{
if (repositoryUri == null)
{
return null;
}

var settings = repositoryUri.IsFile
? CreateSettingsFromLocal(repositoryUri, targetBranch)
? await CreateSettingsFromLocal(repositoryUri, targetBranch)
: CreateSettingsFromRemote(repositoryUri);

if (settings == null)
Expand Down Expand Up @@ -77,22 +78,22 @@ private RepositorySettings CreateSettingsFromRemote(Uri repositoryUri)
return CreateRepositorySettings(org, repositoryUri, project, repoName);
}

private RepositorySettings CreateSettingsFromLocal(Uri repositoryUri, string targetBranch)
private async Task<RepositorySettings> CreateSettingsFromLocal(Uri repositoryUri, string targetBranch)
{
var remoteInfo = new RemoteInfo();

var localCopy = repositoryUri;
if (_gitDriver.IsGitRepo(repositoryUri))
if (await _gitDriver.IsGitRepo(repositoryUri))
{
// Check the origin remotes
var origin = _gitDriver.GetRemoteForPlatform(repositoryUri, PlatformHost);
var origin = await _gitDriver.GetRemoteForPlatform(repositoryUri, PlatformHost);

if (origin != null)
{
remoteInfo.LocalRepositoryUri = _gitDriver.DiscoverRepo(localCopy); // Set to the folder, because we found a remote git repository
remoteInfo.LocalRepositoryUri = await _gitDriver.DiscoverRepo(localCopy); // Set to the folder, because we found a remote git repository
repositoryUri = origin.Url;
remoteInfo.WorkingFolder = localCopy;
remoteInfo.BranchName = targetBranch ?? _gitDriver.GetCurrentHead(remoteInfo.LocalRepositoryUri);
remoteInfo.BranchName = targetBranch ?? await _gitDriver.GetCurrentHead(remoteInfo.LocalRepositoryUri);
remoteInfo.RemoteName = origin.Name;
}
}
Expand Down
5 changes: 3 additions & 2 deletions NuKeeper.AzureDevOps/BaseSettingsReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using NuKeeper.Abstractions;
using NuKeeper.Abstractions.CollaborationPlatform;
using NuKeeper.Abstractions.Configuration;
Expand All @@ -16,7 +17,7 @@ public BaseSettingsReader(IEnvironmentVariablesProvider environmentVariablesProv

public Platform Platform => Platform.AzureDevOps;

public abstract bool CanRead(Uri repositoryUri);
public abstract Task<bool> CanRead(Uri repositoryUri);

public void UpdateCollaborationPlatformSettings(CollaborationPlatformSettings settings)
{
Expand All @@ -26,6 +27,6 @@ public void UpdateCollaborationPlatformSettings(CollaborationPlatformSettings se
settings.ForkMode = settings.ForkMode ?? ForkMode.SingleRepositoryOnly;
}

public abstract RepositorySettings RepositorySettings(Uri repositoryUri, string targetBranch);
public abstract Task<RepositorySettings> RepositorySettings(Uri repositoryUri, string targetBranch);
}
}
19 changes: 10 additions & 9 deletions NuKeeper.AzureDevOps/TfsSettingsReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using NuKeeper.Abstractions;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.Formats;
Expand All @@ -20,7 +21,7 @@ public TfsSettingsReader(IGitDiscoveryDriver gitDriver, IEnvironmentVariablesPro
_gitDriver = gitDriver;
}

public override bool CanRead(Uri repositoryUri)
public override async Task<bool> CanRead(Uri repositoryUri)
{
if (repositoryUri == null)
{
Expand All @@ -30,7 +31,7 @@ public override bool CanRead(Uri repositoryUri)
// Is the specified folder already a git repository?
if (repositoryUri.IsFile)
{
repositoryUri = repositoryUri.GetRemoteUriFromLocalRepo(_gitDriver, PlatformHost);
repositoryUri = await repositoryUri.GetRemoteUriFromLocalRepo(_gitDriver, PlatformHost);
}

var path = repositoryUri.AbsolutePath;
Expand All @@ -43,15 +44,15 @@ public override bool CanRead(Uri repositoryUri)
return tfsInPath || tfsInHost;
}

public override RepositorySettings RepositorySettings(Uri repositoryUri, string targetBranch)
public override async Task<RepositorySettings> RepositorySettings(Uri repositoryUri, string targetBranch)
{
if (repositoryUri == null)
{
return null;
}

var settings = repositoryUri.IsFile
? CreateSettingsFromLocal(repositoryUri, targetBranch)
? await CreateSettingsFromLocal(repositoryUri, targetBranch)
: CreateSettingsFromRemote(repositoryUri);
if (settings == null)
{
Expand All @@ -66,21 +67,21 @@ private static RepositorySettings CreateSettingsFromRemote(Uri repositoryUri)
return RepositorySettings(repositoryUri);
}

private RepositorySettings CreateSettingsFromLocal(Uri repositoryUri, string targetBranch)
private async Task<RepositorySettings> CreateSettingsFromLocal(Uri repositoryUri, string targetBranch)
{
var remoteInfo = new RemoteInfo();

var localFolder = repositoryUri;
if (_gitDriver.IsGitRepo(repositoryUri))
if (await _gitDriver.IsGitRepo(repositoryUri))
{
// Check the origin remotes
var origin = _gitDriver.GetRemoteForPlatform(repositoryUri, PlatformHost);
var origin = await _gitDriver.GetRemoteForPlatform(repositoryUri, PlatformHost);

if (origin != null)
{
remoteInfo.LocalRepositoryUri = _gitDriver.DiscoverRepo(repositoryUri); // Set to the folder, because we found a remote git repository
remoteInfo.LocalRepositoryUri = await _gitDriver.DiscoverRepo(repositoryUri); // Set to the folder, because we found a remote git repository
repositoryUri = origin.Url;
remoteInfo.BranchName = targetBranch ?? _gitDriver.GetCurrentHead(remoteInfo.LocalRepositoryUri);
remoteInfo.BranchName = targetBranch ?? await _gitDriver.GetCurrentHead(remoteInfo.LocalRepositoryUri);
remoteInfo.RemoteName = origin.Name;
remoteInfo.WorkingFolder = localFolder;
}
Expand Down
19 changes: 10 additions & 9 deletions NuKeeper.AzureDevOps/VSTSSettingsReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using NuKeeper.Abstractions;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.Formats;
Expand All @@ -20,7 +21,7 @@ public VstsSettingsReader(IGitDiscoveryDriver gitDriver, IEnvironmentVariablesPr
_gitDriver = gitDriver;
}

public override bool CanRead(Uri repositoryUri)
public override async Task<bool> CanRead(Uri repositoryUri)
{
if (repositoryUri == null)
{
Expand All @@ -30,21 +31,21 @@ public override bool CanRead(Uri repositoryUri)
// Is the specified folder already a git repository?
if (repositoryUri.IsFile)
{
repositoryUri = repositoryUri.GetRemoteUriFromLocalRepo(_gitDriver, PlatformHost);
repositoryUri = await repositoryUri.GetRemoteUriFromLocalRepo(_gitDriver, PlatformHost);
}

return repositoryUri?.Host.Contains(PlatformHost, StringComparison.OrdinalIgnoreCase) == true;
}

public override RepositorySettings RepositorySettings(Uri repositoryUri, string targetBranch)
public override async Task<RepositorySettings> RepositorySettings(Uri repositoryUri, string targetBranch)
{
if (repositoryUri == null)
{
return null;
}

var settings = repositoryUri.IsFile
? CreateSettingsFromLocal(repositoryUri, targetBranch)
? await CreateSettingsFromLocal(repositoryUri, targetBranch)
: CreateSettingsFromRemote(repositoryUri);
if (settings == null)
{
Expand Down Expand Up @@ -80,21 +81,21 @@ private RepositorySettings CreateSettingsFromRemote(Uri repositoryUri)
}


private RepositorySettings CreateSettingsFromLocal(Uri repositoryUri, string targetBranch)
private async Task<RepositorySettings> CreateSettingsFromLocal(Uri repositoryUri, string targetBranch)
{
var remoteInfo = new RemoteInfo();

var localFolder = repositoryUri;
if (_gitDriver.IsGitRepo(repositoryUri))
if (await _gitDriver.IsGitRepo(repositoryUri))
{
// Check the origin remotes
var origin = _gitDriver.GetRemoteForPlatform(repositoryUri, PlatformHost);
var origin = await _gitDriver.GetRemoteForPlatform(repositoryUri, PlatformHost);

if (origin != null)
{
remoteInfo.LocalRepositoryUri = _gitDriver.DiscoverRepo(repositoryUri); // Set to the folder, because we found a remote git repository
remoteInfo.LocalRepositoryUri = await _gitDriver.DiscoverRepo(repositoryUri); // Set to the folder, because we found a remote git repository
repositoryUri = origin.Url;
remoteInfo.BranchName = targetBranch ?? _gitDriver.GetCurrentHead(remoteInfo.LocalRepositoryUri);
remoteInfo.BranchName = targetBranch ?? await _gitDriver.GetCurrentHead(remoteInfo.LocalRepositoryUri);
remoteInfo.RemoteName = origin.Name;
remoteInfo.WorkingFolder = localFolder;
}
Expand Down
Loading

0 comments on commit 59e9207

Please sign in to comment.