Skip to content

Commit 2028024

Browse files
committed
Merge pull request #101 from GeertvanHorrik/master
Added support for remote repositories (with and without authentication)
2 parents 92d8616 + 2052f25 commit 2028024

23 files changed

+466
-103
lines changed

Diff for: GitVersion/ArgumentParser.cs

+46-13
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
1616
if (commandLineArguments.Count == 0)
1717
{
1818
return new Arguments
19-
{
20-
TargetPath = Environment.CurrentDirectory
21-
};
19+
{
20+
TargetPath = Environment.CurrentDirectory
21+
};
2222
}
2323

2424
var firstArgument = commandLineArguments.First();
@@ -33,9 +33,9 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
3333
if (commandLineArguments.Count == 1)
3434
{
3535
return new Arguments
36-
{
37-
TargetPath = firstArgument
38-
};
36+
{
37+
TargetPath = firstArgument
38+
};
3939
}
4040

4141
List<string> namedArguments;
@@ -64,12 +64,48 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
6464
continue;
6565
}
6666

67+
if (IsSwitch("url", name))
68+
{
69+
arguments.TargetUrl = value;
70+
continue;
71+
}
72+
73+
if (IsSwitch("b", name))
74+
{
75+
arguments.TargetBranch = value;
76+
continue;
77+
}
78+
79+
if (IsSwitch("u", name))
80+
{
81+
arguments.Username = value;
82+
continue;
83+
}
84+
85+
if (IsSwitch("p", name))
86+
{
87+
arguments.Password = value;
88+
continue;
89+
}
90+
6791
if ((IsSwitch("v", name)) && VersionParts.Contains(value.ToLower()))
6892
{
6993
arguments.VersionPart = value.ToLower();
7094
continue;
7195
}
7296

97+
if (IsSwitch("output", name))
98+
{
99+
var outputType = OutputType.Json;
100+
if (!Enum.TryParse(value, true, out outputType))
101+
{
102+
throw new ErrorException(string.Format("Value '{0}' cannot be parsed as output type, please use 'json' or 'buildserver'", value));
103+
}
104+
105+
arguments.Output = outputType;
106+
continue;
107+
}
108+
73109
throw new ErrorException(string.Format("Could not parse command line parameter '{0}'.", name));
74110
}
75111
return arguments;
@@ -101,13 +137,10 @@ static void EnsureArgumentsEvenCount(List<string> commandLineArguments, List<str
101137

102138
static bool IsHelp(string singleArgument)
103139
{
104-
return (singleArgument == "?") ||
105-
(singleArgument == "/h") ||
106-
(singleArgument == "/help") ||
107-
(singleArgument == "/?") ||
108-
(singleArgument == "-h") ||
109-
(singleArgument == "-help") ||
110-
(singleArgument == "-?");
140+
return (singleArgument == "?") ||
141+
IsSwitch("h", singleArgument) ||
142+
IsSwitch("help", singleArgument) ||
143+
IsSwitch("?", singleArgument);
111144
}
112145

113146
static string[] VersionParts = {"major", "minor", "patch", "long", "short", "nuget"};

Diff for: GitVersion/Arguments.cs

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
namespace GitVersion
22
{
3-
class Arguments
3+
using System;
4+
5+
public enum OutputType
46
{
7+
BuildServer,
8+
9+
Json
10+
}
11+
12+
public class Arguments
13+
{
14+
public Arguments()
15+
{
16+
Username = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME");
17+
Password = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD");
18+
Output = OutputType.Json;
19+
}
20+
521
public string TargetPath;
22+
23+
public string TargetUrl;
24+
public string TargetBranch;
25+
26+
public string Username;
27+
public string Password;
28+
629
public bool IsHelp;
730
public string LogFilePath;
831
public string VersionPart;
32+
33+
public OutputType Output;
934
}
1035
}

Diff for: GitVersion/BuildServers/BuildServerBase.cs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
5+
public abstract class BuildServerBase : IBuildServer
6+
{
7+
public abstract bool CanApplyToCurrentContext();
8+
public abstract void PerformPreProcessingSteps(string gitDirectory);
9+
public abstract string GenerateSetVersionMessage(string versionToUseForBuildNumber);
10+
public abstract string[] GenerateSetParameterMessage(string name, string value);
11+
12+
public virtual void WriteIntegration(VersionAndBranch versionAndBranch, Action<string> writer)
13+
{
14+
if (versionAndBranch == null)
15+
{
16+
return;
17+
}
18+
19+
if (writer == null)
20+
{
21+
return;
22+
}
23+
24+
writer(string.Format("Executing GenerateSetVersionMessage for '{0}'.", GetType().Name));
25+
writer(GenerateSetVersionMessage(versionAndBranch.GenerateSemVer()));
26+
writer(string.Format("Executing GenerateBuildLogOutput for '{0}'.", GetType().Name));
27+
foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(versionAndBranch, this))
28+
{
29+
writer(buildParameter);
30+
}
31+
}
32+
}
33+
}

Diff for: GitVersion/BuildServers/BuildServerList.cs

+28-12
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,50 @@
55

66
public static class BuildServerList
77
{
8-
public static List<IBuildServer> BuildServers = new List<IBuildServer>
9-
{
10-
new ContinuaCi(),
11-
new TeamCity()
12-
};
8+
static List<IBuildServer> BuildServers;
139

14-
public static Func<IEnumerable<IBuildServer>> Selector = () => DefaultSelector();
10+
public static Func<Arguments, IEnumerable<IBuildServer>> Selector = arguments => DefaultSelector(arguments);
1511

1612
public static void ResetSelector()
1713
{
1814
Selector = DefaultSelector;
1915
}
2016

21-
public static IEnumerable<IBuildServer> GetApplicableBuildServers()
17+
public static IEnumerable<IBuildServer> GetApplicableBuildServers(Arguments arguments)
2218
{
23-
return Selector();
19+
return Selector(arguments);
2420
}
2521

26-
static IEnumerable<IBuildServer> DefaultSelector()
22+
static IEnumerable<IBuildServer> DefaultSelector(Arguments arguments)
2723
{
24+
if (BuildServers == null)
25+
{
26+
BuildServers = new List<IBuildServer>
27+
{
28+
new ContinuaCi(arguments),
29+
new TeamCity(arguments)
30+
};
31+
}
32+
33+
var buildServices = new List<IBuildServer>();
34+
2835
foreach (var buildServer in BuildServers)
2936
{
30-
if (buildServer.CanApplyToCurrentContext())
37+
try
38+
{
39+
if (buildServer.CanApplyToCurrentContext())
40+
{
41+
Logger.WriteInfo(string.Format("Applicable build agent found: '{0}'.", buildServer.GetType().Name));
42+
buildServices.Add(buildServer);
43+
}
44+
}
45+
catch (Exception ex)
3146
{
32-
Logger.WriteInfo(string.Format("Applicable build agent found: '{0}'.", buildServer.GetType().Name));
33-
yield return buildServer;
47+
Logger.WriteWarning(string.Format("Failed to check build server '{0}': {1}", buildServer.GetType().Name, ex.Message));
3448
}
3549
}
50+
51+
return buildServices;
3652
}
3753
}
3854
}

Diff for: GitVersion/BuildServers/ContinuaCi.cs

+34-11
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,61 @@
22
{
33
using Microsoft.Win32;
44

5-
public class ContinuaCi : IBuildServer
5+
public class ContinuaCi : BuildServerBase
66
{
7-
public bool CanApplyToCurrentContext()
7+
readonly Arguments _arguments;
8+
9+
public ContinuaCi(Arguments arguments)
10+
{
11+
_arguments = arguments;
12+
}
13+
14+
public override bool CanApplyToCurrentContext()
815
{
9-
using (var registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\VSoft Technologies\\Continua CI Agent"))
16+
const string KeyName = @"Software\VSoft Technologies\Continua CI Agent";
17+
18+
if (RegistryKeyExists(KeyName, RegistryView.Registry32))
19+
{
20+
return true;
21+
}
22+
23+
if (RegistryKeyExists(KeyName, RegistryView.Registry64))
1024
{
11-
return registryKey != null;
25+
return true;
1226
}
27+
28+
return false;
1329
}
1430

15-
public void PerformPreProcessingSteps(string gitDirectory)
31+
public override void PerformPreProcessingSteps(string gitDirectory)
1632
{
1733
if (string.IsNullOrEmpty(gitDirectory))
1834
{
1935
throw new ErrorException("Failed to find .git directory on agent");
2036
}
2137

22-
GitHelper.NormalizeGitDirectory(gitDirectory);
38+
GitHelper.NormalizeGitDirectory(gitDirectory, _arguments);
2339
}
2440

25-
public string[] GenerateSetParameterMessage(string name, string value)
41+
public override string[] GenerateSetParameterMessage(string name, string value)
2642
{
27-
return new []
43+
return new[]
2844
{
29-
string.Format("@@continua[setVariable name='GitVersion.{0}' value='{1}']", name, value)
45+
string.Format("@@continua[setVariable name='GitVersion_{0}' value='{1}']", name, value)
3046
};
3147
}
3248

33-
public string GenerateSetVersionMessage(string versionToUseForBuildNumber)
49+
public override string GenerateSetVersionMessage(string versionToUseForBuildNumber)
3450
{
3551
return string.Format("@@continua[setBuildVersion value='{0}']", versionToUseForBuildNumber);
3652
}
37-
}
3853

54+
private static bool RegistryKeyExists(string keyName, RegistryView registryView)
55+
{
56+
var localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView);
57+
localKey = localKey.OpenSubKey(keyName);
58+
59+
return localKey != null;
60+
}
61+
}
3962
}

Diff for: GitVersion/BuildServers/GitHelper.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
namespace GitVersion
22
{
3-
using System;
43
using System.Linq;
54
using LibGit2Sharp;
65

76
public static class GitHelper
87
{
9-
public static void NormalizeGitDirectory(string gitDirectory)
8+
public static void NormalizeGitDirectory(string gitDirectory, Arguments arguments)
109
{
1110
using (var repo = new Repository(gitDirectory))
1211
{
@@ -15,7 +14,7 @@ public static void NormalizeGitDirectory(string gitDirectory)
1514
Logger.WriteInfo(string.Format("Fetching from remote '{0}' using the following refspecs: {1}.",
1615
remote.Name, string.Join(", ", remote.FetchRefSpecs.Select(r => r.Specification))));
1716

18-
var fetchOptions = BuildFetchOptions();
17+
var fetchOptions = BuildFetchOptions(arguments.Username, arguments.Password);
1918
repo.Network.Fetch(remote, fetchOptions);
2019

2120
CreateMissingLocalBranchesFromRemoteTrackingOnes(repo, remote.Name);
@@ -32,16 +31,17 @@ public static void NormalizeGitDirectory(string gitDirectory)
3231
}
3332
}
3433

35-
static FetchOptions BuildFetchOptions()
34+
static FetchOptions BuildFetchOptions(string username, string password)
3635
{
37-
var username = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_USERNAME");
38-
var password = Environment.GetEnvironmentVariable("GITVERSION_REMOTE_PASSWORD");
39-
4036
var fetchOptions = new FetchOptions();
4137

4238
if (!string.IsNullOrEmpty(username))
4339
{
44-
fetchOptions.Credentials = new Credentials { Username = username, Password = password };
40+
fetchOptions.Credentials = new Credentials
41+
{
42+
Username = username,
43+
Password = password
44+
};
4545
}
4646

4747
return fetchOptions;

Diff for: GitVersion/BuildServers/IBuildServer.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
namespace GitVersion
22
{
3+
using System;
4+
35
public interface IBuildServer
46
{
57
bool CanApplyToCurrentContext();
68
void PerformPreProcessingSteps(string gitDirectory);
79
string GenerateSetVersionMessage(string versionToUseForBuildNumber);
810
string[] GenerateSetParameterMessage(string name, string value);
11+
12+
void WriteIntegration(VersionAndBranch versionAndBranch, Action<string> writer);
913
}
14+
1015
}

0 commit comments

Comments
 (0)