@@ -17,22 +17,34 @@ After installing via [NuGet](https://www.nuget.org/packages/GitInfo):
1717
1818 PM> Install-Package GitInfo
1919
20- By default, if the containing project is a C#, F# or VB project, a compile-time generated source file will contain
21- all the git information and can be accessed from anywhere within the assembly, as constants in a
22- ` ThisAssembly ` (partial) class and its nested ` Git ` static class:
20+ By default, if the containing project is a C#, F# or VB project, a compile-time generated
21+ source file will contain all the git information and can be accessed from anywhere within
22+ the assembly, as constants in a ` ThisAssembly ` (partial) class and its nested ` Git ` static class:
2323
2424 Console.WriteLine(ThisAssembly.Git.Commit);
2525
26- All generated constants also have a Summary documentation tag that shows the current
27- value in the intellisense tooltip, making it easier to see what the different values contain:
28-
29- ![ ] ( https://raw.githubusercontent.com/devlooped/GitInfo/main/assets/images/tooltip.png )
30-
3126> NOTE: you may need to close and reopen the solution in order
3227> for Visual Studio to refresh intellisense and show the
3328> ThisAssembly type the first time after installing the package.
3429
35- With this information at your fingertips, you can build any versioning attributes you want,
30+ By default, GitInfo will also set ` $(Version) ` and ` $(PackageVersion) ` which the .NET
31+ SDK uses for deriving the AssemblyInfo, FileVersion and InformationalVersion values,
32+ as well as for packing. This default version is formatted from the following populated
33+ MSBuild properties: ` $(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit) ` .
34+
35+ So, straight after install and build/pack, you will get some versioning in place :).
36+
37+ Alternatively, you can opt-out of this default versioning by setting ` GitVersion=false `
38+ in your project file, if you want to just leverage the Git information and/or version
39+ properties/constants yourself:
40+
41+ ``` xml
42+ <PropertyGroup >
43+ <GitVersion >false</GitVersion >
44+ </PropertyGroup >
45+ ```
46+
47+ This allows you to use the provided constants to build any versioning attributes you want,
3648with whatever information you want, without resorting to settings, format strings or anything,
3749just plain code:
3850
8294 ThisAssembly.Git.Commit)>
8395```
8496
97+ > NOTE: when generating your own assembly version attributes, you will need to turn off
98+ > the corresponding assembly version attribute generation from the .NET SDK, by setting
99+ > the relevant properties to false: ` GenerateAssemblyVersionAttribute ` ,
100+ > ` GenerateAssemblyFileVersionAttribute ` and ` GenerateAssemblyInformationalVersionAttribute ` .
101+
102+
85103MSBuild:
86104
87105```
88106<!-- Just edit .csproj file -->
107+ <PropertyGroup>
108+ <!-- We'll do our own versioning -->
109+ <GitVersion>false</GitVersion>
110+ </PropertyGroup>
89111<ItemGroup>
90112 <PackageReference Include="GitInfo" PrivateAssets="all" />
91113</ItemGroup>
92114
93- <Target Name="PopulateInfo" DependsOnTargets="GitInfo " BeforeTargets="PrepareForBuild ">
115+ <Target Name="PopulateInfo" DependsOnTargets="GitVersion " BeforeTargets="GetAssemblyVersion;GenerateNuspec;GetPackageContents ">
94116 <PropertyGroup>
117+ <Version>$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit)</Version>
118+ <PackageVersion>$(Version)</PackageVersion>
119+
95120 <RepositoryBranch>$(GitBranch)</RepositoryBranch>
96121 <RepositoryCommit>$(GitCommit)</RepositoryCommit>
97122 <SourceRevisionId>$(GitBranch) $(GitCommit)</SourceRevisionId>
98123 </PropertyGroup>
99124</Target>
100125```
101126
127+ > NOTE: because the provided properties are populated via targets that need to run
128+ > before they are available, you cannot use the GitInfo-provided properties in a
129+ > PropertyGroup at the project level. You can only use them from within a target that
130+ > in turn depends on the relevant target from GitInfo (typically, ` GitVersion ` as
131+ > shown above, if you consume the SemVer properties).
132+
102133Because this information is readily available whenever you build the project, you
103134never depend on CI build scripts that generate versions for you, and you can
104135always compile locally exactly the same version of an assembly that was built by
@@ -107,22 +138,6 @@ a CI server.
107138You can read more about this project at the
108139[ GitInfo announcement blog post] ( http://www.cazzulino.com/git-info-from-msbuild-and-code.html ) .
109140
110- ### MSBuild
111-
112- If you want to set other properties in your project, such as ` $(Version) ` or ` $(PackageVersion) `
113- based on the populated values from GitInfo, you must do so from a target, such as:
114-
115- ``` xml
116- <Target Name =" _SetVersion" BeforeTargets =" GetAssemblyVersion;GenerateNuspec;GetPackageContents" >
117- <PropertyGroup >
118- <Version >$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit)</Version >
119- <PackageVersion >$(Version)</PackageVersion >
120- </PropertyGroup >
121- </Target >
122- ```
123-
124- In this case, we're setting the version and package version.
125-
126141## Details
127142
128143Exposes the following information for use directly from any MSBuild
@@ -148,31 +163,8 @@ target that depends on the GitInfo target:
148163 $(GitIsDirty)
149164```
150165
151- From C#, F# and VB, by default code is generated too so that the same
152- information can be accessed from code, to construct your own
153- assembly/file version attributes with whatever format you want:
154-
155- ``` csharp
156- [assembly : AssemblyVersion (ThisAssembly .Git .SemVer .Major + " ." + ThisAssembly .Git .SemVer .Minor + " ." + ThisAssembly .Git .SemVer .Patch )]
157- [assembly : AssemblyInformationalVersion (
158- ThisAssembly .Git .SemVer .Major + " ." +
159- ThisAssembly .Git .SemVer .Minor + " ." +
160- ThisAssembly .Git .SemVer .Patch + " -" +
161- ThisAssembly .Git .Branch + " +" +
162- ThisAssembly .Git .Commit )]
163- // i..e ^: 1.0.2-main+c218617
164- ```
165-
166- > NOTE: you may need to close and reopen the solution in order
167- > for Visual Studio to refresh intellisense and show the
168- > ThisAssembly type right after package installation for
169- > the first time.
170-
171- All generated constants also have a Summary documentation tag
172- that shows the current value in the intellisense tooltip, making
173- it very easy to see what the different values contain.
174-
175- The available constants from code are:
166+ For C#, F# and VB, constants are generated too so that the same information can be
167+ accessed from code:
176168
177169```
178170 ThisAssembly.Git.RepositoryUrl
@@ -193,15 +185,21 @@ The available constants from code are:
193185 ThisAssembly.Git.IsDirty
194186```
195187
196- Available [ MSBuild properties] ( https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-properties ) :
188+ Available [ MSBuild properties] ( https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-properties )
189+ to customize the behavior:
197190
198191```
192+ $(GitVersion): set to 'false' to prevent setting Version
193+ and PackageVersion.
194+
199195 $(GitThisAssembly): set to 'false' to prevent assembly
200196 metadata and constants generation.
201197
202198 $(GitThisAssemblyMetadata): set to 'false' to prevent assembly
203199 metadata generation only. Defaults
204- to 'false'.
200+ to 'false'. If 'true', it will also
201+ provide assembly metadata attributes
202+ for each of the populated values.
205203
206204 $(ThisAssemblyNamespace): allows overriding the namespace
207205 for the ThisAssembly class.
0 commit comments