Skip to content

Commit

Permalink
Improve detection of git root by leveraging SourceLink
Browse files Browse the repository at this point in the history
The built-in .NET SDKs incorporate source control information already. If we find that, we don't really need to read from the file system the HEAD branch.
  • Loading branch information
kzu committed Feb 18, 2025
1 parent 6393427 commit dbe3922
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 64 deletions.
61 changes: 1 addition & 60 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -76,36 +76,6 @@
<OutDir>$(OutputPath)</OutDir>
</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>
</PropertyGroup>

<PropertyGroup>
<!-- Default to Just Works resources generation. See https://www.cazzulino.com/resources.html -->
<CoreCompileDependsOn>CoreResGen;$(CoreCompileDependsOn)</CoreCompileDependsOn>
Expand Down Expand Up @@ -140,38 +110,9 @@
<ProjectProperty Include="RepositoryUrl" />
</ItemGroup>

<!-- Make sure the source control info is available before calling source generators -->
<Target Name="EnsureProjectInformation"
BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun"
AfterTargets="InitializeSourceControlInformation"
DependsOnTargets="InitializeSourceControlInformation">

<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>
<!-- This allows the commit label added to the InformationalVersion to be the short sha instead :) -->
<SourceRevisionId>$(RepositorySha)</SourceRevisionId>
</PropertyGroup>

<!-- Add SourceRoot as a project property too -->
<ItemGroup>
<_GitSourceRoot Include="@(SourceRoot -> WithMetadataValue('SourceControl', 'git'))" />
</ItemGroup>

<PropertyGroup>
<RepositoryRoot>@(_GitSourceRoot)</RepositoryRoot>
</PropertyGroup>

</Target>

<Target Name="UpdatePackageMetadata"
BeforeTargets="PrepareForBuild;GenerateMSBuildEditorConfigFileShouldRun;GetAssemblyVersion;GetPackageMetadata;GenerateNuspec;Pack"
DependsOnTargets="EnsureProjectInformation"
DependsOnTargets="InitializeSourceControlInformation"
Condition="'$(SourceControlInformationFeatureSupported)' == 'true' And
'$(IsPackable)' == 'true'">
<PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/Directory.targets
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<Project InitialTargets="SetLocalVersion">

<Import Project="ThisAssembly.Git\ThisAssembly.Git.targets" Condition="'$(IsPackable)' != 'false'" />

<PropertyGroup>
<Description Condition="'$(IsPackable)' != 'false'">$(Description)

Expand Down
11 changes: 7 additions & 4 deletions src/ThisAssembly.Git/ThisAssembly.Git.targets
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<!-- Make sure git info is available before calling source generators -->
<Target Name="InitializeGitInformation"
BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun"
AfterTargets="InitializeSourceControlInformation"
DependsOnTargets="InitializeSourceControlInformation;$(SourceLinkUrlInitializerTargets)">

<PropertyGroup Condition="'$(SourceControlInformationFeatureSupported)' == 'true'">
Expand All @@ -71,10 +72,12 @@
<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>
<!-- If we didnd't populate from CI/ENV, we can still use the source control provider itself to get the branch name -->
<_ThisAssemblyGitBranchName>@(_ThisAssemblyGitSourceRoot -> '%(BranchName)')</_ThisAssemblyGitBranchName>
<!-- The above only works on .NET SDK 9.0+, so account for 8.0 implicit nuget package -->

<_ThisAssemblyGitBranchName Condition="'$(_ThisAssemblyGitBranchName)' == ''">$([System.IO.File]::ReadAllText('$(RepositoryRoot).git/HEAD').Trim())</_ThisAssemblyGitBranchName>
<RepositoryBranch>$(_ThisAssemblyGitBranchName.TrimStart("ref:").Trim().Replace('refs/heads/', '').Replace('refs/tags/', ''))</RepositoryBranch>
</PropertyGroup>

</Target>
Expand Down

0 comments on commit dbe3922

Please sign in to comment.