|
3 | 3 |
|
4 | 4 | Git Info from MSBuild, C# and VB |
5 | 5 |
|
| 6 | +> [!NOTE] |
6 | 7 | > A fresh and transparent approach to Git information retrieval from MSBuild and Code without |
7 | 8 | > using any custom tasks or compiled code and tools, obscure settings, format strings, etc. |
8 | 9 |
|
9 | 10 | [](https://www.nuget.org/packages/GitInfo) |
10 | 11 | [](https://www.nuget.org/packages/GitInfo) |
11 | | -[](https://opensource.org/licenses/mit-license.php) |
12 | | -[](https://github.com/devlooped/GitInfo/actions/workflows/build.yml) |
| 12 | +[](osmfeula.txt) |
| 13 | +[](license.txt) |
13 | 14 |
|
14 | 15 | Install via [NuGet](https://www.nuget.org/packages/GitInfo): |
15 | 16 |
|
16 | 17 | ```pwsh |
17 | 18 | PM> Install-Package GitInfo |
18 | 19 | ``` |
19 | 20 |
|
20 | | -<!-- include https://github.com/devlooped/.github/raw/main/sponsorlinkr.md --> |
21 | | -*This project uses [SponsorLink](https://github.com/devlooped#sponsorlink) to attribute sponsor status (direct, indirect or implicit).* |
22 | | -*For IDE usage (without warnings), sponsor status is required. IDE-only warnings will be issued after a grace period otherwise.* |
| 21 | +<!-- include https://github.com/devlooped/.github/raw/main/osmf.md --> |
23 | 22 |
|
24 | | -<!-- https://github.com/devlooped/.github/raw/main/sponsorlinkr.md --> |
25 | | - |
26 | | -## Usage |
27 | 23 | <!-- #content --> |
| 24 | +## Usage |
28 | 25 | By default, if the containing project is a C#, F# or VB project, a compile-time generated |
29 | 26 | source file will contain all the git information and can be accessed from anywhere within |
30 | 27 | the assembly, as constants in a `ThisAssembly` (partial) class and its nested `Git` static class: |
@@ -194,74 +191,78 @@ accessed from code: |
194 | 191 | ThisAssembly.Git.IsDirty |
195 | 192 | ``` |
196 | 193 |
|
| 194 | +## Goals |
| 195 | + |
| 196 | +- No compiled code or tools -> 100% transparency |
| 197 | +- Trivially added/installed via [a NuGet package](https://www.nuget.org/packages/GitInfo) |
| 198 | +- No format strings or settings to learn |
| 199 | +- Simple well-structured [.targets file](https://github.com/kzu/GitInfo/blob/main/src/GitInfo/build/GitInfo.targets) |
| 200 | + with plain MSBuild and no custom tasks |
| 201 | +- [Optional embedding](https://github.com/kzu/GitInfo/blob/main/src/GitInfo/build/GitInfo.AssemblyMetadata.targets) |
| 202 | + of Git info in assembly metadata |
| 203 | +- Optional use of Git info to build arbitrary assembly/file version information, both |
| 204 | + [in C#](https://github.com/kzu/GitInfoDemo/blob/main/GitInfoDemo/Properties/AssemblyInfo.cs#L10) as well |
| 205 | + [as VB](https://github.com/kzu/GitInfoDemo/blob/main/GitInfoDemoVB/My%20Project/AssemblyInfo.vb#L8). |
| 206 | +- Trivially modified/improved generated code by just adjusting a |
| 207 | + [C#](https://github.com/kzu/GitInfo/blob/main/src/GitInfo/build/GitInfo.cs.pp) or |
| 208 | + [F#](https://github.com/kzu/GitInfo/blob/main/src/GitInfo/build/GitInfo.fs.pp) or |
| 209 | + [VB](https://github.com/kzu/GitInfo/blob/main/src/GitInfo/build/GitInfo.vb.pp) template |
| 210 | + included in the [NuGet package](https://www.nuget.org/packages/GitInfo) |
| 211 | +- 100% incremental build-friendly and high-performing (all proper Inputs/Outputs in place, smart caching of Git info, etc.) |
| 212 | + |
| 213 | +<!-- #content --> |
| 214 | + |
| 215 | +## Customization |
| 216 | + |
197 | 217 | Available [MSBuild properties](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-properties) |
198 | 218 | to customize the behavior: |
199 | 219 |
|
200 | 220 | ``` |
201 | | - $(GitVersion): set to 'false' to avoid setting Version and PackageVersion to a default version with format: |
202 | | - $(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit) |
| 221 | +$(GitVersion): set to 'false' to avoid setting Version and PackageVersion to a default version with format: |
| 222 | + $(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit) |
203 | 223 |
|
204 | | - $(GitThisAssembly): set to 'false' to prevent assembly metadata and constants generation. |
| 224 | +$(GitThisAssembly): set to 'false' to prevent assembly metadata and constants generation. |
205 | 225 |
|
206 | | - $(GitThisAssemblyMetadata): set to 'false' to prevent assembly metadata generation only. Defaults to 'false'. |
207 | | - If 'true', it will also provide assembly metadata attributes for each of the populated values. |
| 226 | +$(GitThisAssemblyMetadata): set to 'false' to prevent assembly metadata generation only. Defaults to 'false'. |
| 227 | + If 'true', it will also provide assembly metadata attributes for each of the populated values. |
208 | 228 |
|
209 | | - $(ThisAssemblyNamespace): allows overriding the namespace for the ThisAssembly class. Defaults to the global namespace. |
| 229 | +$(ThisAssemblyNamespace): allows overriding the namespace for the ThisAssembly class. Defaults to the global namespace. |
210 | 230 |
|
211 | | - $(GitRemote): name of remote to get repository url for. Defaults to 'origin'. |
| 231 | +$(GitRemote): name of remote to get repository url for. Defaults to 'origin'. |
212 | 232 |
|
213 | | - $(GitBranchCI): determines whether the branch name should be populated from default environment variables used by the CI system. Default to 'true'. |
| 233 | +$(GitBranchCI): determines whether the branch name should be populated from default environment variables used by the CI system. Default to 'true'. |
214 | 234 |
|
215 | | - $(GitDefaultBranch): determines the base branch used to calculate commits on top of current branch. Defaults to 'main'. |
| 235 | +$(GitDefaultBranch): determines the base branch used to calculate commits on top of current branch. Defaults to 'main'. |
216 | 236 |
|
217 | | - $(GitVersionFile): determines the name of a file in the Git repository root used to provide the base version info. Defaults to 'GitInfo.txt'. |
| 237 | +$(GitVersionFile): determines the name of a file in the Git repository root used to provide the base version info. Defaults to 'GitInfo.txt'. |
218 | 238 |
|
219 | | - $(GitCommitsRelativeTo): optionally specifies an alternative directory for counting commits on top of the base version. Defaults to the $(GitVersionFile) directory. |
| 239 | +$(GitCommitsRelativeTo): optionally specifies an alternative directory for counting commits on top of the base version. Defaults to the $(GitVersionFile) directory. |
220 | 240 |
|
221 | | - $(GitCommitsIgnoreMerges): set to 'true' to ignore merge commits when calculating the number of commits. Defaults to 'false'. |
| 241 | +$(GitCommitsIgnoreMerges): set to 'true' to ignore merge commits when calculating the number of commits. Defaults to 'false'. |
222 | 242 |
|
223 | | - $(GitInfoReportImportance): allows rendering all the retrieved git information with the specified message importance ('high', 'normal' or 'low'). Defaults to 'low'. |
| 243 | +$(GitInfoReportImportance): allows rendering all the retrieved git information with the specified message importance ('high', 'normal' or 'low'). Defaults to 'low'. |
224 | 244 |
|
225 | | - $(GitIgnoreBranchVersion) and $(GitIgnoreTagVersion): determines whether the branch and tags (if any) will be used to find a base version. Defaults to empty value (no ignoring). |
| 245 | +$(GitIgnoreBranchVersion) and $(GitIgnoreTagVersion): determines whether the branch and tags (if any) will be used to find a base version. Defaults to empty value (no ignoring). |
226 | 246 |
|
227 | | - $(GitNameRevOptions): options passed to git name-rev when finding a branch name for a commit (Detached head). The default is '--refs=refs/heads/* --no-undefined --always' |
228 | | - meaning branch names only, falling back to commit hash. For the legacy behavior where $(GitBranch) for detached head can also be a tag name, use '--refs=refs/*'. |
229 | | - Refs can be included and excluded, see git name-rev docs. |
| 247 | +$(GitNameRevOptions): options passed to git name-rev when finding a branch name for a commit (Detached head). The default is '--refs=refs/heads/* --no-undefined --always' |
| 248 | + meaning branch names only, falling back to commit hash. For the legacy behavior where $(GitBranch) for detached head can also be a tag name, use '--refs=refs/*'. |
| 249 | + Refs can be included and excluded, see git name-rev docs. |
230 | 250 |
|
231 | | - $(GitSkipCache): whether to cache the Git information determined in a previous build in a GitInfo.cache for performance reasons. Defaults to empty value (no ignoring). |
| 251 | +$(GitSkipCache): whether to cache the Git information determined in a previous build in a GitInfo.cache for performance reasons. Defaults to empty value (no ignoring). |
232 | 252 |
|
233 | | - $(GitCachePath): where to cache the determined Git information. Gives the chance to use a shared location for different projects. This can improve the overall build time. |
234 | | - Has to end with a path seperator Defaults to empty value ('$(IntermediateOutputPath)'). |
| 253 | +$(GitCachePath): where to cache the determined Git information. Gives the chance to use a shared location for different projects. This can improve the overall build time. |
| 254 | + Has to end with a path seperator Defaults to empty value ('$(IntermediateOutputPath)'). |
235 | 255 |
|
236 | | - $(GitTagRegex): regular expression used with git describe to filter the tags to consider for base version lookup. Defaults to * (all). |
| 256 | +$(GitTagRegex): regular expression used with git describe to filter the tags to consider for base version lookup. Defaults to * (all). |
237 | 257 |
|
238 | | - $(GitBaseVersionRegex): regular expression used to match and validate valid base versions in branch, tag or file sources. By default, matches any string that *ends* in a valid SemVer2 string. |
239 | | - Defaults to 'v?(?<MAJOR>\d+)\.(?<MINOR>\d+)\.(?<PATCH>\d+)(?:\-(?<LABEL>[\dA-Za-z\-\.]+))?$|^(?<LABEL>[\dA-Za-z\-\.]+)\-v?(?<MAJOR>\d+)\.(?<MINOR>\d+)\.(?<PATCH>\d+)$' |
| 258 | +$(GitBaseVersionRegex): regular expression used to match and validate valid base versions in branch, tag or file sources. By default, matches any string that *ends* in a valid SemVer2 string. |
| 259 | + Defaults to 'v?(?<MAJOR>\d+)\.(?<MINOR>\d+)\.(?<PATCH>\d+)(?:\-(?<LABEL>[\dA-Za-z\-\.]+))?$|^(?<LABEL>[\dA-Za-z\-\.]+)\-v?(?<MAJOR>\d+)\.(?<MINOR>\d+)\.(?<PATCH>\d+)$' |
240 | 260 |
|
241 | | - $(GitCommitDateFormat): value passed as the format option when trying to retrieve the git commit date. Defaults to %%cI (windows) or %cI (non windows). |
| 261 | +$(GitCommitDateFormat): value passed as the format option when trying to retrieve the git commit date. Defaults to %%cI (windows) or %cI (non windows). |
242 | 262 | ``` |
243 | 263 |
|
244 | | -## Goals |
245 | | - |
246 | | -- No compiled code or tools -> 100% transparency |
247 | | -- Trivially added/installed via [a NuGet package](https://www.nuget.org/packages/GitInfo) |
248 | | -- No format strings or settings to learn |
249 | | -- Simple well-structured [.targets file](https://github.com/kzu/GitInfo/blob/main/src/GitInfo/build/GitInfo.targets) |
250 | | - with plain MSBuild and no custom tasks |
251 | | -- [Optional embedding](https://github.com/kzu/GitInfo/blob/main/src/GitInfo/build/GitInfo.AssemblyMetadata.targets) |
252 | | - of Git info in assembly metadata |
253 | | -- Optional use of Git info to build arbitrary assembly/file version information, both |
254 | | - [in C#](https://github.com/kzu/GitInfoDemo/blob/main/GitInfoDemo/Properties/AssemblyInfo.cs#L10) as well |
255 | | - [as VB](https://github.com/kzu/GitInfoDemo/blob/main/GitInfoDemoVB/My%20Project/AssemblyInfo.vb#L8). |
256 | | -- Trivially modified/improved generated code by just adjusting a |
257 | | - [C#](https://github.com/kzu/GitInfo/blob/main/src/GitInfo/build/GitInfo.cs.pp) or |
258 | | - [F#](https://github.com/kzu/GitInfo/blob/main/src/GitInfo/build/GitInfo.fs.pp) or |
259 | | - [VB](https://github.com/kzu/GitInfo/blob/main/src/GitInfo/build/GitInfo.vb.pp) template |
260 | | - included in the [NuGet package](https://www.nuget.org/packages/GitInfo) |
261 | | -- 100% incremental build-friendly and high-performing (all proper Inputs/Outputs in place, smart caching of Git info, etc.) |
262 | | - |
263 | | -<!-- #content --> |
264 | 264 | <!-- include https://github.com/devlooped/sponsors/raw/main/footer.md --> |
| 265 | + |
265 | 266 | # Sponsors |
266 | 267 |
|
267 | 268 | <!-- sponsors.md --> |
|
0 commit comments