Skip to content

Commit 989621f

Browse files
committed
refactored GitPrepare, moved to ArgumentExtensions the computed methods
1 parent 175b730 commit 989621f

19 files changed

+194
-204
lines changed

src/GitVersionCore.Tests/DynamicRepositoryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void FindsVersionInDynamicRepo(string name, string url, string targetBran
6363
var arguments = new Arguments
6464
{
6565
TargetUrl = url,
66-
DynamicRepositoryLocation = dynamicDirectory,
66+
DynamicRepositoryClonePath = dynamicDirectory,
6767
TargetBranch = targetBranch,
6868
NoFetch = false,
6969
TargetPath = workingDirectory,

src/GitVersionCore.Tests/GitToolsTestingExtensions.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static class GitToolsTestingExtensions
2020

2121
static GitToolsTestingExtensions() => sp = ConfigureService();
2222

23-
public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Config configuration = null, IRepository repository = null, string commitId = null, bool onlyTrackedBranches = true, string targetBranch = null)
23+
public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Config configuration = null, IRepository repository = null, string commitId = null, bool onlyTrackedBranches = true, string branch = null)
2424
{
2525
if (configuration == null)
2626
{
@@ -32,12 +32,14 @@ public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Co
3232
var variableProvider = sp.GetService<IVariableProvider>();
3333
var nextVersionCalculator = sp.GetService<INextVersionCalculator>();
3434

35-
var gitVersionContext = new GitVersionContext(repository ?? fixture.Repository, log, targetBranch, configuration, onlyTrackedBranches, commitId);
36-
var executeGitVersion = nextVersionCalculator.FindVersion(gitVersionContext);
37-
var variables = variableProvider.GetVariablesFor(executeGitVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
38-
35+
repository ??= fixture.Repository;
36+
var targetBranch = repository.GetTargetBranch(branch);
37+
var gitVersionContext = new GitVersionContext(repository, log, targetBranch, configuration, onlyTrackedBranches, commitId);
3938
try
4039
{
40+
var executeGitVersion = nextVersionCalculator.FindVersion(gitVersionContext);
41+
var variables = variableProvider.GetVariablesFor(executeGitVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
42+
4143
return variables;
4244
}
4345
catch (Exception)
@@ -92,7 +94,7 @@ public static void InitializeRepo(this RemoteRepositoryFixture fixture)
9294
});
9395

9496
var gitPreparer = serviceProvider.GetService<IGitPreparer>() as GitPreparer;
95-
gitPreparer?.Prepare(true, null);
97+
gitPreparer?.PrepareInternal(true, null);
9698
}
9799

98100
private static IServiceProvider ConfigureService(Action<IServiceCollection> servicesOverrides = null)

src/GitVersionCore.Tests/GitVersionExecutorTests.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using GitVersion;
66
using GitVersion.Cache;
77
using GitVersion.Configuration;
8+
using GitVersion.Extensions;
89
using GitVersion.Logging;
910
using GitVersionCore.Tests.Helpers;
1011
using LibGit2Sharp;
@@ -43,10 +44,10 @@ public void CacheKeySameAfterReNormalizing()
4344

4445
var preparer = sp.GetService<IGitPreparer>() as GitPreparer;
4546

46-
preparer?.Prepare(true, targetBranch);
47+
preparer?.PrepareInternal(true, targetBranch);
4748
var cacheKeyFactory = sp.GetService<IGitVersionCacheKeyFactory>();
4849
var cacheKey1 = cacheKeyFactory.Create(null);
49-
preparer?.Prepare(true, targetBranch);
50+
preparer?.PrepareInternal(true, targetBranch);
5051
var cacheKey2 = cacheKeyFactory.Create(null);
5152

5253
cacheKey2.Value.ShouldBe(cacheKey1.Value);
@@ -402,9 +403,7 @@ public void GetProjectRootDirectoryWorkingDirectoryWithWorktree()
402403

403404
sp = GetServiceProvider(arguments);
404405

405-
var preparer = sp.GetService<IGitPreparer>();
406-
407-
preparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(worktreePath);
406+
arguments.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(worktreePath);
408407
}
409408
finally
410409
{
@@ -426,9 +425,8 @@ public void GetProjectRootDirectoryNoWorktree()
426425

427426
sp = GetServiceProvider(arguments);
428427

429-
var preparer = sp.GetService<IGitPreparer>();
430428
var expectedPath = fixture.RepositoryPath.TrimEnd('/', '\\');
431-
preparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(expectedPath);
429+
arguments.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(expectedPath);
432430
}
433431

434432
[Test]
@@ -461,9 +459,8 @@ public void GetDotGitDirectoryNoWorktree()
461459

462460
sp = GetServiceProvider(arguments);
463461

464-
var preparer = sp.GetService<IGitPreparer>();
465462
var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
466-
preparer.GetDotGitDirectory().ShouldBe(expectedPath);
463+
arguments.GetDotGitDirectory().ShouldBe(expectedPath);
467464
}
468465

469466
[Test]
@@ -491,9 +488,8 @@ public void GetDotGitDirectoryWorktree()
491488

492489
sp = GetServiceProvider(arguments);
493490

494-
var preparer = sp.GetService<IGitPreparer>();
495491
var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
496-
preparer.GetDotGitDirectory().ShouldBe(expectedPath);
492+
arguments.GetDotGitDirectory().ShouldBe(expectedPath);
497493
}
498494
finally
499495
{

src/GitVersionCore/Arguments.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class Arguments
1919
public string TargetUrl;
2020
public string TargetBranch;
2121
public string CommitId;
22-
public string DynamicRepositoryLocation;
22+
public string DynamicRepositoryClonePath;
23+
public string DynamicGitRepositoryPath;
2324

2425
public bool Init;
2526
public bool Diag;
@@ -50,6 +51,7 @@ public class Arguments
5051
public bool UpdateAssemblyInfo;
5152
public ISet<string> UpdateAssemblyInfoFileName = new HashSet<string>();
5253
public bool EnsureAssemblyInfo;
54+
5355
public void AddAssemblyInfoFileName(string fileName)
5456
{
5557
UpdateAssemblyInfoFileName.Add(fileName);

src/GitVersionCore/Cache/GitVersionCache.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using GitVersion.Extensions;
56
using GitVersion.Helpers;
67
using GitVersion.Logging;
78
using GitVersion.OutputVariables;
9+
using Microsoft.Extensions.Options;
810
using YamlDotNet.Serialization;
911

1012
namespace GitVersion.Cache
@@ -13,13 +15,13 @@ public class GitVersionCache : IGitVersionCache
1315
{
1416
private readonly IFileSystem fileSystem;
1517
private readonly ILog log;
16-
private readonly IGitPreparer gitPreparer;
18+
private readonly IOptions<Arguments> options;
1719

18-
public GitVersionCache(IFileSystem fileSystem, ILog log, IGitPreparer gitPreparer)
20+
public GitVersionCache(IFileSystem fileSystem, ILog log, IOptions<Arguments> options)
1921
{
2022
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
2123
this.log = log ?? throw new ArgumentNullException(nameof(log));
22-
this.gitPreparer = gitPreparer ?? throw new ArgumentNullException(nameof(gitPreparer));
24+
this.options = options ?? throw new ArgumentNullException(nameof(options));
2325
}
2426

2527
public void WriteVariablesToDiskCache(GitVersionCacheKey cacheKey, VersionVariables variablesFromCache)
@@ -52,7 +54,7 @@ void WriteCacheOperation()
5254

5355
public string GetCacheDirectory()
5456
{
55-
var gitDir = gitPreparer.GetDotGitDirectory();
57+
var gitDir = options.Value.GetDotGitDirectory();
5658
var cacheDir = Path.Combine(gitDir, "gitversion_cache");
5759
return cacheDir;
5860
}

src/GitVersionCore/Cache/GitVersionCacheKeyFactory.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
using System.Security.Cryptography;
66
using System.Text;
77
using GitVersion.Configuration;
8+
using GitVersion.Extensions;
89
using GitVersion.Logging;
910
using LibGit2Sharp;
11+
using Microsoft.Extensions.Options;
1012

1113
namespace GitVersion.Cache
1214
{
@@ -19,15 +21,15 @@ public class GitVersionCacheKeyFactory : IGitVersionCacheKeyFactory
1921
{
2022
private readonly IFileSystem fileSystem;
2123
private readonly ILog log;
22-
private readonly IGitPreparer gitPreparer;
24+
private readonly IOptions<Arguments> options;
2325
private readonly IConfigFileLocator configFileLocator;
2426

25-
public GitVersionCacheKeyFactory(IFileSystem fileSystem, ILog log, IGitPreparer gitPreparer, IConfigFileLocator configFileLocator)
27+
public GitVersionCacheKeyFactory(IFileSystem fileSystem, ILog log, IOptions<Arguments> options, IConfigFileLocator configFileLocator)
2628
{
27-
this.fileSystem = fileSystem;
28-
this.log = log;
29-
this.gitPreparer = gitPreparer;
30-
this.configFileLocator = configFileLocator;
29+
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
30+
this.log = log ?? throw new ArgumentNullException(nameof(log));
31+
this.options = options ?? throw new ArgumentNullException(nameof(options));
32+
this.configFileLocator = configFileLocator ?? throw new ArgumentNullException(nameof(configFileLocator));
3133
}
3234

3335
public GitVersionCacheKey Create(Config overrideConfig)
@@ -43,7 +45,7 @@ public GitVersionCacheKey Create(Config overrideConfig)
4345

4446
private string GetGitSystemHash()
4547
{
46-
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
48+
var dotGitDirectory = options.Value.GetDotGitDirectory();
4749

4850
// traverse the directory and get a list of files, use that for GetHash
4951
var contents = CalculateDirectoryContents(Path.Combine(dotGitDirectory, "refs"));
@@ -80,13 +82,13 @@ private List<string> CalculateDirectoryContents(string root)
8082
subDirs = Directory.GetDirectories(currentDir);
8183
}
8284
// An UnauthorizedAccessException exception will be thrown if we do not have
83-
// discovery permission on a folder or file. It may or may not be acceptable
84-
// to ignore the exception and continue enumerating the remaining files and
85-
// folders. It is also possible (but unlikely) that a DirectoryNotFound exception
85+
// discovery permission on a folder or file. It may or may not be acceptable
86+
// to ignore the exception and continue enumerating the remaining files and
87+
// folders. It is also possible (but unlikely) that a DirectoryNotFound exception
8688
// will be raised. This will happen if currentDir has been deleted by
87-
// another application or thread after our call to Directory.Exists. The
88-
// choice of which exceptions to catch depends entirely on the specific task
89-
// you are intending to perform and also on how much you know with certainty
89+
// another application or thread after our call to Directory.Exists. The
90+
// choice of which exceptions to catch depends entirely on the specific task
91+
// you are intending to perform and also on how much you know with certainty
9092
// about the systems on which this code will run.
9193
catch (UnauthorizedAccessException e)
9294
{
@@ -143,7 +145,7 @@ private List<string> CalculateDirectoryContents(string root)
143145

144146
private string GetRepositorySnapshotHash()
145147
{
146-
using var repo = new Repository(gitPreparer.GetDotGitDirectory());
148+
using var repo = new Repository(options.Value.GetDotGitDirectory());
147149

148150
var head = repo.Head;
149151
if (head.Tip == null)
@@ -161,7 +163,7 @@ private static string GetOverrideConfigHash(Config overrideConfig)
161163
return string.Empty;
162164
}
163165

164-
// Doesn't depend on command line representation and
166+
// Doesn't depend on command line representation and
165167
// includes possible changes in default values of Config per se.
166168
var stringBuilder = new StringBuilder();
167169
using (var stream = new StringWriter(stringBuilder))
@@ -176,9 +178,9 @@ private static string GetOverrideConfigHash(Config overrideConfig)
176178

177179
private string GetConfigFileHash()
178180
{
179-
// will return the same hash even when config file will be moved
181+
// will return the same hash even when config file will be moved
180182
// from workingDirectory to rootProjectDirectory. It's OK. Config essentially is the same.
181-
var configFilePath = configFileLocator.SelectConfigFilePath(gitPreparer);
183+
var configFilePath = configFileLocator.SelectConfigFilePath(options.Value);
182184
if (!fileSystem.Exists(configFilePath))
183185
{
184186
return string.Empty;

src/GitVersionCore/Configuration/ConfigFileLocator.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using GitVersion.Extensions;
34

45
namespace GitVersion.Configuration
56
{
@@ -18,10 +19,10 @@ protected ConfigFileLocator(IFileSystem fileSystem)
1819

1920
public abstract void Verify(string workingDirectory, string projectRootDirectory);
2021

21-
public string SelectConfigFilePath(IGitPreparer gitPreparer)
22+
public string SelectConfigFilePath(Arguments arguments)
2223
{
23-
var workingDirectory = gitPreparer.GetWorkingDirectory();
24-
var projectRootDirectory = gitPreparer.GetProjectRootDirectory();
24+
var workingDirectory = arguments.GetWorkingDirectory();
25+
var projectRootDirectory = arguments.GetProjectRootDirectory();
2526

2627
return GetConfigFilePath(HasConfigFileAt(workingDirectory) ? workingDirectory : projectRootDirectory);
2728
}
@@ -39,17 +40,17 @@ public Config ReadConfig(string workingDirectory)
3940
return new Config();
4041
}
4142

42-
public void Verify(IGitPreparer gitPreparer)
43+
public void Verify(Arguments arguments)
4344
{
44-
if (!string.IsNullOrWhiteSpace(gitPreparer.GetTargetUrl()))
45+
if (!string.IsNullOrWhiteSpace(arguments.GetTargetUrl()))
4546
{
4647
// Assuming this is a dynamic repository. At this stage it's unsure whether we have
4748
// any .git info so we need to skip verification
4849
return;
4950
}
5051

51-
var workingDirectory = gitPreparer.GetWorkingDirectory();
52-
var projectRootDirectory = gitPreparer.GetProjectRootDirectory();
52+
var workingDirectory = arguments.GetWorkingDirectory();
53+
var projectRootDirectory = arguments.GetProjectRootDirectory();
5354

5455
Verify(workingDirectory, projectRootDirectory);
5556
}

src/GitVersionCore/Configuration/ConfigProvider.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.IO;
33
using GitVersion.Configuration.Init.Wizard;
4+
using GitVersion.Extensions;
45
using GitVersion.Logging;
6+
using Microsoft.Extensions.Options;
57

68
namespace GitVersion.Configuration
79
{
@@ -10,22 +12,22 @@ public class ConfigProvider : IConfigProvider
1012
private readonly IFileSystem fileSystem;
1113
private readonly ILog log;
1214
private readonly IConfigFileLocator configFileLocator;
13-
private readonly IGitPreparer gitPreparer;
15+
private readonly IOptions<Arguments> options;
1416
private readonly IConfigInitWizard configInitWizard;
1517

16-
public ConfigProvider(IFileSystem fileSystem, ILog log, IConfigFileLocator configFileLocator, IGitPreparer gitPreparer, IConfigInitWizard configInitWizard)
18+
public ConfigProvider(IFileSystem fileSystem, ILog log, IConfigFileLocator configFileLocator, IOptions<Arguments> options, IConfigInitWizard configInitWizard)
1719
{
1820
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
1921
this.log = log ?? throw new ArgumentNullException(nameof(log));
2022
this.configFileLocator = configFileLocator ?? throw new ArgumentNullException(nameof(configFileLocator));
21-
this.gitPreparer = gitPreparer ?? throw new ArgumentNullException(nameof(gitPreparer));
23+
this.options = options ?? throw new ArgumentNullException(nameof(options));
2224
this.configInitWizard = configInitWizard ?? throw new ArgumentNullException(nameof(this.configInitWizard));
2325
}
2426

2527
public Config Provide(bool applyDefaults = true, Config overrideConfig = null)
2628
{
27-
var workingDirectory = gitPreparer.GetWorkingDirectory();
28-
var projectRootDirectory = gitPreparer.GetProjectRootDirectory();
29+
var workingDirectory = options.Value.GetWorkingDirectory();
30+
var projectRootDirectory = options.Value.GetProjectRootDirectory();
2931

3032
var rootDirectory = configFileLocator.HasConfigFileAt(workingDirectory) ? workingDirectory : projectRootDirectory;
3133
return Provide(rootDirectory, applyDefaults, overrideConfig);

src/GitVersionCore/Configuration/IConfigFileLocator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ public interface IConfigFileLocator
44
{
55
bool HasConfigFileAt(string workingDirectory);
66
string GetConfigFilePath(string workingDirectory);
7-
void Verify(IGitPreparer gitPreparer);
7+
void Verify(Arguments arguments);
88
void Verify(string workingDirectory, string projectRootDirectory);
9-
string SelectConfigFilePath(IGitPreparer gitPreparer);
9+
string SelectConfigFilePath(Arguments arguments);
1010
Config ReadConfig(string workingDirectory);
1111
}
1212
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.IO;
2+
using LibGit2Sharp;
3+
4+
namespace GitVersion.Extensions
5+
{
6+
public static class ArgumentExtensions
7+
{
8+
public static string GetTargetUrl(this Arguments arguments) => arguments.TargetUrl;
9+
10+
public static string GetWorkingDirectory(this Arguments arguments) => arguments.TargetPath.TrimEnd('/', '\\');
11+
12+
public static bool IsDynamicGitRepository(this Arguments arguments) => !string.IsNullOrWhiteSpace(arguments.DynamicGitRepositoryPath);
13+
14+
public static string GetDotGitDirectory(this Arguments arguments)
15+
{
16+
var gitDirectory = arguments.IsDynamicGitRepository() ? arguments.DynamicGitRepositoryPath : Repository.Discover(arguments.GetWorkingDirectory());
17+
18+
gitDirectory = gitDirectory?.TrimEnd('/', '\\');
19+
if (string.IsNullOrEmpty(gitDirectory))
20+
throw new DirectoryNotFoundException("Can't find the .git directory in " + gitDirectory);
21+
22+
return gitDirectory.Contains(Path.Combine(".git", "worktrees"))
23+
? Directory.GetParent(Directory.GetParent(gitDirectory).FullName).FullName
24+
: gitDirectory;
25+
}
26+
27+
public static string GetProjectRootDirectory(this Arguments arguments)
28+
{
29+
if (arguments.IsDynamicGitRepository())
30+
{
31+
return arguments.GetWorkingDirectory();
32+
}
33+
34+
var dotGitDirectory = Repository.Discover(arguments.GetWorkingDirectory());
35+
36+
if (string.IsNullOrEmpty(dotGitDirectory))
37+
throw new DirectoryNotFoundException($"Can't find the .git directory in {dotGitDirectory}");
38+
39+
using var repo = new Repository(dotGitDirectory);
40+
var result = repo.Info.WorkingDirectory;
41+
return result;
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)