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

Commit 59e9207

Browse files
sharpSteffMarcBruins
authored andcommitted
Feature: Optional Git cmd support (#800)
* 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
1 parent c36eb7d commit 59e9207

File tree

52 files changed

+908
-276
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+908
-276
lines changed

NuKeeper.Abstractions/CollaborationPlatform/ICollaborationFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System;
2+
using System.Threading.Tasks;
23
using NuKeeper.Abstractions.Configuration;
34

45
namespace NuKeeper.Abstractions.CollaborationPlatform
56
{
67
public interface ICollaborationFactory
78
{
8-
ValidationResult Initialise(Uri apiUri, string token,
9+
Task<ValidationResult> Initialise(Uri apiUri, string token,
910
ForkMode? forkModeFromSettings, Platform? platformFromSettings);
1011

1112
ICommitWorder CommitWorder { get; }

NuKeeper.Abstractions/CollaborationPlatform/ISettingsReader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using NuKeeper.Abstractions.Configuration;
34

45
namespace NuKeeper.Abstractions.CollaborationPlatform
@@ -7,9 +8,9 @@ public interface ISettingsReader
78
{
89
Platform Platform { get; }
910

10-
bool CanRead(Uri repositoryUri);
11+
Task<bool> CanRead(Uri repositoryUri);
1112

12-
RepositorySettings RepositorySettings(Uri repositoryUri, string targetBranch = null);
13+
Task<RepositorySettings> RepositorySettings(Uri repositoryUri, string targetBranch = null);
1314

1415
void UpdateCollaborationPlatformSettings(CollaborationPlatformSettings settings);
1516
}

NuKeeper.Abstractions/Configuration/FileSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class FileSettings
4242
public string BranchNamePrefix { get; set; }
4343
public bool? DeleteBranchAfterMerge { get; set; }
4444

45+
public string GitCliPath { get; set; }
46+
4547
public static FileSettings Empty()
4648
{
4749
return new FileSettings();

NuKeeper.Abstractions/Configuration/UserSettings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ public class UserSettings
2121
public string OutputFileName { get; set; }
2222

2323
public string Directory { get; set; }
24+
25+
public string GitPath { get; set; }
2426
}
2527
}

NuKeeper.Abstractions/Formats/UriFormats.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Threading.Tasks;
34
using NuKeeper.Abstractions.Git;
45

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

26-
public static Uri GetRemoteUriFromLocalRepo(this Uri repositoryUri, IGitDiscoveryDriver discoveryDriver, string shouldMatchTo)
27+
public static async Task<Uri> GetRemoteUriFromLocalRepo(this Uri repositoryUri, IGitDiscoveryDriver discoveryDriver, string shouldMatchTo)
2728
{
28-
if (discoveryDriver.IsGitRepo(repositoryUri))
29+
if (await discoveryDriver.IsGitRepo(repositoryUri))
2930
{
3031
// Check the origin remotes
31-
var origin = discoveryDriver.GetRemoteForPlatform(repositoryUri, shouldMatchTo);
32+
var origin = await discoveryDriver.GetRemoteForPlatform(repositoryUri, shouldMatchTo);
3233

3334
if (origin != null)
3435
{
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34

45
namespace NuKeeper.Abstractions.Git
56
{
@@ -10,18 +11,18 @@ public interface IGitDiscoveryDriver
1011
/// </summary>
1112
/// <param name="repositoryUri"></param>
1213
/// <returns></returns>
13-
bool IsGitRepo(Uri repositoryUri);
14+
Task<bool> IsGitRepo(Uri repositoryUri);
1415

1516
/// <summary>
1617
/// Get all the configured remotes
1718
/// </summary>
1819
/// <param name="repositoryUri"></param>
19-
IEnumerable<GitRemote> GetRemotes(Uri repositoryUri);
20+
Task<IEnumerable<GitRemote>> GetRemotes(Uri repositoryUri);
2021

21-
Uri DiscoverRepo(Uri repositoryUri);
22+
Task<Uri> DiscoverRepo(Uri repositoryUri);
2223

23-
string GetCurrentHead(Uri repositoryUri);
24+
Task<string> GetCurrentHead(Uri repositoryUri);
2425

25-
GitRemote GetRemoteForPlatform(Uri repositoryUri, string platformHost);
26+
Task<GitRemote> GetRemoteForPlatform(Uri repositoryUri, string platformHost);
2627
}
2728
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using NuKeeper.Abstractions.Inspections.Files;
34

45
namespace NuKeeper.Abstractions.Git
@@ -7,21 +8,21 @@ public interface IGitDriver
78
{
89
IFolder WorkingFolder { get; }
910

10-
void Clone(Uri pullEndpoint);
11+
Task Clone(Uri pullEndpoint);
1112

12-
void Clone(Uri pullEndpoint, string branchName);
13+
Task Clone(Uri pullEndpoint, string branchName);
1314

14-
void AddRemote(string name, Uri endpoint);
15+
Task AddRemote(string name, Uri endpoint);
1516

16-
void Checkout(string branchName);
17+
Task Checkout(string branchName);
1718

18-
void CheckoutNewBranch(string branchName);
19+
Task CheckoutNewBranch(string branchName);
1920

20-
void Commit(string message);
21+
Task Commit(string message);
2122

22-
void Push(string remoteName, string branchName);
23+
Task Push(string remoteName, string branchName);
2324

24-
string GetCurrentHead();
25+
Task<string> GetCurrentHead();
2526

2627
}
2728
}

NuKeeper.Abstractions/LinqAsync.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,28 @@ public static async Task<IEnumerable<T>> WhereAsync<T>(this IEnumerable<T> items
2727
.Where(x => x.PredTask.Result)
2828
.Select(x => x.Item);
2929
}
30+
31+
/// <summary>
32+
/// Async first or default implementation
33+
/// </summary>
34+
/// <typeparam name="T"></typeparam>
35+
/// <param name="items"></param>
36+
/// <param name="predicate"></param>
37+
/// <returns></returns>
38+
public static async Task<T> FirstOrDefaultAsync<T>(this IEnumerable<T> items, Func<T, Task<bool>> predicate)
39+
{
40+
var itemTaskList = items
41+
.Select(item => new { Item = item, PredTask = predicate.Invoke(item) })
42+
.ToList();
43+
44+
await Task.WhenAll(itemTaskList.Select(x => x.PredTask));
45+
var firstOrDefault = itemTaskList.FirstOrDefault(x => x.PredTask.Result);
46+
if (firstOrDefault == null)
47+
{
48+
return await Task.FromResult(default(T));
49+
}
50+
51+
return firstOrDefault.Item;
52+
}
3053
}
3154
}

NuKeeper.AzureDevOps/AzureDevOpsSettingsReader.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using System.Threading.Tasks;
34
using NuKeeper.Abstractions;
45
using NuKeeper.Abstractions.Configuration;
56
using NuKeeper.Abstractions.Formats;
@@ -20,7 +21,7 @@ public AzureDevOpsSettingsReader(IGitDiscoveryDriver gitDriver, IEnvironmentVari
2021
_gitDriver = gitDriver;
2122
}
2223

23-
public override bool CanRead(Uri repositoryUri)
24+
public override async Task<bool> CanRead(Uri repositoryUri)
2425
{
2526
if (repositoryUri == null)
2627
{
@@ -30,22 +31,22 @@ public override bool CanRead(Uri repositoryUri)
3031
// Is the specified folder already a git repository?
3132
if (repositoryUri.IsFile)
3233
{
33-
repositoryUri = repositoryUri.GetRemoteUriFromLocalRepo(_gitDriver, PlatformHost);
34+
repositoryUri = await repositoryUri.GetRemoteUriFromLocalRepo(_gitDriver, PlatformHost);
3435
}
3536

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

40-
public override RepositorySettings RepositorySettings(Uri repositoryUri, string targetBranch)
41+
public override async Task<RepositorySettings> RepositorySettings(Uri repositoryUri, string targetBranch)
4142
{
4243
if (repositoryUri == null)
4344
{
4445
return null;
4546
}
4647

4748
var settings = repositoryUri.IsFile
48-
? CreateSettingsFromLocal(repositoryUri, targetBranch)
49+
? await CreateSettingsFromLocal(repositoryUri, targetBranch)
4950
: CreateSettingsFromRemote(repositoryUri);
5051

5152
if (settings == null)
@@ -77,22 +78,22 @@ private RepositorySettings CreateSettingsFromRemote(Uri repositoryUri)
7778
return CreateRepositorySettings(org, repositoryUri, project, repoName);
7879
}
7980

80-
private RepositorySettings CreateSettingsFromLocal(Uri repositoryUri, string targetBranch)
81+
private async Task<RepositorySettings> CreateSettingsFromLocal(Uri repositoryUri, string targetBranch)
8182
{
8283
var remoteInfo = new RemoteInfo();
8384

8485
var localCopy = repositoryUri;
85-
if (_gitDriver.IsGitRepo(repositoryUri))
86+
if (await _gitDriver.IsGitRepo(repositoryUri))
8687
{
8788
// Check the origin remotes
88-
var origin = _gitDriver.GetRemoteForPlatform(repositoryUri, PlatformHost);
89+
var origin = await _gitDriver.GetRemoteForPlatform(repositoryUri, PlatformHost);
8990

9091
if (origin != null)
9192
{
92-
remoteInfo.LocalRepositoryUri = _gitDriver.DiscoverRepo(localCopy); // Set to the folder, because we found a remote git repository
93+
remoteInfo.LocalRepositoryUri = await _gitDriver.DiscoverRepo(localCopy); // Set to the folder, because we found a remote git repository
9394
repositoryUri = origin.Url;
9495
remoteInfo.WorkingFolder = localCopy;
95-
remoteInfo.BranchName = targetBranch ?? _gitDriver.GetCurrentHead(remoteInfo.LocalRepositoryUri);
96+
remoteInfo.BranchName = targetBranch ?? await _gitDriver.GetCurrentHead(remoteInfo.LocalRepositoryUri);
9697
remoteInfo.RemoteName = origin.Name;
9798
}
9899
}

NuKeeper.AzureDevOps/BaseSettingsReader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using NuKeeper.Abstractions;
34
using NuKeeper.Abstractions.CollaborationPlatform;
45
using NuKeeper.Abstractions.Configuration;
@@ -16,7 +17,7 @@ public BaseSettingsReader(IEnvironmentVariablesProvider environmentVariablesProv
1617

1718
public Platform Platform => Platform.AzureDevOps;
1819

19-
public abstract bool CanRead(Uri repositoryUri);
20+
public abstract Task<bool> CanRead(Uri repositoryUri);
2021

2122
public void UpdateCollaborationPlatformSettings(CollaborationPlatformSettings settings)
2223
{
@@ -26,6 +27,6 @@ public void UpdateCollaborationPlatformSettings(CollaborationPlatformSettings se
2627
settings.ForkMode = settings.ForkMode ?? ForkMode.SingleRepositoryOnly;
2728
}
2829

29-
public abstract RepositorySettings RepositorySettings(Uri repositoryUri, string targetBranch);
30+
public abstract Task<RepositorySettings> RepositorySettings(Uri repositoryUri, string targetBranch);
3031
}
3132
}

0 commit comments

Comments
 (0)