From 4b82d4e636347dd8b10a9a49cbb7bceb269c5b22 Mon Sep 17 00:00:00 2001 From: Sean Fausett Date: Fri, 22 May 2020 10:01:15 +1200 Subject: [PATCH 1/3] Check early for commit on current branch --- .../BaseVersionCalculators/FallbackVersionStrategy.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/GitVersionCore/VersionCalculation/BaseVersionCalculators/FallbackVersionStrategy.cs b/src/GitVersionCore/VersionCalculation/BaseVersionCalculators/FallbackVersionStrategy.cs index 22cd0df014..4e9a6a7b9a 100644 --- a/src/GitVersionCore/VersionCalculation/BaseVersionCalculators/FallbackVersionStrategy.cs +++ b/src/GitVersionCore/VersionCalculation/BaseVersionCalculators/FallbackVersionStrategy.cs @@ -22,6 +22,10 @@ public override IEnumerable GetVersions() { Commit baseVersionSource; var currentBranchTip = Context.CurrentBranch.Tip; + if (currentBranchTip == null) + { + throw new GitVersionException("No commits found on the current branch."); + } try { From b1fcbc4ae8799690f448dd9c9f36716c8ddfad73 Mon Sep 17 00:00:00 2001 From: Sean Fausett Date: Fri, 22 May 2020 10:33:10 +1200 Subject: [PATCH 2/3] Add tests for commit on current branch --- .../Core/GitVersionExecutorTests.cs | 15 +++++++++++++++ .../ExecCmdLineArgumentTest.cs | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs b/src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs index affd588844..5a82188489 100644 --- a/src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs +++ b/src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs @@ -394,6 +394,21 @@ public void WorkingDirectoryWithoutGit() exception.Message.ShouldContain("Can't find the .git directory in"); } + [Test] + public void WorkingDirectoryWithoutCommits() + { + using var fixture = new EmptyRepositoryFixture(); + + var gitVersionOptions = new GitVersionOptions { WorkingDirectory = fixture.RepositoryPath }; + + var exception = Assert.Throws(() => + { + var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions); + gitVersionCalculator.CalculateVersionVariables(); + }); + exception.Message.ShouldContain("No commits found on the current branch."); + } + [Test] [Category("NoMono")] [Description("LibGit2Sharp fails when running under Mono")] diff --git a/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs b/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs index b82b186556..3ec3da3f2e 100644 --- a/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs +++ b/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs @@ -80,6 +80,17 @@ public void WorkingDirectoryWithoutGitFolderFailsWithInformativeMessage() result.Output.ShouldContain("Can't find the .git directory in"); } + [Test] + public void WorkingDirectoryWithoutCommitsFailsWithInformativeMessage() + { + using var fixture = new EmptyRepositoryFixture(); + + var result = GitVersionHelper.ExecuteIn(fixture.RepositoryPath, arguments: null, logToFile: false); + + result.ExitCode.ShouldNotBe(0); + result.Output.ShouldContain("No commits found on the current branch."); + } + [Test] public void WorkingDirectoryDoesNotExistFailsWithInformativeMessage() { From 3b67d8190b02e1c74182df882355236ad295bd3b Mon Sep 17 00:00:00 2001 From: Sean Fausett Date: Fri, 22 May 2020 11:27:59 +1200 Subject: [PATCH 3/3] Cosmetic tweaks to improve error messages --- src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs | 2 +- src/GitVersionCore/Extensions/GitVersionOptionsExtensions.cs | 4 ++-- .../BaseVersionCalculators/FallbackVersionStrategy.cs | 2 +- src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs b/src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs index 5a82188489..7a8a21077e 100644 --- a/src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs +++ b/src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs @@ -391,7 +391,7 @@ public void WorkingDirectoryWithoutGit() var gitVersionCalculator = GetGitVersionCalculator(gitVersionOptions); gitVersionCalculator.CalculateVersionVariables(); }); - exception.Message.ShouldContain("Can't find the .git directory in"); + exception.Message.ShouldContain("Cannot find the .git directory"); } [Test] diff --git a/src/GitVersionCore/Extensions/GitVersionOptionsExtensions.cs b/src/GitVersionCore/Extensions/GitVersionOptionsExtensions.cs index 3be5854eef..25e7c8425a 100644 --- a/src/GitVersionCore/Extensions/GitVersionOptionsExtensions.cs +++ b/src/GitVersionCore/Extensions/GitVersionOptionsExtensions.cs @@ -15,7 +15,7 @@ public static string GetDotGitDirectory(this GitVersionOptions gitVersionOptions dotGitDirectory = dotGitDirectory?.TrimEnd('/', '\\'); if (string.IsNullOrEmpty(dotGitDirectory)) - throw new DirectoryNotFoundException($"Can't find the .git directory in {dotGitDirectory}"); + throw new DirectoryNotFoundException("Cannot find the .git directory"); return dotGitDirectory.Contains(Path.Combine(".git", "worktrees")) ? Directory.GetParent(Directory.GetParent(dotGitDirectory).FullName).FullName @@ -32,7 +32,7 @@ public static string GetProjectRootDirectory(this GitVersionOptions gitVersionOp var dotGitDirectory = Repository.Discover(gitVersionOptions.WorkingDirectory); if (string.IsNullOrEmpty(dotGitDirectory)) - throw new DirectoryNotFoundException($"Can't find the .git directory in {dotGitDirectory}"); + throw new DirectoryNotFoundException("Cannot find the .git directory"); using var repository = new Repository(dotGitDirectory); return repository.Info.WorkingDirectory; diff --git a/src/GitVersionCore/VersionCalculation/BaseVersionCalculators/FallbackVersionStrategy.cs b/src/GitVersionCore/VersionCalculation/BaseVersionCalculators/FallbackVersionStrategy.cs index 4e9a6a7b9a..8f97d37fdb 100644 --- a/src/GitVersionCore/VersionCalculation/BaseVersionCalculators/FallbackVersionStrategy.cs +++ b/src/GitVersionCore/VersionCalculation/BaseVersionCalculators/FallbackVersionStrategy.cs @@ -33,7 +33,7 @@ public override IEnumerable GetVersions() } catch (NotFoundException exception) { - throw new GitVersionException($"Can't find commit {currentBranchTip.Sha}. Please ensure that the repository is an unshallow clone with `git fetch --unshallow`.", exception); + throw new GitVersionException($"Cannot find commit {currentBranchTip.Sha}. Please ensure that the repository is an unshallow clone with `git fetch --unshallow`.", exception); } yield return new BaseVersion("Fallback base version", false, new SemanticVersion(minor: 1), baseVersionSource, null); diff --git a/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs b/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs index 3ec3da3f2e..1faf77a444 100644 --- a/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs +++ b/src/GitVersionExe.Tests/ExecCmdLineArgumentTest.cs @@ -77,7 +77,7 @@ public void WorkingDirectoryWithoutGitFolderFailsWithInformativeMessage() var result = GitVersionHelper.ExecuteIn(Environment.SystemDirectory, arguments: null, logToFile: false); result.ExitCode.ShouldNotBe(0); - result.Output.ShouldContain("Can't find the .git directory in"); + result.Output.ShouldContain("Cannot find the .git directory"); } [Test]