Skip to content

Commit 57c6802

Browse files
committed
applied code review changes
1 parent 8c4fda5 commit 57c6802

File tree

7 files changed

+66
-63
lines changed

7 files changed

+66
-63
lines changed

src/GitVersionCore.Tests/SemanticVersionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void ValidateVersionParsing(
3737
string versionString, int major, int minor, int patch, string tag, int? tagNumber, int? numberOfBuilds,
3838
string branchName, string sha, string otherMetaData, string fullFormattedVersionString, string tagPrefixRegex)
3939
{
40-
fullFormattedVersionString = fullFormattedVersionString ?? versionString;
40+
fullFormattedVersionString ??= versionString;
4141

4242
SemanticVersion.TryParse(versionString, tagPrefixRegex, out var version).ShouldBe(true, versionString);
4343
Assert.AreEqual(major, version.Major);

src/GitVersionCore/GitVersionContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private static Branch GetTargetBranch(IRepository repository, string targetBranc
182182
b.NameWithoutRemote() == targetBranch);
183183

184184
// Failsafe in case the specified branch is invalid
185-
desiredBranch = desiredBranch ?? repository.Head;
185+
desiredBranch ??= repository.Head;
186186
}
187187
}
188188

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace GitVersion.Helpers
5+
{
6+
public static class EnumerableExtensions
7+
{
8+
public static T OnlyOrDefault<T>(this IEnumerable<T> source)
9+
{
10+
switch (source)
11+
{
12+
case null:
13+
throw new ArgumentNullException(nameof(source));
14+
case IList<T> list when list.Count == 1:
15+
return list[0];
16+
}
17+
18+
using (var e = source.GetEnumerator())
19+
{
20+
if (!e.MoveNext())
21+
return default;
22+
var current = e.Current;
23+
if (!e.MoveNext())
24+
return current;
25+
}
26+
27+
return default;
28+
}
29+
}
30+
}

src/GitVersionCore/Helpers/ExtensionMethods.cs

Lines changed: 0 additions & 57 deletions
This file was deleted.

src/GitVersionCore/Helpers/LibGitExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,22 @@ public static void DumpGraph(string workingDirectory, Action<string> writer = nu
138138
Console.Write(output.ToString());
139139
}
140140
}
141+
142+
public static bool IsBranch(this string branchName, string branchNameToCompareAgainst)
143+
{
144+
// "develop" == "develop"
145+
if (String.Equals(branchName, branchNameToCompareAgainst, StringComparison.OrdinalIgnoreCase))
146+
{
147+
return true;
148+
}
149+
150+
// "refs/head/develop" == "develop"
151+
if (branchName.EndsWith($"/{branchNameToCompareAgainst}", StringComparison.OrdinalIgnoreCase))
152+
{
153+
return true;
154+
}
155+
156+
return false;
157+
}
141158
}
142159
}

src/GitVersionExe/ExtensionMethods.cs renamed to src/GitVersionCore/Helpers/StringExtensions.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
using System;
22
using System.IO;
33
using System.Linq;
4+
using System.Text;
45
using System.Text.RegularExpressions;
56

6-
namespace GitVersion
7+
namespace GitVersion.Helpers
78
{
8-
public static class ExtensionMethods
9+
public static class StringExtensions
910
{
1011
private static readonly string[] trues;
1112
private static readonly string[] falses;
1213

1314

14-
static ExtensionMethods()
15+
static StringExtensions()
1516
{
1617
trues = new[]
1718
{
@@ -115,5 +116,16 @@ public static bool ArgumentRequiresValue(this string argument, int argumentIndex
115116

116117
return argumentMightRequireValue;
117118
}
119+
120+
public static void AppendLineFormat(this StringBuilder stringBuilder, string format, params object[] args)
121+
{
122+
stringBuilder.AppendFormat(format, args);
123+
stringBuilder.AppendLine();
124+
}
125+
126+
public static string RegexReplace(this string input, string pattern, string replace, RegexOptions options = RegexOptions.None)
127+
{
128+
return Regex.Replace(input, pattern, replace, options);
129+
}
118130
}
119131
}

src/GitVersionExe/ArgumentParser.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using GitVersion.Configuration;
77
using GitVersion.Exceptions;
8+
using GitVersion.Helpers;
89
using GitVersion.Logging;
910
using GitVersion.OutputVariables;
1011
using GitVersion.OutputFormatters;
@@ -416,7 +417,7 @@ private static NameValueCollection CollectSwitchesAndValuesFromArguments(IList<s
416417
string currentKey = null;
417418
var argumentRequiresValue = false;
418419

419-
for (var i = 0; i < namedArguments.Count; i = i + 1)
420+
for (var i = 0; i < namedArguments.Count; i += 1)
420421
{
421422
var arg = namedArguments[i];
422423

0 commit comments

Comments
 (0)