Skip to content

Commit 173518b

Browse files
Added support for remote repositories (with and without authentication)
1 parent 07c72e0 commit 173518b

File tree

8 files changed

+226
-27
lines changed

8 files changed

+226
-27
lines changed

GitVersion/ArgumentParser.cs

Lines changed: 30 additions & 6 deletions
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,6 +64,30 @@ 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();

GitVersion/Arguments.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ namespace GitVersion
33
class Arguments
44
{
55
public string TargetPath;
6+
7+
public string TargetUrl;
8+
public string TargetBranch;
9+
10+
public string Username;
11+
public string Password;
12+
613
public bool IsHelp;
714
public string LogFilePath;
815
public string VersionPart;

GitVersion/GitPreparer.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
namespace GitVersion
2+
{
3+
using System.IO;
4+
using LibGit2Sharp;
5+
6+
public class GitPreparer
7+
{
8+
public GitPreparer(string targetPath, string url, string branchName, string username, string password)
9+
{
10+
TargetPath = targetPath;
11+
Url = url;
12+
BranchName = branchName;
13+
Username = username;
14+
Password = password;
15+
}
16+
17+
public string TargetPath { get; private set; }
18+
19+
public string Url { get; private set; }
20+
21+
public string BranchName { get; private set; }
22+
23+
public string Username { get; private set; }
24+
25+
public string Password { get; private set; }
26+
27+
public bool IsDynamicGitRepository
28+
{
29+
get { return !string.IsNullOrWhiteSpace(DynamicGitRepositoryPath); }
30+
}
31+
32+
public string DynamicGitRepositoryPath { get; private set; }
33+
34+
public string Prepare()
35+
{
36+
var gitPath = TargetPath;
37+
38+
if (!string.IsNullOrWhiteSpace(Url))
39+
{
40+
gitPath = GetGitInfoFromUrl();
41+
}
42+
43+
return GitDirFinder.TreeWalkForGitDir(gitPath);
44+
}
45+
46+
private string GetGitInfoFromUrl()
47+
{
48+
var gitDirectory = Path.Combine(TargetPath, "_dynamicrepository", ".git");
49+
if (Directory.Exists(gitDirectory))
50+
{
51+
Logger.WriteInfo(string.Format("Deleting existing .git folder from '{0}' to force new checkout from url", gitDirectory));
52+
53+
DeleteHelper.DeleteGitRepository(gitDirectory);
54+
}
55+
56+
Credentials credentials = null;
57+
if (!string.IsNullOrWhiteSpace(Username) && !string.IsNullOrWhiteSpace(Password))
58+
{
59+
Logger.WriteInfo(string.Format("Setting up credentials using name '{0}'", Username));
60+
61+
credentials = new Credentials()
62+
{
63+
Username = Username,
64+
Password = Password
65+
};
66+
}
67+
68+
Logger.WriteInfo(string.Format("Retrieving git info from url '{0}'", Url));
69+
70+
Repository.Clone(Url, gitDirectory, checkout: false, credentials: credentials);
71+
72+
DynamicGitRepositoryPath = gitDirectory;
73+
74+
return gitDirectory;
75+
}
76+
}
77+
}

GitVersion/GitVersion.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
<Compile Include="GitHubFlow\LastTaggedReleaseFinder.cs" />
6969
<Compile Include="GitHubFlow\NextSemverCalculator.cs" />
7070
<Compile Include="GitHubFlow\NextVersionTxtFileFinder.cs" />
71+
<Compile Include="GitPreparer.cs" />
72+
<Compile Include="Helpers\DeleteHelper.cs" />
7173
<Compile Include="OutputFormatters\BuildOutputFormatter.cs" />
7274
<Compile Include="BuildServers\BuildServerList.cs" />
7375
<Compile Include="BuildServers\ContinuaCi.cs" />

GitVersion/HelpWriter.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ public static void Write()
1111
1212
GitVersion [path] [/l logFilePath]
1313
14-
path The directory containing .git. If not defined current directory is used.
15-
/l Path to logfile.
14+
path The directory containing .git. If not defined current directory is used.
15+
/url Url to remote git repository.
16+
/b Name of the branch to use on the remote repository, must be used in combination with /url.
17+
/u Username in case authentication is required.
18+
/p Password in case authentication is required.
19+
/l Path to logfile.
1620
";
1721
Console.Write(message);
1822
}

GitVersion/Helpers/DeleteHelper.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace GitVersion
2+
{
3+
using System.IO;
4+
5+
public static class DeleteHelper
6+
{
7+
public static void DeleteGitRepository(string directory)
8+
{
9+
if (string.IsNullOrEmpty(directory))
10+
{
11+
return;
12+
}
13+
14+
foreach (var fileName in Directory.GetFiles(directory, "*.*", SearchOption.AllDirectories))
15+
{
16+
var fileInfo = new FileInfo(fileName)
17+
{
18+
IsReadOnly = false
19+
};
20+
21+
fileInfo.Delete();
22+
}
23+
24+
Directory.Delete(directory, true);
25+
}
26+
}
27+
}

GitVersion/Program.cs

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,33 @@ namespace GitVersion
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.IO;
67
using System.Linq;
78

89
class Program
910
{
1011
static void Main()
1112
{
13+
int? exitCode = null;
14+
1215
try
1316
{
1417
var arguments = ArgumentParser.ParseArguments(GetArgumentsWithoutExeName());
15-
1618
if (arguments.IsHelp)
1719
{
1820
HelpWriter.Write();
1921
return;
2022
}
23+
2124
ConfigureLogging(arguments);
22-
var gitDirectory = GitDirFinder.TreeWalkForGitDir(arguments.TargetPath);
2325

26+
var gitPreparer = new GitPreparer(arguments.TargetPath, arguments.TargetUrl,
27+
arguments.TargetBranch, arguments.Username, arguments.Password);
28+
var gitDirectory = gitPreparer.Prepare();
2429
if (string.IsNullOrEmpty(gitDirectory))
2530
{
26-
Console.Error.WriteLine("Could not find .git directory");
31+
Console.Error.WriteLine("Failed to prepare or find the .git directory in path '{0}'", arguments.TargetPath);
2732
Environment.Exit(1);
2833
}
2934

@@ -40,8 +45,9 @@ static void Main()
4045
switch (arguments.VersionPart)
4146
{
4247
case null:
43-
Console.WriteLine(JsonOutputFormatter.ToJson(versionAsKeyValue));
48+
Console.WriteLine(JsonOutputFormatter.ToJson(versionAsKeyValue));
4449
break;
50+
4551
default:
4652
string part;
4753
if (!versionAsKeyValue.TryGetValue(arguments.VersionPart, out part))
@@ -51,18 +57,38 @@ static void Main()
5157
Console.WriteLine(part);
5258
break;
5359
}
54-
60+
61+
if (gitPreparer.IsDynamicGitRepository)
62+
{
63+
DeleteHelper.DeleteGitRepository(gitPreparer.DynamicGitRepositoryPath);
64+
}
5565
}
5666
catch (ErrorException exception)
5767
{
58-
Console.Error.Write("An error occurred:\r\n{0}", exception.Message);
59-
Environment.Exit(1);
68+
var error = string.Format("An error occurred:\r\n{0}", exception.Message);
69+
Logger.WriteWarning(error);
70+
71+
exitCode = 1;
6072
}
6173
catch (Exception exception)
6274
{
63-
Console.Error.Write("An unexpected error occurred:\r\n{0}", exception);
64-
Environment.Exit(1);
75+
var error = string.Format("An unexpected error occurred:\r\n{0}", exception);
76+
Logger.WriteWarning(error);
77+
78+
exitCode = 1;
79+
}
80+
81+
if (Debugger.IsAttached)
82+
{
83+
Console.ReadKey();
84+
}
85+
86+
if (!exitCode.HasValue)
87+
{
88+
exitCode = 0;
6589
}
90+
91+
Environment.Exit(exitCode.Value);
6692
}
6793

6894
static IEnumerable<IBuildServer> GetApplicableBuildServers()
@@ -72,20 +98,32 @@ static IEnumerable<IBuildServer> GetApplicableBuildServers()
7298

7399
static void ConfigureLogging(Arguments arguments)
74100
{
101+
Action<string> writeAction;
102+
75103
if (arguments.LogFilePath == null)
76104
{
77-
Logger.WriteInfo = s =>{};
105+
writeAction = x =>
106+
{
107+
Console.WriteLine(x);
108+
};
78109
}
79110
else
80111
{
81112
Directory.CreateDirectory(Path.GetDirectoryName(arguments.LogFilePath));
82113
if (File.Exists(arguments.LogFilePath))
83114
{
84-
using (File.CreateText(arguments.LogFilePath)) { }
115+
using (File.CreateText(arguments.LogFilePath)) { }
85116
}
86117

87-
Logger.WriteInfo = s => WriteLogEntry(arguments, s);
118+
writeAction = x =>
119+
{
120+
Console.WriteLine(x);
121+
WriteLogEntry(arguments, x);
122+
};
88123
}
124+
125+
Logger.WriteInfo = writeAction;
126+
Logger.WriteWarning = writeAction;
89127
}
90128

91129
static void WriteLogEntry(Arguments arguments, string s)

0 commit comments

Comments
 (0)