From fc00f5037f68c3a43b0cb4af95b1ba7aa8c6c0bd Mon Sep 17 00:00:00 2001 From: Phil Asmar Date: Tue, 1 Apr 2025 10:18:28 -0400 Subject: [PATCH] fix: fix error when using --use-version --- .../27ad7e46-2b02-4a45-aa84-7680e019ca91.json | 11 ++++ src/AutoVer/Commands/VersionCommand.cs | 3 +- src/AutoVer/Services/GitHandler.cs | 11 ++++ src/AutoVer/Services/IGitHandler.cs | 1 + .../AutoVer.UnitTests.csproj | 4 +- test/AutoVer.UnitTests/VersionTest.cs | 51 +++++++++++++++++++ 6 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 .autover/changes/27ad7e46-2b02-4a45-aa84-7680e019ca91.json diff --git a/.autover/changes/27ad7e46-2b02-4a45-aa84-7680e019ca91.json b/.autover/changes/27ad7e46-2b02-4a45-aa84-7680e019ca91.json new file mode 100644 index 0000000..4a48548 --- /dev/null +++ b/.autover/changes/27ad7e46-2b02-4a45-aa84-7680e019ca91.json @@ -0,0 +1,11 @@ +{ + "Projects": [ + { + "Name": "AutoVer", + "Type": "Patch", + "ChangelogMessages": [ + "Fixed an issue when --use-version has the same version as the csproj and no version update is made. Previously, this would error out with a non-zero exit code." + ] + } + ] +} \ No newline at end of file diff --git a/src/AutoVer/Commands/VersionCommand.cs b/src/AutoVer/Commands/VersionCommand.cs index 99c090b..7e7097f 100644 --- a/src/AutoVer/Commands/VersionCommand.cs +++ b/src/AutoVer/Commands/VersionCommand.cs @@ -140,7 +140,8 @@ public async Task ExecuteAsync( if (!optionNoCommit) { - gitHandler.CommitChanges(userConfiguration, versionHandler.GetNewReleaseName(userConfiguration)); + if (gitHandler.HasStagedChanges(userConfiguration)) + gitHandler.CommitChanges(userConfiguration, versionHandler.GetNewReleaseName(userConfiguration)); if (!optionNoTag) { diff --git a/src/AutoVer/Services/GitHandler.cs b/src/AutoVer/Services/GitHandler.cs index 2830344..afec3d0 100644 --- a/src/AutoVer/Services/GitHandler.cs +++ b/src/AutoVer/Services/GitHandler.cs @@ -61,6 +61,17 @@ public void StageChanges(UserConfiguration userConfiguration, string currentPath } } + public bool HasStagedChanges(UserConfiguration userConfiguration) + { + using (var gitRepository = new Repository(userConfiguration.GitRoot)) + { + var headTree = gitRepository.Head.Tip.Tree; + var changes = gitRepository.Diff.Compare(headTree, DiffTargets.Index); + + return changes.Count > 0; + } + } + public void CommitChanges(UserConfiguration userConfiguration, string commitMessage) { using var gitRepository = new Repository(userConfiguration.GitRoot); diff --git a/src/AutoVer/Services/IGitHandler.cs b/src/AutoVer/Services/IGitHandler.cs index c7a240d..62571da 100644 --- a/src/AutoVer/Services/IGitHandler.cs +++ b/src/AutoVer/Services/IGitHandler.cs @@ -14,4 +14,5 @@ public interface IGitHandler List GetVersionCommits(UserConfiguration userConfiguration, string? lastVersionTag = null); string GetFileByTag(string gitRoot, string tagName, string filePath); List GetFolderByTag(string gitRoot, string tagName, string folderPath); + bool HasStagedChanges(UserConfiguration userConfiguration); } \ No newline at end of file diff --git a/test/AutoVer.UnitTests/AutoVer.UnitTests.csproj b/test/AutoVer.UnitTests/AutoVer.UnitTests.csproj index e04b29e..81f25b1 100644 --- a/test/AutoVer.UnitTests/AutoVer.UnitTests.csproj +++ b/test/AutoVer.UnitTests/AutoVer.UnitTests.csproj @@ -8,8 +8,8 @@ - - + + diff --git a/test/AutoVer.UnitTests/VersionTest.cs b/test/AutoVer.UnitTests/VersionTest.cs index 30c1763..82cd9c8 100644 --- a/test/AutoVer.UnitTests/VersionTest.cs +++ b/test/AutoVer.UnitTests/VersionTest.cs @@ -71,6 +71,57 @@ public async Task CsProj_UseChangeFiles() await Assert.That(changelog).Contains("Important change"); await Assert.That(GitUtilities.GetLastCommitMessage(tempDir)).IsEqualTo("Updated changelog"); } + + [Test] + public async Task CsProj_UseChangeFiles_VersionNoChangedFiles() + { + string tempDir = TestContext.Current?.ObjectBag["tempDir"]?.ToString() ?? throw new Exception("Temp directory is null"); + + await Assert.That(await IOUtilities.CreateProject(tempDir, "src", "Project1")).IsTrue(); + await IOUtilities.SetProjectVersion(Path.Combine(tempDir, "src", "Project1", "Project1.csproj"), "1.0.0"); + + string autoVerFile = +$@"{{ + ""Projects"": [ + {{ + ""Name"": ""Project1"", + ""Path"": ""src/Project1/Project1.csproj"" + }} + ], + ""UseCommitsForChangelog"": false, + ""UseSameVersionForAllProjects"": false, + ""DefaultIncrementType"": ""Patch"", + ""ChangeFilesDetermineIncrementType"": true +}}"; + + var autoVerFilePath = IOUtilities.AddAutoVerFile(tempDir, autoVerFile); + GitUtilities.StageChanges(tempDir, "*"); + GitUtilities.CommitChanges(tempDir, "Initial Commit"); + + var changeFilePath = await IOUtilities.AddChangeFile("Project1", IncrementType.Patch, "Important change", tempDir); + GitUtilities.StageChanges(tempDir, changeFilePath); + GitUtilities.CommitChanges(tempDir, "First change"); + + var app = AutoVerUtilities.InitializeApp(); + await Assert.That(app).IsNotNull(); + + var versionArgs = new[] { "version", "--project-path", tempDir, "--use-version", "1.0.0" }; + var exitCode = await app!.Run(versionArgs); + + await Assert.That(exitCode).IsEqualTo(0); + await Assert.That(await IOUtilities.GetProjectVersion(Path.Combine(tempDir, "src", "Project1", "Project1.csproj"))).IsEqualTo("1.0.0"); + await Assert.That(GitUtilities.GetLastTag(tempDir)).IsEqualTo($"release_{DateTime.UtcNow:yyyy-MM-dd}"); + + app = AutoVerUtilities.InitializeApp(); + var changelogArgs = new[] { "changelog", "--project-path", tempDir }; + exitCode = await app!.Run(changelogArgs); + + await Assert.That(exitCode).IsEqualTo(0); + var changelog = await IOUtilities.GetChangelog(tempDir); + await Assert.That(changelog).Contains($"Release {DateTime.UtcNow:yyyy-MM-dd}"); + await Assert.That(changelog).Contains("Important change"); + await Assert.That(GitUtilities.GetLastCommitMessage(tempDir)).IsEqualTo("Updated changelog"); + } [Test] public async Task CsProj_DontUseChangeFiles_DefaultIncrement()