Skip to content

Commit ff805e3

Browse files
sergiorykovJakeGinnivan
authored andcommitted
* ConfigurationProvider.cs clean code
1 parent e9662c6 commit ff805e3

File tree

7 files changed

+106
-60
lines changed

7 files changed

+106
-60
lines changed

src/GitVersionCore/Configuration/ConfigurationProvider.cs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
namespace GitVersion
22
{
3+
using GitVersion.Configuration.Init.Wizard;
4+
using GitVersion.Helpers;
35
using System.Collections.Generic;
46
using System.ComponentModel;
57
using System.IO;
68
using System.Linq;
79
using System.Text;
8-
using GitVersion.Configuration.Init.Wizard;
9-
using GitVersion.Helpers;
1010

1111
public class ConfigurationProvider
1212
{
@@ -103,10 +103,27 @@ static void MigrateBranches(Config config)
103103
// Map of current names and previous names
104104
var dict = new Dictionary<string, string[]>
105105
{
106-
{ "hotfix(es)?[/-]", new [] { "hotfix[/-]" }},
107-
{ "features?[/-]", new [] { "feature[/-]", "feature(s)?[/-]" }},
108-
{ "releases?[/-]", new [] { "release[/-]" }},
109-
{ "dev(elop)?(ment)?$", new [] { "develop" }}
106+
{"hotfix(es)?[/-]", new[]
107+
{
108+
"hotfix[/-]"
109+
}
110+
},
111+
{"features?[/-]", new[]
112+
{
113+
"feature[/-]",
114+
"feature(s)?[/-]"
115+
}
116+
},
117+
{"releases?[/-]", new[]
118+
{
119+
"release[/-]"
120+
}
121+
},
122+
{"dev(elop)?(ment)?$", new[]
123+
{
124+
"develop"
125+
}
126+
}
110127
};
111128

112129
foreach (var mapping in dict)
@@ -186,12 +203,12 @@ public static void Verify(GitPreparer gitPreparer, IFileSystem fileSystem)
186203
var workingDirectory = gitPreparer.WorkingDirectory;
187204
var projectRootDirectory = gitPreparer.GetProjectRootDirectory();
188205

189-
Verify(workingDirectory,projectRootDirectory, fileSystem);
206+
Verify(workingDirectory, projectRootDirectory, fileSystem);
190207
}
191208

192209
public static void Verify(string workingDirectory, string projectRootDirectory, IFileSystem fileSystem)
193210
{
194-
if(fileSystem.PathsEqual(workingDirectory, projectRootDirectory))
211+
if (fileSystem.PathsEqual(workingDirectory, projectRootDirectory))
195212
{
196213
WarnAboutObsoleteConfigFile(workingDirectory, fileSystem);
197214
return;
@@ -200,6 +217,11 @@ public static void Verify(string workingDirectory, string projectRootDirectory,
200217
WarnAboutObsoleteConfigFile(workingDirectory, fileSystem);
201218
WarnAboutObsoleteConfigFile(projectRootDirectory, fileSystem);
202219

220+
WarnAboutAmbigousConfigFileSelection(workingDirectory, projectRootDirectory, fileSystem);
221+
}
222+
223+
private static void WarnAboutAmbigousConfigFileSelection(string workingDirectory, string projectRootDirectory, IFileSystem fileSystem)
224+
{
203225
var workingConfigFile = GetConfigFilePath(workingDirectory, fileSystem);
204226
var projectRootConfigFile = GetConfigFilePath(projectRootDirectory, fileSystem);
205227

@@ -280,4 +302,4 @@ public static void Init(string workingDirectory, IFileSystem fileSystem, IConsol
280302
}
281303
}
282304
}
283-
}
305+
}

src/GitVersionCore/ExecuteCore.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
namespace GitVersion
22
{
3+
using GitVersion.Helpers;
4+
using LibGit2Sharp;
35
using System;
46
using System.ComponentModel;
57
using System.IO;
68
using System.Linq;
7-
using GitVersion.Helpers;
8-
9-
using LibGit2Sharp;
109

1110
public class ExecuteCore
1211
{
@@ -16,7 +15,7 @@ public class ExecuteCore
1615
public ExecuteCore(IFileSystem fileSystem)
1716
{
1817
if (fileSystem == null) throw new ArgumentNullException("fileSystem");
19-
18+
2019
this.fileSystem = fileSystem;
2120
gitVersionCache = new GitVersionCache(fileSystem);
2221
}
@@ -52,10 +51,16 @@ public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicReposi
5251
throw new Exception(string.Format("Failed to prepare or find the .git directory in path '{0}'.", workingDirectory));
5352
}
5453

54+
if (overrideConfig != null)
55+
{
56+
var overridenVersionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, buildServer, overrideConfig: overrideConfig);
57+
return overridenVersionVariables;
58+
}
59+
5560
var versionVariables = gitVersionCache.LoadVersionVariablesFromDiskCache(gitPreparer);
5661
if (versionVariables == null)
5762
{
58-
versionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, buildServer, overrideConfig: overrideConfig);
63+
versionVariables = ExecuteInternal(targetBranch, commitId, gitPreparer, buildServer);
5964
try
6065
{
6166
gitVersionCache.WriteVariablesToDiskCache(repo, dotGitDirectory, versionVariables);
@@ -100,7 +105,7 @@ static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch
100105
VersionVariables ExecuteInternal(string targetBranch, string commitId, GitPreparer gitPreparer, IBuildServer buildServer, Config overrideConfig = null)
101106
{
102107
var versionFinder = new GitVersionFinder();
103-
var configuration = ConfigurationProvider.Provide(gitPreparer, fileSystem, overrideConfig: overrideConfig);
108+
var configuration = ConfigurationProvider.Provide(gitPreparer, fileSystem, overrideConfig: overrideConfig);
104109

105110
return gitPreparer.WithRepository(repo =>
106111
{

src/GitVersionCore/GitVersionCache.cs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
namespace GitVersion
22
{
3+
using GitVersion.Helpers;
34
using System;
45
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq;
78
using System.Security.Cryptography;
89
using System.Text;
9-
using GitVersion.Helpers;
10-
using LibGit2Sharp;
1110
using YamlDotNet.Serialization;
1211

1312
public class GitVersionCache
@@ -53,7 +52,7 @@ public void WriteVariablesToDiskCache(GitPreparer gitPreparer, VersionVariables
5352
private string PrepareCacheDirectory(GitPreparer gitPreparer)
5453
{
5554
var gitDir = gitPreparer.GetDotGitDirectory();
56-
55+
5756
// If the cacheDir already exists, CreateDirectory just won't do anything (it won't fail). @asbjornu
5857
var cacheDir = GetCacheDir(gitDir);
5958
fileSystem.CreateDirectory(cacheDir);
@@ -68,51 +67,52 @@ public VersionVariables LoadVersionVariablesFromDiskCache(GitPreparer gitPrepare
6867
var cacheDir = PrepareCacheDirectory(gitPreparer);
6968

7069
var cacheFileName = GetCacheFileName(GetKey(gitPreparer), cacheDir);
71-
VersionVariables vv = null;
72-
if (fileSystem.Exists(cacheFileName))
70+
if (!fileSystem.Exists(cacheFileName))
71+
{
72+
Logger.WriteInfo("Cache file " + cacheFileName + " not found.");
73+
return null;
74+
}
75+
76+
using (Logger.IndentLog("Deserializing version variables from cache file " + cacheFileName))
7377
{
74-
using (Logger.IndentLog("Deserializing version variables from cache file " + cacheFileName))
78+
try
7579
{
80+
var loadedVariables = VersionVariables.FromFile(cacheFileName, fileSystem);
81+
return loadedVariables;
82+
}
83+
catch (Exception ex)
84+
{
85+
Logger.WriteWarning("Unable to read cache file " + cacheFileName + ", deleting it.");
86+
Logger.WriteInfo(ex.ToString());
7687
try
7788
{
78-
vv = VersionVariables.FromFile(cacheFileName, fileSystem);
89+
fileSystem.Delete(cacheFileName);
7990
}
80-
catch (Exception ex)
91+
catch (Exception deleteEx)
8192
{
82-
Logger.WriteWarning("Unable to read cache file " + cacheFileName + ", deleting it.");
83-
Logger.WriteInfo(ex.ToString());
84-
try
85-
{
86-
fileSystem.Delete(cacheFileName);
87-
}
88-
catch (Exception deleteEx)
89-
{
90-
Logger.WriteWarning(string.Format("Unable to delete corrupted version cache file {0}. Got {1} exception.", cacheFileName, deleteEx.GetType().FullName));
91-
}
93+
Logger.WriteWarning(string.Format("Unable to delete corrupted version cache file {0}. Got {1} exception.", cacheFileName, deleteEx.GetType().FullName));
9294
}
95+
96+
return null;
9397
}
9498
}
95-
else
96-
{
97-
Logger.WriteInfo("Cache file " + cacheFileName + " not found.");
98-
}
99-
100-
return vv;
10199
}
102100
}
103101

104102
string GetKey(GitPreparer gitPreparer)
105103
{
106-
var gitDir = gitPreparer.GetDotGitDirectory();
104+
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
107105

108106
// Maybe using timestamp in .git/refs directory is enough?
109-
var ticks = fileSystem.GetLastDirectoryWrite(Path.Combine(gitDir, "refs"));
107+
var lastGitRefsChangedTicks = fileSystem.GetLastDirectoryWrite(Path.Combine(dotGitDirectory, "refs"));
110108

111-
var configPath = ConfigurationProvider.SelectConfigFilePath(gitPreparer, fileSystem);
112-
var configText = fileSystem.Exists(configPath) ? fileSystem.ReadAllText(configPath) : null;
113-
var configHash = configText != null ? GetHash(configText) : null;
109+
// will return the same hash even when config file will be moved
110+
// from workingDirectory to rootProjectDirectory. It's OK. Config essentially is the same.
111+
var configFilePath = ConfigurationProvider.SelectConfigFilePath(gitPreparer, fileSystem);
112+
var configFileContent = fileSystem.Exists(configFilePath) ? fileSystem.ReadAllText(configFilePath) : null;
113+
var configFileHash = configFileContent != null ? GetHash(configFileContent) : null;
114114

115-
return gitPreparer.WithRepository(repo => string.Join(":", gitDir, repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks, configHash));
115+
return gitPreparer.WithRepository(repo => string.Join(":", dotGitDirectory, repo.Head.CanonicalName, repo.Head.Tip.Sha, lastGitRefsChangedTicks, configFileHash));
116116
}
117117

118118
static string GetCacheFileName(string key, string cacheDir)
@@ -137,4 +137,4 @@ static string GetHash(string textToHash)
137137
}
138138
}
139139
}
140-
}
140+
}

src/GitVersionExe/ArgumentParser.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,25 +263,43 @@ public static Arguments ParseArguments(List<string> commandLineArguments)
263263

264264
if (arguments.UpdateAssemblyInfoFileName.Count > 1 && arguments.EnsureAssemblyInfo)
265265
{
266-
throw new WarningException("Can't specify multiple assembly info files when using -ensureassemblyinfo switch, either use a single assembly info file or do not specify -ensureassemblyinfo and create assembly info files manually");
266+
throw new WarningException("Can't specify multiple assembly info files when using /ensureassemblyinfo switch, either use a single assembly info file or do not specify /ensureassemblyinfo and create assembly info files manually");
267267
}
268268
continue;
269269
}
270270

271271
if (name.IsSwitch("overrideconfig"))
272272
{
273-
foreach (var item in value.Split(';'))
273+
var keyValueOptions = value.Split(';');
274+
if (keyValueOptions.Length == 0)
275+
{
276+
continue;
277+
}
278+
279+
arguments.HasOverrideConfig = true;
280+
281+
if (keyValueOptions.Length > 1)
274282
{
275-
var configOverride = item.Split('=');
283+
throw new WarningException("Can't specify multiple /overrideconfig options: currently supported only 'tag-prefix' option");
284+
}
285+
286+
// key=value
287+
foreach (var keyValueOption in keyValueOptions)
288+
{
289+
var keyAndValue = keyValueOption.Split('=');
290+
if (keyAndValue.Length > 1)
291+
{
292+
throw new WarningException(string.Format("Could not parse /overrideconfig option: {0}. Ensure it is in format 'key=value'", keyValueOption));
293+
}
276294

277-
switch (configOverride[0])
295+
var optionKey = keyAndValue[0].ToLowerInvariant();
296+
switch (optionKey)
278297
{
279298
case "tag-prefix":
280-
if (1 < configOverride.Length)
281-
{
282-
arguments.OverrideConfig.TagPrefix = configOverride[1];
283-
}
299+
arguments.OverrideConfig.TagPrefix = keyAndValue[1];
284300
break;
301+
default:
302+
throw new WarningException(string.Format("Could not parse /overrideconfig option: {0}. Currently supported only 'tag-prefix' option", optionKey));
285303
}
286304
}
287305

src/GitVersionExe/Arguments.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public Arguments()
1515
public Authentication Authentication;
1616

1717
public Config OverrideConfig;
18+
public bool HasOverrideConfig { get; set; }
1819

1920
public string TargetPath;
2021

@@ -30,7 +31,7 @@ public Arguments()
3031
public string ShowVariable;
3132

3233
public OutputType Output;
33-
34+
3435
public string Proj;
3536
public string ProjArgs;
3637
public string Exec;

src/GitVersionExe/HelpWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ path The directory containing .git. If not defined current directory
2424
/showvariable Used in conjuntion with /output json, will output just a particular variable.
2525
eg /output json /showvariable SemVer - will output `1.2.3+beta.4`
2626
/l Path to logfile.
27-
/showconfig Outputs the effective GitVersion config (defaults + custom from GitVersion.yaml) in yaml format
27+
/showconfig Outputs the effective GitVersion config (defaults + custom from GitVersion.yml) in yaml format
2828
/overrideconfig Overrides GitVersion config values inline (semicolon-separated key value pairs e.g. /overrideconfig:tag-prefix=Foo)
2929
Currently supported config overrides: tag-prefix
3030

src/GitVersionExe/SpecifiedArgumentRunner.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
namespace GitVersion
22
{
3+
using GitTools;
4+
using GitVersion.Helpers;
35
using System;
46
using System.Collections.Generic;
57
using System.Linq;
6-
using GitTools;
7-
using GitVersion.Helpers;
88
using WarningException = System.ComponentModel.WarningException;
99

1010
class SpecifiedArgumentRunner
1111
{
1212
private static readonly bool runningOnMono = Type.GetType("Mono.Runtime") != null;
13-
public static readonly string BuildTool = runningOnMono? "xbuild" : @"c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe";
13+
public static readonly string BuildTool = runningOnMono ? "xbuild" : @"c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe";
1414

1515
public static void Run(Arguments arguments, IFileSystem fileSystem)
1616
{
@@ -23,7 +23,7 @@ public static void Run(Arguments arguments, IFileSystem fileSystem)
2323
var dynamicRepositoryLocation = arguments.DynamicRepositoryLocation;
2424
var targetBranch = arguments.TargetBranch;
2525
var commitId = arguments.CommitId;
26-
var overrideConfig = arguments.OverrideConfig;
26+
var overrideConfig = arguments.HasOverrideConfig ? arguments.OverrideConfig : null;
2727

2828
var executeCore = new ExecuteCore(fileSystem);
2929
var variables = executeCore.ExecuteGitVersion(targetUrl, dynamicRepositoryLocation, authentication, targetBranch, noFetch, targetPath, commitId, overrideConfig: overrideConfig);

0 commit comments

Comments
 (0)