Skip to content

Commit e3fa0e8

Browse files
committed
code cleanup for GitVersionComputer & GitPrepare
1 parent 6a3911a commit e3fa0e8

File tree

6 files changed

+146
-104
lines changed

6 files changed

+146
-104
lines changed

src/GitVersionCore.Tests/GitToolsTestingExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,14 @@ private static SemanticVersion ExecuteGitVersion(GitVersionContext context)
8080
/// </summary>
8181
public static void InitialiseRepo(this RemoteRepositoryFixture fixture)
8282
{
83-
new GitPreparer(new NullLog(), null, null, new Authentication(), false, fixture.LocalRepositoryFixture.RepositoryPath).Initialise(true, null);
83+
var log = new NullLog();
84+
85+
var arguments = new Arguments
86+
{
87+
Authentication = new Authentication(),
88+
TargetPath = fixture.LocalRepositoryFixture.RepositoryPath
89+
};
90+
new GitPreparer(log, arguments).Initialize(true, null);
8491
}
8592
}
8693
}

src/GitVersionCore.Tests/GitVersionExecutorTests.cs

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,17 @@ public void CacheKeySameAfterReNormalizing()
4646
{
4747
var targetUrl = "https://github.com/GitTools/GitVersion.git";
4848
var targetBranch = "refs/head/master";
49-
var gitPreparer = new GitPreparer(log, targetUrl, null, new Authentication(), false, fixture.RepositoryPath);
50-
var configFileLocator = new DefaultConfigFileLocator(fileSystem, log);
51-
gitPreparer.Initialise(true, targetBranch);
49+
50+
var arguments = new Arguments
51+
{
52+
TargetUrl = targetUrl,
53+
TargetPath = fixture.RepositoryPath
54+
};
55+
var gitPreparer = new GitPreparer(log, arguments);
56+
configFileLocator = new DefaultConfigFileLocator(fileSystem, log);
57+
gitPreparer.Initialize(true, targetBranch);
5258
var cacheKey1 = GitVersionCacheKeyFactory.Create(fileSystem, log, gitPreparer, null, configFileLocator);
53-
gitPreparer.Initialise(true, targetBranch);
59+
gitPreparer.Initialize(true, targetBranch);
5460
var cacheKey2 = GitVersionCacheKeyFactory.Create(fileSystem, log, gitPreparer, null, configFileLocator);
5561

5662
cacheKey2.Value.ShouldBe(cacheKey1.Value);
@@ -74,8 +80,15 @@ public void CacheKeyForWorktree()
7480
repo.Worktrees.Add("worktree", worktreePath, false);
7581

7682
var targetUrl = "https://github.com/GitTools/GitVersion.git";
77-
var gitPreparer = new GitPreparer(log, targetUrl, null, new Authentication(), false, worktreePath);
78-
var configFileLocator = new DefaultConfigFileLocator(fileSystem, log);
83+
84+
var arguments = new Arguments
85+
{
86+
TargetUrl = targetUrl,
87+
TargetPath = worktreePath
88+
};
89+
90+
var gitPreparer = new GitPreparer(log, arguments);
91+
configFileLocator = new DefaultConfigFileLocator(fileSystem, log);
7992
var cacheKey = GitVersionCacheKeyFactory.Create(fileSystem, log, gitPreparer, null, configFileLocator);
8093
cacheKey.Value.ShouldNotBeEmpty();
8194
}
@@ -183,7 +196,12 @@ public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDyn
183196
{
184197
fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
185198

186-
var gitPreparer = new GitPreparer(log, null, null, null, false, fixture.RepositoryPath);
199+
var arguments = new Arguments
200+
{
201+
TargetPath = fixture.RepositoryPath
202+
};
203+
204+
var gitPreparer = new GitPreparer(log, arguments);
187205
var cacheDirectory = GitVersionCache.GetCacheDirectory(gitPreparer);
188206

189207
var cacheDirectoryTimestamp = fileSystem.GetLastDirectoryWrite(cacheDirectory);
@@ -353,7 +371,14 @@ public void GetProjectRootDirectory_WorkingDirectoryWithWorktree()
353371
repo.Worktrees.Add("worktree", worktreePath, false);
354372

355373
var targetUrl = "https://github.com/GitTools/GitVersion.git";
356-
var gitPreparer = new GitPreparer(log, targetUrl, null, new Authentication(), false, worktreePath);
374+
375+
var arguments = new Arguments
376+
{
377+
TargetUrl = targetUrl,
378+
TargetPath = worktreePath
379+
};
380+
381+
var gitPreparer = new GitPreparer(log, arguments);
357382
gitPreparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(worktreePath);
358383
}
359384
finally
@@ -371,7 +396,14 @@ public void GetProjectRootDirectory_NoWorktree()
371396
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
372397
{
373398
var targetUrl = "https://github.com/GitTools/GitVersion.git";
374-
var gitPreparer = new GitPreparer(log, targetUrl, null, new Authentication(), false, fixture.RepositoryPath);
399+
400+
var arguments = new Arguments
401+
{
402+
TargetUrl = targetUrl,
403+
TargetPath = fixture.RepositoryPath
404+
};
405+
406+
var gitPreparer = new GitPreparer(log, arguments);
375407
var expectedPath = fixture.RepositoryPath.TrimEnd('/', '\\');
376408
gitPreparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(expectedPath);
377409
});
@@ -403,7 +435,14 @@ public void GetDotGitDirectory_NoWorktree()
403435
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
404436
{
405437
var targetUrl = "https://github.com/GitTools/GitVersion.git";
406-
var gitPreparer = new GitPreparer(log, targetUrl, null, new Authentication(), false, fixture.RepositoryPath);
438+
439+
var arguments = new Arguments
440+
{
441+
TargetUrl = targetUrl,
442+
TargetPath = fixture.RepositoryPath
443+
};
444+
445+
var gitPreparer = new GitPreparer(log, arguments);
407446
var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
408447
gitPreparer.GetDotGitDirectory().ShouldBe(expectedPath);
409448
});
@@ -427,7 +466,14 @@ public void GetDotGitDirectory_Worktree()
427466
repo.Worktrees.Add("worktree", worktreePath, false);
428467

429468
var targetUrl = "https://github.com/GitTools/GitVersion.git";
430-
var gitPreparer = new GitPreparer(log, targetUrl, null, new Authentication(), false, worktreePath);
469+
470+
var arguments = new Arguments
471+
{
472+
TargetUrl = targetUrl,
473+
TargetPath = worktreePath
474+
};
475+
476+
var gitPreparer = new GitPreparer(log, arguments);
431477
var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
432478
gitPreparer.GetDotGitDirectory().ShouldBe(expectedPath);
433479
}
@@ -438,7 +484,7 @@ public void GetDotGitDirectory_Worktree()
438484
});
439485
}
440486

441-
private void RepositoryScope(GitVersionComputer gitVersionComputer, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
487+
private void RepositoryScope(IGitVersionComputer gitVersionComputer, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
442488
{
443489
// Make sure GitVersion doesn't trigger build server mode when we are running the tests
444490
environment.SetEnvironmentVariable(AppVeyor.EnvironmentVariableName, null);

src/GitVersionCore/GitPreparer.cs

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,58 +15,58 @@ public class GitPreparer
1515
private readonly bool noFetch;
1616

1717
private const string defaultRemoteName = "origin";
18+
private string dynamicGitRepositoryPath;
1819

1920
public GitPreparer(ILog log, Arguments arguments)
20-
: this(log, arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication, arguments.NoFetch, arguments.TargetPath)
21-
{
22-
}
23-
24-
public GitPreparer(ILog log, string targetUrl, string dynamicRepositoryLocation, Authentication authentication, bool noFetch, string targetPath)
2521
{
2622
this.log = log;
27-
TargetUrl = targetUrl;
28-
this.dynamicRepositoryLocation = dynamicRepositoryLocation;
29-
this.authentication =
30-
new AuthenticationInfo
31-
{
32-
Username = authentication?.Username,
33-
Password = authentication?.Password
34-
};
35-
this.noFetch = noFetch;
36-
WorkingDirectory = targetPath.TrimEnd('/', '\\');
37-
}
38-
39-
public string TargetUrl { get; }
4023

41-
public string WorkingDirectory { get; }
24+
TargetUrl = arguments.TargetUrl;
25+
WorkingDirectory = arguments.TargetPath.TrimEnd('/', '\\');
4226

43-
private bool IsDynamicGitRepository => !string.IsNullOrWhiteSpace(DynamicGitRepositoryPath);
27+
dynamicRepositoryLocation = arguments.DynamicRepositoryLocation;
28+
authentication = new AuthenticationInfo
29+
{
30+
Username = arguments.Authentication?.Username,
31+
Password = arguments.Authentication?.Password
32+
};
4433

45-
private string DynamicGitRepositoryPath { get; set; }
34+
noFetch = arguments.NoFetch;
35+
}
4636

47-
public void Initialise(bool normalizeGitDirectory, string currentBranch, bool shouldCleanUpRemotes = false)
37+
public void Initialize(bool normalizeGitDirectory, string currentBranch, bool shouldCleanUpRemotes = false)
4838
{
4939
if (string.IsNullOrWhiteSpace(TargetUrl))
5040
{
51-
if (normalizeGitDirectory)
41+
if (!normalizeGitDirectory) return;
42+
using (log.IndentLog($"Normalizing git directory for branch '{currentBranch}'"))
5243
{
53-
using (log.IndentLog($"Normalizing git directory for branch '{currentBranch}'"))
44+
if (shouldCleanUpRemotes)
5445
{
55-
if (shouldCleanUpRemotes)
56-
{
57-
CleanupDuplicateOrigin();
58-
}
59-
GitRepositoryHelper.NormalizeGitDirectory(log, GetDotGitDirectory(), authentication, noFetch, currentBranch, IsDynamicGitRepository);
46+
CleanupDuplicateOrigin();
6047
}
48+
GitRepositoryHelper.NormalizeGitDirectory(log, GetDotGitDirectory(), authentication, noFetch, currentBranch, IsDynamicGitRepository());
6149
}
6250
return;
6351
}
6452

6553
var tempRepositoryPath = CalculateTemporaryRepositoryPath(TargetUrl, dynamicRepositoryLocation);
6654

67-
DynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, TargetUrl, currentBranch, noFetch);
55+
dynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, TargetUrl, currentBranch, noFetch);
6856
}
6957

58+
public TResult WithRepository<TResult>(Func<IRepository, TResult> action)
59+
{
60+
using IRepository repo = new Repository(GetDotGitDirectory());
61+
return action(repo);
62+
}
63+
64+
public string TargetUrl { get; }
65+
66+
public string WorkingDirectory { get; }
67+
68+
private bool IsDynamicGitRepository() => !string.IsNullOrWhiteSpace(dynamicGitRepositoryPath);
69+
7070
private void CleanupDuplicateOrigin()
7171
{
7272
var remoteToKeep = defaultRemoteName;
@@ -91,12 +91,6 @@ private void CleanupDuplicateOrigin()
9191
}
9292
}
9393

94-
public TResult WithRepository<TResult>(Func<IRepository, TResult> action)
95-
{
96-
using IRepository repo = new Repository(GetDotGitDirectory());
97-
return action(repo);
98-
}
99-
10094
private static string CalculateTemporaryRepositoryPath(string targetUrl, string dynamicRepositoryLocation)
10195
{
10296
var userTemp = dynamicRepositoryLocation ?? Path.GetTempPath();
@@ -137,7 +131,7 @@ private static bool GitRepoHasMatchingRemote(string possiblePath, string targetU
137131

138132
public string GetDotGitDirectory()
139133
{
140-
var dotGitDirectory = IsDynamicGitRepository ? DynamicGitRepositoryPath : Repository.Discover(WorkingDirectory);
134+
var dotGitDirectory = IsDynamicGitRepository() ? dynamicGitRepositoryPath : Repository.Discover(WorkingDirectory);
141135

142136
dotGitDirectory = dotGitDirectory?.TrimEnd('/', '\\');
143137
if (string.IsNullOrEmpty(dotGitDirectory))
@@ -151,8 +145,8 @@ public string GetDotGitDirectory()
151145

152146
public string GetProjectRootDirectory()
153147
{
154-
log.Info($"IsDynamicGitRepository: {IsDynamicGitRepository}");
155-
if (IsDynamicGitRepository)
148+
log.Info($"IsDynamicGitRepository: {IsDynamicGitRepository()}");
149+
if (IsDynamicGitRepository())
156150
{
157151
log.Info($"Returning Project Root as {WorkingDirectory}");
158152
return WorkingDirectory;

src/GitVersionCore/GitVersionComputer.cs

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,43 @@ public GitVersionComputer(IFileSystem fileSystem, ILog log, IConfigFileLocator c
2727

2828
public VersionVariables ComputeVersionVariables(Arguments arguments)
2929
{
30-
return ComputeVersionVariables(
31-
arguments.TargetUrl, arguments.DynamicRepositoryLocation, arguments.Authentication,
32-
arguments.TargetBranch, arguments.NoFetch, arguments.TargetPath,
33-
arguments.CommitId, arguments.OverrideConfig, arguments.NoCache, arguments.NoNormalize);
30+
var buildServer = buildServerResolver.GetCurrentBuildServer();
31+
32+
// Normalize if we are running on build server
33+
var normalizeGitDirectory = !arguments.NoNormalize && buildServer != null;
34+
arguments.NoFetch = arguments.NoFetch || buildServer != null && buildServer.PreventFetch();
35+
var shouldCleanUpRemotes = buildServer != null && buildServer.ShouldCleanUpRemotes();
36+
37+
var gitPreparer = new GitPreparer(log, arguments);
38+
39+
var currentBranch = ResolveCurrentBranch(buildServer, arguments.TargetBranch, !string.IsNullOrWhiteSpace(arguments.DynamicRepositoryLocation));
40+
41+
gitPreparer.Initialize(normalizeGitDirectory, currentBranch, shouldCleanUpRemotes);
42+
43+
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
44+
var projectRoot = gitPreparer.GetProjectRootDirectory();
45+
46+
log.Info($"Project root is: {projectRoot}");
47+
log.Info($"DotGit directory is: {dotGitDirectory}");
48+
if (string.IsNullOrEmpty(dotGitDirectory) || string.IsNullOrEmpty(projectRoot))
49+
{
50+
// TODO Link to wiki article
51+
throw new Exception($"Failed to prepare or find the .git directory in path '{arguments.TargetPath}'.");
52+
}
53+
54+
return GetCachedGitVersionInfo(arguments.TargetBranch, arguments.CommitId, arguments.OverrideConfig, arguments.NoCache, gitPreparer);
3455
}
3556

36-
public bool TryGetVersion(string directory, out VersionVariables versionVariables, bool noFetch)
57+
public bool TryGetVersion(string directory, bool noFetch, out VersionVariables versionVariables)
3758
{
3859
try
3960
{
40-
versionVariables = ComputeVersionVariables(null, null, null, null, noFetch, directory, null);
61+
var arguments = new Arguments
62+
{
63+
NoFetch = noFetch,
64+
TargetPath = directory
65+
};
66+
versionVariables = ComputeVersionVariables(arguments);
4167
return true;
4268
}
4369
catch (Exception ex)
@@ -48,39 +74,21 @@ public bool TryGetVersion(string directory, out VersionVariables versionVariable
4874
}
4975
}
5076

51-
private VersionVariables ComputeVersionVariables(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId, Config overrideConfig = null, bool noCache = false, bool noNormalize = false)
77+
private string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch, bool isDynamicRepository)
5278
{
53-
// Normalize if we are running on build server
54-
var buildServer = buildServerResolver.GetCurrentBuildServer();
55-
var normalizeGitDirectory = !noNormalize && buildServer != null;
56-
var fetch = noFetch || buildServer != null && buildServer.PreventFetch();
57-
var shouldCleanUpRemotes = buildServer != null && buildServer.ShouldCleanUpRemotes();
58-
var gitPreparer = new GitPreparer(log, targetUrl, dynamicRepositoryLocation, authentication, fetch, workingDirectory);
59-
60-
gitPreparer.Initialise(normalizeGitDirectory, ResolveCurrentBranch(buildServer, targetBranch, !string.IsNullOrWhiteSpace(dynamicRepositoryLocation)), shouldCleanUpRemotes);
61-
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
62-
var projectRoot = gitPreparer.GetProjectRootDirectory();
63-
64-
// TODO Can't use this, it still needs work
65-
//var gitRepository = GitRepositoryFactory.CreateRepository(new RepositoryInfo
66-
//{
67-
// Url = targetUrl,
68-
// Branch = targetBranch,
69-
// Authentication = new AuthenticationInfo
70-
// {
71-
// Username = authentication.Username,
72-
// Password = authentication.Password
73-
// },
74-
// Directory = workingDirectory
75-
//});
76-
log.Info($"Project root is: {projectRoot}");
77-
log.Info($"DotGit directory is: {dotGitDirectory}");
78-
if (string.IsNullOrEmpty(dotGitDirectory) || string.IsNullOrEmpty(projectRoot))
79+
if (buildServer == null)
7980
{
80-
// TODO Link to wiki article
81-
throw new Exception($"Failed to prepare or find the .git directory in path '{workingDirectory}'.");
81+
return targetBranch;
8282
}
8383

84+
var currentBranch = buildServer.GetCurrentBranch(isDynamicRepository) ?? targetBranch;
85+
log.Info("Branch from build environment: " + currentBranch);
86+
87+
return currentBranch;
88+
}
89+
90+
private VersionVariables GetCachedGitVersionInfo(string targetBranch, string commitId, Config overrideConfig, bool noCache, GitPreparer gitPreparer)
91+
{
8492
var cacheKey = GitVersionCacheKeyFactory.Create(fileSystem, log, gitPreparer, overrideConfig, configFileLocator);
8593
var versionVariables = noCache ? default : gitVersionCache.LoadVersionVariablesFromDiskCache(gitPreparer, cacheKey);
8694
if (versionVariables == null)
@@ -91,7 +99,7 @@ private VersionVariables ComputeVersionVariables(string targetUrl, string dynami
9199
{
92100
try
93101
{
94-
gitVersionCache.WriteVariablesToDiskCache(gitPreparer, cacheKey, versionVariables);
102+
gitVersionCache.WriteVariablesToDiskCache(gitPreparer, cacheKey, versionVariables);
95103
}
96104
catch (AggregateException e)
97105
{
@@ -103,20 +111,7 @@ private VersionVariables ComputeVersionVariables(string targetUrl, string dynami
103111
return versionVariables;
104112
}
105113

106-
private string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch, bool isDynamicRepository)
107-
{
108-
if (buildServer == null)
109-
{
110-
return targetBranch;
111-
}
112-
113-
var currentBranch = buildServer.GetCurrentBranch(isDynamicRepository) ?? targetBranch;
114-
log.Info("Branch from build environment: " + currentBranch);
115-
116-
return currentBranch;
117-
}
118-
119-
private VersionVariables ExecuteInternal(string targetBranch, string commitId, GitPreparer gitPreparer, Config overrideConfig = null)
114+
private VersionVariables ExecuteInternal(string targetBranch, string commitId, GitPreparer gitPreparer, Config overrideConfig)
120115
{
121116
var versionFinder = new GitVersionFinder();
122117
var configuration = ConfigurationProvider.Provide(gitPreparer, overrideConfig: overrideConfig, configFileLocator: configFileLocator);

0 commit comments

Comments
 (0)