Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race conditions #330

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 37 additions & 44 deletions src/GitInfo/build/GitInfo.targets
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@

Customization:

$(GitVersion): set to 'false' to avoid setting Version and PackageVersion
to a default version with format:
$(GitVersion): set to 'false' to avoid setting Version and PackageVersion
to a default version with format:
$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$(GitBranch).$(GitCommit)

$(GitThisAssembly): set to 'false' to prevent assembly
metadata and constants generation.

$(GitThisAssemblyMetadata): set to 'false' to prevent assembly
metadata generation only. Defaults
to 'false'. If 'true', it will also
provide assembly metadata attributes
$(GitThisAssemblyMetadata): set to 'false' to prevent assembly
metadata generation only. Defaults
to 'false'. If 'true', it will also
provide assembly metadata attributes
for each of the populated values.

$(ThisAssemblyNamespace): allows overriding the namespace
Expand Down Expand Up @@ -70,27 +70,27 @@
in a previous build in a GitInfo.cache for
performance reasons.
Defaults to empty value (no ignoring).

$(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.
has to end with a path seperator
Defaults to empty value ('$(IntermediateOutputPath)').

$(GitTagRegex): Regular expression used with git describe to filter the tags
$(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
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+)$'

$(GitCommitDateFormat): The value passed as the format option when trying to retrieve
the git commit date
Defaults to %%cI (windows) or %cI (non windows)

==============================================================
-->

Expand Down Expand Up @@ -150,8 +150,11 @@
<!-- Cache file used to avoid running all git commands. Only GitRoot will be retrieved to determine the path of this cache file. -->
<_GitInfoFile>$(GitCachePath)GitInfo.cache</_GitInfoFile>

<!-- Stores value of GitIsDirty variable from last execution, file is changed when the value changes betwee runs. -->
<!-- Stores value of GitIsDirty variable from last execution. File is changed when the value changes between runs. -->
<_GitIsDirtyFile>$(GitCachePath)GitIsDirty.cache</_GitIsDirtyFile>

<!-- Stores value of _GitHeadPath variable from last execution. Added as an input for the fast up-to-date check VS performs. -->
<_GitHeadFile>$(GitCachePath)GitHead.cache</_GitHeadFile>
</PropertyGroup>

<Target Name="GitInfoReport" DependsOnTargets="GitInfo;GitVersion">
Expand Down Expand Up @@ -351,7 +354,7 @@
<GitIsDirty>0</GitIsDirty>
</PropertyGroup>

<WriteLinesToFile File="$(_GitIsDirtyFile)" Lines="$(GitIsDirty)" Overwrite="true" WriteOnlyWhenDifferent="true"/>
<WriteLinesToFile File="$(_GitIsDirtyFile)" Lines="$(GitIsDirty)" Overwrite="true" WriteOnlyWhenDifferent="true" ContinueOnError="true" />
</Target>

<Target Name="_GitInputs" DependsOnTargets="_GitRoot" Returns="@(_GitInput)">
Expand Down Expand Up @@ -381,12 +384,12 @@
<Delete Files="$(_GitInfoFile)" />
</Target>

<Target Name="_GitReadCache" Condition="Exists('$(_GitInfoFile)') And '$(SkipReadGitCache)' != 'true' ">
<PropertyGroup>
<_GitCachedInfo>$([System.IO.File]::ReadAllText('$(_GitInfoFile)'))</_GitCachedInfo>
</PropertyGroup>
<Target Name="_GitReadCache" Condition="Exists('$(_GitInfoFile)') And '$(SkipReadGitCache)' != 'true'">
<ReadLinesFromFile File="$(_GitInfoFile)" ContinueOnError="true">
<Output TaskParameter="Lines" ItemName="_GitCachedInfo" />
</ReadLinesFromFile>

<CreateItem Include="GitInfo" AdditionalMetadata="$(_GitCachedInfo.Split(';'))">
<CreateItem Include="GitInfo" AdditionalMetadata="@(_GitCachedInfo -> TrimEnd(';'))">
<Output TaskParameter="Include" ItemName="GitInfo" />
</CreateItem>

Expand Down Expand Up @@ -414,9 +417,7 @@
</Target>

<Target Name="_GitRepositoryUrl" Returns="$(GitRepositoryUrl)" DependsOnTargets="_GitRoot"
Condition="'$(GitRoot)' != '' and '$(GitRepositoryUrl)' == ''"
Inputs="@(_GitInput)"
Outputs="$(_GitInfoFile)">
Condition="'$(GitRoot)' != '' and '$(GitRepositoryUrl)' == ''">

<Exec Command="$(GitExe) config --get remote.$(GitRemote).url"
Condition="'$(GitRepositoryUrl)' == ''"
Expand Down Expand Up @@ -444,8 +445,6 @@

<Target Name="_GitCommitDate" Returns="$(GitCommitDate)"
DependsOnTargets="_GitRoot"
Inputs="@(_GitInput)"
Outputs="$(_GitInfoFile)"
Condition="'$(GitRoot)' != '' And '$(GitCommitDate)' == ''">

<Exec Command='$(GitExe) -c log.showSignature=false show --format=$(GitCommitDateFormat) -s'
Expand All @@ -467,10 +466,8 @@
</Target>

<Target Name="_GitBranch" Returns="$(GitBranch)"
DependsOnTargets="_GitInputs"
Condition="'$(GitBranch)' == ''"
Inputs="@(_GitInput)"
Outputs="$(_GitInfoFile)">
DependsOnTargets="_GitInputs"
Condition="'$(GitBranch)' == ''">

<Exec Command='$(GitExe) rev-parse --abbrev-ref HEAD'
Condition="'$(GitRoot)' != ''"
Expand All @@ -487,7 +484,7 @@

<!-- CI systems may checkout the specific commit, rather than the branch, so we need to fallback -->
<Exec Command='$(GitExe) name-rev --name-only $(GitNameRevOptions) HEAD'
Condition=" '$(GitBranch)' == '' "
Condition="'$(GitRoot)' != '' and '$(GitBranch)' == ''"
EchoOff='true'
StandardErrorImportance="low"
StandardOutputImportance="low"
Expand All @@ -506,8 +503,6 @@

<Target Name="_GitCommit" Returns="$(GitCommit)"
DependsOnTargets="_GitRoot"
Inputs="@(_GitInput)"
Outputs="$(_GitInfoFile)"
Condition="'$(GitRoot)' != '' And '$(GitCommit)' == ''">

<Exec Command='$(GitExe) -c log.showSignature=false log --format=format:$(_ShortShaFormat) -n 1'
Expand Down Expand Up @@ -578,7 +573,7 @@
<Version>$(GitSemVerMajor).$(GitSemVerMinor).$(GitSemVerPatch)$(GitSemVerDashLabel)+$([System.Text.RegularExpressions.Regex]::Replace($(GitBranch), "[^0-9A-Za-z-]", "-")).$(GitCommit)</Version>
<PackageVersion>$(Version)</PackageVersion>
</PropertyGroup>
</Target>
</Target>

<Target Name="_GitBaseVersionFile" Returns="$(GitBaseVersion)"
Inputs="@(_GitInput)"
Expand Down Expand Up @@ -986,25 +981,23 @@

</Target>

<!-- This combination of prop/item/target fix incremental builds after HEAD changes. -->
<PropertyGroup>
<_GitCacheFullPath>$([System.IO.Path]::Combine($(MSBuildProjectDirectory), $(GitCachePath)))</_GitCacheFullPath>
<!-- We read the path to the HEAD file, written in a previously run _GitWriteHeadPath target -->
<_GitHeadPath Condition="Exists('$(_GitCacheFullPath)GitHead.cache')">$([System.IO.File]::ReadAllText('$(_GitCacheFullPath)GitHead.cache').Trim())</_GitHeadPath>
</PropertyGroup>
<!-- This combination of target/item/target fixes incremental builds after HEAD changes. -->
<Target Name="_GitReadHeadPath" AfterTargets="GitVersion">
<ReadLinesFromFile File="$(_GitHeadFile)" ContinueOnError="true">
<Output TaskParameter="Lines" ItemName="_GitHeadPath" />
</ReadLinesFromFile>
</Target>

<ItemGroup>
<!-- And if the HEAD path exists, we add it as an input for the fast up-to-date check VS performs -->
<UpToDateCheckInput Condition="'$(_GitHeadPath)' != '' and Exists('$(_GitHeadPath)')" Include="$(_GitHeadPath)" />
<UpToDateCheckInput Condition="'@(_GitHeadPath)' != '' and Exists('@(_GitHeadPath)')" Include="@(_GitHeadPath)" />
</ItemGroup>

<!-- NOTE: we write the path to HEAD after first calculating a version, since we can be anywhere in
the solution structure. This file will only be written if there's a change in the git root,
<!-- NOTE: we write the path to HEAD after first calculating a version, since we can be anywhere in
the solution structure. This file will only be written if there's a change in the git root,
which should never happen. -->
<Target Name="_GitWriteHeadPath" AfterTargets="GitVersion">
<WriteLinesToFile Lines="$(GitRoot).git\HEAD"
File="$(GitCachePath)GitHead.cache"
Overwrite="true" WriteOnlyWhenDifferent="true" />
<WriteLinesToFile File="$(_GitHeadFile)" Lines="$(GitRoot).git\HEAD" Overwrite="true" WriteOnlyWhenDifferent="true" ContinueOnError="true" />
</Target>

<!--
Expand Down
Loading