Skip to content

Commit 7f8e50e

Browse files
committed
wwip
1 parent 198fbf9 commit 7f8e50e

File tree

26 files changed

+214
-341
lines changed

26 files changed

+214
-341
lines changed

src/GitVersion.App/ArgumentParser.cs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -177,25 +177,23 @@ private void ParseTargetPath(Arguments arguments, string? name, IReadOnlyList<st
177177
var couldNotParseMessage = $"Could not parse command line parameter '{name}'.";
178178

179179
// If we've reached through all argument switches without a match, we can relatively safely assume that the first argument isn't a switch, but the target path.
180-
if (parseEnded)
180+
if (!parseEnded) throw new WarningException(couldNotParseMessage);
181+
if (name?.StartsWith('/') == true)
181182
{
182-
if (name?.StartsWith('/') == true)
183-
{
184-
if (FileSystemHelper.Path.DirectorySeparatorChar == '/' && name.IsValidPath())
185-
{
186-
arguments.TargetPath = name;
187-
return;
188-
}
189-
}
190-
else if (!name.IsSwitchArgument())
183+
if (FileSystemHelper.Path.DirectorySeparatorChar == '/' && name.IsValidPath())
191184
{
192185
arguments.TargetPath = name;
193186
return;
194187
}
195-
196-
couldNotParseMessage += " If it is the target path, make sure it exists.";
188+
}
189+
else if (!name.IsSwitchArgument())
190+
{
191+
arguments.TargetPath = name;
192+
return;
197193
}
198194

195+
couldNotParseMessage += " If it is the target path, make sure it exists.";
196+
199197
throw new WarningException(couldNotParseMessage);
200198
}
201199

@@ -289,13 +287,9 @@ private static bool ParseSwitches(Arguments arguments, string? name, IReadOnlyLi
289287
return true;
290288
}
291289

292-
if (name.IsSwitch("updatewixversionfile"))
293-
{
294-
arguments.UpdateWixVersionFile = true;
295-
return true;
296-
}
297-
298-
return false;
290+
if (!name.IsSwitch("updatewixversionfile")) return false;
291+
arguments.UpdateWixVersionFile = true;
292+
return true;
299293
}
300294

301295
private static bool ParseConfigArguments(Arguments arguments, string? name, IReadOnlyList<string>? values, string? value)
@@ -357,14 +351,10 @@ private static bool ParseRemoteArguments(Arguments arguments, string? name, IRea
357351
return true;
358352
}
359353

360-
if (name.IsSwitch("b"))
361-
{
362-
EnsureArgumentValueCount(values);
363-
arguments.TargetBranch = value;
364-
return true;
365-
}
366-
367-
return false;
354+
if (!name.IsSwitch("b")) return false;
355+
EnsureArgumentValueCount(values);
356+
arguments.TargetBranch = value;
357+
return true;
368358
}
369359

370360
private static void ParseShowVariable(Arguments arguments, string? value, string? name)

src/GitVersion.App/FileSystemGlobbing/DirectoryInfoGlobbingWrapper.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,28 @@ this.directoryInfo.Parent is null
3636

3737
public override IEnumerable<Microsoft.Extensions.FileSystemGlobbing.Abstractions.FileSystemInfoBase> EnumerateFileSystemInfos()
3838
{
39-
if (this.directoryInfo.Exists)
39+
if (!this.directoryInfo.Exists) yield break;
40+
IEnumerable<IFileSystemInfo> fileSystemInfos;
41+
try
4042
{
41-
IEnumerable<IFileSystemInfo> fileSystemInfos;
42-
try
43-
{
44-
fileSystemInfos = this.directoryInfo.EnumerateFileSystemInfos(
45-
"*",
46-
SearchOption.TopDirectoryOnly
47-
);
48-
}
49-
catch (DirectoryNotFoundException)
50-
{
51-
yield break;
52-
}
43+
fileSystemInfos = this.directoryInfo.EnumerateFileSystemInfos(
44+
"*",
45+
SearchOption.TopDirectoryOnly
46+
);
47+
}
48+
catch (DirectoryNotFoundException)
49+
{
50+
yield break;
51+
}
5352

54-
foreach (var fileSystemInfo in fileSystemInfos)
53+
foreach (var fileSystemInfo in fileSystemInfos)
54+
{
55+
yield return fileSystemInfo switch
5556
{
56-
yield return fileSystemInfo switch
57-
{
58-
IDirectoryInfo info => new DirectoryInfoGlobbingWrapper(this.fileSystem, info),
59-
IFileInfo info => new FileInfoGlobbingWrapper(this.fileSystem, info),
60-
_ => new FileSystemInfoGlobbingWrapper(this.fileSystem, fileSystemInfo)
61-
};
62-
}
57+
IDirectoryInfo info => new DirectoryInfoGlobbingWrapper(this.fileSystem, info),
58+
IFileInfo info => new FileInfoGlobbingWrapper(this.fileSystem, info),
59+
_ => new FileSystemInfoGlobbingWrapper(this.fileSystem, fileSystemInfo)
60+
};
6361
}
6462
}
6563

src/GitVersion.BuildAgents/Agents/AppVeyor.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ public override string[] SetOutputVariables(string name, string? value)
6767
public override string? GetCurrentBranch(bool usingDynamicRepos)
6868
{
6969
var pullRequestBranchName = Environment.GetEnvironmentVariable("APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH");
70-
if (!pullRequestBranchName.IsNullOrWhiteSpace())
71-
{
72-
return pullRequestBranchName;
73-
}
74-
return Environment.GetEnvironmentVariable("APPVEYOR_REPO_BRANCH");
70+
return !pullRequestBranchName.IsNullOrWhiteSpace()
71+
? pullRequestBranchName
72+
: this.Environment.GetEnvironmentVariable("APPVEYOR_REPO_BRANCH");
7573
}
7674

7775
public override bool PreventFetch() => false;

src/GitVersion.BuildAgents/Agents/AzurePipelines.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@ public override string[] SetOutputVariables(string name, string? value) =>
2828
// https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml
2929
// BUILD_SOURCEBRANCH must be used only for "real" branches, not for tags.
3030
// Azure Pipelines sets BUILD_SOURCEBRANCH to refs/tags/<tag> when the pipeline is triggered for a tag.
31-
if (sourceBranch?.StartsWith("refs/tags", StringComparison.OrdinalIgnoreCase) == true)
32-
{
33-
return null;
34-
}
35-
36-
return sourceBranch;
31+
return sourceBranch?.StartsWith("refs/tags", StringComparison.OrdinalIgnoreCase) == true ? null : sourceBranch;
3732
}
3833

3934
public override bool PreventFetch() => true;
@@ -49,16 +44,12 @@ public override string SetBuildNumber(GitVersionVariables variables)
4944
var newBuildNumber = variables.OrderBy(x => x.Key).Aggregate(buildNumberEnv, ReplaceVariables);
5045

5146
// If no variable substitution has happened, use FullSemVer
52-
if (buildNumberEnv == newBuildNumber)
53-
{
54-
var buildNumber = variables.FullSemVer.EndsWith("+0")
55-
? variables.FullSemVer[..^2]
56-
: variables.FullSemVer;
57-
58-
return $"##vso[build.updatebuildnumber]{buildNumber}";
59-
}
47+
if (buildNumberEnv != newBuildNumber) return $"##vso[build.updatebuildnumber]{newBuildNumber}";
48+
var buildNumber = variables.FullSemVer.EndsWith("+0")
49+
? variables.FullSemVer[..^2]
50+
: variables.FullSemVer;
6051

61-
return $"##vso[build.updatebuildnumber]{newBuildNumber}";
52+
return $"##vso[build.updatebuildnumber]{buildNumber}";
6253
}
6354

6455
private static string ReplaceVariables(string buildNumberEnv, KeyValuePair<string, string?> variable)

src/GitVersion.BuildAgents/Agents/BitBucketPipelines.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ public override void WriteIntegration(Action<string?> writer, GitVersionVariable
6767
public override string? GetCurrentBranch(bool usingDynamicRepos)
6868
{
6969
var branchName = EvaluateEnvironmentVariable(BranchEnvironmentVariableName);
70-
if (branchName?.StartsWith("refs/heads/") == true)
71-
{
72-
return branchName;
73-
}
74-
75-
return null;
70+
return branchName?.StartsWith("refs/heads/") == true ? branchName : null;
7671
}
7772

7873
private string? EvaluateEnvironmentVariable(string variableName)

src/GitVersion.BuildAgents/Agents/Drone.cs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,19 @@ public override string[] SetOutputVariables(string name, string? value) =>
2424
// https://discourse.drone.io/t/getting-the-branch-a-pull-request-is-created-from/670
2525
// Unfortunately, DRONE_REFSPEC isn't populated, however CI_COMMIT_REFSPEC can be used instead of.
2626
var pullRequestNumber = Environment.GetEnvironmentVariable("DRONE_PULL_REQUEST");
27-
if (!pullRequestNumber.IsNullOrWhiteSpace())
28-
{
29-
// DRONE_SOURCE_BRANCH is available in Drone 1.x.x version
30-
var sourceBranch = Environment.GetEnvironmentVariable("DRONE_SOURCE_BRANCH");
31-
if (!sourceBranch.IsNullOrWhiteSpace())
32-
return sourceBranch;
33-
34-
// In drone lower than 1.x.x source branch can be parsed from CI_COMMIT_REFSPEC
35-
// CI_COMMIT_REFSPEC - {sourceBranch}:{destinationBranch}
36-
// https://github.com/drone/drone/issues/2222
37-
var ciCommitRefSpec = Environment.GetEnvironmentVariable("CI_COMMIT_REFSPEC");
38-
if (!ciCommitRefSpec.IsNullOrWhiteSpace())
39-
{
40-
var colonIndex = ciCommitRefSpec.IndexOf(':');
41-
if (colonIndex > 0)
42-
{
43-
return ciCommitRefSpec.Substring(0, colonIndex);
44-
}
45-
}
46-
}
47-
48-
return Environment.GetEnvironmentVariable("DRONE_BRANCH");
27+
if (pullRequestNumber.IsNullOrWhiteSpace()) return this.Environment.GetEnvironmentVariable("DRONE_BRANCH");
28+
// DRONE_SOURCE_BRANCH is available in Drone 1.x.x version
29+
var sourceBranch = this.Environment.GetEnvironmentVariable("DRONE_SOURCE_BRANCH");
30+
if (!sourceBranch.IsNullOrWhiteSpace())
31+
return sourceBranch;
32+
33+
// In drone lower than 1.x.x source branch can be parsed from CI_COMMIT_REFSPEC
34+
// CI_COMMIT_REFSPEC - {sourceBranch}:{destinationBranch}
35+
// https://github.com/drone/drone/issues/2222
36+
var ciCommitRefSpec = this.Environment.GetEnvironmentVariable("CI_COMMIT_REFSPEC");
37+
if (ciCommitRefSpec.IsNullOrWhiteSpace()) return this.Environment.GetEnvironmentVariable("DRONE_BRANCH");
38+
var colonIndex = ciCommitRefSpec.IndexOf(':');
39+
return colonIndex > 0 ? ciCommitRefSpec[..colonIndex] : this.Environment.GetEnvironmentVariable("DRONE_BRANCH");
4940
}
5041

5142
public override bool PreventFetch() => false;

src/GitVersion.BuildAgents/Agents/EnvRun.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,9 @@ internal class EnvRun(IEnvironment environment, ILog log, IFileSystem fileSystem
1212
public override bool CanApplyToCurrentContext()
1313
{
1414
var envRunDatabasePath = Environment.GetEnvironmentVariable(EnvironmentVariableName);
15-
if (!envRunDatabasePath.IsNullOrEmpty())
16-
{
17-
if (!this.FileSystem.File.Exists(envRunDatabasePath))
18-
{
19-
this.Log.Error($"The database file of EnvRun.exe was not found at {envRunDatabasePath}.");
20-
return false;
21-
}
22-
23-
return true;
24-
}
15+
if (envRunDatabasePath.IsNullOrEmpty()) return false;
16+
if (this.FileSystem.File.Exists(envRunDatabasePath)) return true;
17+
this.Log.Error($"The database file of EnvRun.exe was not found at {envRunDatabasePath}.");
2518

2619
return false;
2720
}

src/GitVersion.BuildAgents/Agents/GitLabCi.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,14 @@ public override string[] SetOutputVariables(string name, string? value) =>
2222
$"GitVersion_{name}={value}"
2323
];
2424

25-
public override string? GetCurrentBranch(bool usingDynamicRepos)
26-
{
27-
// CI_COMMIT_REF_NAME can contain either the branch or the tag
28-
// See https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
29-
30-
// CI_COMMIT_TAG is only available in tag pipelines,
31-
// so we can exit if CI_COMMIT_REF_NAME would return the tag
32-
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CI_COMMIT_TAG")))
33-
return null;
34-
35-
return Environment.GetEnvironmentVariable("CI_COMMIT_REF_NAME");
36-
}
25+
// CI_COMMIT_REF_NAME can contain either the branch or the tag
26+
// See https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
27+
// CI_COMMIT_TAG is only available in tag pipelines,
28+
// so we can exit if CI_COMMIT_REF_NAME would return the tag
29+
public override string? GetCurrentBranch(bool usingDynamicRepos) =>
30+
string.IsNullOrEmpty(this.Environment.GetEnvironmentVariable("CI_COMMIT_TAG"))
31+
? this.Environment.GetEnvironmentVariable("CI_COMMIT_REF_NAME")
32+
: null;
3733

3834
public override bool PreventFetch() => true;
3935

src/GitVersion.BuildAgents/Agents/TeamCity.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@ internal class TeamCity(IEnvironment environment, ILog log, IFileSystem fileSyst
1616
{
1717
var branchName = Environment.GetEnvironmentVariable("Git_Branch");
1818

19-
if (branchName.IsNullOrEmpty())
19+
if (!branchName.IsNullOrEmpty()) return branchName;
20+
if (!usingDynamicRepos)
2021
{
21-
if (!usingDynamicRepos)
22-
{
23-
WriteBranchEnvVariableWarning();
24-
}
25-
26-
return base.GetCurrentBranch(usingDynamicRepos);
22+
WriteBranchEnvVariableWarning();
2723
}
2824

29-
return branchName;
25+
return base.GetCurrentBranch(usingDynamicRepos);
3026
}
3127

3228
private void WriteBranchEnvVariableWarning() => this.Log.Warning(@"TeamCity doesn't make the current branch available through environmental variables.

src/GitVersion.Configuration/ConfigurationFileLocator.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,24 @@ public void Verify(string? workingDirectory, string? projectRootDirectory)
5757
{
5858
this.log.Debug($"Trying to find configuration file {fileName} at '{directoryPath}'");
5959
var matchingFile = files.FirstOrDefault(file => string.Equals(FileSystemHelper.Path.GetFileName(file), fileName, StringComparison.OrdinalIgnoreCase));
60-
if (matchingFile != null)
61-
{
62-
this.log.Info($"Found configuration file at '{matchingFile}'");
63-
return matchingFile;
64-
}
60+
if (matchingFile == null) continue;
61+
this.log.Info($"Found configuration file at '{matchingFile}'");
62+
return matchingFile;
6563
}
6664

6765
return null;
6866
}
6967

7068
private string? GetCustomConfigurationFilePathIfEligable(string? directoryPath)
7169
{
72-
if (!string.IsNullOrWhiteSpace(this.ConfigurationFile))
70+
if (string.IsNullOrWhiteSpace(this.ConfigurationFile)) return null;
71+
var configurationFilePath = this.ConfigurationFile;
72+
if (!string.IsNullOrWhiteSpace(directoryPath))
7373
{
74-
var configurationFilePath = this.ConfigurationFile;
75-
if (!string.IsNullOrWhiteSpace(directoryPath))
76-
{
77-
configurationFilePath = FileSystemHelper.Path.Combine(directoryPath, this.ConfigurationFile);
78-
}
79-
80-
if (fileSystem.File.Exists(configurationFilePath))
81-
{
82-
return configurationFilePath;
83-
}
74+
configurationFilePath = FileSystemHelper.Path.Combine(directoryPath, this.ConfigurationFile);
8475
}
8576

86-
return null;
77+
return this.fileSystem.File.Exists(configurationFilePath) ? configurationFilePath : null;
8778
}
8879

8980
private void WarnAboutAmbiguousConfigFileSelection(string? workingDirectory, string? projectRootDirectory)

0 commit comments

Comments
 (0)