Skip to content

Commit

Permalink
Allow tag and base version customization, flex matching
Browse files Browse the repository at this point in the history
Fixes #100, brings #72 in.

It didn't make a lot of sense to require the entire branch or tag to be semver2, from beginning to end. It's usually the case that branches are named like `rel/vX.Y.Z`, for example, and we were forcing such common naming conventions to customize GitInfo unnecessarily.

So to make it more flexible and powerful, we add two things with this commit:

1. We no longer enfore version to match from beginning of string (`^` removed from default regex). We still require the version to be the last part. However, if that still doesn't match the desired behavior, users can now override the default expression by just setting `GitBaseVersionRegex` which is now "public" and documented.

2. We also allow the filter for tags to be specified, so that "weird" tags aren't considered for matching, and can easily be filtered out by providing, for example, a `rel/v*` expression for `GitTagRegex)`.
  • Loading branch information
kzu committed Sep 24, 2020
1 parent 64fd2e4 commit 7c22783
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ indent_size = 4
indent_style = tab
indent_size = 4

[*.{csproj,ps1,resx,rst,fsproj}]
[*.{csproj,ps1,resx,rst,fsproj,txt}]
indent_style = space
indent_size = 2
40 changes: 27 additions & 13 deletions src/GitInfo/build/GitInfo.targets
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@
performance reasons.
Defaults to empty value (no ignoring).
$(GitTagRegex): Regular expression used with git describe to filter the tags
to consider for base version lookup.
Defaults to * (all)
$(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.
Defaults to 'v?(?<MAJOR>\d+)\.(?<MINOR>\d+)\.(?<PATCH>\d+)(?:\-(?<LABEL>[\dA-Za-z\-\.]+))?$|^(?<LABEL>[\dA-Za-z\-\.]+)\-v?(?<MAJOR>\d+)\.(?<MINOR>\d+)\.(?<PATCH>\d+)$'
==============================================================
-->

Expand Down Expand Up @@ -102,6 +111,12 @@

<GitNameRevOptions Condition="'$(GitNameRevOptions)' == ''">--refs=refs/heads/*</GitNameRevOptions>

<GitTagRegex Condition="'$(GitTagRegex)' == ''">*</GitTagRegex>

<!-- For backwards compatibility if anyone has changed the expression before. -->
<GitBaseVersionRegex Condition="'$(_GitBaseVersionExpr)' != ''">$(_GitBaseVersionExpr)</GitBaseVersionRegex>
<GitBaseVersionRegex Condition="'$(GitBaseVersionRegex)' == ''">v?(?&lt;MAJOR&gt;\d+)\.(?&lt;MINOR&gt;\d+)(?:\-(?&lt;LABEL&gt;[\dA-Za-z\-\.]+))?$|^v?(?&lt;MAJOR&gt;\d+)\.(?&lt;MINOR&gt;\d+)\.(?&lt;PATCH&gt;\d+)(?:\-(?&lt;LABEL&gt;[\dA-Za-z\-\.]+))?$|^(?&lt;LABEL&gt;[\dA-Za-z\-\.]+)\-v?(?&lt;MAJOR&gt;\d+)\.(?&lt;MINOR&gt;\d+)\.(?&lt;PATCH&gt;\d+)$</GitBaseVersionRegex>

<GitMinVersion>2.5.0</GitMinVersion>
</PropertyGroup>

Expand All @@ -115,7 +130,6 @@
$(CoreCompileDependsOn)
</CoreCompileDependsOn>

<_GitBaseVersionExpr Condition="'$(_GitBaseVersionExpr)' == ''">^v?(?&lt;MAJOR&gt;\d+)\.(?&lt;MINOR&gt;\d+)(?:\-(?&lt;LABEL&gt;[\dA-Za-z\-\.]+))?$|^v?(?&lt;MAJOR&gt;\d+)\.(?&lt;MINOR&gt;\d+)\.(?&lt;PATCH&gt;\d+)(?:\-(?&lt;LABEL&gt;[\dA-Za-z\-\.]+))?$|^(?&lt;LABEL&gt;[\dA-Za-z\-\.]+)\-v?(?&lt;MAJOR&gt;\d+)\.(?&lt;MINOR&gt;\d+)\.(?&lt;PATCH&gt;\d+)$</_GitBaseVersionExpr>
<!-- Cache file used to avoid running all git commands. Only GitRoot will be retrieved to determine the path of this cache file. -->
<_GitInfoFile>$(IntermediateOutputPath)GitInfo.cache</_GitInfoFile>
</PropertyGroup>
Expand Down Expand Up @@ -534,12 +548,12 @@
<GitBaseVersion>$([System.IO.File]::ReadAllText('$(GitVersionFile)'))</GitBaseVersion>
<GitBaseVersion>$(GitBaseVersion.Trim())</GitBaseVersion>
<IsValidGitBaseVersion>
$([System.Text.RegularExpressions.Regex]::IsMatch($(GitBaseVersion), $(_GitBaseVersionExpr)))
$([System.Text.RegularExpressions.Regex]::IsMatch($(GitBaseVersion), $(GitBaseVersionRegex)))
</IsValidGitBaseVersion>
<IsValidGitBaseVersion>$(IsValidGitBaseVersion.Trim())</IsValidGitBaseVersion>
</PropertyGroup>

<Error Text="$(GitVersionFile) does not contain a valid base version (found '$(GitBaseVersion)', regex: $(_GitBaseVersionExpr))."
<Error Text="$(GitVersionFile) does not contain a valid base version (found '$(GitBaseVersion)', regex: $(GitBaseVersionRegex))."
Condition="'$(IsValidGitBaseVersion)' == 'False'" />

<PropertyGroup>
Expand Down Expand Up @@ -638,7 +652,7 @@
<_IndexOfSlash Condition="'$(_IndexOfSlash)' == '-1'">$(_CandidateValue.LastIndexOf('\'))</_IndexOfSlash>
<_CandidateValue Condition="'$(_GitBranchIndexOfSlash)' != '-1'">$(_CandidateValue.Substring($([MSBuild]::Add($(_IndexOfSlash), 1))))</_CandidateValue>
<IsValidGitBaseVersion>
$([System.Text.RegularExpressions.Regex]::IsMatch($(_CandidateValue), $(_GitBaseVersionExpr)))
$([System.Text.RegularExpressions.Regex]::IsMatch($(_CandidateValue), $(GitBaseVersionRegex)))
</IsValidGitBaseVersion>
<IsValidGitBaseVersion>$(IsValidGitBaseVersion.Trim())</IsValidGitBaseVersion>
</PropertyGroup>
Expand Down Expand Up @@ -695,7 +709,7 @@
DependsOnTargets="_GitBranch;_GitCommit"
Condition="'$(GitBaseVersion)' == '' And '$(GitIgnoreTagVersion)' != 'true' ">

<Exec Command='$(GitExe) describe --tags --abbrev=0'
<Exec Command='$(GitExe) describe --tags --match=$(GitTagRegex) --abbrev=0'
EchoOff='true'
StandardErrorImportance="low"
StandardOutputImportance="low"
Expand Down Expand Up @@ -725,7 +739,7 @@
Condition="'$(GitBaseVersion)' == '' And '$(GitIgnoreTagVersion)' != 'true' And '$(GitBaseTag)' != ''">

<!-- At this point, we now there is a base tag already we can leverage -->
<Exec Command='$(GitExe) describe --tags'
<Exec Command='$(GitExe) describe --match=$(GitTagRegex) --tags'
EchoOff='true'
StandardErrorImportance="low"
StandardOutputImportance="low"
Expand All @@ -739,7 +753,7 @@

<PropertyGroup>
<IsValidGitBaseVersion>
$([System.Text.RegularExpressions.Regex]::IsMatch($(GitBaseTag), $(_GitBaseVersionExpr)))
$([System.Text.RegularExpressions.Regex]::IsMatch($(GitBaseTag), $(GitBaseVersionRegex)))
</IsValidGitBaseVersion>
<IsValidGitBaseVersion>$(IsValidGitBaseVersion.Trim())</IsValidGitBaseVersion>

Expand Down Expand Up @@ -797,7 +811,7 @@

<PropertyGroup>
<IsValidGitDefaultVersion>
$([System.Text.RegularExpressions.Regex]::IsMatch($(GitDefaultVersion), $(_GitBaseVersionExpr)))
$([System.Text.RegularExpressions.Regex]::IsMatch($(GitDefaultVersion), $(GitBaseVersionRegex)))
</IsValidGitDefaultVersion>
<IsValidGitDefaultVersion>$(IsValidGitDefaultVersion.Trim())</IsValidGitDefaultVersion>
<GitCommits>0</GitCommits>
Expand Down Expand Up @@ -837,7 +851,7 @@

<PropertyGroup>
<IsValidGitBaseVersion>
$([System.Text.RegularExpressions.Regex]::IsMatch($(GitBaseVersion), $(_GitBaseVersionExpr)))
$([System.Text.RegularExpressions.Regex]::IsMatch($(GitBaseVersion), $(GitBaseVersionRegex)))
</IsValidGitBaseVersion>
<IsValidGitBaseVersion>$(IsValidGitBaseVersion.Trim())</IsValidGitBaseVersion>
</PropertyGroup>
Expand All @@ -853,14 +867,14 @@
<!-- Remove the initial optional 'v' or 'V' from the base version. -->
<GitBaseVersion Condition="$(GitBaseVersion.StartsWith('v'))">$(GitBaseVersion.TrimStart('v'))</GitBaseVersion>
<GitBaseVersion Condition="$(GitBaseVersion.StartsWith('V'))">$(GitBaseVersion.TrimStart('V'))</GitBaseVersion>
<GitBaseVersionMajor>$([System.Text.RegularExpressions.Regex]::Match($(GitBaseVersion), $(_GitBaseVersionExpr)).Groups['MAJOR'].Value)</GitBaseVersionMajor>
<GitBaseVersionMinor>$([System.Text.RegularExpressions.Regex]::Match($(GitBaseVersion), $(_GitBaseVersionExpr)).Groups['MINOR'].Value)</GitBaseVersionMinor>
<GitBaseVersionPatch>$([System.Text.RegularExpressions.Regex]::Match($(GitBaseVersion), $(_GitBaseVersionExpr)).Groups['PATCH'].Value)</GitBaseVersionPatch>
<GitBaseVersionMajor>$([System.Text.RegularExpressions.Regex]::Match($(GitBaseVersion), $(GitBaseVersionRegex)).Groups['MAJOR'].Value)</GitBaseVersionMajor>
<GitBaseVersionMinor>$([System.Text.RegularExpressions.Regex]::Match($(GitBaseVersion), $(GitBaseVersionRegex)).Groups['MINOR'].Value)</GitBaseVersionMinor>
<GitBaseVersionPatch>$([System.Text.RegularExpressions.Regex]::Match($(GitBaseVersion), $(GitBaseVersionRegex)).Groups['PATCH'].Value)</GitBaseVersionPatch>
<GitBaseVersionPatch Condition="'$(GitBaseVersionPatch)' == ''">0</GitBaseVersionPatch>
<GitSemVerMajor>$(GitBaseVersionMajor)</GitSemVerMajor>
<GitSemVerMinor>$(GitBaseVersionMinor)</GitSemVerMinor>
<GitSemVerPatch>$([MSBuild]::Add('$(GitBaseVersionPatch)', '$(GitCommits)'))</GitSemVerPatch>
<GitSemVerLabel>$([System.Text.RegularExpressions.Regex]::Match($(GitBaseVersion), $(_GitBaseVersionExpr)).Groups['LABEL'].Value)</GitSemVerLabel>
<GitSemVerLabel>$([System.Text.RegularExpressions.Regex]::Match($(GitBaseVersion), $(GitBaseVersionRegex)).Groups['LABEL'].Value)</GitSemVerLabel>
<GitSemVerDashLabel Condition="'$(GitSemVerLabel)' != ''" >-$(GitSemVerLabel)</GitSemVerDashLabel>
</PropertyGroup>

Expand Down
17 changes: 13 additions & 4 deletions src/GitInfo/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ Available MSBuild customizations:
will be used to find a base version.
Defaults to empty value (no ignoring).

$(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).

$(GitNameRevOptions): Options passed to git name-rev when finding
a branch name for the current commit (Detached head).
The default is '--refs=refs/heads/*'
Expand All @@ -107,7 +112,11 @@ Available MSBuild customizations:
use '--refs=refs/*'.
Refs can be included and excluded, see git name-rev docs.

$(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).
$(GitTagRegex): Regular expression used with git describe to filter the tags
to consider for base version lookup.
Defaults to * (all)

$(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.
Defaults to 'v?(?<MAJOR>\d+)\.(?<MINOR>\d+)\.(?<PATCH>\d+)(?:\-(?<LABEL>[\dA-Za-z\-\.]+))?$|^(?<LABEL>[\dA-Za-z\-\.]+)\-v?(?<MAJOR>\d+)\.(?<MINOR>\d+)\.(?<PATCH>\d+)$'

0 comments on commit 7c22783

Please sign in to comment.