Skip to content

Commit 813cbf1

Browse files
committed
Merge remote-tracking branch 'origin/master' into IsNullmaster
2 parents 9ead478 + 5789043 commit 813cbf1

File tree

19 files changed

+210
-66
lines changed

19 files changed

+210
-66
lines changed

doc/msbuild.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22

33
Installing the `Nerdbank.GitVersioning` package from NuGet into your MSBuild-based projects is the recommended way to add version information to your MSBuild project outputs including assemblies, native dll's, and packages.
44

5+
If you want the package to affect all the projects in your repo, and you use `PackageReference` (rather than `packages.config`),
6+
you can add this to a repo-level `Directory.Build.props` file:
7+
8+
```xml
9+
<ItemGroup>
10+
<PackageReference Include="Nerdbank.GitVersioning"
11+
Version="(latest-version-here)"
12+
PrivateAssets="all"
13+
Condition="!Exists('packages.config')" />
14+
</ItemGroup>
15+
```
16+
17+
The condition prevents the `PackageReference` from impacting any `packages.config`-based projects
18+
such as vcxproj that may be in your repo.
19+
Such projects will require individual installation of the Nerdbank.GitVersioning nuget package
20+
using the NuGet Package Manager in Visual Studio.
21+
522
## Public releases
623

724
By default, each build of a Nuget package will include the git commit ID.
@@ -21,6 +38,64 @@ You should only build with this property set from one release branch per
2138
major.minor version to avoid the risk of producing multiple unique NuGet
2239
packages with a colliding version spec.
2340

41+
## Custom build authoring
42+
43+
If you are writing your own MSBuild targets or properties and need to consume version information,
44+
Nerdbank.GitVersioning is there to help.
45+
The version information created by this package is expressed as MSBuild properties.
46+
These properties are set by the `GetBuildVersion` target defined in this package.
47+
This means any dependency you have on these properties must ensure this target has already executed.
48+
This can be done by defining your own msbuild target like so:
49+
50+
```xml
51+
<Target Name="MyPropertySetter" DependsOnTargets="GetBuildVersion">
52+
<PropertyGroup>
53+
<MyOwnProperty>My assembly version is: $(AssemblyVersion)</MyOwnProperty>
54+
</PropertyGroup>
55+
</Target>
56+
```
57+
58+
In the above example, the `AssemblyVersion` property, which is set by the
59+
`GetBuildVersion` target defined by Nerdbank.GitVersioning, is used to define
60+
another property.
61+
It could also be used to define msbuild items or anything else you want.
62+
63+
### MSBuild properties defined in `GetBuildVersion`
64+
65+
Many MSBuild properties are set by `GetBuildVersion` allowing you to define or redefine
66+
properties in virtually any format you like.
67+
The authoritative list is [here](https://github.com/dotnet/Nerdbank.GitVersioning/blob/dae20a6d15f04d8161fd092c36fdf1f57c021ba1/src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs#L300-L323) (switch to the default branch to see the current set).
68+
69+
Below is a snapshot of the properties with example values.
70+
Note that the values and formats can vary depending on your `version.json` settings and version height.
71+
72+
Property | Example value
73+
--|--
74+
AssemblyFileVersion | 2.7.74.11627
75+
AssemblyInformationalVersion | 2.7.74-alpha+6b2d14ba68
76+
AssemblyVersion | 2.7.0.0
77+
BuildingRef | refs/heads/fix299
78+
BuildNumber | 74
79+
BuildVersion | 2.7.74.11627
80+
BuildVersion3Components | 2.7.74
81+
BuildVersionNumberComponent | 74
82+
BuildVersionSimple | 2.7.74
83+
ChocolateyPackageVersion | 2.7.74-alpha-g6b2d14ba68
84+
CloudBuildNumber | (empty except in cloud build)
85+
FileVersion | 2.7.74.11627
86+
GitCommitDateTicks | 637547960670000000
87+
GitCommitId | 6b2d14ba6844d2152c48268a8d2c1933759e7229
88+
GitCommitIdShort | 6b2d14ba68
89+
GitVersionHeight | 74
90+
MajorMinorVersion | 2.7
91+
NPMPackageVersion | 2.7.74-alpha.g6b2d14ba68
92+
NuGetPackageVersion | 2.7.74-alpha-g6b2d14ba68
93+
PackageVersion | 2.7.74-alpha-g6b2d14ba68
94+
PrereleaseVersion | -alpha
95+
PublicRelease | False
96+
SemVerBuildSuffix | +6b2d14ba68
97+
Version | 2.7.74-alpha-g6b2d14ba68
98+
2499
## Build performance
25100

26101
Repos with many projects or many commits between major/minor version bumps can suffer from compromised build performance due to the MSBuild task that computes the version information for each project.

doc/public_vs_stable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ When a branch becomes stable, the `-prerelease` tag can be removed by adding a c
2525
To remove the `-prerelease`, the version.json file must be changed to remove it.
2626
Committing this change communicates to everyone looking at the repo that this software is stable.
2727

28-
The natural evolution of a product includes usually includes entering and exiting a `-prerelease` stage many times, but within a branded release (usually recognized by an intentional version number like "1.2") the progression usually transitions only one direction: from `-prerelease` to stable quality.
28+
The natural evolution of a product usually includes entering and exiting a `-prerelease` stage many times, but within a branded release (usually recognized by an intentional version number like "1.2") the progression usually transitions only one direction: from `-prerelease` to stable quality.
2929
For example, an anticipated version 1.2 might first be released to the public as 1.2-beta before releasing as 1.2 (without the `-beta` suffix).
3030
If the product is undergoing significant changes that warrant downgrading the stability rating to pre-release quality, the version number tends to be incremented at the same time.
3131
So a 1.2 product's subsequent release might appear as 1.3-beta or 2.0-beta.

src/Cake.GitVersioning/Cake.GitVersioning.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<Company>andarno</Company>
99
<Description>Cake wrapper for Nerdbank.GitVersioning. Stamps your assemblies with semver 2.0 compliant git commit specific version information and provides NuGet versioning information as well.</Description>
1010
<Copyright>Copyright (c) .NET Foundation and Contributors</Copyright>
11-
<PackageTags>git commit versioning version assemblyinfo</PackageTags>
12-
<PackageIconUrl>https://cdn.jsdelivr.net/gh/cake-contrib/graphics/png/cake-contrib-medium.png</PackageIconUrl>
13-
<PackageIcon>cake-contrib-medium.png</PackageIcon>
11+
<PackageTags>git commit versioning version assemblyinfo cake-addin</PackageTags>
12+
<PackageIconUrl>https://cdn.jsdelivr.net/gh/cake-contrib/graphics/png/addin/cake-contrib-addin-medium.png</PackageIconUrl>
13+
<PackageIcon>cake-contrib-addin-medium.png</PackageIcon>
1414
<PackageProjectUrl>http://github.com/dotnet/Nerdbank.GitVersioning</PackageProjectUrl>
1515
<SignAssembly>false</SignAssembly>
1616

@@ -36,7 +36,7 @@
3636
</ItemGroup>
3737

3838
<ItemGroup>
39-
<None Include="cake-contrib-medium.png" Pack="true" PackagePath="" />
39+
<None Include="cake-contrib-addin-medium.png" Pack="true" PackagePath="" />
4040

4141
<!-- Include native binaries -->
4242
<None Include="$(LibGit2SharpNativeBinaries)runtimes\**\*.*" Pack="true" PackagePath="lib\netstandard2.0\lib\" LinkBase="lib" />
15.5 KB
Loading
-15.2 KB
Binary file not shown.

src/NerdBank.GitVersioning.Tests/GitExtensionsTests.cs renamed to src/NerdBank.GitVersioning.Tests/LibGit2GitExtensionsTests.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Buffers.Binary;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.IO;
@@ -11,9 +12,9 @@
1112
using Xunit.Abstractions;
1213
using Version = System.Version;
1314

14-
public partial class GitExtensionsTests : RepoTestBase
15+
public class LibGit2GitExtensionsTests : RepoTestBase
1516
{
16-
public GitExtensionsTests(ITestOutputHelper Logger)
17+
public LibGit2GitExtensionsTests(ITestOutputHelper Logger)
1718
: base(Logger)
1819
{
1920
this.InitializeSourceControl();
@@ -166,22 +167,6 @@ public void GetVersionHeight_ProgressAndReset(string version1, string version2,
166167
Assert.Equal(!versionHeightReset, height2 > height1);
167168
}
168169

169-
[Fact]
170-
public void GetTruncatedCommitIdAsInteger_Roundtrip()
171-
{
172-
var firstCommit = this.LibGit2Repository.Commit("First", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true });
173-
var secondCommit = this.LibGit2Repository.Commit("Second", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true });
174-
175-
int id1 = firstCommit.GetTruncatedCommitIdAsInt32();
176-
int id2 = secondCommit.GetTruncatedCommitIdAsInt32();
177-
178-
this.Logger.WriteLine($"Commit {firstCommit.Id.Sha.Substring(0, 8)} as int: {id1}");
179-
this.Logger.WriteLine($"Commit {secondCommit.Id.Sha.Substring(0, 8)} as int: {id2}");
180-
181-
Assert.Equal(firstCommit, this.LibGit2Repository.GetCommitFromTruncatedIdInteger(id1));
182-
Assert.Equal(secondCommit, this.LibGit2Repository.GetCommitFromTruncatedIdInteger(id2));
183-
}
184-
185170
[Fact]
186171
public void GetIdAsVersion_ReadsMajorMinorFromVersionTxt()
187172
{
@@ -384,6 +369,19 @@ public void GetIdAsVersion_Roundtrip_UnstableOffset(int startingOffset, int offs
384369
}
385370
}
386371

372+
[Fact]
373+
public void GetCommitsFromVersion_MatchesOnEitherEndian()
374+
{
375+
this.InitializeSourceControl();
376+
Commit commit = this.WriteVersionFile(new VersionOptions { Version = SemanticVersion.Parse("1.2"), GitCommitIdShortAutoMinimum = 4 });
377+
378+
Version originalVersion = new VersionOracle(this.Context).Version;
379+
Version swappedEndian = new Version(originalVersion.Major, originalVersion.Minor, originalVersion.Build, BinaryPrimitives.ReverseEndianness((ushort)originalVersion.Revision));
380+
ushort twoBytesFromCommitId = checked((ushort)originalVersion.Revision);
381+
Assert.Contains(commit, LibGit2GitExtensions.GetCommitsFromVersion(this.Context, originalVersion));
382+
Assert.Contains(commit, LibGit2GitExtensions.GetCommitsFromVersion(this.Context, swappedEndian));
383+
}
384+
387385
[Fact]
388386
public void GetIdAsVersion_Roundtrip_WithSubdirectoryVersionFiles()
389387
{

src/NerdBank.GitVersioning.Tests/ManagedGit/GitObjectIdTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void AsUInt16Test()
9393
{
9494
// The hash code is the int32 representation of the first 4 bytes
9595
var objectId = GitObjectId.ParseHex(this.shaAsHexAsciiByteArray);
96-
Assert.Equal(0x914e, objectId.AsUInt16());
96+
Assert.Equal(0x4e91, objectId.AsUInt16());
9797
Assert.Equal(0, GitObjectId.Empty.GetHashCode());
9898
}
9999

src/NerdBank.GitVersioning.Tests/ReleaseManagerTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,30 @@ public void PrepareRelease_MasterWithVersionDecrement(string initialVersion, str
392392
this.WriteVersionFile(versionOptions);
393393

394394
// running PrepareRelease should result in an error
395-
// because we're trying to add a prerelease tag to a version without prerelease tag
395+
// because we're setting the version on master to a lower version
396396
this.AssertError(
397397
() => new ReleaseManager().PrepareRelease(this.RepoPath, releaseUnstableTag, (nextVersion is null ? null : Version.Parse(nextVersion))),
398398
ReleasePreparationError.VersionDecrement);
399399
}
400400

401+
[Theory]
402+
[InlineData("1.2", "1.2")]
403+
public void PrepareRelease_MasterWithoutVersionIncrement(string initialVersion, string nextVersion)
404+
{
405+
// create and configure repository
406+
this.InitializeSourceControl();
407+
408+
// create version.json
409+
var versionOptions = new VersionOptions() { Version = SemanticVersion.Parse(initialVersion) };
410+
this.WriteVersionFile(versionOptions);
411+
412+
// running PrepareRelease should result in an error
413+
// because we're trying to set master to the version it already has
414+
this.AssertError(
415+
() => new ReleaseManager().PrepareRelease(this.RepoPath, null, (nextVersion is null ? null : Version.Parse(nextVersion))),
416+
ReleasePreparationError.NoVersionIncrement);
417+
}
418+
401419
[Fact]
402420
public void PrepareRelease_DetachedHead()
403421
{

src/NerdBank.GitVersioning.Tests/TestUtilities.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ internal static ExpandedRepo ExtractRepoArchive(string repoArchiveName)
7979
}
8080
}
8181

82+
internal static string ToHex(ushort number) => number.ToString("X");
83+
84+
internal static ushort FromHex(string hex) => ushort.Parse(hex, System.Globalization.NumberStyles.HexNumber);
85+
8286
internal class ExpandedRepo : IDisposable
8387
{
8488
internal ExpandedRepo(string repoPath)

src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,23 @@ public void GitCommitIdShort()
845845
}
846846
}
847847

848+
[Fact]
849+
public void GitCommidIdLeading16BitsDecodedWithBigEndian()
850+
{
851+
this.WriteVersionFile(new VersionOptions { Version = SemanticVersion.Parse("1.2"), GitCommitIdShortAutoMinimum = 4 });
852+
this.InitializeSourceControl();
853+
this.AddCommits(1);
854+
var oracle = new VersionOracle(this.Context);
855+
856+
string leadingFourChars = this.Context.GitCommitId.Substring(0, 4);
857+
ushort expectedNumber = TestUtilities.FromHex(leadingFourChars);
858+
ushort actualNumber = checked((ushort)oracle.Version.Revision);
859+
this.Logger.WriteLine("First two characters from commit ID in hex is {0}", leadingFourChars);
860+
this.Logger.WriteLine("First two characters, converted to a number is {0}", expectedNumber);
861+
this.Logger.WriteLine("Generated 16-bit ushort from commit ID is {0}, whose hex representation is {1}", actualNumber, TestUtilities.ToHex(actualNumber));
862+
Assert.Equal(expectedNumber, actualNumber);
863+
}
864+
848865
[Fact(Skip = "Slow test")]
849866
public void GetVersionHeight_VeryLongHistory()
850867
{

0 commit comments

Comments
 (0)