Skip to content

Commit 02f992d

Browse files
committed
touch-ups
1 parent 28d04ed commit 02f992d

File tree

2 files changed

+51
-49
lines changed

2 files changed

+51
-49
lines changed

src/NerdBank.GitVersioning/ReleaseManager.cs

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public enum ReleaseManagerOutputMode
104104
Json = 1,
105105
}
106106

107+
public void WriteToOutput(ReleaseInfo releaseInfo)
108+
{
109+
string json = JsonConvert.SerializeObject(releaseInfo, Formatting.Indented, new SemanticVersionJsonConverter());
110+
this.stdout.WriteLine(json);
111+
}
112+
107113
/// <summary>
108114
/// Prepares a release for the specified directory by creating a release branch and incrementing the version in the current branch.
109115
/// </summary>
@@ -142,6 +148,24 @@ public ReleaseInfo PrepareRelease(string projectDirectory, string releaseUnstabl
142148
return this.PrepareReleaseCore(projectDirectory, releaseUnstableTag, nextVersion, versionIncrement, outputMode, unformattedCommitMessage, whatIf);
143149
}
144150

151+
private static bool IsVersionDecrement(SemanticVersion oldVersion, SemanticVersion newVersion)
152+
{
153+
if (newVersion.Version > oldVersion.Version)
154+
{
155+
return false;
156+
}
157+
else if (newVersion.Version == oldVersion.Version)
158+
{
159+
return string.IsNullOrEmpty(oldVersion.Prerelease) &&
160+
!string.IsNullOrEmpty(newVersion.Prerelease);
161+
}
162+
else
163+
{
164+
// newVersion.Version < oldVersion.Version
165+
return true;
166+
}
167+
}
168+
145169
/// <summary>
146170
/// Core implementation of prepare-release functionality that can either simulate or execute the operation.
147171
/// </summary>
@@ -252,18 +276,10 @@ private ReleaseInfo PrepareReleaseCore(string projectDirectory, string releaseUn
252276
throw new ReleasePreparationException(ReleasePreparationError.BranchAlreadyExists);
253277
}
254278

255-
if (outputMode == ReleaseManagerOutputMode.Text)
279+
if (outputMode == ReleaseManagerOutputMode.Text && whatIf)
256280
{
257-
if (whatIf)
258-
{
259-
this.stdout.WriteLine($"What-if: {releaseBranchName} branch would track v{releaseVersion} stabilization and release.");
260-
this.stdout.WriteLine($"What-if: {originalBranchName} branch would track v{nextDevVersion} development.");
261-
}
262-
else
263-
{
264-
this.stdout.WriteLine($"{releaseBranchName} branch now tracks v{releaseVersion} stabilization and release.");
265-
this.stdout.WriteLine($"{originalBranchName} branch now tracks v{nextDevVersion} development.");
266-
}
281+
this.stdout.WriteLine($"What-if: {releaseBranchName} branch would track v{releaseVersion} stabilization and release.");
282+
this.stdout.WriteLine($"What-if: {originalBranchName} branch would track v{nextDevVersion} development.");
267283
}
268284

269285
var originalBranchInfo = new ReleaseBranchInfo(originalBranchName, repository.Head.Tip.Sha, nextDevVersion);
@@ -280,10 +296,20 @@ private ReleaseInfo PrepareReleaseCore(string projectDirectory, string releaseUn
280296
global::LibGit2Sharp.Commands.Checkout(repository, releaseBranch);
281297
this.UpdateVersion(context, versionOptions.Version, releaseVersion, unformattedCommitMessage);
282298

299+
if (outputMode == ReleaseManagerOutputMode.Text)
300+
{
301+
this.stdout.WriteLine($"{releaseBranchName} branch now tracks v{releaseVersion} stabilization and release.");
302+
}
303+
283304
// update version on main branch
284305
global::LibGit2Sharp.Commands.Checkout(repository, originalBranchName);
285306
this.UpdateVersion(context, versionOptions.Version, nextDevVersion, unformattedCommitMessage);
286307

308+
if (outputMode == ReleaseManagerOutputMode.Text)
309+
{
310+
this.stdout.WriteLine($"{originalBranchName} branch now tracks v{nextDevVersion} development.");
311+
}
312+
287313
// Merge release branch back to main branch
288314
var mergeOptions = new MergeOptions()
289315
{
@@ -298,37 +324,13 @@ private ReleaseInfo PrepareReleaseCore(string projectDirectory, string releaseUn
298324
var finalOriginalBranchInfo = new ReleaseBranchInfo(originalBranchName, repository.Head.Tip.Sha, nextDevVersion);
299325
var finalReleaseBranchInfo = new ReleaseBranchInfo(releaseBranchName, repository.Branches[releaseBranchName].Tip.Id.ToString(), releaseVersion);
300326
var finalReleaseInfo = new ReleaseInfo(finalOriginalBranchInfo, finalReleaseBranchInfo);
301-
327+
302328
this.WriteToOutput(finalReleaseInfo);
303329
}
304330

305331
return null;
306332
}
307333

308-
public void WriteToOutput(ReleaseInfo releaseInfo)
309-
{
310-
string json = JsonConvert.SerializeObject(releaseInfo, Formatting.Indented, new SemanticVersionJsonConverter());
311-
this.stdout.WriteLine(json);
312-
}
313-
314-
private static bool IsVersionDecrement(SemanticVersion oldVersion, SemanticVersion newVersion)
315-
{
316-
if (newVersion.Version > oldVersion.Version)
317-
{
318-
return false;
319-
}
320-
else if (newVersion.Version == oldVersion.Version)
321-
{
322-
return string.IsNullOrEmpty(oldVersion.Prerelease) &&
323-
!string.IsNullOrEmpty(newVersion.Prerelease);
324-
}
325-
else
326-
{
327-
// newVersion.Version < oldVersion.Version
328-
return true;
329-
}
330-
}
331-
332334
private string GetReleaseBranchName(VersionOptions versionOptions)
333335
{
334336
Requires.NotNull(versionOptions, nameof(versionOptions));

test/Nerdbank.GitVersioning.Tests/ReleaseManagerTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -703,19 +703,6 @@ public void PrepareRelease_WithCustomCommitMessagePattern(string initialVersion,
703703
Assert.Equal(expectedCommitMessage, releaseBranchCommit.MessageShort);
704704
}
705705

706-
/// <inheritdoc/>
707-
protected override void InitializeSourceControl(bool withInitialCommit = true)
708-
{
709-
base.InitializeSourceControl(withInitialCommit);
710-
this.Ignore_git2_UntrackedFile();
711-
}
712-
713-
private void AssertError(Action testCode, ReleasePreparationError expectedError)
714-
{
715-
ReleasePreparationException ex = Assert.Throws<ReleasePreparationException>(testCode);
716-
Assert.Equal(expectedError, ex.Error);
717-
}
718-
719706
[Fact]
720707
public void SimulatePrepareRelease_BasicScenario()
721708
{
@@ -876,4 +863,17 @@ public void SimulatePrepareRelease_OnReleaseBranch()
876863
// When on release branch, no new branch is created
877864
Assert.Null(result.NewBranch);
878865
}
866+
867+
/// <inheritdoc/>
868+
protected override void InitializeSourceControl(bool withInitialCommit = true)
869+
{
870+
base.InitializeSourceControl(withInitialCommit);
871+
this.Ignore_git2_UntrackedFile();
872+
}
873+
874+
private void AssertError(Action testCode, ReleasePreparationError expectedError)
875+
{
876+
ReleasePreparationException ex = Assert.Throws<ReleasePreparationException>(testCode);
877+
Assert.Equal(expectedError, ex.Error);
878+
}
879879
}

0 commit comments

Comments
 (0)