Skip to content
Closed
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
30 changes: 30 additions & 0 deletions src/GitVersionCore.Tests/ExecuteCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.IO;
using System.Text;

using LibGit2Sharp;

[TestFixture]
[Parallelizable(ParallelScope.None)]
public class ExecuteCoreTests : TestBase
Expand Down Expand Up @@ -40,6 +42,34 @@ 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(Path.GetTempPath(), "TestRepositories", 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
20 changes: 19 additions & 1 deletion src/GitVersionCore/GitVersionCacheKeyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,30 @@ static string GetGitSystemHash(GitPreparer gitPreparer)
{
var dotGitDirectory = gitPreparer.GetDotGitDirectory();

// get repo .git directory -- not the worktree gitdir
dotGitDirectory = GetWorktreeParentDotGetDirectory(dotGitDirectory);

// traverse the directory and get a list of files, use that for GetHash
var contents = CalculateDirectoryContents(Path.Combine(dotGitDirectory, "refs"));

return GetHash(contents.ToArray());
}

static string GetWorktreeParentDotGetDirectory(string path)
{
if (string.IsNullOrEmpty(path)) return path;

var parentPath = Path.GetDirectoryName(path);
if (string.IsNullOrEmpty(parentPath)) return path;

var worktreesPath = Path.Combine(".git", "worktrees");

if (!parentPath.EndsWith(worktreesPath, StringComparison.Ordinal))
return path;

return parentPath.Substring(0, parentPath.Length - 10);
}

// based on https://msdn.microsoft.com/en-us/library/bb513869.aspx
static List<string> CalculateDirectoryContents(string root)
{
Expand All @@ -42,7 +60,7 @@ static List<string> CalculateDirectoryContents(string root)

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

dirs.Push(root);
Expand Down