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
28 changes: 28 additions & 0 deletions src/GitVersionCore.Tests/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 @@ -507,6 +508,33 @@ public void GetDotGitDirectoryWorktree()
});
}

[Test]
[Category("NoMono")]
[Description("LibGit2Sharp fails when running under Mono")]
public void CalculateVersionFromWorktreeHead()
{
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 arguments = new Arguments { TargetPath = worktreePath };

var calculator = GetGitVersionCalculator(arguments);
var version = calculator.CalculateVersionVariables();

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

private void RepositoryScope(Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
{
// Make sure GitVersion doesn't trigger build server mode when we are running the tests
Expand Down
32 changes: 32 additions & 0 deletions src/GitVersionCore.Tests/IntegrationTests/WorktreeScenarios.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using GitTools.Testing;
using LibGit2Sharp;
using NUnit.Framework;
using System.IO;

namespace GitVersionCore.Tests.IntegrationTests
{

[TestFixture]
public class WorktreeScenarios : TestBase
{

[Test]
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");
}

}
}
2 changes: 1 addition & 1 deletion src/GitVersionCore/GitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class GitPreparer : IGitPreparer

public string GetProjectRootDirectory() => projectRootDirectory ??= GetProjectRootDirectoryInternal();

private bool IsDynamicGitRepository => !string.IsNullOrWhiteSpace(DynamicGitRepositoryPath);
public bool IsDynamicGitRepository => !string.IsNullOrWhiteSpace(DynamicGitRepositoryPath);
private string DynamicGitRepositoryPath;

public GitPreparer(ILog log, IEnvironment environment, IOptions<Arguments> options)
Expand Down
6 changes: 5 additions & 1 deletion src/GitVersionCore/GitVersionCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ private VersionVariables ExecuteInternal(string targetBranch, string commitId, C
{
var configuration = configProvider.Provide(overrideConfig: overrideConfig);

return gitPreparer.GetDotGitDirectory().WithRepository(repo =>
var path = gitPreparer.IsDynamicGitRepository
? gitPreparer.GetDotGitDirectory()
: gitPreparer.GetProjectRootDirectory();

return path.WithRepository(repo =>
{
var gitVersionContext = new GitVersionContext(repo, log, targetBranch, configuration, commitId: commitId);
var semanticVersion = gitVersionFinder.FindVersion(gitVersionContext);
Expand Down
6 changes: 4 additions & 2 deletions src/GitVersionCore/GitVersionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,12 @@ private static Branch GetTargetBranch(IRepository repository, string targetBranc
{
// 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 == targetBranch ||
b.FriendlyName == targetBranch ||
b.NameWithoutRemote() == targetBranch);
b.NameWithoutRemote() == targetBranch)
.OrderBy(b => b.IsRemote)
.FirstOrDefault();

// Failsafe in case the specified branch is invalid
desiredBranch ??= repository.Head;
Expand Down
1 change: 1 addition & 0 deletions src/GitVersionCore/IGitPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public interface IGitPreparer
string GetDotGitDirectory();
string GetTargetUrl();
string GetWorkingDirectory();
bool IsDynamicGitRepository { get; }
}
}