Skip to content

Commit ea2a10c

Browse files
authored
Merge pull request #1793 from ermshiperete/ImproveWorktree
Fix several issues related to worktree
2 parents a55a4b7 + 5595ca5 commit ea2a10c

File tree

3 files changed

+129
-12
lines changed

3 files changed

+129
-12
lines changed

src/GitVersionCore.Tests/ExecuteCoreTests.cs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System;
88
using System.IO;
99
using System.Text;
10+
using LibGit2Sharp;
1011

1112
[TestFixture]
1213
[Parallelizable(ParallelScope.None)]
@@ -40,6 +41,35 @@ public void CacheKeySameAfterReNormalizing()
4041
});
4142
}
4243

44+
[Test]
45+
[Category("NoMono")]
46+
[Description("LibGit2Sharp fails here when running under Mono")]
47+
public void CacheKeyForWorktree()
48+
{
49+
var versionAndBranchFinder = new ExecuteCore(fileSystem);
50+
51+
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
52+
{
53+
var worktreePath = Path.Combine(Directory.GetParent(fixture.RepositoryPath).FullName, Guid.NewGuid().ToString());
54+
try
55+
{
56+
// create a branch and a new worktree for it
57+
var repo = new Repository(fixture.RepositoryPath);
58+
repo.Worktrees.Add("worktree", worktreePath, false);
59+
60+
var targetUrl = "https://github.com/GitTools/GitVersion.git";
61+
var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, worktreePath);
62+
var configFileLocator = new DefaultConfigFileLocator();
63+
var cacheKey = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, null, configFileLocator);
64+
cacheKey.Value.ShouldNotBeEmpty();
65+
}
66+
finally
67+
{
68+
DirectoryHelper.DeleteDirectory(worktreePath);
69+
}
70+
});
71+
}
72+
4373
[Test]
4474
public void CacheFileExistsOnDisk()
4575
{
@@ -266,6 +296,47 @@ public void WorkingDirectoryWithoutGit()
266296
});
267297
}
268298

299+
[Test]
300+
[Category("NoMono")]
301+
[Description("LibGit2Sharp fails when running under Mono")]
302+
public void GetProjectRootDirectory_WorkingDirectoryWithWorktree()
303+
{
304+
var versionAndBranchFinder = new ExecuteCore(fileSystem);
305+
306+
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
307+
{
308+
var worktreePath = Path.Combine(Directory.GetParent(fixture.RepositoryPath).FullName, Guid.NewGuid().ToString());
309+
try
310+
{
311+
// create a branch and a new worktree for it
312+
var repo = new Repository(fixture.RepositoryPath);
313+
repo.Worktrees.Add("worktree", worktreePath, false);
314+
315+
var targetUrl = "https://github.com/GitTools/GitVersion.git";
316+
var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, worktreePath);
317+
gitPreparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(worktreePath);
318+
}
319+
finally
320+
{
321+
DirectoryHelper.DeleteDirectory(worktreePath);
322+
}
323+
});
324+
}
325+
326+
[Test]
327+
public void GetProjectRootDirectory_NoWorktree()
328+
{
329+
var versionAndBranchFinder = new ExecuteCore(fileSystem);
330+
331+
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
332+
{
333+
var targetUrl = "https://github.com/GitTools/GitVersion.git";
334+
var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, fixture.RepositoryPath);
335+
var expectedPath = fixture.RepositoryPath.TrimEnd('/', '\\');
336+
gitPreparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(expectedPath);
337+
});
338+
}
339+
269340
[Test]
270341
public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
271342
{
@@ -277,6 +348,48 @@ public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
277348
});
278349
}
279350

351+
[Test]
352+
public void GetDotGitDirectory_NoWorktree()
353+
{
354+
var versionAndBranchFinder = new ExecuteCore(fileSystem);
355+
356+
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
357+
{
358+
var targetUrl = "https://github.com/GitTools/GitVersion.git";
359+
var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, fixture.RepositoryPath);
360+
var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
361+
gitPreparer.GetDotGitDirectory().ShouldBe(expectedPath);
362+
});
363+
}
364+
365+
[Test]
366+
[Category("NoMono")]
367+
[Description("LibGit2Sharp fails when running under Mono")]
368+
public void GetDotGitDirectory_Worktree()
369+
{
370+
var versionAndBranchFinder = new ExecuteCore(fileSystem);
371+
372+
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
373+
{
374+
var worktreePath = Path.Combine(Directory.GetParent(fixture.RepositoryPath).FullName, Guid.NewGuid().ToString());
375+
try
376+
{
377+
// create a branch and a new worktree for it
378+
var repo = new Repository(fixture.RepositoryPath);
379+
repo.Worktrees.Add("worktree", worktreePath, false);
380+
381+
var targetUrl = "https://github.com/GitTools/GitVersion.git";
382+
var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, worktreePath);
383+
var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
384+
gitPreparer.GetDotGitDirectory().ShouldBe(expectedPath);
385+
}
386+
finally
387+
{
388+
DirectoryHelper.DeleteDirectory(worktreePath);
389+
}
390+
});
391+
}
392+
280393
LogMessages RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
281394
{
282395
// Make sure GitVersion doesn't trigger build server mode when we are running the tests

src/GitVersionCore/GitPreparer.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,14 @@ static bool GitRepoHasMatchingRemote(string possiblePath, string targetUrl)
135135

136136
public string GetDotGitDirectory()
137137
{
138-
if (IsDynamicGitRepository)
139-
return DynamicGitRepositoryPath;
140-
141-
var dotGitDirectory = Repository.Discover(targetPath);
138+
var dotGitDirectory = IsDynamicGitRepository ? DynamicGitRepositoryPath : Repository.Discover(targetPath);
142139

140+
dotGitDirectory = dotGitDirectory?.TrimEnd('/', '\\');
143141
if (string.IsNullOrEmpty(dotGitDirectory))
144142
throw new DirectoryNotFoundException("Can't find the .git directory in " + targetPath);
145143

146-
dotGitDirectory = dotGitDirectory.TrimEnd('/', '\\');
147-
if (string.IsNullOrEmpty(dotGitDirectory))
148-
throw new DirectoryNotFoundException("Can't find the .git directory in " + targetPath);
144+
if (dotGitDirectory.Contains(Path.Combine(".git", "worktrees")))
145+
return Directory.GetParent(Directory.GetParent(dotGitDirectory).FullName).FullName;
149146

150147
return dotGitDirectory;
151148
}
@@ -159,10 +156,17 @@ public string GetProjectRootDirectory()
159156
return targetPath;
160157
}
161158

162-
var dotGetGitDirectory = GetDotGitDirectory();
163-
var result = Directory.GetParent(dotGetGitDirectory).FullName;
164-
Logger.WriteInfo($"Returning Project Root from DotGitDirectory: {dotGetGitDirectory} - {result}");
165-
return result;
159+
var dotGitDirectory = Repository.Discover(targetPath);
160+
161+
if (string.IsNullOrEmpty(dotGitDirectory))
162+
throw new DirectoryNotFoundException($"Can't find the .git directory in {targetPath}");
163+
164+
using (var repo = new Repository(dotGitDirectory))
165+
{
166+
var result = repo.Info.WorkingDirectory;
167+
Logger.WriteInfo($"Returning Project Root from DotGitDirectory: {dotGitDirectory} - {result}");
168+
return result;
169+
}
166170
}
167171

168172
static string CreateDynamicRepository(string targetPath, AuthenticationInfo authentication, string repositoryUrl, string targetBranch, bool noFetch)

src/GitVersionCore/GitVersionCacheKeyFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static List<string> CalculateDirectoryContents(string root)
4242

4343
if (!Directory.Exists(root))
4444
{
45-
throw new ArgumentException();
45+
throw new DirectoryNotFoundException($"Root directory does not exist: {root}");
4646
}
4747

4848
dirs.Push(root);

0 commit comments

Comments
 (0)