-
Notifications
You must be signed in to change notification settings - Fork 865
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
Branch name for git detached head #1098
Changes from 8 commits
2634cc8
34cabfb
ff92a6e
561be0c
2375d60
8406788
9204df9
3d9a5b5
5f474cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.DocAsCode.Common.Git | ||
{ | ||
using System; | ||
using System.Linq; | ||
using System.IO; | ||
using System.Text; | ||
using System.Collections.Concurrent; | ||
|
@@ -22,6 +23,16 @@ public static class GitUtility | |
private static readonly string GetOriginUrlCommand = "config --get remote.origin.url"; | ||
private static readonly string GetLocalHeadIdCommand = "rev-parse HEAD"; | ||
private static readonly string GetRemoteHeadIdCommand = "rev-parse @{u}"; | ||
|
||
private static readonly string[] BuildSystemBranchName = new[] | ||
{ | ||
"APPVEYOR_REPO_BRANCH", // AppVeyor | ||
"Git_Branch", // Team City | ||
"CI_BUILD_REF_NAME", // GitLab CI | ||
"GIT_LOCAL_BRANCH", // Jenkins | ||
"GIT_BRANCH", // Jenkins | ||
"BUILD_SOURCEBRANCHNAME" // VSO Agent | ||
}; | ||
|
||
private static readonly ConcurrentDictionary<string, GitRepoInfo> Cache = new ConcurrentDictionary<string, GitRepoInfo>(); | ||
|
||
|
@@ -112,22 +123,47 @@ private static GitDetail GetFileDetailCore(string filePath) | |
private static GitRepoInfo GetRepoInfoCore(string directory) | ||
{ | ||
var repoRootPath = RunGitCommandAndGetFirstLine(directory, GetRepoRootCommand); | ||
var localBranch = GetLocalBranchName(repoRootPath); | ||
|
||
// The "docfx..". environment variable specifies the branch name to use. | ||
var localBranch = Environment.GetEnvironmentVariable("DOCFX_SOURCE_BRANCH_NAME"); | ||
string remoteBranch; | ||
try | ||
|
||
// Many build systems use a "detached head", which means that the normal git commands | ||
// to get branch names do not work. Thankfully, they set an environment variable. | ||
if (string.IsNullOrEmpty(localBranch)) | ||
{ | ||
remoteBranch = RunGitCommandAndGetFirstLine(repoRootPath, GetRemoteBranchCommand); | ||
var index = remoteBranch.IndexOf('/'); | ||
if (index > 0) | ||
var isDetached = "HEAD" == RunGitCommandAndGetFirstLine(repoRootPath, GetLocalBranchCommand); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
merge with GetLocalBranchName? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the addition of I want nothing to do with this. Its evil. Not sure what you mean by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However the code here is exactly |
||
if (isDetached) | ||
{ | ||
remoteBranch = remoteBranch.Substring(index + 1); | ||
localBranch = BuildSystemBranchName | ||
.Select(Environment.GetEnvironmentVariable) | ||
.Where(name => !string.IsNullOrEmpty(name)) | ||
.FirstOrDefault(); | ||
} | ||
} | ||
catch (Exception ex) | ||
|
||
if (!string.IsNullOrEmpty(localBranch)) | ||
{ | ||
Logger.LogInfo($"Can't find remote branch in this repo and fallback to use local branch [{localBranch}]: {ex.Message}"); | ||
remoteBranch = localBranch; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LogInfo to tell users that branch name is found in env |
||
Logger.LogInfo($"Using branch '{localBranch}' from the environment variable."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also log the environment variable name |
||
} | ||
else | ||
{ | ||
localBranch = GetLocalBranchName(repoRootPath); | ||
try | ||
{ | ||
remoteBranch = RunGitCommandAndGetFirstLine(repoRootPath, GetRemoteBranchCommand); | ||
var index = remoteBranch.IndexOf('/'); | ||
if (index > 0) | ||
{ | ||
remoteBranch = remoteBranch.Substring(index + 1); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.LogInfo($"Can't find remote branch in this repo and fallback to use local branch [{localBranch}]: {ex.Message}"); | ||
remoteBranch = localBranch; | ||
} | ||
} | ||
|
||
var originUrl = RunGitCommandAndGetFirstLine(repoRootPath, GetOriginUrlCommand); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.DocAsCode.Common.Tests | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
using Xunit; | ||
using YamlDotNet.Core; | ||
|
||
using Microsoft.DocAsCode.Common.Git; | ||
using Microsoft.DocAsCode.YamlSerialization; | ||
|
||
[Trait("Owner", "makaretu")] | ||
public class GitUtilityTest | ||
{ | ||
[Fact] | ||
public void Environment_ForBranchName() | ||
{ | ||
const string envName = "DOCFX_SOURCE_BRANCH_NAME"; | ||
var original = Environment.GetEnvironmentVariable(envName); | ||
try | ||
{ | ||
Environment.SetEnvironmentVariable(envName, "special-branch"); | ||
var info = GitUtility.GetFileDetail(Directory.GetCurrentDirectory()); | ||
Assert.Equal("special-branch", info.RemoteBranch); | ||
} | ||
finally | ||
{ | ||
Environment.SetEnvironmentVariable(envName, original); | ||
} | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Support
DOCFX_SOURCE_BRANCH_NAME
is enough? Use documentation to tell CI users how to configure environment variables with different build systems?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to have docfx run on common the build system without any special instructions.