Skip to content

Commit ddb7c6e

Browse files
committed
Properly get project root directory
This returns the correct workdir when using worktrees where the git repo resides inside of the main working tree. Partial fix for #1063.
1 parent a55a4b7 commit ddb7c6e

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/GitVersionCore.Tests/ExecuteCoreTests.cs

Lines changed: 28 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)]
@@ -266,6 +267,33 @@ public void WorkingDirectoryWithoutGit()
266267
});
267268
}
268269

270+
[Test]
271+
[Category("NoMono")]
272+
[Description("LibGit2Sharp fails when running under Mono")]
273+
public void WorkingDirectoryWithWorktree()
274+
{
275+
var versionAndBranchFinder = new ExecuteCore(fileSystem);
276+
277+
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
278+
{
279+
var worktreePath = Path.Combine(Directory.GetParent(fixture.RepositoryPath).FullName, Guid.NewGuid().ToString());
280+
try
281+
{
282+
// create a branch and a new worktree for it
283+
var repo = new Repository(fixture.RepositoryPath);
284+
repo.Worktrees.Add("worktree", worktreePath, false);
285+
286+
var targetUrl = "https://github.com/GitTools/GitVersion.git";
287+
var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, worktreePath);
288+
gitPreparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(worktreePath);
289+
}
290+
finally
291+
{
292+
DirectoryHelper.DeleteDirectory(worktreePath);
293+
}
294+
});
295+
}
296+
269297
[Test]
270298
public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
271299
{

src/GitVersionCore/GitPreparer.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,12 @@ public string GetProjectRootDirectory()
160160
}
161161

162162
var dotGetGitDirectory = GetDotGitDirectory();
163-
var result = Directory.GetParent(dotGetGitDirectory).FullName;
164-
Logger.WriteInfo($"Returning Project Root from DotGitDirectory: {dotGetGitDirectory} - {result}");
165-
return result;
163+
using (var repo = new Repository(dotGetGitDirectory))
164+
{
165+
var result = repo.Info.WorkingDirectory;
166+
Logger.WriteInfo($"Returning Project Root from DotGitDirectory: {dotGetGitDirectory} - {result}");
167+
return result;
168+
}
166169
}
167170

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

0 commit comments

Comments
 (0)