build/targets/GitHash.props (33 lines of code) (raw):
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -->
<!--
Copied from RepoToolset. Might be slightly modified to adjust for the current F# build system specifics if necessary.
-->
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!--
Defines the following properties:
GitHeadSha
-->
<PropertyGroup>
<RepoRoot>$(MSBuildThisFileDirectory)..\..\</RepoRoot>
</PropertyGroup>
<Choose>
<When Condition="'$(BUILD_SOURCEVERSION)' != ''">
<PropertyGroup>
<GitHeadSha>$(BUILD_SOURCEVERSION)</GitHeadSha>
</PropertyGroup>
</When>
<When Condition="'$(BUILD_SOURCEVERSION)' == '' and '$(GIT_COMMIT)' != ''">
<PropertyGroup>
<GitHeadSha>$(GIT_COMMIT)</GitHeadSha>
</PropertyGroup>
</When>
<When Condition="'$(CI)' != '1'">
<PropertyGroup>
<GitHeadSha><developer build></GitHeadSha>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<GitHeadSha></GitHeadSha>
<_DotGitDir>$(RepoRoot).git</_DotGitDir>
<_HeadFileContent Condition="Exists('$(_DotGitDir)/HEAD')">$([System.IO.File]::ReadAllText('$(_DotGitDir)/HEAD').Trim())</_HeadFileContent>
<_RefPath Condition="$(_HeadFileContent.StartsWith('ref: '))">$(_DotGitDir)/$(_HeadFileContent.Substring(5))</_RefPath>
<GitHeadSha Condition="'$(_RefPath)' != '' and Exists('$(_RefPath)')">$([System.IO.File]::ReadAllText('$(_RefPath)').Trim())</GitHeadSha>
<GitHeadSha Condition="'$(_HeadFileContent)' != '' and '$(_RefPath)' == ''">$(_HeadFileContent)</GitHeadSha>
</PropertyGroup>
</Otherwise>
</Choose>
</Project>