Skip to content

Commit ec2d796

Browse files
asbjornuJakeGinnivan
authored andcommitted
Deleted IFileSystem.TreeWalkForDotGitDir() and replaced its usage with Repository.Discover().
1 parent 5076a66 commit ec2d796

File tree

9 files changed

+47
-63
lines changed

9 files changed

+47
-63
lines changed

src/GitVersionCore.Tests/FileSystemTests.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,13 @@ public class FileSystemTests
1111
{
1212
string workDirectory;
1313
string gitDirectory;
14-
IFileSystem fileSystem;
1514

1615
[SetUp]
1716
public void CreateTemporaryRepository()
1817
{
19-
this.fileSystem = new FileSystem();
2018
workDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
2119

22-
gitDirectory = Repository.Init(workDirectory)
23-
.TrimEnd(new[] { Path.DirectorySeparatorChar });
20+
gitDirectory = Repository.Init(workDirectory);
2421

2522
Assert.NotNull(gitDirectory);
2623
}
@@ -34,26 +31,26 @@ public void Cleanup()
3431
[Test]
3532
public void From_WorkingDirectory()
3633
{
37-
Assert.AreEqual(gitDirectory, fileSystem.TreeWalkForDotGitDir(workDirectory));
34+
Assert.AreEqual(gitDirectory, Repository.Discover(workDirectory));
3835
}
3936

4037
[Test]
4138
public void From_WorkingDirectory_Parent()
4239
{
4340
var parentDirectory = Directory.GetParent(workDirectory).FullName;
44-
Assert.Null(fileSystem.TreeWalkForDotGitDir(parentDirectory));
41+
Assert.Null(Repository.Discover(parentDirectory));
4542
}
4643

4744
[Test]
4845
public void From_GitDirectory()
4946
{
50-
Assert.AreEqual(gitDirectory, fileSystem.TreeWalkForDotGitDir(gitDirectory));
47+
Assert.AreEqual(gitDirectory, Repository.Discover(gitDirectory));
5148
}
5249

5350
[Test]
5451
public void From_RefsDirectory()
5552
{
5653
var refsDirectory = Path.Combine(gitDirectory, "refs");
57-
Assert.AreEqual(gitDirectory, fileSystem.TreeWalkForDotGitDir(refsDirectory));
54+
Assert.AreEqual(gitDirectory, Repository.Discover(refsDirectory));
5855
}
5956
}

src/GitVersionCore.Tests/Fixtures/RemoteRepositoryFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public RemoteRepositoryFixture(Config configuration)
2626
/// </summary>
2727
public void InitialiseRepo()
2828
{
29-
new GitPreparer(null, null, new Authentication(), false, LocalRepositoryFixture.RepositoryPath, new FileSystem()).Initialise(true, null);
29+
new GitPreparer(null, null, new Authentication(), false, LocalRepositoryFixture.RepositoryPath).Initialise(true, null);
3030
}
3131

3232
static IRepository CreateNewRepository(string path)

src/GitVersionCore.Tests/GitPreparerTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void WorksCorrectlyWithRemoteRepository(string branchName, string expecte
3939
// Copy contents into working directory
4040
File.Copy(Path.Combine(fixture.RepositoryPath, "TestFile.txt"), Path.Combine(tempDir, "TestFile.txt"));
4141

42-
var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), false, tempDir, new FileSystem());
42+
var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), false, tempDir);
4343
gitPreparer.Initialise(false, branchName);
4444
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
4545

@@ -77,7 +77,7 @@ public void UpdatesExistingDynamicRepository()
7777
{
7878
mainRepositoryFixture.Repository.MakeCommits(1);
7979

80-
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir, new FileSystem());
80+
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir);
8181
gitPreparer.Initialise(false, "master");
8282
dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
8383

@@ -118,7 +118,7 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
118118
expectedDynamicRepoLocation = Path.Combine(tempPath, fixture.RepositoryPath.Split('\\').Last());
119119
Directory.CreateDirectory(expectedDynamicRepoLocation);
120120

121-
var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), false, tempDir, new FileSystem());
121+
var gitPreparer = new GitPreparer(fixture.RepositoryPath, null, new Authentication(), false, tempDir);
122122
gitPreparer.Initialise(false, "master");
123123

124124
gitPreparer.IsDynamicGitRepository.ShouldBe(true);
@@ -139,7 +139,7 @@ public void PicksAnotherDirectoryNameWhenDynamicRepoFolderTaken()
139139
public void WorksCorrectlyWithLocalRepository()
140140
{
141141
var tempDir = Path.GetTempPath();
142-
var gitPreparer = new GitPreparer(null, null, null, false, tempDir, new FileSystem());
142+
var gitPreparer = new GitPreparer(null, null, null, false, tempDir);
143143
var dynamicRepositoryPath = gitPreparer.GetDotGitDirectory();
144144

145145
dynamicRepositoryPath.ShouldBe(null);
@@ -160,7 +160,7 @@ public void UsingDynamicRepositoryWithFeatureBranchWorks()
160160
{
161161
mainRepositoryFixture.Repository.MakeACommit();
162162

163-
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir, new FileSystem());
163+
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir);
164164
gitPreparer.Initialise(true, "feature1");
165165

166166
mainRepositoryFixture.Repository.Checkout(mainRepositoryFixture.Repository.CreateBranch("feature1"));
@@ -188,7 +188,7 @@ public void UsingDynamicRepositoryWithoutTargetBranchFails()
188188
{
189189
mainRepositoryFixture.Repository.MakeACommit();
190190

191-
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir, new FileSystem());
191+
var gitPreparer = new GitPreparer(mainRepositoryFixture.RepositoryPath, null, new Authentication(), false, tempDir);
192192

193193
Should.Throw<Exception>(() => gitPreparer.Initialise(true, null));
194194
}
@@ -209,7 +209,7 @@ public void TestErrorThrownForInvalidRepository()
209209

210210
try
211211
{
212-
var gitPreparer = new GitPreparer("http://127.0.0.1/testrepo.git", null, new Authentication(), false, tempDir, new FileSystem());
212+
var gitPreparer = new GitPreparer("http://127.0.0.1/testrepo.git", null, new Authentication(), false, tempDir);
213213

214214
Should.Throw<Exception>(() => gitPreparer.Initialise(true, "master"));
215215
}

src/GitVersionCore.Tests/TestFileSystem.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ public long GetLastDirectoryWrite(string path)
9393
}
9494

9595

96-
public string TreeWalkForDotGitDir(string directory)
97-
{
98-
return directory;
99-
}
100-
101-
10296
public IRepository GetRepository(string gitDirectory)
10397
{
10498
var repository = Substitute.For<IRepository>();

src/GitVersionCore/ExecuteCore.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ namespace GitVersion
22
{
33
using System;
44
using System.Linq;
5+
56
using GitVersion.Helpers;
67

78
public static class ExecuteCore
89
{
910
public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId)
1011
{
1112
// Normalise if we are running on build server
12-
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, noFetch, workingDirectory, fileSystem);
13+
var gitPreparer = new GitPreparer(targetUrl, dynamicRepositoryLocation, authentication, noFetch, workingDirectory);
1314
var applicableBuildServers = BuildServerList.GetApplicableBuildServers();
1415
var buildServer = applicableBuildServers.FirstOrDefault();
1516

@@ -29,17 +30,21 @@ public static VersionVariables ExecuteGitVersion(IFileSystem fileSystem, string
2930

3031
using (var repo = fileSystem.GetRepository(dotGitDirectory))
3132
{
32-
var gitVersionContext = new GitVersionContext(repo, configuration, commitId: commitId);
33+
var gitVersionContext = new GitVersionContext(repo, configuration, commitId : commitId);
3334
var semanticVersion = versionFinder.FindVersion(gitVersionContext);
3435
variables = VariableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
3536
}
3637

3738
return variables;
3839
}
3940

40-
private static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch)
41+
42+
static string ResolveCurrentBranch(IBuildServer buildServer, string targetBranch)
4143
{
42-
if (buildServer == null) return targetBranch;
44+
if (buildServer == null)
45+
{
46+
return targetBranch;
47+
}
4348

4449
var currentBranch = buildServer.GetCurrentBranch() ?? targetBranch;
4550
Logger.WriteInfo("Branch from build environment: " + currentBranch);

src/GitVersionCore/GitPreparer.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,35 @@ namespace GitVersion
44
using System.IO;
55
using System.Linq;
66

7-
using GitVersion.Helpers;
8-
97
using LibGit2Sharp;
108

119
public class GitPreparer
1210
{
13-
string targetUrl;
14-
string dynamicRepositoryLocation;
1511
Authentication authentication;
12+
string dynamicRepositoryLocation;
1613
bool noFetch;
1714
string targetPath;
18-
readonly IFileSystem fileSystem;
15+
string targetUrl;
1916

2017

21-
public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, bool noFetch, string targetPath, IFileSystem fileSystem)
18+
public GitPreparer(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, bool noFetch, string targetPath)
2219
{
23-
if (fileSystem == null)
24-
{
25-
throw new ArgumentNullException("fileSystem");
26-
}
27-
2820
this.targetUrl = targetUrl;
2921
this.dynamicRepositoryLocation = dynamicRepositoryLocation;
3022
this.authentication = authentication;
3123
this.noFetch = noFetch;
3224
this.targetPath = targetPath;
33-
this.fileSystem = fileSystem;
3425
}
3526

27+
3628
public bool IsDynamicGitRepository
3729
{
3830
get { return !string.IsNullOrWhiteSpace(DynamicGitRepositoryPath); }
3931
}
4032

4133
public string DynamicGitRepositoryPath { get; private set; }
4234

35+
4336
public void Initialise(bool normaliseGitDirectory, string currentBranch)
4437
{
4538
if (string.IsNullOrWhiteSpace(targetUrl))
@@ -56,6 +49,7 @@ public void Initialise(bool normaliseGitDirectory, string currentBranch)
5649
DynamicGitRepositoryPath = CreateDynamicRepository(tempRepositoryPath, authentication, targetUrl, currentBranch, noFetch);
5750
}
5851

52+
5953
static string CalculateTemporaryRepositoryPath(string targetUrl, string dynamicRepositoryLocation)
6054
{
6155
var userTemp = dynamicRepositoryLocation ?? Path.GetTempPath();
@@ -81,6 +75,7 @@ static string CalculateTemporaryRepositoryPath(string targetUrl, string dynamicR
8175
return possiblePath;
8276
}
8377

78+
8479
static bool GitRepoHasMatchingRemote(string possiblePath, string targetUrl)
8580
{
8681
try
@@ -94,30 +89,34 @@ static bool GitRepoHasMatchingRemote(string possiblePath, string targetUrl)
9489
{
9590
return false;
9691
}
97-
9892
}
9993

94+
10095
public string GetDotGitDirectory()
10196
{
10297
if (IsDynamicGitRepository)
98+
{
10399
return DynamicGitRepositoryPath;
100+
}
104101

105-
return this.fileSystem.TreeWalkForDotGitDir(targetPath);
102+
return Repository.Discover(this.targetPath);
106103
}
107104

105+
108106
public string GetProjectRootDirectory()
109107
{
110108
if (IsDynamicGitRepository)
111109
return this.targetPath;
112110

113-
var gitDir = fileSystem.TreeWalkForDotGitDir(this.targetPath);
111+
var gitDir = Repository.Discover(this.targetPath);
114112

115113
if (String.IsNullOrEmpty(gitDir))
116114
throw new DirectoryNotFoundException("Can't find the .git directory in " + targetPath);
117115

118116
return Directory.GetParent(gitDir).FullName;
119117
}
120118

119+
121120
static string CreateDynamicRepository(string targetPath, Authentication authentication, string repositoryUrl, string targetBranch, bool noFetch)
122121
{
123122
if (string.IsNullOrWhiteSpace(targetBranch))
@@ -143,7 +142,8 @@ static string CreateDynamicRepository(string targetPath, Authentication authenti
143142
return gitDirectory;
144143
}
145144

146-
private static void CloneRepository(string repositoryUrl, string gitDirectory, Authentication authentication)
145+
146+
static void CloneRepository(string repositoryUrl, string gitDirectory, Authentication authentication)
147147
{
148148
Credentials credentials = null;
149149
if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password))
@@ -162,11 +162,11 @@ private static void CloneRepository(string repositoryUrl, string gitDirectory, A
162162
try
163163
{
164164
Repository.Clone(repositoryUrl, gitDirectory,
165-
new CloneOptions
166-
{
167-
Checkout = false,
168-
CredentialsProvider = (url, usernameFromUrl, types) => credentials
169-
});
165+
new CloneOptions
166+
{
167+
Checkout = false,
168+
CredentialsProvider = (url, usernameFromUrl, types) => credentials
169+
});
170170
}
171171
catch (LibGit2SharpException ex)
172172
{
@@ -183,7 +183,7 @@ private static void CloneRepository(string repositoryUrl, string gitDirectory, A
183183
{
184184
throw new Exception("Not found: The repository was not found");
185185
}
186-
186+
187187
throw new Exception("There was an unknown problem with the Git repository you provided");
188188
}
189189
}

src/GitVersionCore/Helpers/FileSystem.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,6 @@ public long GetLastDirectoryWrite(string path)
8080
}
8181

8282

83-
public string TreeWalkForDotGitDir(string directory)
84-
{
85-
var gitDirectory = Repository.Discover(directory);
86-
87-
if (gitDirectory != null)
88-
{
89-
return gitDirectory.TrimEnd(Path.DirectorySeparatorChar);
90-
}
91-
92-
return null;
93-
}
94-
95-
9683
public IRepository GetRepository(string gitDirectory)
9784
{
9885
try

src/GitVersionCore/Helpers/IFileSystem.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public interface IFileSystem
1818
Stream OpenRead(string path);
1919
void CreateDirectory(string path);
2020
long GetLastDirectoryWrite(string path);
21-
string TreeWalkForDotGitDir(string directory);
2221
IRepository GetRepository(string gitDirectory);
2322
}
2423
}

src/GitVersionTask/VersionAndBranchFinder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using GitVersion;
1010
using GitVersion.Helpers;
1111

12+
using LibGit2Sharp;
13+
1214
using YamlDotNet.Serialization;
1315

1416
public static class VersionAndBranchFinder
@@ -34,7 +36,7 @@ public static bool TryGetVersion(string directory, out VersionVariables versionV
3436

3537
public static VersionVariables GetVersion(string directory, Authentication authentication, bool noFetch, IFileSystem fileSystem)
3638
{
37-
var gitDir = fileSystem.TreeWalkForDotGitDir(directory);
39+
var gitDir = Repository.Discover(directory);
3840
using (var repo = fileSystem.GetRepository(gitDir))
3941
{
4042
// Maybe using timestamp in .git/refs directory is enough?

0 commit comments

Comments
 (0)