Skip to content

Commit 3049e66

Browse files
committed
Merge branch 'NoFetch'
2 parents 55d6f2c + b677335 commit 3049e66

File tree

19 files changed

+119
-35
lines changed

19 files changed

+119
-35
lines changed

GitVersionCore.Tests/BuildServers/BuildServerBaseTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public override bool CanApplyToCurrentContext()
3535
throw new NotImplementedException();
3636
}
3737

38-
public override void PerformPreProcessingSteps(string gitDirectory)
38+
public override void PerformPreProcessingSteps(string gitDirectory, bool noFetch)
3939
{
4040
throw new NotImplementedException();
4141
}

GitVersionCore/BuildServers/AppVeyor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ public override bool CanApplyToCurrentContext()
1818
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("APPVEYOR"));
1919
}
2020

21-
public override void PerformPreProcessingSteps(string gitDirectory)
21+
public override void PerformPreProcessingSteps(string gitDirectory, bool noFetch)
2222
{
2323
if (string.IsNullOrEmpty(gitDirectory))
2424
{
2525
throw new WarningException("Failed to find .git directory on agent.");
2626
}
2727

28-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
28+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch);
2929
}
3030

3131
public override string GenerateSetVersionMessage(string versionToUseForBuildNumber)

GitVersionCore/BuildServers/BuildServerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public abstract class BuildServerBase : IBuildServer
66
{
77
public abstract bool CanApplyToCurrentContext();
8-
public abstract void PerformPreProcessingSteps(string gitDirectory);
8+
public abstract void PerformPreProcessingSteps(string gitDirectory, bool noFetch);
99
public abstract string GenerateSetVersionMessage(string versionToUseForBuildNumber);
1010
public abstract string[] GenerateSetParameterMessage(string name, string value);
1111

GitVersionCore/BuildServers/ContinuaCi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public override bool CanApplyToCurrentContext()
2828
return false;
2929
}
3030

31-
public override void PerformPreProcessingSteps(string gitDirectory)
31+
public override void PerformPreProcessingSteps(string gitDirectory, bool noFetch)
3232
{
3333
if (string.IsNullOrEmpty(gitDirectory))
3434
{
3535
throw new WarningException("Failed to find .git directory on agent");
3636
}
3737

38-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
38+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch);
3939
}
4040

4141
public override string[] GenerateSetParameterMessage(string name, string value)

GitVersionCore/BuildServers/GitHelper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ public static class GitHelper
99
{
1010
const string MergeMessageRegexPattern = "refs/heads/(pr|pull(-requests)?/(?<issuenumber>[0-9]*)/(merge|head))";
1111

12-
public static void NormalizeGitDirectory(string gitDirectory, Authentication authentication)
12+
public static void NormalizeGitDirectory(string gitDirectory, Authentication authentication, bool noFetch)
1313
{
14+
//If noFetch is enabled, then GitVersion will assume that the git repository is normalized before execution, so that fetching from remotes is not required.
15+
if (noFetch) return;
16+
1417
using (var repo = new Repository(gitDirectory))
1518
{
1619
var remote = EnsureOnlyOneRemoteIsDefined(repo);

GitVersionCore/BuildServers/IBuildServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public interface IBuildServer
66
{
77
bool CanApplyToCurrentContext();
8-
void PerformPreProcessingSteps(string gitDirectory);
8+
void PerformPreProcessingSteps(string gitDirectory, bool noFetch);
99
string GenerateSetVersionMessage(string versionToUseForBuildNumber);
1010
string[] GenerateSetParameterMessage(string name, string value);
1111

GitVersionCore/BuildServers/MyGet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public override bool CanApplyToCurrentContext()
2020
&& buildRunner.Equals("MyGet", StringComparison.InvariantCultureIgnoreCase);
2121
}
2222

23-
public override void PerformPreProcessingSteps(string gitDirectory)
23+
public override void PerformPreProcessingSteps(string gitDirectory, bool noFetch)
2424
{
2525
if (string.IsNullOrEmpty(gitDirectory))
2626
{
2727
throw new WarningException("Failed to find .git directory on agent.");
2828
}
2929

30-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
30+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch);
3131
}
3232

3333
public override string[] GenerateSetParameterMessage(string name, string value)

GitVersionCore/BuildServers/TeamCity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public override bool CanApplyToCurrentContext()
1616
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TEAMCITY_VERSION"));
1717
}
1818

19-
public override void PerformPreProcessingSteps(string gitDirectory)
19+
public override void PerformPreProcessingSteps(string gitDirectory, bool noFetch)
2020
{
2121
if (string.IsNullOrEmpty(gitDirectory))
2222
{
2323
throw new WarningException("Failed to find .git directory on agent. Please make sure agent checkout mode is enabled for you VCS roots - http://confluence.jetbrains.com/display/TCD8/VCS+Checkout+Mode");
2424
}
2525

26-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
26+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch);
2727
}
2828

2929
public override string[] GenerateSetParameterMessage(string name, string value)

GitVersionExe.Tests/ArgumentParserTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,27 @@ public void can_log_to_console()
218218
var arguments = ArgumentParser.ParseArguments("-l console -proj foo.sln");
219219
arguments.LogFilePath.ShouldBe("console");
220220
}
221+
222+
[Test]
223+
public void nofetch_true_when_defined()
224+
{
225+
var arguments = ArgumentParser.ParseArguments("-nofetch");
226+
arguments.NoFetch = true;
227+
}
228+
229+
[Test]
230+
public void other_arguments_can_be_parsed_before_nofetch()
231+
{
232+
var arguments = ArgumentParser.ParseArguments("targetpath -nofetch ");
233+
arguments.TargetPath = "targetpath";
234+
arguments.NoFetch = true;
235+
}
236+
237+
[Test]
238+
public void other_arguments_can_be_parsed_after_nofetch()
239+
{
240+
var arguments = ArgumentParser.ParseArguments("-nofetch -proj foo.sln");
241+
arguments.NoFetch = true;
242+
arguments.Proj = "foo.sln";
243+
}
221244
}

GitVersionExe/ArgumentParser.cs

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ namespace GitVersion
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Collections.Specialized;
56
using System.Linq;
67
using System.Reflection;
8+
using System.Text.RegularExpressions;
9+
710

8-
911
public class ArgumentParser
1012
{
1113
static ArgumentParser()
@@ -66,11 +68,23 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
6668
arguments.TargetPath = firstArgument;
6769
namedArguments = commandLineArguments.Skip(1).ToList();
6870
}
71+
72+
var args = CollectSwitchesAndValuesFromArguments(namedArguments);
6973

70-
for (var index = 0; index < namedArguments.Count; index = index + 2)
74+
foreach (var name in args.AllKeys)
7175
{
72-
var name = namedArguments[index];
73-
var value = namedArguments.Count > index + 1 ? namedArguments[index + 1] : null;
76+
var values = args.GetValues(name);
77+
78+
string value = null;
79+
80+
if (values != null)
81+
{
82+
//Currently, no arguments use more than one value, so having multiple values is an input error.
83+
//In the future, this exception can be removed to support multiple values for a switch.
84+
if (values.Length > 1) throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", values[1]));
85+
86+
value = values.FirstOrDefault();
87+
}
7488

7589
if (IsSwitch("l", name))
7690
{
@@ -161,8 +175,7 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
161175
}
162176
else
163177
{
164-
arguments.UpdateAssemblyInfo = true;
165-
index--;
178+
arguments.UpdateAssemblyInfo = true;
166179
}
167180
continue;
168181
}
@@ -190,8 +203,7 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
190203
}
191204
else
192205
{
193-
arguments.ShowConfig = true;
194-
index--;
206+
arguments.ShowConfig = true;
195207
}
196208
continue;
197209
}
@@ -208,15 +220,54 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
208220
continue;
209221
}
210222

223+
if (IsSwitch("nofetch", name))
224+
{
225+
arguments.NoFetch = true;
226+
continue;
227+
}
228+
211229
throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", name));
212230
}
213231

214232
return arguments;
215233
}
216234

235+
static NameValueCollection CollectSwitchesAndValuesFromArguments(List<string> namedArguments)
236+
{
237+
var args = new NameValueCollection();
238+
239+
string currentKey = null;
240+
for (var index = 0; index < namedArguments.Count; index = index + 1)
241+
{
242+
var arg = namedArguments[index];
243+
//If this is a switch, create new name/value entry for it, with a null value.
244+
if (IsSwitchArgument(arg))
245+
{
246+
currentKey = arg;
247+
args.Add(currentKey, null);
248+
}
249+
//If this is a value (not a switch)
250+
else
251+
{
252+
//And if the current switch does not have a value yet, set it's value to this argument.
253+
if (String.IsNullOrEmpty(args[currentKey]))
254+
{
255+
args[currentKey] = arg;
256+
}
257+
//Otherwise add the value under the same switch.
258+
else
259+
{
260+
args.Add(currentKey, arg);
261+
}
262+
}
263+
}
264+
return args;
265+
}
266+
217267
static bool IsSwitchArgument(string value)
218268
{
219-
return value != null && (value.StartsWith("-") || value.StartsWith("/"));
269+
return value != null && (value.StartsWith("-") || value.StartsWith("/"))
270+
&& !Regex.Match(value, @"/\w+:").Success; //Exclude msbuild & project parameters in form /blah:, which should be parsed as values, not switch names.
220271
}
221272

222273
static bool IsSwitch(string switchName, string value)

0 commit comments

Comments
 (0)