Skip to content

Commit 54cf2c3

Browse files
committed
Prevent fetch for Vso and Jenkins
Fixes #604 #606
1 parent 6af005e commit 54cf2c3

File tree

7 files changed

+44
-22
lines changed

7 files changed

+44
-22
lines changed

src/GitVersionCore/BuildServers/BuildServerBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public virtual string GetCurrentBranch()
1313
return null;
1414
}
1515

16+
public virtual bool PreventFetch()
17+
{
18+
return false;
19+
}
20+
1621
public virtual void WriteIntegration(Action<string> writer, VersionVariables variables)
1722
{
1823
if (writer == null)

src/GitVersionCore/BuildServers/IBuildServer.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ public interface IBuildServer
77
bool CanApplyToCurrentContext();
88
string GenerateSetVersionMessage(VersionVariables variables);
99
string[] GenerateSetParameterMessage(string name, string value);
10-
1110
void WriteIntegration(Action<string> writer, VersionVariables variables);
1211
string GetCurrentBranch();
12+
/// <summary>
13+
/// If the build server should not try and fetch
14+
/// </summary>
15+
bool PreventFetch();
1316
}
1417
}

src/GitVersionCore/BuildServers/Jenkins.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ public override string[] GenerateSetParameterMessage(string name, string value)
3434
};
3535
}
3636

37+
public override string GetCurrentBranch()
38+
{
39+
return Environment.GetEnvironmentVariable("GIT_BRANCH");
40+
}
41+
42+
public override bool PreventFetch()
43+
{
44+
return true;
45+
}
46+
3747
public override void WriteIntegration(Action<string> writer, VersionVariables variables)
3848
{
3949
base.WriteIntegration(writer, variables);

src/GitVersionCore/BuildServers/VsoAgent.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public override string GetCurrentBranch()
2424
return Environment.GetEnvironmentVariable("BUILD_SOURCEBRANCH");
2525
}
2626

27+
public override bool PreventFetch()
28+
{
29+
return true;
30+
}
31+
2732
public override string GenerateSetVersionMessage(VersionVariables variables)
2833
{
2934
// For VSO, we'll get the Build Number and insert GitVersion variables where

src/GitVersionCore/ExecuteCore.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ public ExecuteCore(IFileSystem fileSystem)
2222
public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId)
2323
{
2424
// Normalise if we are running on build server
25-
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, noFetch, workingDirectory);
25+
var applicableBuildServers = BuildServerList.GetApplicableBuildServers();
26+
var buildServer = applicableBuildServers.FirstOrDefault();
27+
var fetch = noFetch || (buildServer != null && buildServer.PreventFetch());
28+
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, fetch, workingDirectory);
2629
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
2730
var projectRoot = gitPreparer.GetProjectRootDirectory();
2831
Logger.WriteInfo(string.Format("Project root is: " + projectRoot));
@@ -37,7 +40,7 @@ public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicReposi
3740
var versionVariables = gitVersionCache.LoadVersionVariablesFromDiskCache(repo, dotGitDirectory);
3841
if (versionVariables == null)
3942
{
40-
versionVariables = ExecuteInternal(targetBranch, commitId, repo, gitPreparer, projectRoot);
43+
versionVariables = ExecuteInternal(targetBranch, commitId, repo, gitPreparer, projectRoot, buildServer);
4144
gitVersionCache.WriteVariablesToDiskCache(repo, dotGitDirectory, versionVariables);
4245
}
4346

@@ -73,11 +76,8 @@ static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch
7376
return currentBranch;
7477
}
7578

76-
VersionVariables ExecuteInternal(string targetBranch, string commitId, IRepository repo, GitPreparer gitPreparer, string projectRoot)
79+
VersionVariables ExecuteInternal(string targetBranch, string commitId, IRepository repo, GitPreparer gitPreparer, string projectRoot, IBuildServer buildServer)
7780
{
78-
var applicableBuildServers = BuildServerList.GetApplicableBuildServers();
79-
var buildServer = applicableBuildServers.FirstOrDefault();
80-
8181
gitPreparer.Initialise(buildServer != null, ResolveCurrentBranch(buildServer, targetBranch));
8282

8383
var versionFinder = new GitVersionFinder();

src/GitVersionExe/ArgumentParser.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace GitVersion
66
using System.Linq;
77
using System.Text.RegularExpressions;
88

9-
109
public class ArgumentParser
1110
{
1211
public static Arguments ParseArguments(string commandLineArguments)
@@ -61,23 +60,23 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
6160
arguments.TargetPath = firstArgument;
6261
namedArguments = commandLineArguments.Skip(1).ToList();
6362
}
64-
63+
6564
var args = CollectSwitchesAndValuesFromArguments(namedArguments);
6665

6766
foreach (var name in args.AllKeys)
6867
{
6968
var values = args.GetValues(name);
70-
69+
7170
string value = null;
72-
71+
7372
if (values != null)
7473
{
7574
//Currently, no arguments use more than one value, so having multiple values is an input error.
7675
//In the future, this exception can be removed to support multiple values for a switch.
7776
if (values.Length > 1) throw new WarningException(string.Format("Could not parse command line parameter '{0}'.", values[1]));
78-
77+
7978
value = values.FirstOrDefault();
80-
}
79+
}
8180

8281
if (IsSwitch("l", name))
8382
{
@@ -168,7 +167,7 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
168167
}
169168
else
170169
{
171-
arguments.UpdateAssemblyInfo = true;
170+
arguments.UpdateAssemblyInfo = true;
172171
}
173172
continue;
174173
}
@@ -186,11 +185,11 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
186185
{
187186
versionVariable = VersionVariables.AvailableVariables.SingleOrDefault(av => av.Equals(value.Replace("'", ""), StringComparison.CurrentCultureIgnoreCase));
188187
}
189-
188+
190189
if (versionVariable == null)
191190
{
192191
var messageFormat = "{0} requires a valid version variable. Available variables are:\n{1}";
193-
var message = string.Format(messageFormat, name, String.Join(", ", VersionVariables.AvailableVariables.Select(x=>string.Concat("'", x, "'"))));
192+
var message = string.Format(messageFormat, name, String.Join(", ", VersionVariables.AvailableVariables.Select(x => string.Concat("'", x, "'"))));
194193
throw new WarningException(message);
195194
}
196195

@@ -210,7 +209,7 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
210209
}
211210
else
212211
{
213-
arguments.ShowConfig = true;
212+
arguments.ShowConfig = true;
214213
}
215214
continue;
216215
}
@@ -253,15 +252,15 @@ static NameValueCollection CollectSwitchesAndValuesFromArguments(List<string> na
253252
currentKey = arg;
254253
args.Add(currentKey, null);
255254
}
256-
//If this is a value (not a switch)
255+
//If this is a value (not a switch)
257256
else
258257
{
259258
//And if the current switch does not have a value yet, set it's value to this argument.
260-
if (String.IsNullOrEmpty(args[currentKey]))
259+
if (string.IsNullOrEmpty(args[currentKey]))
261260
{
262261
args[currentKey] = arg;
263262
}
264-
//Otherwise add the value under the same switch.
263+
//Otherwise add the value under the same switch.
265264
else
266265
{
267266
args.Add(currentKey, arg);
@@ -273,7 +272,7 @@ static NameValueCollection CollectSwitchesAndValuesFromArguments(List<string> na
273272

274273
static bool IsSwitchArgument(string value)
275274
{
276-
return value != null && (value.StartsWith("-") || value.StartsWith("/"))
275+
return value != null && (value.StartsWith("-") || value.StartsWith("/"))
277276
&& !Regex.Match(value, @"/\w+:").Success; //Exclude msbuild & project parameters in form /blah:, which should be parsed as values, not switch names.
278277
}
279278

src/GitVersionExe/Arguments.cs

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

3636
public bool ShowConfig;
37-
public bool NoFetch { get; set; }
37+
public bool NoFetch;
3838
}
3939
}

0 commit comments

Comments
 (0)