Skip to content

Instantly share code, notes, and snippets.

@assarbad
Created August 9, 2023 22:03
Show Gist options
  • Save assarbad/6d92afcf8c8fc6f2f2d9c596b07fc5bc to your computer and use it in GitHub Desktop.
Save assarbad/6d92afcf8c8fc6f2f2d9c596b07fc5bc to your computer and use it in GitHub Desktop.
Just playing around with the retrieval of the Git revision from within MSBuild and caching it ...
<?xml version="1.0" encoding="utf-8"?>
<Project InitialTargets="GetGitRevision" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<GitWorkTreeRootDir>$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), '.git\index'))</GitWorkTreeRootDir>
<GitWorkTreeRootDir Condition="'$(GitWorkTreeRootDir)' != '' and !HasTrailingSlash('$(GitWorkTreeRootDir)')">$(GitWorkTreeRootDir)\</GitWorkTreeRootDir>
<GitDirSentinelFile Condition="Exists('$(GitWorkTreeRootDir).git\index')">$(GitWorkTreeRootDir).git\index</GitDirSentinelFile>
<GitRevPropsFile>$(MSBuildThisFileDirectory)__GitRev.props</GitRevPropsFile>
</PropertyGroup>
<Target Name="FindGitExecutablePath" Condition="'$(GitDirSentinelFile)' != ''">
<PropertyGroup>
<_GitExecutableToFind>git.exe</_GitExecutableToFind>
</PropertyGroup>
<ItemGroup>
<_GitExecPaths Include="$([System.Environment]::GetEnvironmentVariable('PATH').Split(';'))" />
<_GitFilteredExecPaths Include="@(_GitExecPaths)" Condition="Exists('%(FullPath)\$(_GitExecutableToFind)')" />
<_GitFilteredExecPaths Include="@(_GitFilteredExecPaths->Reverse())" Condition="Exists('%(FullPath)\$(_GitExecutableToFind)')" />
</ItemGroup>
<PropertyGroup>
<GitExecutableFullPath Condition="'%(_GitFilteredExecPaths.Identity)' != ''">%(_GitFilteredExecPaths.Identity)\$(_GitExecutableToFind)</GitExecutableFullPath>
<GitExecutableFullPath Condition="'%(_GitFilteredExecPaths.Identity)' == ''"></GitExecutableFullPath>
</PropertyGroup>
</Target>
<Target Name="ActualGetGitRevision" DependsOnTargets="FindGitExecutablePath" Condition="'$(GitDirSentinelFile)' != ''">
<Message Importance="low" Text="Retrieved %24(GitWorkTreeRootDir) = '$(GitWorkTreeRootDir)'" />
<Message Importance="low" Text="Detecting Git revision, since '$(GitDirSentinelFile)' was found. Using %24(GitExecutableFullPath) = '$(GitExecutableFullPath)'." Condition="'$(GitExecutableFullPath)' != '' and '$(GitWorkTreeRootDir)' != ''" />
<Exec Command="&quot;$(GitExecutableFullPath)&quot; -C &quot;$(GitWorkTreeRootDir)\&quot; rev-parse HEAD" EchoOff="true" Condition="'$(GitExecutableFullPath)' != '' and '$(GitWorkTreeRootDir)' != ''" ConsoleToMsBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="RetrievedGitRevision" />
</Exec>
<Message Importance="low" Text="Retrieved for HEAD %24(RetrievedGitRevision) = '$(RetrievedGitRevision)'." Condition="'$(RetrievedGitRevision)' != ''" />
</Target>
<Target Name="GetGitRevision" DependsOnTargets="ActualGetGitRevision">
<Message Importance="low" Text="%24(Exists('$(GitRevPropsFile)')) = $([System.IO.File]::Exists('$(GitRevPropsFile)'))" />
<Message Importance="low" Text="%24([System.IO.File]::GetLastWriteTime('$(GitDirSentinelFile)').Ticks) = $([System.IO.File]::GetLastWriteTime('$(GitDirSentinelFile)').Ticks) [$([System.IO.File]::GetLastWriteTime('$(GitDirSentinelFile)'))]" />
<Message Importance="low" Text="%24([System.IO.File]::GetLastWriteTime('$(GitRevPropsFile)').Ticks) = $([System.IO.File]::GetLastWriteTime('$(GitRevPropsFile)').Ticks) [$([System.IO.File]::GetLastWriteTime('$(GitRevPropsFile)'))]" />
<CallTarget Targets="WriteNewGitRevsPropsFile" />
</Target>
<Target Name="WriteNewGitRevsPropsFile" Condition="'$(RetrievedGitRevision)' != '' AND '$(RetrievedGitRevision)' != '$(GitRevision)'">
<PropertyGroup>
<GitRevision>$(RetrievedGitRevision)</GitRevision>
</PropertyGroup>
<Message Importance="high" Text="%24(GitRevPropsFile) = '$(GitRevPropsFile)'" />
<Message Importance="low" Text="Old revision HEAD %24(GitRevision) = '$(GitRevision)'." Condition="'$(GitRevision)' != ''" />
<ItemGroup Condition="'$(GitRevision)' != ''">
<MSBuildSnippetLine Include='&lt;%3Fxml version="1.0" encoding="utf-8"%3F&gt;' />
<MSBuildSnippetLine Include='&lt;!-- File was auto-generated by $(MSBuildThisFile) --&gt;' />
<MSBuildSnippetLine Include='&lt;Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;' />
<MSBuildSnippetLine Include='%09&lt;PropertyGroup&gt;' />
<MSBuildSnippetLine Include='%09%09&lt;GitRevision&gt;$(GitRevision)&lt;/GitRevision&gt;' />
<MSBuildSnippetLine Include='%09&lt;/PropertyGroup&gt;' />
<MSBuildSnippetLine Include='%09&lt;Target Name="SetClCompileGitRev" BeforeTargets="ClCompile"&gt;' />
<MSBuildSnippetLine Include='%09%09&lt;ItemGroup&gt;' />
<MSBuildSnippetLine Include='%09%09%09&lt;ClCompile&gt;' />
<MSBuildSnippetLine Include='%09%09%09%09&lt;PreprocessorDefinitions&gt;GIT_COMMIT="%24(GitRevision)"%3B%25(PreprocessorDefinitions)&lt;/PreprocessorDefinitions&gt;' />
<MSBuildSnippetLine Include='%09%09%09&lt;/ClCompile&gt;' />
<MSBuildSnippetLine Include='%09%09&lt;/ItemGroup&gt;' />
<MSBuildSnippetLine Include='%09&lt;/Target&gt;' />
<MSBuildSnippetLine Include='%09&lt;Target Name="SetResourceCompileGitRev" BeforeTargets="ResourceCompile"&gt;' />
<MSBuildSnippetLine Include='%09%09&lt;ItemGroup&gt;' />
<MSBuildSnippetLine Include='%09%09%09&lt;ResourceCompile&gt;' />
<MSBuildSnippetLine Include='%09%09%09%09&lt;PreprocessorDefinitions&gt;GIT_COMMIT="%24(GitRevision)"%3B%25(PreprocessorDefinitions)&lt;/PreprocessorDefinitions&gt;' />
<MSBuildSnippetLine Include='%09%09%09&lt;/ResourceCompile&gt;' />
<MSBuildSnippetLine Include='%09%09&lt;/ItemGroup&gt;' />
<MSBuildSnippetLine Include='%09&lt;/Target&gt;' />
<MSBuildSnippetLine Include='&lt;/Project&gt;' />
</ItemGroup>
<WriteLinesToFile Condition="'$(GitRevision)' != ''" File="$(GitRevPropsFile)" Lines="@(MSBuildSnippetLine)" Overwrite="true" WriteOnlyWhenDifferent="true" />
</Target>
<ImportGroup>
<Import Project="$(GitRevPropsFile)" Condition="Exists('$(GitRevPropsFile)')" />
</ImportGroup>
</Project>
@assarbad
Copy link
Author

assarbad commented Aug 9, 2023

This was an abandoned approach I tried.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment