Skip to content

Commit 175b730

Browse files
committed
decoupled GitVersionCalculator and GitPreparer
1 parent 8ce81ad commit 175b730

File tree

7 files changed

+66
-56
lines changed

7 files changed

+66
-56
lines changed

src/GitVersionCore.Tests/DynamicRepositoryTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public static void InitializeRepo(this RemoteRepositoryFixture fixture)
9191
services.AddSingleton(options);
9292
});
9393

94-
var gitPreparer = serviceProvider.GetService<IGitPreparer>();
95-
gitPreparer.Prepare(true, null);
94+
var gitPreparer = serviceProvider.GetService<IGitPreparer>() as GitPreparer;
95+
gitPreparer?.Prepare(true, null);
9696
}
9797

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

src/GitVersionCore.Tests/GitVersionExecutorTests.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class GitVersionExecutorTests : TestBase
2323
private IFileSystem fileSystem;
2424
private ILog log;
2525
private IGitVersionCache gitVersionCache;
26+
private IGitPreparer gitPreparer;
2627
private IServiceProvider sp;
2728

2829
[Test]
@@ -40,12 +41,12 @@ public void CacheKeySameAfterReNormalizing()
4041

4142
sp = GetServiceProvider(arguments);
4243

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

45-
gitPreparer.Prepare(true, targetBranch);
46+
preparer?.Prepare(true, targetBranch);
4647
var cacheKeyFactory = sp.GetService<IGitVersionCacheKeyFactory>();
4748
var cacheKey1 = cacheKeyFactory.Create(null);
48-
gitPreparer.Prepare(true, targetBranch);
49+
preparer?.Prepare(true, targetBranch);
4950
var cacheKey2 = cacheKeyFactory.Create(null);
5051

5152
cacheKey2.Value.ShouldBe(cacheKey1.Value);
@@ -240,7 +241,7 @@ public void CacheFileIsMissing()
240241
var gitVersionCalculator = GetGitVersionCalculator(arguments, log);
241242

242243
fixture.Repository.MakeACommit();
243-
_ = gitVersionCalculator.CalculateVersionVariables();
244+
gitVersionCalculator.CalculateVersionVariables();
244245

245246
var logsMessages = stringBuilder.ToString();
246247
logsMessages.ShouldContain("yml not found", () => logsMessages);
@@ -401,9 +402,9 @@ public void GetProjectRootDirectoryWorkingDirectoryWithWorktree()
401402

402403
sp = GetServiceProvider(arguments);
403404

404-
var gitPreparer = sp.GetService<IGitPreparer>();
405+
var preparer = sp.GetService<IGitPreparer>();
405406

406-
gitPreparer.GetProjectRootDirectoryInternal().TrimEnd('/', '\\').ShouldBe(worktreePath);
407+
preparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(worktreePath);
407408
}
408409
finally
409410
{
@@ -425,9 +426,9 @@ public void GetProjectRootDirectoryNoWorktree()
425426

426427
sp = GetServiceProvider(arguments);
427428

428-
var gitPreparer = sp.GetService<IGitPreparer>();
429+
var preparer = sp.GetService<IGitPreparer>();
429430
var expectedPath = fixture.RepositoryPath.TrimEnd('/', '\\');
430-
gitPreparer.GetProjectRootDirectoryInternal().TrimEnd('/', '\\').ShouldBe(expectedPath);
431+
preparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(expectedPath);
431432
}
432433

433434
[Test]
@@ -442,7 +443,7 @@ public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
442443
};
443444

444445
var gitVersionCalculator = GetGitVersionCalculator(arguments);
445-
446+
gitPreparer.Prepare();
446447
gitVersionCalculator.CalculateVersionVariables();
447448
}
448449

@@ -460,9 +461,9 @@ public void GetDotGitDirectoryNoWorktree()
460461

461462
sp = GetServiceProvider(arguments);
462463

463-
var gitPreparer = sp.GetService<IGitPreparer>();
464+
var preparer = sp.GetService<IGitPreparer>();
464465
var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
465-
gitPreparer.GetDotGitDirectory().ShouldBe(expectedPath);
466+
preparer.GetDotGitDirectory().ShouldBe(expectedPath);
466467
}
467468

468469
[Test]
@@ -490,9 +491,9 @@ public void GetDotGitDirectoryWorktree()
490491

491492
sp = GetServiceProvider(arguments);
492493

493-
var gitPreparer = sp.GetService<IGitPreparer>();
494+
var preparer = sp.GetService<IGitPreparer>();
494495
var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
495-
gitPreparer.GetDotGitDirectory().ShouldBe(expectedPath);
496+
preparer.GetDotGitDirectory().ShouldBe(expectedPath);
496497
}
497498
finally
498499
{
@@ -507,6 +508,7 @@ private IGitVersionCalculator GetGitVersionCalculator(Arguments arguments, ILog
507508
fileSystem = sp.GetService<IFileSystem>();
508509
log = sp.GetService<ILog>();
509510
gitVersionCache = sp.GetService<IGitVersionCache>();
511+
gitPreparer = sp.GetService<IGitPreparer>();
510512

511513
return sp.GetService<IGitVersionCalculator>();
512514
}

src/GitVersionCore/GitPreparer.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class GitPreparer : IGitPreparer
1313
private readonly ILog log;
1414
private readonly IEnvironment environment;
1515
private readonly IOptions<Arguments> options;
16+
private readonly IBuildServerResolver buildServerResolver;
1617

1718
private const string DefaultRemoteName = "origin";
1819
private string dotGitDirectory;
@@ -29,11 +30,36 @@ public class GitPreparer : IGitPreparer
2930
private bool IsDynamicGitRepository => !string.IsNullOrWhiteSpace(DynamicGitRepositoryPath);
3031
private string DynamicGitRepositoryPath;
3132

32-
public GitPreparer(ILog log, IEnvironment environment, IOptions<Arguments> options)
33+
public GitPreparer(ILog log, IEnvironment environment, IOptions<Arguments> options, IBuildServerResolver buildServerResolver)
3334
{
3435
this.log = log ?? throw new ArgumentNullException(nameof(log));
3536
this.environment = environment ?? throw new ArgumentNullException(nameof(environment));
3637
this.options = options ?? throw new ArgumentNullException(nameof(options));
38+
this.buildServerResolver = buildServerResolver ?? throw new ArgumentNullException(nameof(buildServerResolver));
39+
}
40+
41+
public void Prepare()
42+
{
43+
var arguments = options.Value;
44+
var buildServer = buildServerResolver.Resolve();
45+
46+
// Normalize if we are running on build server
47+
var normalizeGitDirectory = !arguments.NoNormalize && buildServer != null;
48+
var shouldCleanUpRemotes = buildServer != null && buildServer.ShouldCleanUpRemotes();
49+
50+
var currentBranch = ResolveCurrentBranch(buildServer, arguments.TargetBranch, !string.IsNullOrWhiteSpace(arguments.DynamicRepositoryLocation));
51+
52+
Prepare(normalizeGitDirectory, currentBranch, shouldCleanUpRemotes);
53+
54+
var dotGitDirectory = GetDotGitDirectory();
55+
var projectRoot = GetProjectRootDirectory();
56+
57+
log.Info($"Project root is: {projectRoot}");
58+
log.Info($"DotGit directory is: {dotGitDirectory}");
59+
if (string.IsNullOrEmpty(dotGitDirectory) || string.IsNullOrEmpty(projectRoot))
60+
{
61+
throw new Exception($"Failed to prepare or find the .git directory in path '{arguments.TargetPath}'.");
62+
}
3763
}
3864

3965
public void Prepare(bool normalizeGitDirectory, string currentBranch, bool shouldCleanUpRemotes = false)
@@ -77,7 +103,7 @@ private string GetDotGitDirectoryInternal()
77103
: gitDirectory;
78104
}
79105

80-
public string GetProjectRootDirectoryInternal()
106+
private string GetProjectRootDirectoryInternal()
81107
{
82108
log.Info($"IsDynamicGitRepository: {IsDynamicGitRepository}");
83109
if (IsDynamicGitRepository)
@@ -97,6 +123,19 @@ public string GetProjectRootDirectoryInternal()
97123
return result;
98124
}
99125

126+
private string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch, bool isDynamicRepository)
127+
{
128+
if (buildServer == null)
129+
{
130+
return targetBranch;
131+
}
132+
133+
var currentBranch = buildServer.GetCurrentBranch(isDynamicRepository) ?? targetBranch;
134+
log.Info("Branch from build environment: " + currentBranch);
135+
136+
return currentBranch;
137+
}
138+
100139
private void CleanupDuplicateOrigin()
101140
{
102141
var remoteToKeep = DefaultRemoteName;

src/GitVersionCore/GitVersionCalculator.cs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class GitVersionCalculator : IGitVersionCalculator
1313
{
1414
private readonly ILog log;
1515
private readonly IConfigProvider configProvider;
16-
private readonly IBuildServerResolver buildServerResolver;
1716
private readonly IGitVersionCache gitVersionCache;
1817
private readonly INextVersionCalculator nextVersionCalculator;
1918
private readonly IGitPreparer gitPreparer;
@@ -27,7 +26,6 @@ public GitVersionCalculator(ILog log, IConfigProvider configProvider, IBuildServ
2726
{
2827
this.log = log ?? throw new ArgumentNullException(nameof(log));
2928
this.configProvider = configProvider ?? throw new ArgumentNullException(nameof(configProvider));
30-
this.buildServerResolver = buildServerResolver ?? throw new ArgumentNullException(nameof(buildServerResolver));
3129
this.gitVersionCache = gitVersionCache ?? throw new ArgumentNullException(nameof(gitVersionCache));
3230
this.nextVersionCalculator = nextVersionCalculator ?? throw new ArgumentNullException(nameof(nextVersionCalculator));
3331
this.gitPreparer = gitPreparer ?? throw new ArgumentNullException(nameof(gitPreparer));
@@ -38,44 +36,12 @@ public GitVersionCalculator(ILog log, IConfigProvider configProvider, IBuildServ
3836

3937
public VersionVariables CalculateVersionVariables()
4038
{
39+
//gitPreparer.Prepare();
4140
var arguments = options.Value;
42-
var buildServer = buildServerResolver.Resolve();
43-
44-
// Normalize if we are running on build server
45-
var normalizeGitDirectory = !arguments.NoNormalize && buildServer != null;
46-
var shouldCleanUpRemotes = buildServer != null && buildServer.ShouldCleanUpRemotes();
47-
48-
var currentBranch = ResolveCurrentBranch(buildServer, arguments.TargetBranch, !string.IsNullOrWhiteSpace(arguments.DynamicRepositoryLocation));
49-
50-
gitPreparer.Prepare(normalizeGitDirectory, currentBranch, shouldCleanUpRemotes);
51-
52-
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
53-
var projectRoot = gitPreparer.GetProjectRootDirectory();
54-
55-
log.Info($"Project root is: {projectRoot}");
56-
log.Info($"DotGit directory is: {dotGitDirectory}");
57-
if (string.IsNullOrEmpty(dotGitDirectory) || string.IsNullOrEmpty(projectRoot))
58-
{
59-
// TODO Link to wiki article
60-
throw new Exception($"Failed to prepare or find the .git directory in path '{arguments.TargetPath}'.");
61-
}
6241

6342
return GetCachedGitVersionInfo(arguments.TargetBranch, arguments.CommitId, arguments.OverrideConfig, arguments.NoCache);
6443
}
6544

66-
private string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch, bool isDynamicRepository)
67-
{
68-
if (buildServer == null)
69-
{
70-
return targetBranch;
71-
}
72-
73-
var currentBranch = buildServer.GetCurrentBranch(isDynamicRepository) ?? targetBranch;
74-
log.Info("Branch from build environment: " + currentBranch);
75-
76-
return currentBranch;
77-
}
78-
7945
private VersionVariables GetCachedGitVersionInfo(string targetBranch, string commitId, Config overrideConfig, bool noCache)
8046
{
8147
var cacheKey = cacheKeyFactory.Create(overrideConfig);

src/GitVersionCore/IGitPreparer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ namespace GitVersion
22
{
33
public interface IGitPreparer
44
{
5-
void Prepare(bool normalizeGitDirectory, string currentBranch, bool shouldCleanUpRemotes = false);
65
string GetProjectRootDirectory();
76
string GetDotGitDirectory();
87
string GetTargetUrl();
98
string GetWorkingDirectory();
10-
string GetProjectRootDirectoryInternal();
9+
void Prepare();
1110
}
1211
}

src/GitVersionExe/ExecCommand.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ public class ExecCommand : IExecCommand
2020
private readonly IFileSystem fileSystem;
2121
private readonly IBuildServerResolver buildServerResolver;
2222
private readonly ILog log;
23+
private readonly IGitPreparer gitPreparer;
2324
private readonly IGitVersionCalculator gitVersionCalculator;
2425
private readonly IOptions<Arguments> options;
2526

26-
public ExecCommand(IFileSystem fileSystem, IBuildServerResolver buildServerResolver, ILog log, IGitVersionCalculator gitVersionCalculator, IOptions<Arguments> options)
27+
public ExecCommand(IFileSystem fileSystem, IBuildServerResolver buildServerResolver, ILog log, IGitPreparer gitPreparer, IGitVersionCalculator gitVersionCalculator, IOptions<Arguments> options)
2728
{
2829
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
2930
this.buildServerResolver = buildServerResolver ?? throw new ArgumentNullException(nameof(buildServerResolver));
3031
this.log = log ?? throw new ArgumentNullException(nameof(log));
32+
this.gitPreparer = gitPreparer ?? throw new ArgumentNullException(nameof(gitPreparer));
3133
this.gitVersionCalculator = gitVersionCalculator ?? throw new ArgumentNullException(nameof(gitVersionCalculator));
3234
this.options = options ?? throw new ArgumentNullException(nameof(options));
3335
}
@@ -36,6 +38,7 @@ public void Execute()
3638
{
3739
log.Info($"Running on {(RunningOnUnix ? "Unix" : "Windows")}.");
3840

41+
gitPreparer.Prepare();
3942
var variables = gitVersionCalculator.CalculateVersionVariables();
4043

4144
var arguments = options.Value;

0 commit comments

Comments
 (0)