Skip to content

Commit f6a0c06

Browse files
committed
code cleanup for GitPrepare
1 parent e3fa0e8 commit f6a0c06

File tree

8 files changed

+67
-52
lines changed

8 files changed

+67
-52
lines changed

src/GitVersionCore/Cache/GitVersionCache.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public GitVersionCache(IFileSystem fileSystem, ILog log)
2121
this.log = log;
2222
}
2323

24-
public void WriteVariablesToDiskCache(GitPreparer gitPreparer, GitVersionCacheKey cacheKey, VersionVariables variablesFromCache)
24+
public void WriteVariablesToDiskCache(IGitPreparer gitPreparer, GitVersionCacheKey cacheKey, VersionVariables variablesFromCache)
2525
{
2626
var cacheDir = PrepareCacheDirectory(gitPreparer);
2727
var cacheFileName = GetCacheFileName(cacheKey, cacheDir);
@@ -49,14 +49,14 @@ void WriteCacheOperation()
4949
retryOperation.ExecuteAsync().Wait();
5050
}
5151

52-
public static string GetCacheDirectory(GitPreparer gitPreparer)
52+
public static string GetCacheDirectory(IGitPreparer gitPreparer)
5353
{
5454
var gitDir = gitPreparer.GetDotGitDirectory();
5555
var cacheDir = Path.Combine(gitDir, "gitversion_cache");
5656
return cacheDir;
5757
}
5858

59-
public VersionVariables LoadVersionVariablesFromDiskCache(GitPreparer gitPreparer, GitVersionCacheKey key)
59+
public VersionVariables LoadVersionVariablesFromDiskCache(IGitPreparer gitPreparer, GitVersionCacheKey key)
6060
{
6161
using (log.IndentLog("Loading version variables from disk cache"))
6262
{
@@ -95,7 +95,7 @@ public VersionVariables LoadVersionVariablesFromDiskCache(GitPreparer gitPrepare
9595
}
9696
}
9797

98-
private string PrepareCacheDirectory(GitPreparer gitPreparer)
98+
private string PrepareCacheDirectory(IGitPreparer gitPreparer)
9999
{
100100
var cacheDir = GetCacheDirectory(gitPreparer);
101101

src/GitVersionCore/Cache/GitVersionCacheKeyFactory.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace GitVersion.Cache
1212
{
1313
internal class GitVersionCacheKeyFactory
1414
{
15-
public static GitVersionCacheKey Create(IFileSystem fileSystem, ILog log, GitPreparer gitPreparer, Config overrideConfig, IConfigFileLocator configFileLocator)
15+
public static GitVersionCacheKey Create(IFileSystem fileSystem, ILog log, IGitPreparer gitPreparer, Config overrideConfig, IConfigFileLocator configFileLocator)
1616
{
1717
var gitSystemHash = GetGitSystemHash(gitPreparer, log);
1818
var configFileHash = GetConfigFileHash(fileSystem, gitPreparer, configFileLocator);
@@ -23,7 +23,7 @@ public static GitVersionCacheKey Create(IFileSystem fileSystem, ILog log, GitPre
2323
return new GitVersionCacheKey(compositeHash);
2424
}
2525

26-
private static string GetGitSystemHash(GitPreparer gitPreparer, ILog log)
26+
private static string GetGitSystemHash(IGitPreparer gitPreparer, ILog log)
2727
{
2828
var dotGitDirectory = gitPreparer.GetDotGitDirectory();
2929

@@ -123,7 +123,7 @@ private static List<string> CalculateDirectoryContents(ILog log, string root)
123123
return result;
124124
}
125125

126-
private static string GetRepositorySnapshotHash(GitPreparer gitPreparer)
126+
private static string GetRepositorySnapshotHash(IGitPreparer gitPreparer)
127127
{
128128
var repositorySnapshot = gitPreparer.WithRepository(repo => {
129129
var head = repo.Head;
@@ -157,7 +157,7 @@ private static string GetOverrideConfigHash(Config overrideConfig)
157157
return GetHash(configContent);
158158
}
159159

160-
private static string GetConfigFileHash(IFileSystem fileSystem, GitPreparer gitPreparer, IConfigFileLocator configFileLocator)
160+
private static string GetConfigFileHash(IFileSystem fileSystem, IGitPreparer gitPreparer, IConfigFileLocator configFileLocator)
161161
{
162162
// will return the same hash even when config file will be moved
163163
// from workingDirectory to rootProjectDirectory. It's OK. Config essentially is the same.

src/GitVersionCore/Configuration/ConfigFileLocator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected ConfigFileLocator(IFileSystem fileSystem, ILog log)
2121

2222
public abstract void Verify(string workingDirectory, string projectRootDirectory);
2323

24-
public string SelectConfigFilePath(GitPreparer gitPreparer)
24+
public string SelectConfigFilePath(IGitPreparer gitPreparer)
2525
{
2626
var workingDirectory = gitPreparer.WorkingDirectory;
2727
var projectRootDirectory = gitPreparer.GetProjectRootDirectory();
@@ -48,7 +48,7 @@ public Config ReadConfig(string workingDirectory)
4848
return new Config();
4949
}
5050

51-
public void Verify(GitPreparer gitPreparer)
51+
public void Verify(IGitPreparer gitPreparer)
5252
{
5353
if (!string.IsNullOrWhiteSpace(gitPreparer.TargetUrl))
5454
{

src/GitVersionCore/Configuration/ConfigurationProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class ConfigurationProvider
4343

4444
private const IncrementStrategy DefaultIncrementStrategy = IncrementStrategy.Inherit;
4545

46-
public static Config Provide(GitPreparer gitPreparer, IConfigFileLocator configFileLocator, bool applyDefaults = true, Config overrideConfig = null)
46+
public static Config Provide(IGitPreparer gitPreparer, IConfigFileLocator configFileLocator, bool applyDefaults = true, Config overrideConfig = null)
4747
{
4848
var workingDirectory = gitPreparer.WorkingDirectory;
4949
var projectRootDirectory = gitPreparer.GetProjectRootDirectory();

src/GitVersionCore/Configuration/IConfigFileLocator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ public interface IConfigFileLocator
55
bool HasConfigFileAt(string workingDirectory);
66
string GetConfigFilePath(string workingDirectory);
77
void Verify(string workingDirectory, string projectRootDirectory);
8-
string SelectConfigFilePath(GitPreparer gitPreparer);
8+
string SelectConfigFilePath(IGitPreparer gitPreparer);
99
Config ReadConfig(string workingDirectory);
10-
void Verify(GitPreparer gitPreparer);
10+
void Verify(IGitPreparer gitPreparer);
1111
}
1212
}

src/GitVersionCore/GitPreparer.cs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace GitVersion
99
{
10-
public class GitPreparer
10+
public class GitPreparer : IGitPreparer
1111
{
1212
private readonly ILog log;
1313
private readonly string dynamicRepositoryLocation;
@@ -52,14 +52,48 @@ public void Initialize(bool normalizeGitDirectory, string currentBranch, bool sh
5252

5353
var tempRepositoryPath = CalculateTemporaryRepositoryPath(TargetUrl, dynamicRepositoryLocation);
5454

55-
dynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, TargetUrl, currentBranch, noFetch);
55+
dynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, TargetUrl, currentBranch);
5656
}
5757

5858
public TResult WithRepository<TResult>(Func<IRepository, TResult> action)
5959
{
6060
using IRepository repo = new Repository(GetDotGitDirectory());
6161
return action(repo);
6262
}
63+
64+
public string GetDotGitDirectory()
65+
{
66+
var dotGitDirectory = IsDynamicGitRepository() ? dynamicGitRepositoryPath : Repository.Discover(WorkingDirectory);
67+
68+
dotGitDirectory = dotGitDirectory?.TrimEnd('/', '\\');
69+
if (string.IsNullOrEmpty(dotGitDirectory))
70+
throw new DirectoryNotFoundException("Can't find the .git directory in " + WorkingDirectory);
71+
72+
if (dotGitDirectory.Contains(Path.Combine(".git", "worktrees")))
73+
return Directory.GetParent(Directory.GetParent(dotGitDirectory).FullName).FullName;
74+
75+
return dotGitDirectory;
76+
}
77+
78+
public string GetProjectRootDirectory()
79+
{
80+
log.Info($"IsDynamicGitRepository: {IsDynamicGitRepository()}");
81+
if (IsDynamicGitRepository())
82+
{
83+
log.Info($"Returning Project Root as {WorkingDirectory}");
84+
return WorkingDirectory;
85+
}
86+
87+
var dotGitDirectory = Repository.Discover(WorkingDirectory);
88+
89+
if (string.IsNullOrEmpty(dotGitDirectory))
90+
throw new DirectoryNotFoundException($"Can't find the .git directory in {WorkingDirectory}");
91+
92+
using var repo = new Repository(dotGitDirectory);
93+
var result = repo.Info.WorkingDirectory;
94+
log.Info($"Returning Project Root from DotGitDirectory: {dotGitDirectory} - {result}");
95+
return result;
96+
}
6397

6498
public string TargetUrl { get; }
6599

@@ -129,41 +163,7 @@ private static bool GitRepoHasMatchingRemote(string possiblePath, string targetU
129163
}
130164
}
131165

132-
public string GetDotGitDirectory()
133-
{
134-
var dotGitDirectory = IsDynamicGitRepository() ? dynamicGitRepositoryPath : Repository.Discover(WorkingDirectory);
135-
136-
dotGitDirectory = dotGitDirectory?.TrimEnd('/', '\\');
137-
if (string.IsNullOrEmpty(dotGitDirectory))
138-
throw new DirectoryNotFoundException("Can't find the .git directory in " + WorkingDirectory);
139-
140-
if (dotGitDirectory.Contains(Path.Combine(".git", "worktrees")))
141-
return Directory.GetParent(Directory.GetParent(dotGitDirectory).FullName).FullName;
142-
143-
return dotGitDirectory;
144-
}
145-
146-
public string GetProjectRootDirectory()
147-
{
148-
log.Info($"IsDynamicGitRepository: {IsDynamicGitRepository()}");
149-
if (IsDynamicGitRepository())
150-
{
151-
log.Info($"Returning Project Root as {WorkingDirectory}");
152-
return WorkingDirectory;
153-
}
154-
155-
var dotGitDirectory = Repository.Discover(WorkingDirectory);
156-
157-
if (string.IsNullOrEmpty(dotGitDirectory))
158-
throw new DirectoryNotFoundException($"Can't find the .git directory in {WorkingDirectory}");
159-
160-
using var repo = new Repository(dotGitDirectory);
161-
var result = repo.Info.WorkingDirectory;
162-
log.Info($"Returning Project Root from DotGitDirectory: {dotGitDirectory} - {result}");
163-
return result;
164-
}
165-
166-
private string CreateDynamicRepository(string targetPath, AuthenticationInfo auth, string repositoryUrl, string targetBranch, bool noFetch)
166+
private string CreateDynamicRepository(string targetPath, AuthenticationInfo auth, string repositoryUrl, string targetBranch)
167167
{
168168
if (string.IsNullOrWhiteSpace(targetBranch))
169169
{

src/GitVersionCore/GitVersionComputer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private string ResolveCurrentBranch(IBuildServer buildServer, string targetBranc
8787
return currentBranch;
8888
}
8989

90-
private VersionVariables GetCachedGitVersionInfo(string targetBranch, string commitId, Config overrideConfig, bool noCache, GitPreparer gitPreparer)
90+
private VersionVariables GetCachedGitVersionInfo(string targetBranch, string commitId, Config overrideConfig, bool noCache, IGitPreparer gitPreparer)
9191
{
9292
var cacheKey = GitVersionCacheKeyFactory.Create(fileSystem, log, gitPreparer, overrideConfig, configFileLocator);
9393
var versionVariables = noCache ? default : gitVersionCache.LoadVersionVariablesFromDiskCache(gitPreparer, cacheKey);
@@ -111,7 +111,7 @@ private VersionVariables GetCachedGitVersionInfo(string targetBranch, string com
111111
return versionVariables;
112112
}
113113

114-
private VersionVariables ExecuteInternal(string targetBranch, string commitId, GitPreparer gitPreparer, Config overrideConfig)
114+
private VersionVariables ExecuteInternal(string targetBranch, string commitId, IGitPreparer gitPreparer, Config overrideConfig)
115115
{
116116
var versionFinder = new GitVersionFinder();
117117
var configuration = ConfigurationProvider.Provide(gitPreparer, overrideConfig: overrideConfig, configFileLocator: configFileLocator);

src/GitVersionCore/IGitPreparer.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using LibGit2Sharp;
3+
4+
namespace GitVersion
5+
{
6+
public interface IGitPreparer
7+
{
8+
void Initialize(bool normalizeGitDirectory, string currentBranch, bool shouldCleanUpRemotes = false);
9+
TResult WithRepository<TResult>(Func<IRepository, TResult> action);
10+
string GetDotGitDirectory();
11+
string GetProjectRootDirectory();
12+
string TargetUrl { get; }
13+
string WorkingDirectory { get; }
14+
}
15+
}

0 commit comments

Comments
 (0)