-
Notifications
You must be signed in to change notification settings - Fork 654
/
Copy pathConfigurationExtensions.cs
151 lines (127 loc) · 6.09 KB
/
ConfigurationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Text.RegularExpressions;
using GitVersion.Extensions;
namespace GitVersion.Configuration;
public static class ConfigurationExtensions
{
public static EffectiveConfiguration GetEffectiveConfiguration(this IGitVersionConfiguration configuration, IBranch branch)
=> GetEffectiveConfiguration(configuration, branch.NotNull().Name);
public static EffectiveConfiguration GetEffectiveConfiguration(this IGitVersionConfiguration configuration, ReferenceName branchName)
{
IBranchConfiguration branchConfiguration = configuration.GetBranchConfiguration(branchName);
return new EffectiveConfiguration(configuration, branchConfiguration);
}
public static IBranchConfiguration GetBranchConfiguration(this IGitVersionConfiguration configuration, IBranch branch)
=> GetBranchConfiguration(configuration, branch.NotNull().Name);
public static IBranchConfiguration GetBranchConfiguration(this IGitVersionConfiguration configuration, ReferenceName branchName)
{
var branchConfiguration = GetBranchConfigurations(configuration, branchName.WithoutOrigin).FirstOrDefault();
branchConfiguration ??= new BranchConfiguration
{
RegularExpression = string.Empty,
Label = ConfigurationConstants.BranchNamePlaceholder,
Increment = IncrementStrategy.Inherit
};
return branchConfiguration;
}
private static IEnumerable<IBranchConfiguration> GetBranchConfigurations(IGitVersionConfiguration configuration, string branchName)
{
IBranchConfiguration? unknownBranchConfiguration = null;
foreach ((string key, IBranchConfiguration branchConfiguration) in configuration.Branches)
{
if (branchConfiguration.IsMatch(branchName))
{
if (key == "unknown")
{
unknownBranchConfiguration = branchConfiguration;
}
else
{
yield return branchConfiguration;
}
}
}
if (unknownBranchConfiguration != null) yield return unknownBranchConfiguration;
}
public static IBranchConfiguration GetFallbackBranchConfiguration(this IGitVersionConfiguration configuration) => configuration;
public static bool IsReleaseBranch(this IGitVersionConfiguration configuration, IBranch branch)
=> IsReleaseBranch(configuration, branch.NotNull().Name);
public static bool IsReleaseBranch(this IGitVersionConfiguration configuration, ReferenceName branchName)
=> configuration.GetBranchConfiguration(branchName).IsReleaseBranch ?? false;
public static string? GetBranchSpecificLabel(
this EffectiveConfiguration configuration, ReferenceName branchName, string? branchNameOverride)
=> GetBranchSpecificLabel(configuration, branchName.WithoutOrigin, branchNameOverride);
public static string? GetBranchSpecificLabel(
this EffectiveConfiguration configuration, string? branchName, string? branchNameOverride)
{
configuration.NotNull();
var label = configuration.Label;
if (label is null)
{
return label;
}
var effectiveBranchName = branchNameOverride ?? branchName;
if (!configuration.RegularExpression.IsNullOrWhiteSpace() && !effectiveBranchName.IsNullOrEmpty())
{
effectiveBranchName = effectiveBranchName.RegexReplace("[^a-zA-Z0-9-_]", "-");
var pattern = new Regex(configuration.RegularExpression, RegexOptions.IgnoreCase);
var match = pattern.Match(effectiveBranchName);
if (match.Success)
{
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var groupName in pattern.GetGroupNames())
{
label = label.Replace("{" + groupName + "}", match.Groups[groupName].Value);
}
}
}
// Evaluate tag number pattern and append to prerelease tag, preserving build metadata
if (!configuration.LabelNumberPattern.IsNullOrEmpty() && !effectiveBranchName.IsNullOrEmpty())
{
var match = Regex.Match(effectiveBranchName, configuration.LabelNumberPattern);
var numberGroup = match.Groups["number"];
if (numberGroup.Success)
{
label += numberGroup.Value;
}
}
return label;
}
public static (string GitDirectory, string WorkingTreeDirectory)? FindGitDir(this string path)
{
string? startingDir = path;
while (startingDir is not null)
{
var dirOrFilePath = Path.Combine(startingDir, ".git");
if (Directory.Exists(dirOrFilePath))
{
return (dirOrFilePath, Path.GetDirectoryName(dirOrFilePath)!);
}
if (File.Exists(dirOrFilePath))
{
string? relativeGitDirPath = ReadGitDirFromFile(dirOrFilePath);
if (!string.IsNullOrWhiteSpace(relativeGitDirPath))
{
var fullGitDirPath = Path.GetFullPath(Path.Combine(startingDir, relativeGitDirPath));
if (Directory.Exists(fullGitDirPath))
{
return (fullGitDirPath, Path.GetDirectoryName(dirOrFilePath)!);
}
}
}
startingDir = Path.GetDirectoryName(startingDir);
}
return null;
}
private static string? ReadGitDirFromFile(string fileName)
{
const string expectedPrefix = "gitdir: ";
var firstLineOfFile = File.ReadLines(fileName).FirstOrDefault();
if (firstLineOfFile?.StartsWith(expectedPrefix) ?? false)
{
return firstLineOfFile[expectedPrefix.Length..]; // strip off the prefix, leaving just the path
}
return null;
}
public static List<KeyValuePair<string, IBranchConfiguration>> GetReleaseBranchConfiguration(this IGitVersionConfiguration configuration) =>
configuration.Branches.Where(b => b.Value.IsReleaseBranch == true).ToList();
}