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

[Bug] GitLab: Branch names cannot contain the word 'refs' #3103

Closed
Daniel-Khodabakhsh opened this issue Apr 28, 2022 · 6 comments · Fixed by #3459
Closed

[Bug] GitLab: Branch names cannot contain the word 'refs' #3103

Daniel-Khodabakhsh opened this issue Apr 28, 2022 · 6 comments · Fixed by #3459
Labels
Milestone

Comments

@Daniel-Khodabakhsh
Copy link

Daniel-Khodabakhsh commented Apr 28, 2022

In GitLab (currently on GitLab Enterprise Edition 14.11.0-pre bf8e20209d4) and using the latest GitVersion available on DockerHub (5.10.1), if a branch has a name which contains the word refs such as:

  • my-refs-branch
  • my-test-refs

then detemining the version via gitversion results in the following logs:

INFO [04/28/22 0:45:15:93] Applicable build agent found: 'GitLabCi'.
INFO [04/28/22 0:45:15:98] Working directory: /builds/Daniel-Khodabakhsh/gitversion-bug
INFO [04/28/22 0:45:15:99] Project root is: /builds/Daniel-Khodabakhsh/gitversion-bug/
INFO [04/28/22 0:45:15:99] DotGit directory is: /builds/Daniel-Khodabakhsh/gitversion-bug/.git
INFO [04/28/22 0:45:15:99] Branch from build environment: my-test-refs
INFO [04/28/22 0:45:16:00] Begin: Normalizing git directory for branch 'my-test-refs'
  INFO [04/28/22 0:45:16:03] One remote found (origin -> 'https://gitlab-ci-token:*******@gitlab.com/Daniel-Khodabakhsh/gitversion-bug.git').
  INFO [04/28/22 0:45:16:04] Skipping fetching, if GitVersion does not calculate your version as expected you might need to allow fetching or use dynamic repositories
  INFO [04/28/22 0:45:16:04] End: Normalizing git directory for branch 'my-test-refs' (Took: 43.80ms)
  ERROR [04/28/22 0:45:16:06] An unexpected error occurred:
System.ArgumentException: The canonicalName is not a Canonical name
   at GitVersion.ReferenceName.Parse(String canonicalName) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Git\ReferenceName.cs:line 39
   at GitVersion.GitPreparer.EnsureLocalBranchExistsForCurrentBranch(IRemote remote, String currentBranch) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 385
   at GitVersion.GitPreparer.NormalizeGitDirectory(Boolean noFetch, String currentBranchName, Boolean isDynamicRepository) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 164
   at GitVersion.GitPreparer.NormalizeGitDirectory(String targetBranch, Boolean isDynamicRepository) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 134
   at GitVersion.GitPreparer.PrepareInternal(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 69
   at GitVersion.GitPreparer.Prepare() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitPreparer.cs:line 48
   at GitVersion.GitVersionCalculateTool.CalculateVersionVariables() in D:\a\GitVersion\GitVersion\src\GitVersion.Core\Core\GitVersionCalculateTool.cs:line 43
   at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions) in D:\a\GitVersion\GitVersion\src\GitVersion.App\GitVersionExecutor.cs:line 66
  INFO [04/28/22 0:45:16:06] Attempting to show the current git graph (please include in issue): 
  INFO [04/28/22 0:45:16:06] Showing max of 100 commits
  INFO [04/28/22 0:45:16:11] * 6a3039b 2 minutes ago  (HEAD, origin/my-test-refs, origin/main, refs/pipelines/526468609)
* e06a5d0 4 minutes ago 
* 9d9d4b9 6 minutes ago 
* dfae72e 7 minutes ago 
* 9da59da 8 minutes ago 
* 8bec4b1 10 minutes ago 

The GitLab CI job is using the docker image of GitLab and is defined below:

stages:
  - build

Determine version:
  stage: build
  image:
    name: gittools/gitversion:5.10.1-debian.11-6.0-amd64
    entrypoint: ['']
  variables:
    GIT_DEPTH: 0
  script: |
    echo "VERSION=$(/tools/dotnet-gitversion /showvariable FullSemVer)" >> build.env
    cat build.env
  artifacts:
    reports:
      dotenv: build.env

First saw it on an older version of GitVersion as well (5.6.11-debian.10-x64).

Expected Behavior

Regardless of the branch name, there should be no error generating the version.

Actual Behavior

Branch names with the word refs fail.

Possible Fix

Checked the source and it seems like the word refs in the branch name interferes with the prefix checking in https://github.com/GitTools/GitVersion/blob/support/5.x/src/GitVersion.Core/Git/ReferenceName.cs when checked against these prefixes:
image

Steps to Reproduce

  1. Create a GitLab project
  2. Add the following step to the project's .gitlab-ci.yml file:
stages:
  - build

Determine version:
  stage: build
  image:
    name: gittools/gitversion:5.10.1-debian.11-6.0-amd64
    entrypoint: ['']
  variables:
    GIT_DEPTH: 0
  script: |
    echo "VERSION=$(/tools/dotnet-gitversion /showvariable FullSemVer)" >> build.env
    cat build.env
  artifacts:
    reports:
      dotenv: build.env
  1. Create a branch with the word refs in it, such as my-test-refs.
  2. Run the pipeline.
  3. Observe that the Determine version step of the pipeline fails with the logs mentioned above.

Context

Trying to add automated versioning to my pipeline.

Your Environment

@asbjornu
Copy link
Member

Probably a duplicate of #2134

@HHobeck
Copy link
Contributor

HHobeck commented Mar 4, 2023

The problem is not located in ReferenceName.cs class it is lcoated in GitPreparer.cs:

image

I don't know what the intention of checking isRef is but what I agree with: This code looks fragile. ;)

@HHobeck HHobeck added this to the 6.x milestone Mar 4, 2023
@asbjornu
Copy link
Member

asbjornu commented Mar 4, 2023

Thanks for digging out the problematic piece of code, @HHobeck. That is indeed fragile. I would assume we can replace .Contains("refs") with .Contains("refs/") at least?

@HHobeck
Copy link
Contributor

HHobeck commented Mar 5, 2023

I don't understand the intention of replacing refs/ with refs/heads/. It only affects remote and pull request branches. We have already a definition in ReferenceName.cs which I would like to reuse:

    public const string LocalBranchPrefix = "refs/heads/";
    public const string RemoteBranchPrefix = "refs/remotes/";
    public static readonly string[] PullRequestPrefixes = new[]
    {
        "refs/pull/",
        "refs/pull-requests/",
        "refs/remotes/pull/",
        "refs/remotes/pull-requests/"
    };

Thus I see following scenarios in EnsureLocalBranchExistsForCurrentBranch:

  1. just-a-branch -> refs/heads/just-a-branch // okay
  2. refs/heads/just-a-branch -> refs/heads/just-a-branch // okay
  3. refs/remotes/origin/just-a-branch ->refs/heads/remotes/origin/just-a-branch // not okay
  4. refs/remotes/custom/just-a-branch ->refs/heads/remotes/custom/just-a-branch // not okay
  5. refs/pull/5/merge ->refs/heads/pull/5/merge // ??
  6. refs/pull-requests/5/merge ->refs/heads/pull-requests/5/merge // ??
  7. refs/remotes/pull/5/merge ->refs/heads/remotes/pull/5/merge // not okay
  8. refs/remotes/pull-requests/5/merge ->refs/heads/remotes/pull-requests/5/merge // not okay

@asbjornu
Copy link
Member

asbjornu commented Mar 6, 2023

Yea, that seems quite wonky. If we could just use ReferenceName.cs, that would be much better.

@arturcic
Copy link
Member

arturcic commented Apr 6, 2023

🎉 This issue has been resolved in version 6.0.0-beta.2 🎉
The release is available on:

Your GitReleaseManager bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants