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)

GitVersionExe/Arguments.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ public Arguments()
3434
public string UpdateAssemblyInfoFileName;
3535

3636
public bool ShowConfig;
37+
public bool NoFetch { get; set; }
3738
}
3839
}

GitVersionExe/GitPreparer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void InitialiseDynamicRepositoryIfNeeded()
2626
if (string.IsNullOrWhiteSpace(arguments.TargetUrl)) return;
2727

2828
var targetPath = CalculateTemporaryRepositoryPath(arguments.TargetUrl, arguments.DynamicRepositoryLocation);
29-
DynamicGitRepositoryPath = CreateDynamicRepository(targetPath, arguments.Authentication, arguments.TargetUrl, arguments.TargetBranch);
29+
DynamicGitRepositoryPath = CreateDynamicRepository(targetPath, arguments.Authentication, arguments.TargetUrl, arguments.TargetBranch, arguments.NoFetch);
3030
}
3131

3232
string CalculateTemporaryRepositoryPath(string targetUrl, string dynamicRepositoryLocation)
@@ -78,7 +78,7 @@ public string GetDotGitDirectory()
7878
return GitDirFinder.TreeWalkForDotGitDir(arguments.TargetPath);
7979
}
8080

81-
static string CreateDynamicRepository(string targetPath, Authentication authentication, string repositoryUrl, string targetBranch)
81+
static string CreateDynamicRepository(string targetPath, Authentication authentication, string repositoryUrl, string targetBranch, bool noFetch)
8282
{
8383
var gitDirectory = Path.Combine(targetPath, ".git");
8484
if (Directory.Exists(targetPath))
@@ -111,7 +111,7 @@ static string CreateDynamicRepository(string targetPath, Authentication authenti
111111
});
112112

113113
// Normalize (download branches) before using the branch
114-
GitHelper.NormalizeGitDirectory(gitDirectory, authentication);
114+
GitHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch);
115115

116116
using (var repository = new Repository(gitDirectory))
117117
{

GitVersionExe/SpecifiedArgumentRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void Run(Arguments arguments, IFileSystem fileSystem)
2222

2323
foreach (var buildServer in applicableBuildServers)
2424
{
25-
buildServer.PerformPreProcessingSteps(dotGitDirectory);
25+
buildServer.PerformPreProcessingSteps(dotGitDirectory, arguments.NoFetch);
2626
}
2727
VersionVariables variables;
2828
var versionFinder = new GitVersionFinder();

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
{

GitVersionTask.Tests/VersionAndBranchFinderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void ShouldBeAbleGetVersionFromGitDir()
2020
}
2121

2222
Tuple<CachedVersion, GitVersionContext> versionAndBranch;
23-
VersionAndBranchFinder.TryGetVersion(ASBMTestRepoWorkingDirPath, out versionAndBranch, new Config());
23+
VersionAndBranchFinder.TryGetVersion(ASBMTestRepoWorkingDirPath, out versionAndBranch, new Config(), false);
2424
Assert.IsNotNull(versionAndBranch);
2525
}
2626
}

GitVersionTask/AssemblyInfoBuilder/UpdateAssemblyInfo.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class UpdateAssemblyInfo : Task
2525
[Output]
2626
public string AssemblyInfoTempFilePath { get; set; }
2727

28+
public bool NoFetch { get; set; }
29+
2830
TaskLogger logger;
2931
IFileSystem fileSystem;
3032

@@ -73,7 +75,7 @@ public void InnerExecute()
7375
var configuration = ConfigurationProvider.Provide(gitDirectory, fileSystem);
7476

7577
Tuple<CachedVersion, GitVersionContext> semanticVersion;
76-
if (!VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out semanticVersion, configuration))
78+
if (!VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out semanticVersion, configuration, NoFetch))
7779
{
7880
return;
7981
}

GitVersionTask/GetVersion.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class GetVersion : Task
1212
[Required]
1313
public string SolutionDirectory { get; set; }
1414

15+
public bool NoFetch { get; set; }
16+
1517
[Output]
1618
public string Major { get; set; }
1719

@@ -85,7 +87,7 @@ public override bool Execute()
8587
var gitDirectory = GitDirFinder.TreeWalkForDotGitDir(SolutionDirectory);
8688
var configuration = ConfigurationProvider.Provide(gitDirectory, fileSystem);
8789

88-
if (VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out versionAndBranch, configuration))
90+
if (VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out versionAndBranch, configuration, NoFetch))
8991
{
9092
var thisType = typeof(GetVersion);
9193
var cachedVersion = versionAndBranch.Item1;

GitVersionTask/VersionAndBranchFinder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public static class VersionAndBranchFinder
66
{
77
static List<string> processedDirectories = new List<string>();
8-
public static bool TryGetVersion(string directory, out Tuple<CachedVersion, GitVersionContext> versionAndBranch, Config configuration)
8+
public static bool TryGetVersion(string directory, out Tuple<CachedVersion, GitVersionContext> versionAndBranch, Config configuration, bool noFetch)
99
{
1010
var gitDirectory = GitDirFinder.TreeWalkForDotGitDir(directory);
1111

@@ -28,7 +28,7 @@ public static bool TryGetVersion(string directory, out Tuple<CachedVersion, GitV
2828
foreach (var buildServer in BuildServerList.GetApplicableBuildServers(authentication))
2929
{
3030
Logger.WriteInfo(string.Format("Executing PerformPreProcessingSteps for '{0}'.", buildServer.GetType().Name));
31-
buildServer.PerformPreProcessingSteps(gitDirectory);
31+
buildServer.PerformPreProcessingSteps(gitDirectory, noFetch);
3232
}
3333
}
3434
versionAndBranch = VersionCache.GetVersion(gitDirectory, configuration);

0 commit comments

Comments
 (0)