Skip to content

Commit f467854

Browse files
author
Sebastian Nemeth
committed
Incorporated Jake's feedback.
1. Passed down noFetch param to NormalizeGitDirectory method. 2. Updated ArgumentParser to ensure that individual switches such as -nofetch do not interfere with existing functionality. ArgumentParser should also now support switches with multiple values such as '-myswitch value1 value2' if the validation checks are removed.
1 parent 92231b6 commit f467854

File tree

9 files changed

+80
-30
lines changed

9 files changed

+80
-30
lines changed

GitVersionCore/BuildServers/AppVeyor.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ public override void PerformPreProcessingSteps(string gitDirectory, bool noFetch
2525
throw new WarningException("Failed to find .git directory on agent.");
2626
}
2727

28-
if (!noFetch)
29-
{
30-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
31-
}
28+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch);
3229
}
3330

3431
public override string GenerateSetVersionMessage(string versionToUseForBuildNumber)

GitVersionCore/BuildServers/ContinuaCi.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ public override void PerformPreProcessingSteps(string gitDirectory, bool noFetch
3535
throw new WarningException("Failed to find .git directory on agent");
3636
}
3737

38-
if (!noFetch)
39-
{
40-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
41-
}
38+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch);
4239
}
4340

4441
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/MyGet.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ public override void PerformPreProcessingSteps(string gitDirectory, bool noFetch
2727
throw new WarningException("Failed to find .git directory on agent.");
2828
}
2929

30-
if (!noFetch)
31-
{
32-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
33-
}
30+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch);
3431
}
3532

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

GitVersionCore/BuildServers/TeamCity.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ public override void PerformPreProcessingSteps(string gitDirectory, bool noFetch
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-
if (!noFetch)
27-
{
28-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
29-
}
26+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch);
3027
}
3128

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

GitVersionExe.Tests/ArgumentParserTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,20 @@ public void nofetch_true_when_defined()
218218
var arguments = ArgumentParser.ParseArguments("-nofetch");
219219
arguments.NoFetch = true;
220220
}
221+
222+
[Test]
223+
public void other_arguments_can_be_parsed_before_nofetch()
224+
{
225+
var arguments = ArgumentParser.ParseArguments("targetpath -nofetch ");
226+
arguments.TargetPath = "targetpath";
227+
arguments.NoFetch = true;
228+
}
229+
230+
[Test]
231+
public void other_arguments_can_be_parsed_after_nofetch()
232+
{
233+
var arguments = ArgumentParser.ParseArguments("-nofetch -proj foo.sln");
234+
arguments.NoFetch = true;
235+
arguments.Proj = "foo.sln";
236+
}
221237
}

GitVersionExe/ArgumentParser.cs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace GitVersion
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Collections.Specialized;
56
using System.Linq;
67
using System.Reflection;
78

@@ -66,11 +67,23 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
6667
arguments.TargetPath = firstArgument;
6768
namedArguments = commandLineArguments.Skip(1).ToList();
6869
}
70+
71+
var args = CollectSwitchesAndValuesFromArguments(namedArguments);
6972

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

7588
if (IsSwitch("l", name))
7689
{
@@ -155,8 +168,7 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
155168
}
156169
else
157170
{
158-
arguments.UpdateAssemblyInfo = true;
159-
index--;
171+
arguments.UpdateAssemblyInfo = true;
160172
}
161173
continue;
162174
}
@@ -184,8 +196,7 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
184196
}
185197
else
186198
{
187-
arguments.ShowConfig = true;
188-
index--;
199+
arguments.ShowConfig = true;
189200
}
190201
continue;
191202
}
@@ -214,9 +225,41 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
214225
return arguments;
215226
}
216227

228+
static NameValueCollection CollectSwitchesAndValuesFromArguments(List<string> namedArguments)
229+
{
230+
var args = new NameValueCollection();
231+
232+
string currentKey = null;
233+
for (var index = 0; index < namedArguments.Count; index = index + 1)
234+
{
235+
var arg = namedArguments[index];
236+
//If this is a switch, create new name/value entry for it, with a null value.
237+
if (IsSwitchArgument(arg))
238+
{
239+
currentKey = arg;
240+
args.Add(currentKey, null);
241+
}
242+
//If this is a value (not a switch)
243+
else
244+
{
245+
//And if the current switch does not have a value yet, set it's value to this argument.
246+
if (String.IsNullOrEmpty(args[currentKey]))
247+
{
248+
args[currentKey] = arg;
249+
}
250+
//Otherwise add the value under the same switch.
251+
else
252+
{
253+
args.Add(currentKey, arg);
254+
}
255+
}
256+
}
257+
return args;
258+
}
259+
217260
static bool IsSwitchArgument(string value)
218261
{
219-
return value != null && (value.StartsWith("-") || value.StartsWith("/"));
262+
return value != null && (value.StartsWith("-") || value.StartsWith("/")) && !value.StartsWith("/p:"); //Exclude msbuild parameters, which should be parsed as values, not switch names.
220263
}
221264

222265
static bool IsSwitch(string switchName, string value)

GitVersionExe/GitPreparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ string GetGitInfoFromUrl()
6767
});
6868

6969
// Normalize (download branches) before using the branch
70-
GitHelper.NormalizeGitDirectory(gitDirectory, arguments.Authentication);
70+
GitHelper.NormalizeGitDirectory(gitDirectory, arguments.Authentication, arguments.NoFetch);
7171

7272
using (var repository = new Repository(gitDirectory))
7373
{

GitVersionTask.Tests/GitHelperTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void CanDetermineTheVersionFromAFetchedMaster()
1313
var gitDirectory = FakeTeamCityFetchAndCheckout(ASBMTestRepoWorkingDirPath, "refs/heads/master");
1414

1515
var authentication = new Authentication();
16-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
16+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, false);
1717

1818
using (var repository = new Repository(gitDirectory))
1919
{
@@ -31,7 +31,7 @@ public void CanDetermineTheVersionFromAPullRequest()
3131
var gitDirectory = FakeTeamCityFetchAndCheckout(repoPath, "refs/pull/1735/merge");
3232

3333
var authentication = new Authentication();
34-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
34+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, false);
3535

3636
using (var repository = new Repository(gitDirectory))
3737
{
@@ -46,7 +46,7 @@ public void CanDetermineTheVersionFromAFetchedDevelop()
4646
var gitDirectory = FakeTeamCityFetchAndCheckout(ASBMTestRepoWorkingDirPath, "refs/heads/develop");
4747

4848
var authentication = new Authentication();
49-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
49+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, false);
5050

5151
using (var repository = new Repository(gitDirectory))
5252
{
@@ -61,7 +61,7 @@ public void CanDetermineTheVersionFromAFetchedFeature()
6161
var gitDirectory = FakeTeamCityFetchAndCheckout(ASBMTestRepoWorkingDirPath, "refs/heads/feature/one");
6262

6363
var authentication = new Authentication();
64-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
64+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, false);
6565

6666
using (var repository = new Repository(gitDirectory))
6767
{

0 commit comments

Comments
 (0)