From f8c6a6c9e2f05093dcc0c6fac0229e43db315912 Mon Sep 17 00:00:00 2001 From: Chris Huseman Date: Wed, 9 Oct 2019 08:31:35 -0500 Subject: [PATCH 1/4] Take the first matching branch, preferring local If you've got a local branch and remote branch with the same name, SingleOrDefault() throws. --- src/GitVersionCore/GitVersionContext.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/GitVersionCore/GitVersionContext.cs b/src/GitVersionCore/GitVersionContext.cs index 364ed7bf8f..caf27c7f83 100644 --- a/src/GitVersionCore/GitVersionContext.cs +++ b/src/GitVersionCore/GitVersionContext.cs @@ -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; From 61af5c8b195ae8132e1c18f118c827ca88ec972b Mon Sep 17 00:00:00 2001 From: Chris Huseman Date: Thu, 21 Nov 2019 10:37:09 -0600 Subject: [PATCH 2/4] Add a worktree repository test --- .../IntegrationTests/WorktreeScenarios.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/GitVersionCore.Tests/IntegrationTests/WorktreeScenarios.cs diff --git a/src/GitVersionCore.Tests/IntegrationTests/WorktreeScenarios.cs b/src/GitVersionCore.Tests/IntegrationTests/WorktreeScenarios.cs new file mode 100644 index 0000000000..2e61926fc0 --- /dev/null +++ b/src/GitVersionCore.Tests/IntegrationTests/WorktreeScenarios.cs @@ -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"); + } + + } +} From abe70f8d8fc6adc7fea7ce0116d9343d0b8a1ca1 Mon Sep 17 00:00:00 2001 From: Chris Huseman Date: Thu, 21 Nov 2019 10:37:42 -0600 Subject: [PATCH 3/4] Add a failing worktree version calculation test --- .../GitVersionExecutorTests.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/GitVersionCore.Tests/GitVersionExecutorTests.cs b/src/GitVersionCore.Tests/GitVersionExecutorTests.cs index 4fdffc44b5..c767900bcc 100644 --- a/src/GitVersionCore.Tests/GitVersionExecutorTests.cs +++ b/src/GitVersionCore.Tests/GitVersionExecutorTests.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using System.Text; using GitTools.Testing; using GitVersion; @@ -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 fixtureAction = null) { // Make sure GitVersion doesn't trigger build server mode when we are running the tests From c908d6ae51b331da2f4aa387ff0fbc440b2de67a Mon Sep 17 00:00:00 2001 From: Chris Huseman Date: Thu, 21 Nov 2019 10:43:20 -0600 Subject: [PATCH 4/4] Use the project root for the current repository GetDotGitDirectory() returns the base repository .git, not the the worktree's .git, so HEAD was wrong. For dynamic repositories, there isn't a .git at all so it still needs to use the discovered .git directory. --- src/GitVersionCore/GitPreparer.cs | 2 +- src/GitVersionCore/GitVersionCalculator.cs | 6 +++++- src/GitVersionCore/IGitPreparer.cs | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/GitVersionCore/GitPreparer.cs b/src/GitVersionCore/GitPreparer.cs index c25128b6f7..a2064ea165 100644 --- a/src/GitVersionCore/GitPreparer.cs +++ b/src/GitVersionCore/GitPreparer.cs @@ -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 options) diff --git a/src/GitVersionCore/GitVersionCalculator.cs b/src/GitVersionCore/GitVersionCalculator.cs index 8e2f12866c..e65b078ce9 100644 --- a/src/GitVersionCore/GitVersionCalculator.cs +++ b/src/GitVersionCore/GitVersionCalculator.cs @@ -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); diff --git a/src/GitVersionCore/IGitPreparer.cs b/src/GitVersionCore/IGitPreparer.cs index 30db2be4f8..c3daacbd44 100644 --- a/src/GitVersionCore/IGitPreparer.cs +++ b/src/GitVersionCore/IGitPreparer.cs @@ -7,5 +7,6 @@ public interface IGitPreparer string GetDotGitDirectory(); string GetTargetUrl(); string GetWorkingDirectory(); + bool IsDynamicGitRepository { get; } } }