Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/GitVersionCore.Tests/ExecuteCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.IO;
using System.Text;
using LibGit2Sharp;

[TestFixture]
[Parallelizable(ParallelScope.None)]
Expand Down Expand Up @@ -40,6 +41,35 @@ public void CacheKeySameAfterReNormalizing()
});
}

[Test]
[Category("NoMono")]
[Description("LibGit2Sharp fails here when running under Mono")]
public void CacheKeyForWorktree()
{
var versionAndBranchFinder = new ExecuteCore(fileSystem);

RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
{
var worktreePath = Path.Combine(Directory.GetParent(fixture.RepositoryPath).FullName, Guid.NewGuid().ToString());
try
{
// create a branch and a new worktree for it
var repo = new Repository(fixture.RepositoryPath);
repo.Worktrees.Add("worktree", worktreePath, false);

var targetUrl = "https://github.com/GitTools/GitVersion.git";
var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, worktreePath);
var configFileLocator = new DefaultConfigFileLocator();
var cacheKey = GitVersionCacheKeyFactory.Create(fileSystem, gitPreparer, null, configFileLocator);
cacheKey.Value.ShouldNotBeEmpty();
}
finally
{
DirectoryHelper.DeleteDirectory(worktreePath);
}
});
}

[Test]
public void CacheFileExistsOnDisk()
{
Expand Down Expand Up @@ -266,6 +296,47 @@ public void WorkingDirectoryWithoutGit()
});
}

[Test]
[Category("NoMono")]
[Description("LibGit2Sharp fails when running under Mono")]
public void GetProjectRootDirectory_WorkingDirectoryWithWorktree()
{
var versionAndBranchFinder = new ExecuteCore(fileSystem);

RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
{
var worktreePath = Path.Combine(Directory.GetParent(fixture.RepositoryPath).FullName, Guid.NewGuid().ToString());
try
{
// create a branch and a new worktree for it
var repo = new Repository(fixture.RepositoryPath);
repo.Worktrees.Add("worktree", worktreePath, false);

var targetUrl = "https://github.com/GitTools/GitVersion.git";
var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, worktreePath);
gitPreparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(worktreePath);
}
finally
{
DirectoryHelper.DeleteDirectory(worktreePath);
}
});
}

[Test]
public void GetProjectRootDirectory_NoWorktree()
{
var versionAndBranchFinder = new ExecuteCore(fileSystem);

RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
{
var targetUrl = "https://github.com/GitTools/GitVersion.git";
var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, fixture.RepositoryPath);
var expectedPath = fixture.RepositoryPath.TrimEnd('/', '\\');
gitPreparer.GetProjectRootDirectory().TrimEnd('/', '\\').ShouldBe(expectedPath);
});
}

[Test]
public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
{
Expand All @@ -277,6 +348,48 @@ public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
});
}

[Test]
public void GetDotGitDirectory_NoWorktree()
{
var versionAndBranchFinder = new ExecuteCore(fileSystem);

RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
{
var targetUrl = "https://github.com/GitTools/GitVersion.git";
var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, fixture.RepositoryPath);
var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
gitPreparer.GetDotGitDirectory().ShouldBe(expectedPath);
});
}

[Test]
[Category("NoMono")]
[Description("LibGit2Sharp fails when running under Mono")]
public void GetDotGitDirectory_Worktree()
{
var versionAndBranchFinder = new ExecuteCore(fileSystem);

RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
{
var worktreePath = Path.Combine(Directory.GetParent(fixture.RepositoryPath).FullName, Guid.NewGuid().ToString());
try
{
// create a branch and a new worktree for it
var repo = new Repository(fixture.RepositoryPath);
repo.Worktrees.Add("worktree", worktreePath, false);

var targetUrl = "https://github.com/GitTools/GitVersion.git";
var gitPreparer = new GitPreparer(targetUrl, null, new Authentication(), false, worktreePath);
var expectedPath = Path.Combine(fixture.RepositoryPath, ".git");
gitPreparer.GetDotGitDirectory().ShouldBe(expectedPath);
}
finally
{
DirectoryHelper.DeleteDirectory(worktreePath);
}
});
}

LogMessages RepositoryScope(ExecuteCore executeCore = null, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
{
// Make sure GitVersion doesn't trigger build server mode when we are running the tests
Expand Down
26 changes: 15 additions & 11 deletions src/GitVersionCore/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,14 @@ static bool GitRepoHasMatchingRemote(string possiblePath, string targetUrl)

public string GetDotGitDirectory()
{
if (IsDynamicGitRepository)
return DynamicGitRepositoryPath;

var dotGitDirectory = Repository.Discover(targetPath);
var dotGitDirectory = IsDynamicGitRepository ? DynamicGitRepositoryPath : Repository.Discover(targetPath);

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

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

return dotGitDirectory;
}
Expand All @@ -159,10 +156,17 @@ public string GetProjectRootDirectory()
return targetPath;
}

var dotGetGitDirectory = GetDotGitDirectory();
var result = Directory.GetParent(dotGetGitDirectory).FullName;
Logger.WriteInfo($"Returning Project Root from DotGitDirectory: {dotGetGitDirectory} - {result}");
return result;
var dotGitDirectory = Repository.Discover(targetPath);

if (string.IsNullOrEmpty(dotGitDirectory))
throw new DirectoryNotFoundException($"Can't find the .git directory in {targetPath}");

using (var repo = new Repository(dotGitDirectory))
{
var result = repo.Info.WorkingDirectory;
Logger.WriteInfo($"Returning Project Root from DotGitDirectory: {dotGitDirectory} - {result}");
return result;
}
}

static string CreateDynamicRepository(string targetPath, AuthenticationInfo authentication, string repositoryUrl, string targetBranch, bool noFetch)
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersionCore/GitVersionCacheKeyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static List<string> CalculateDirectoryContents(string root)

if (!Directory.Exists(root))
{
throw new ArgumentException();
throw new DirectoryNotFoundException($"Root directory does not exist: {root}");
}

dirs.Push(root);
Expand Down