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
32 changes: 32 additions & 0 deletions src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using GitTools.Testing;
using GitVersion;
Expand Down Expand Up @@ -512,6 +513,37 @@ public void GetDotGitDirectoryWorktree()
}
}

[Test]
[Category("NoMono")]
[Description("LibGit2Sharp fails when running under Mono")]
public void CalculateVersionFromWorktreeHead()
{
// Setup
using var fixture = new EmptyRepositoryFixture();
var repoDir = new DirectoryInfo(fixture.RepositoryPath);
var worktreePath = Path.Combine(repoDir.Parent.FullName, $"{repoDir.Name}-v1");

fixture.Repository.MakeATaggedCommit("v1.0.0");
var branchV1 = fixture.Repository.CreateBranch("support/1.0");

fixture.Repository.MakeATaggedCommit("v2.0.0");

fixture.Repository.Worktrees.Add(branchV1.CanonicalName, "1.0", worktreePath, false);
using var worktreeFixture = new LocalRepositoryFixture(new Repository(worktreePath));

var gitVersionOptions = new GitVersionOptions { WorkingDirectory = worktreeFixture.RepositoryPath };

var sut = GetGitVersionCalculator(gitVersionOptions);

// Execute
var version = sut.CalculateVersionVariables();

// Verify
version.SemVer.ShouldBe("1.0.0");
var commits = worktreeFixture.Repository.Head.Commits;
version.Sha.ShouldBe(commits.First().Sha);
}

private IGitVersionTool GetGitVersionCalculator(GitVersionOptions gitVersionOptions, ILog logger = null, IRepository repository = null, IFileSystem fs = null)
{
sp = GetServiceProvider(gitVersionOptions, logger, repository, fs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Co

var options = Options.Create(new GitVersionOptions
{
WorkingDirectory = repository.GetRepositoryDirectory(),
WorkingDirectory = repository.Info.WorkingDirectory,
ConfigInfo = { OverrideConfig = configuration },
RepositoryInfo =
{
Expand Down
35 changes: 35 additions & 0 deletions src/GitVersionCore.Tests/IntegrationTests/WorktreeScenarios.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using GitTools.Testing;
using LibGit2Sharp;
using NUnit.Framework;
using System.IO;
using GitVersionCore.Tests.Helpers;

namespace GitVersionCore.Tests.IntegrationTests
{

[TestFixture]
public class WorktreeScenarios : TestBase
{

[Test]
[Category("NoMono")]
[Description("LibGit2Sharp fails here when running under Mono")]
public void UseWorktreeRepositoryForVersion()
{
using var fixture = new EmptyRepositoryFixture();
var repoDir = new DirectoryInfo(fixture.RepositoryPath);
var worktreePath = Path.Combine(repoDir.Parent.FullName, $"{repoDir.Name}-v1");

fixture.Repository.MakeATaggedCommit("v1.0.0");
var branchV1 = fixture.Repository.CreateBranch("support/1.0");

fixture.Repository.MakeATaggedCommit("v2.0.0");
fixture.AssertFullSemver("2.0.0");

fixture.Repository.Worktrees.Add(branchV1.CanonicalName, "1.0", worktreePath, false);
using var worktreeFixture = new LocalRepositoryFixture(new Repository(worktreePath));
worktreeFixture.AssertFullSemver("1.0.0");
}

}
}
3 changes: 1 addition & 2 deletions src/GitVersionCore/Core/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ private string ResolveCurrentBranch()
private void CleanupDuplicateOrigin()
{
var remoteToKeep = DefaultRemoteName;

using var repo = new Repository(options.Value.DotGitDirectory);
using var repo = new Repository(options.Value.GitRootPath);

// check that we have a remote that matches defaultRemoteName if not take the first remote
if (!repo.Network.Remotes.Any(remote => remote.Name.Equals(DefaultRemoteName, StringComparison.InvariantCultureIgnoreCase)))
Expand Down
6 changes: 3 additions & 3 deletions src/GitVersionCore/Core/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class GitRepository : IGitRepository
private IRepository repositoryInstance => repositoryLazy.Value;

public GitRepository(IOptions<GitVersionOptions> options)
: this(() => options.Value.DotGitDirectory)
: this(() => options.Value.GitRootPath)
{
}

public GitRepository(Func<string> getDotGitDirectory)
public GitRepository(Func<string> getGitRootDirectory)
{
repositoryLazy = new Lazy<IRepository>(() => new Repository(getDotGitDirectory()));
repositoryLazy = new Lazy<IRepository>(() => new Repository(getGitRootDirectory()));
Commands = new GitRepositoryCommands(repositoryLazy);
}

Expand Down
6 changes: 4 additions & 2 deletions src/GitVersionCore/Core/RepositoryMetadataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,12 @@ public Branch GetTargetBranch(string targetBranch)
{
// In the case where HEAD is not the desired branch, try to find the branch with matching name
desiredBranch = repository.Branches?
.SingleOrDefault(b =>
.Where(b =>
b.CanonicalName.IsEquivalentTo(targetBranch) ||
b.FriendlyName.IsEquivalentTo(targetBranch) ||
b.NameWithoutRemote().IsEquivalentTo(targetBranch));
b.NameWithoutRemote().IsEquivalentTo(targetBranch))
.OrderBy(b => b.IsRemote)
.FirstOrDefault();

// Failsafe in case the specified branch is invalid
desiredBranch ??= repository.Head;
Expand Down
8 changes: 8 additions & 0 deletions src/GitVersionCore/Extensions/GitVersionOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public static string GetProjectRootDirectory(this GitVersionOptions gitVersionOp
return repository.Info.WorkingDirectory;
}

public static string GetGitRootPath(this GitVersionOptions options)
{
var isDynamicRepo = !string.IsNullOrWhiteSpace(options.DynamicGitRepositoryPath);
var rootDirectory = isDynamicRepo ? options.DotGitDirectory : options.ProjectRootDirectory;

return rootDirectory;
}

public static string GetDynamicGitRepositoryPath(this GitVersionOptions gitVersionOptions)
{
if (string.IsNullOrWhiteSpace(gitVersionOptions.RepositoryInfo.TargetUrl)) return null;
Expand Down
3 changes: 3 additions & 0 deletions src/GitVersionCore/Model/GitVersionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ public class GitVersionOptions
private Lazy<string> dotGitDirectory;
private Lazy<string> projectRootDirectory;
private Lazy<string> dynamicGitRepositoryPath;
private Lazy<string> gitRootPath;

public GitVersionOptions()
{
dotGitDirectory = new Lazy<string>(this.GetDotGitDirectory);
projectRootDirectory = new Lazy<string>(this.GetProjectRootDirectory);
dynamicGitRepositoryPath = new Lazy<string>(this.GetDynamicGitRepositoryPath);
gitRootPath = new Lazy<string>(this.GetGitRootPath);
}

public string WorkingDirectory { get; set; }

public string DotGitDirectory => dotGitDirectory.Value;
public string ProjectRootDirectory => projectRootDirectory.Value;
public string DynamicGitRepositoryPath => dynamicGitRepositoryPath.Value;
public string GitRootPath => gitRootPath.Value;

public AssemblyInfoData AssemblyInfo { get; } = new AssemblyInfoData();
public AuthenticationInfo Authentication { get; } = new AuthenticationInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private List<string> CalculateDirectoryContents(string root)

private string GetRepositorySnapshotHash()
{
using var repo = new Repository(options.Value.DotGitDirectory);
using var repo = new Repository(options.Value.GitRootPath);

var head = repo.Head;
if (head.Tip == null)
Expand Down