-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathThisAssembly.Git.targets
104 lines (91 loc) · 8.08 KB
/
ThisAssembly.Git.targets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<Project>
<ItemGroup>
<!-- Make sure we're always private to the referencing project.
Prevents analyzers from "flowing out" of the referencing project. -->
<PackageReference Update="ThisAssembly.Git" PrivateAssets="all" PackTransitive="false" />
</ItemGroup>
<PropertyGroup>
<ThisAssemblyGitComment>Provides access to build-time git information</ThisAssemblyGitComment>
</PropertyGroup>
<PropertyGroup Condition="'$(RepositoryBranch)' == ''">
<!-- GitHub Actions: https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables -->
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(GITHUB_REF)' != '' and $(GITHUB_REF.Contains('refs/pull/'))">pr$(GITHUB_REF.Replace('refs/pull/', '').Replace('/merge', ''))</RepositoryBranch>
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(GITHUB_REF)' != ''">$(GITHUB_REF.Replace('refs/heads/', '').Replace('refs/tags/', ''))</RepositoryBranch>
<!-- Azure DevOps: https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables -->
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(BUILD_SOURCEBRANCH)' != ''">$(BUILD_SOURCEBRANCH.Replace('refs/heads/', '').Replace('refs/tags/', ''))</RepositoryBranch>
<!-- AppVeyor: https://www.appveyor.com/docs/environment-variables/ -->
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(APPVEYOR_PULL_REQUEST_NUMBER)' != ''">pr$(APPVEYOR_PULL_REQUEST_NUMBER)</RepositoryBranch>
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(APPVEYOR_REPO_TAG_NAME)' != ''">$(APPVEYOR_REPO_TAG_NAME)</RepositoryBranch>
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(APPVEYOR_REPO_BRANCH)' != ''">$(APPVEYOR_REPO_BRANCH)</RepositoryBranch>
<!-- TeamCity: https://www.jetbrains.com/help/teamcity/predefined-build-parameters.html#Branch-Related+Parameters -->
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(TEAMCITY_BUILD_BRANCH)' != ''">$(TEAMCITY_BUILD_BRANCH)</RepositoryBranch>
<!--TravisCI: https://docs.travis-ci.com/user/environment-variables/ -->
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(TRAVIS_PULL_REQUEST)' != '' and '$(TRAVIS_PULL_REQUEST)' != 'false'">pr$(TRAVIS_PULL_REQUEST)</RepositoryBranch>
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(TRAVIS_BRANCH)' != ''">$(TRAVIS_BRANCH)</RepositoryBranch>
<!-- CircleCI: https://circleci.com/docs/2.0/env-vars/ -->
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(CIRCLE_PR_NUMBER)' != ''">pr$(CIRCLE_PR_NUMBER)</RepositoryBranch>
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(CIRCLE_TAG)' != ''">$(CIRCLE_TAG)</RepositoryBranch>
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(CIRCLE_BRANCH)' != ''">$(CIRCLE_BRANCH)</RepositoryBranch>
<!-- GitLab: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html -->
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(CI_COMMIT_TAG)' != ''">$(CI_COMMIT_TAG)</RepositoryBranch>
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(CI_MERGE_REQUEST_IID)' != ''">pr$(CI_MERGE_REQUEST_IID)</RepositoryBranch>
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(CI_EXTERNAL_PULL_REQUEST_IID)' != ''">pr$(CI_EXTERNAL_PULL_REQUEST_IID)</RepositoryBranch>
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(CI_COMMIT_BRANCH)' != ''">$(CI_COMMIT_BRANCH)</RepositoryBranch>
<!-- Buddy: https://buddy.works/docs/pipelines/environment-variables#default-environment-variables -->
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(BUDDY_EXECUTION_PULL_REQUEST_NO)' != ''">pr$(BUDDY_EXECUTION_PULL_REQUEST_NO)</RepositoryBranch>
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(BUDDY_EXECUTION_TAG)' != ''">$(BUDDY_EXECUTION_TAG)</RepositoryBranch>
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(BUDDY_EXECUTION_BRANCH)' != ''">$(BUDDY_EXECUTION_BRANCH)</RepositoryBranch>
<!-- Jenkins: https://plugins.jenkins.io/git/#plugin-content-environment-variables -->
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(GIT_LOCAL_BRANCH)' != ''">$(GIT_LOCAL_BRANCH)</RepositoryBranch>
</PropertyGroup>
<!-- Make sure git info is available before calling source generators -->
<Target Name="InitializeGitInformation"
BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun"
DependsOnTargets="InitializeSourceControlInformation;$(SourceLinkUrlInitializerTargets)">
<PropertyGroup Condition="'$(SourceControlInformationFeatureSupported)' == 'true'">
<!-- The project must specify PublishRepositoryUrl=true in order to publish the URL, in order to prevent inadvertent leak of internal URL. -->
<RepositoryUrl Condition="'$(RepositoryUrl)' == '' and '$(PublishRepositoryUrl)' == 'true'">$(PrivateRepositoryUrl)</RepositoryUrl>
</PropertyGroup>
<PropertyGroup Condition="'$(SourceRevisionId)' != ''">
<RepositoryCommit Condition="'$(RepositoryCommit)' == ''">$(SourceRevisionId)</RepositoryCommit>
<RepositorySha Condition="'$(RepositorySha)' == ''">$(SourceRevisionId.Substring(0, 9))</RepositorySha>
</PropertyGroup>
<!-- Add SourceRoot as a project property too -->
<ItemGroup>
<_ThisAssemblyGitSourceRoot Include="@(SourceRoot -> WithMetadataValue('SourceControl', 'git'))"
Exclude="@(SourceRoot -> HasMetadata('NestedRoot'))" />
</ItemGroup>
<PropertyGroup>
<RepositoryRoot>@(_ThisAssemblyGitSourceRoot)</RepositoryRoot>
<SourceLinkUrl>@(_ThisAssemblyGitSourceRoot -> '%(SourceLinkUrl)')</SourceLinkUrl>
</PropertyGroup>
<Warning Code="TA001" Text="A valid SourceLink provider does not seem to be installed for the current repository/project. Some values may be empty." Condition="'$(SourceLinkUrl)' == ''" />
<PropertyGroup Condition="'$(RepositoryBranch)' == '' and '$(RepositoryRoot)' != ''">
<!-- We may not be in CI at all. If we got a git repo root, we can directly read HEAD -->
<RepositoryHead>$(RepositoryRoot).git/HEAD</RepositoryHead>
<RepositoryBranch Condition="Exists($(RepositoryHead))">$([System.IO.File]::ReadAllText($(RepositoryHead)).Trim())</RepositoryBranch>
<RepositoryBranch Condition="$(RepositoryBranch) != ''">$(RepositoryBranch.TrimStart("ref:").Trim().TrimStart("refs").Trim("/").TrimStart("heads").Trim("/").Trim())</RepositoryBranch>
</PropertyGroup>
</Target>
<Target Name="PrepareGitConstants"
BeforeTargets="PrepareConstants"
DependsOnTargets="InitializeGitInformation">
<PropertyGroup>
<EmptySourceControlValue>[pending build]</EmptySourceControlValue>
</PropertyGroup>
<ItemGroup Condition="'$(EnableSourceControlManagerQueries)' != 'true'">
<Constant Include="Git.Branch" Value="$(EmptySourceControlValue)" Root="." RootComment="$(ThisAssemblyGitComment)" />
<Constant Include="Git.Commit" Value="$(EmptySourceControlValue)" Root="." RootComment="$(ThisAssemblyGitComment)" />
<Constant Include="Git.Sha" Value="$(EmptySourceControlValue)" Root="." RootComment="$(ThisAssemblyGitComment)" />
<Constant Include="Git.Root" Value="$(EmptySourceControlValue)" Root="." RootComment="$(ThisAssemblyGitComment)" />
<Constant Include="Git.Url" Value="$(EmptySourceControlValue)" Root="." RootComment="$(ThisAssemblyGitComment)" />
</ItemGroup>
<ItemGroup Condition="'$(EnableSourceControlManagerQueries)' == 'true'">
<Constant Include="Git.Branch" Value="$(RepositoryBranch)" Root="." RootComment="$(ThisAssemblyGitComment)" />
<Constant Include="Git.Commit" Value="$(RepositoryCommit)" Root="." RootComment="$(ThisAssemblyGitComment)" />
<Constant Include="Git.Sha" Value="$(RepositorySha)" Root="." RootComment="$(ThisAssemblyGitComment)" />
<Constant Include="Git.Root" Value="$(RepositoryRoot.Replace('\', '/'))" Root="." RootComment="$(ThisAssemblyGitComment)" />
<Constant Include="Git.Url" Value="$(RepositoryUrl)" Root="." RootComment="$(ThisAssemblyGitComment)" />
</ItemGroup>
</Target>
</Project>