Skip to content

Commit e5e403a

Browse files
authored
Merge branch 'master' into feat/format-fallback
2 parents efb857e + 7215f41 commit e5e403a

24 files changed

+261
-277
lines changed

src/GitVersionCore.Tests/DynamicRepositoryTests.cs

Lines changed: 3 additions & 2 deletions
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,
@@ -79,9 +79,10 @@ public void FindsVersionInDynamicRepo(string name, string url, string targetBran
7979
services.AddSingleton(options);
8080
});
8181

82-
82+
var gitPreparer = sp.GetService<IGitPreparer>();
8383
var gitVersionCalculator = sp.GetService<IGitVersionCalculator>();
8484

85+
gitPreparer.Prepare();
8586
var versionVariables = gitVersionCalculator.CalculateVersionVariables();
8687

8788
Assert.AreEqual(expectedFullSemVer, versionVariables.FullSemVer);

src/GitVersionCore.Tests/GitToolsTestingExtensions.cs

Lines changed: 9 additions & 7 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)
@@ -91,8 +93,8 @@ public static void InitializeRepo(this RemoteRepositoryFixture fixture)
9193
services.AddSingleton(options);
9294
});
9395

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

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

src/GitVersionCore.Tests/GitVersionExecutorTests.cs

Lines changed: 12 additions & 14 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;
@@ -23,6 +24,7 @@ public class GitVersionExecutorTests : TestBase
2324
private IFileSystem fileSystem;
2425
private ILog log;
2526
private IGitVersionCache gitVersionCache;
27+
private IGitPreparer gitPreparer;
2628
private IServiceProvider sp;
2729

2830
[Test]
@@ -40,12 +42,12 @@ public void CacheKeySameAfterReNormalizing()
4042

4143
sp = GetServiceProvider(arguments);
4244

43-
var gitPreparer = sp.GetService<IGitPreparer>();
45+
var preparer = sp.GetService<IGitPreparer>() as GitPreparer;
4446

45-
gitPreparer.Prepare(true, targetBranch);
47+
preparer?.PrepareInternal(true, targetBranch);
4648
var cacheKeyFactory = sp.GetService<IGitVersionCacheKeyFactory>();
4749
var cacheKey1 = cacheKeyFactory.Create(null);
48-
gitPreparer.Prepare(true, targetBranch);
50+
preparer?.PrepareInternal(true, targetBranch);
4951
var cacheKey2 = cacheKeyFactory.Create(null);
5052

5153
cacheKey2.Value.ShouldBe(cacheKey1.Value);
@@ -240,7 +242,7 @@ public void CacheFileIsMissing()
240242
var gitVersionCalculator = GetGitVersionCalculator(arguments, log);
241243

242244
fixture.Repository.MakeACommit();
243-
_ = gitVersionCalculator.CalculateVersionVariables();
245+
gitVersionCalculator.CalculateVersionVariables();
244246

245247
var logsMessages = stringBuilder.ToString();
246248
logsMessages.ShouldContain("yml not found", () => logsMessages);
@@ -401,9 +403,7 @@ public void GetProjectRootDirectoryWorkingDirectoryWithWorktree()
401403

402404
sp = GetServiceProvider(arguments);
403405

404-
var gitPreparer = sp.GetService<IGitPreparer>();
405-
406-
gitPreparer.GetProjectRootDirectoryInternal().TrimEnd('/', '\\').ShouldBe(worktreePath);
406+
arguments.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(worktreePath);
407407
}
408408
finally
409409
{
@@ -425,9 +425,8 @@ public void GetProjectRootDirectoryNoWorktree()
425425

426426
sp = GetServiceProvider(arguments);
427427

428-
var gitPreparer = sp.GetService<IGitPreparer>();
429428
var expectedPath = fixture.RepositoryPath.TrimEnd('/', '\\');
430-
gitPreparer.GetProjectRootDirectoryInternal().TrimEnd('/', '\\').ShouldBe(expectedPath);
429+
arguments.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(expectedPath);
431430
}
432431

433432
[Test]
@@ -442,7 +441,7 @@ public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
442441
};
443442

444443
var gitVersionCalculator = GetGitVersionCalculator(arguments);
445-
444+
gitPreparer.Prepare();
446445
gitVersionCalculator.CalculateVersionVariables();
447446
}
448447

@@ -460,9 +459,8 @@ public void GetDotGitDirectoryNoWorktree()
460459

461460
sp = GetServiceProvider(arguments);
462461

463-
var gitPreparer = sp.GetService<IGitPreparer>();
464462
var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
465-
gitPreparer.GetDotGitDirectory().ShouldBe(expectedPath);
463+
arguments.GetDotGitDirectory().ShouldBe(expectedPath);
466464
}
467465

468466
[Test]
@@ -490,9 +488,8 @@ public void GetDotGitDirectoryWorktree()
490488

491489
sp = GetServiceProvider(arguments);
492490

493-
var gitPreparer = sp.GetService<IGitPreparer>();
494491
var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
495-
gitPreparer.GetDotGitDirectory().ShouldBe(expectedPath);
492+
arguments.GetDotGitDirectory().ShouldBe(expectedPath);
496493
}
497494
finally
498495
{
@@ -507,6 +504,7 @@ private IGitVersionCalculator GetGitVersionCalculator(Arguments arguments, ILog
507504
fileSystem = sp.GetService<IFileSystem>();
508505
log = sp.GetService<ILog>();
509506
gitVersionCache = sp.GetService<IGitVersionCache>();
507+
gitPreparer = sp.GetService<IGitPreparer>();
510508

511509
return sp.GetService<IGitVersionCalculator>();
512510
}

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/BranchCommit.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace GitVersion
55
/// <summary>
66
/// A commit, together with the branch to which the commit belongs.
77
/// </summary>
8-
public struct BranchCommit
8+
public readonly struct BranchCommit
99
{
1010
public static readonly BranchCommit Empty = new BranchCommit();
1111

@@ -18,7 +18,7 @@ public BranchCommit(Commit commit, Branch branch) : this()
1818
public Branch Branch { get; }
1919
public Commit Commit { get; }
2020

21-
public bool Equals(BranchCommit other)
21+
private bool Equals(BranchCommit other)
2222
{
2323
return Equals(Branch, other.Branch) && Equals(Commit, other.Commit);
2424
}

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
}

0 commit comments

Comments
 (0)