Skip to content

Commit 58708fc

Browse files
committed
Fix NullReferenceException in GetBuildVersion with missing version file
Fix #24
1 parent 8c3b6f1 commit 58708fc

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/NerdBank.GitVersioning.Tests/BuildIntegrationTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public async Task GetBuildVersion_In_Git_But_Without_Commits()
6868
Repository.Init(this.RepoPath);
6969
var repo = new Repository(this.RepoPath); // do not assign Repo property to avoid commits being generated later
7070
this.WriteVersionFile("3.4");
71+
Assumes.False(repo.Head.Commits.Any()); // verification that the test is doing what it claims
7172
var buildResult = await this.BuildAsync();
7273
Assert.Equal("3.4", buildResult.BuildVersion);
7374
Assert.Equal("3.4.0", buildResult.AssemblyInformationalVersion);
@@ -80,6 +81,18 @@ public async Task GetBuildVersion_In_Git_But_Head_Lacks_VersionFile()
8081
var repo = new Repository(this.RepoPath); // do not assign Repo property to avoid commits being generated later
8182
repo.Commit("empty", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true });
8283
this.WriteVersionFile("3.4");
84+
Assumes.True(repo.Index[VersionFile.JsonFileName] == null);
85+
var buildResult = await this.BuildAsync();
86+
Assert.Equal("0.0.1." + repo.Head.Commits.First().GetIdAsVersion().Revision, buildResult.BuildVersion);
87+
Assert.Equal("0.0.1+g" + repo.Head.Commits.First().Id.Sha.Substring(0, 10), buildResult.AssemblyInformationalVersion);
88+
}
89+
90+
[Fact]
91+
public async Task GetBuildVersion_In_Git_No_VersionFile_At_All()
92+
{
93+
Repository.Init(this.RepoPath);
94+
var repo = new Repository(this.RepoPath); // do not assign Repo property to avoid commits being generated later
95+
repo.Commit("empty", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true });
8396
var buildResult = await this.BuildAsync();
8497
Assert.Equal("0.0.1." + repo.Head.Commits.First().GetIdAsVersion().Revision, buildResult.BuildVersion);
8598
Assert.Equal("0.0.1+g" + repo.Head.Commits.First().Id.Sha.Substring(0, 10), buildResult.AssemblyInformationalVersion);

src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ public override bool Execute()
100100
VersionFile.GetVersion(commit) ??
101101
VersionFile.GetVersion(Environment.CurrentDirectory);
102102

103-
this.PrereleaseVersion = versionOptions.Version.Prerelease;
103+
this.PrereleaseVersion = versionOptions?.Version.Prerelease ?? string.Empty;
104104

105105
var repoRoot = git?.Info?.WorkingDirectory;
106106
var relativeRepoProjectDirectory = !string.IsNullOrWhiteSpace(repoRoot)
107107
? Environment.CurrentDirectory.Substring(repoRoot.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
108108
: null;
109109

110110
// Override the typedVersion with the special build number and revision components, when available.
111-
typedVersion = commit?.GetIdAsVersion(relativeRepoProjectDirectory) ?? versionOptions.Version.Version;
111+
typedVersion = commit?.GetIdAsVersion(relativeRepoProjectDirectory) ?? versionOptions?.Version.Version;
112112
}
113113

114114
typedVersion = typedVersion ?? new Version();
@@ -117,7 +117,7 @@ public override bool Execute()
117117
: new Version(typedVersion.Major, typedVersion.Minor);
118118
this.SimpleVersion = typedVersionWithoutRevision.ToString();
119119
this.MajorMinorVersion = new Version(typedVersion.Major, typedVersion.Minor).ToString();
120-
this.AssemblyVersion = versionOptions.AssemblyVersion?.ToString() ?? this.MajorMinorVersion;
120+
this.AssemblyVersion = versionOptions?.AssemblyVersion?.ToString() ?? this.MajorMinorVersion;
121121
this.BuildNumber = Math.Max(0, typedVersion.Build);
122122
this.Version = typedVersion.ToString();
123123
}

0 commit comments

Comments
 (0)