Skip to content
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

Merged
merged 9 commits into from
Jan 11, 2017
Merged

Conversation

richardschneider
Copy link
Contributor

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. The various environment variable names was obtained from https://github.com/GitTools/GitVersion

Bug #1097

Here's an example of it now working on AppVeyor https://ci.appveyor.com/project/richardschneider/docfx-seed/build/1.0.8

@dnfclas
Copy link

dnfclas commented Jan 1, 2017

Hi @richardschneider, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!
We will now validate the agreement and then real humans will evaluate your PR.

TTYL, DNFBOT;

@qinezh
Copy link
Contributor

qinezh commented Jan 1, 2017

Hi @richardschneider , thanks for your contribution. From my point, I don't think DocFX should handle the case. Consider a scenario, what if users just need generate theview source url with commit id instead of branch name? And, it seems a litter weird to add these specific logic in an utility class... Maybe you could add a post processor to do it if you really need it.

My suggestion is that you need resolve the issue in CI tools instead of in DocFX. Such as running command git checkout master after step that CI has cloned you repo.

What do you think about it?

@richardschneider
Copy link
Contributor Author

git checkout master will not work because then I will be building the doc against the latest source code. AppVeyor uses the detached head so the doc is built against the specified commit or a Pull Request. An advantage is that the CI can be used to build from history.

AppVeyor is popular in open source dev because it uses a Windows VM and is free. My workflow is simple: Build code, Test Code, Publish to Nuget, Generate Doc, publish to gh-pages.

docfx does not work in this environment. It has the wrong branch name in the generated .yml files. How is this not a docfx bug?

The logic was placed in the Utility class because this is where the bug is. It uses git rev-parse --abbrev-ref HEAD to get the local branch name. This command does not work in a detached head and always returns 'HEAD`.

@richardschneider
Copy link
Contributor Author

richardschneider commented Jan 2, 2017 via email

@qinezh
Copy link
Contributor

qinezh commented Jan 2, 2017

What if users just need generate the view source url with commit id instead of branch name in AppVeyor?

@richardschneider
Copy link
Contributor Author

@qinezh I don't understand your concern about view source with commit id..

I am simply fixing the branch name for an edge case (a git detached head).

Existing code will work as before. This fix has nothing to do with a commit id.

@vicancy
Copy link
Contributor

vicancy commented Jan 5, 2017

The concern is that the code here highly dependents on CI's implementation.
How about a environment variable for docfx to fallback to, e.g. DOCFX_SOURCE_BRANCH_NAME? @qinezh @richardschneider

@richardschneider
Copy link
Contributor Author

@vicancy good idea. I'll add it add the head of list, so it overrides all other environment variables.

I want to keep the other env names so that that docfx works with other build systems without having to make to make any changes.

@vicancy
Copy link
Contributor

vicancy commented Jan 5, 2017

@richardschneider How about a section talking about setting up CI in docfx documentation? We can put that in Readme.md so that people can easily find that section. I would prefer removing those CI env names, considering that these names are prone to change, for instance, Jenkins already contains 2 env names.

@richardschneider
Copy link
Contributor Author

@vicancy excellent. I was just looking at folder structure to find out where to this info. Could you tell me the folder/file to update.

@vicancy
Copy link
Contributor

vicancy commented Jan 5, 2017


Setting the environment variable `DOCFX_SOURCE_BRANCH_NAME` tells DocFX which branch name to use.

Many build systems set an environment variable with the branch name. DocFX uses the following:
Copy link
Contributor

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?

Copy link
Contributor Author

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.

[Trait("Owner", "makaretu")]
public class GitUtilityTest
{

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove extra empty lines

{
remoteBranch = remoteBranch.Substring(index + 1);
}
remoteBranch = localBranch;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LogInfo to tell users that branch name is found in env


// Many build systems use a "detach head", which means that the normal git commands
// to get branch names do not work. Thankfully, they set an environment variable.
string localBranch = BuildSystemBranchName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the BuildSystemBranchName is found in env, however the repo is not in a detached mode?

Copy link
Contributor Author

@richardschneider richardschneider Jan 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The environment variable names are overrides. If any is specified, then the first specifies the branch name.

@vicancy
Copy link
Contributor

vicancy commented Jan 6, 2017

Hi @richardschneider, the concern with your PR is that it introduces a new overwriting mechanism through Environment Variables for a specific case.

Actually docfx allows overwriting URL for documents through _gitContribute in http://dotnet.github.io/docfx/tutorial/docfx.exe_user_manual.html#322-reserved-metadata.

To support your case, we plan to support overwriting the git url for source files, aka, href for View Source button. So after the change, in CI machines, passing %BRANCH_NAME% through docfx --globalMatadata command line can work.

@richardschneider
Copy link
Contributor Author

richardschneider commented Jan 6, 2017

@vicancy I was just trying to get doxfx to work, as expected, with appyevor. If you want developers to jump through hoops and spend hours googling to get it to work then go for it and document it.

Also, it's my opinion that most documentation is generated on a build server, not on an individual devops machine. My issue #1078 is still open.

I thought I had a simple fix for this, but obviously I am wrong.

@vicancy
Copy link
Contributor

vicancy commented Jan 6, 2017

Hi @richardschneider I agree with you that the simplicity of setting up docfx on CI machine is quite important. After further discussion, we plan to support both overwriting from globalMetadata and Environment Variables. We will take your PR to support Environment Variables.
For the PR, do you think it is more reasonable to change the logic to:

  1. If DOCFX_SOURCE_BRANCH_NAME is set, set branch name to DOCFX_SOURCE_BRANCH_NAME
  2. If the repo is in detached mode, set branch name to CI env variable if it exists
  3. Otherwise, use current commit id

So that DOCFX_SOURCE_BRANCH_NAME has the highest priority, and CI env variables only take effect when the repo is in detached mode.

Again, really appreciate your patience and continuous contribution to docfx. 😄

@richardschneider
Copy link
Contributor Author

Hi @vicancy and thanks for all comments.

If DOCFX_SOURCE_BRANCH_NAME is set, set branch name to DOCFX_SOURCE_BRANCH_NAME

PR already does this.

If the repo is in detached mode, set branch name to CI env variable if it exists

Okay, will make a change.

Otherwise, use current commit id

PR already does this.

…tached head

The code is ugly but this logic has been been requested by the team.
remoteBranch = RunGitCommandAndGetFirstLine(repoRootPath, GetRemoteBranchCommand);
var index = remoteBranch.IndexOf('/');
if (index > 0)
var isDetached = "HEAD" == RunGitCommandAndGetFirstLine(repoRootPath, GetLocalBranchCommand);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"HEAD" == Run [](start = 32, length = 14)

merge with GetLocalBranchName?

Copy link
Contributor Author

@richardschneider richardschneider Jan 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the addition of GetLocalBranchName is wrong. It changes the semantics of both RepoInfo.LocalBranch and RepoInfo.RemoteBranch. Previously they held a git branch name. Now they can also hold a commit id.

I want nothing to do with this. Its evil.

Not sure what you mean by "HEAD" == Run [](start = 32, length = 14)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However the code here is exactly GetLocalBranchName. And they have duplicate logic checking detached. Please make the code change cleaner rather than more complex.

remoteBranch = localBranch;
Logger.LogInfo($"Using branch '{localBranch}' from the environment variable.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also log the environment variable name

@superyyrrzz
Copy link
Contributor

:shipit:

@vicancy vicancy merged commit 89fae94 into dotnet:dev Jan 11, 2017
@richardschneider richardschneider deleted the detect-branch branch January 11, 2017 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants