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

Using Bitbucket Pipelines the current branch can not be evaluated when on develop branch #2900

Closed
smuellener opened this issue Nov 4, 2021 · 11 comments
Labels
Milestone

Comments

@smuellener
Copy link

smuellener commented Nov 4, 2021

When on develop and using Bitbucket Pipelines and gitversion from a docker container, the branch can not be evaluated and (no branch) is shown in the diag log. If the same is run when on a feature branch, gitversion works fine.

running:

docker run --volume $(pwd):/repo gittools/gitversion:5.7.1-alpine.3.12-x64-5.0 /repo -nofetch -nonormalize -diag

shows:

INFO [11/04/21 18:33:53:85] Working directory: /repo
INFO [11/04/21 18:33:53:88] Project root is: /repo/
INFO [11/04/21 18:33:53:88] DotGit directory is: /repo/.git
INFO [11/04/21 18:33:54:06] Using latest commit on specified branch
INFO [11/04/21 18:33:54:08] Begin: Getting branches containing the commit '8c55d5a'.
INFO [11/04/21 18:33:54:08] Trying to find direct branches.
INFO [11/04/21 18:33:54:08] No direct branches found, searching through all branches.
INFO [11/04/21 18:33:54:08] Searching for commits reachable from 'develop'.
INFO [11/04/21 18:33:54:09] The branch 'develop' has a matching commit.
INFO [11/04/21 18:33:54:09] Searching for commits reachable from 'origin/HEAD'.
INFO [11/04/21 18:33:54:09] The branch 'origin/HEAD' has a matching commit.
INFO [11/04/21 18:33:54:09] End: Getting branches containing the commit '8c55d5a'. (Took: 11.28ms)
INFO [11/04/21 18:33:54:10] No branch configuration found for branch (no branch), falling back to default configuration
INFO [11/04/21 18:33:54:10] Begin: Attempting to inherit branch configuration from parent branch
INFO [11/04/21 18:33:54:11] Begin: Finding branch source of '(no branch)'
INFO [11/04/21 18:33:54:13] Begin: Finding merge base between '(no branch)' and 'develop'.
INFO [11/04/21 18:33:54:14] Found merge base of 3d6f3ea Merge branch 'feature/xyz' into develop
INFO [11/04/21 18:33:54:15] Merge base of (no branch)' and 'develop is 3d6f3ea Merge branch 'feature/xyz' into develop
INFO [11/04/21 18:33:54:15] End: Finding merge base between '(no branch)' and 'develop'. (Took: 16.75ms)
INFO [11/04/21 18:33:54:15] Begin: Finding merge base between '(no branch)' and 'origin/develop'.
INFO [11/04/21 18:33:54:15] Found merge base of 3d6f3ea Merge branch 'feature/xyz' into develop
INFO [11/04/21 18:33:54:15] Merge base of (no branch)' and 'origin/develop is 3d6f3ea Merge branch 'feature/improveqmlbackendserver' into develop
INFO [11/04/21 18:33:54:15] End: Finding merge base between '(no branch)' and 'origin/develop'. (Took: 0.34ms)

. . .

INFO [11/04/21 18:33:54:31] Multiple source branches have been found, picking the first one (develop).
This may result in incorrect commit counting.
Options were:
develop, origin/develop, origin/hotfix/2.9.7, origin/master, origin/release/2.7.0, origin/release/2.6.0, origin/release/2.5.0, origin/release/2.4.0
INFO [11/04/21 18:33:54:31] End: Finding branch source of '(no branch)' (Took: 193.34ms)
INFO [11/04/21 18:33:54:31] Begin: Getting branches containing the commit '3d6f3ea'.
INFO [11/04/21 18:33:54:31] Trying to find direct branches.
INFO [11/04/21 18:33:54:31] No direct branches found, searching through all branches.
INFO [11/04/21 18:33:54:31] Searching for commits reachable from 'develop'.
INFO [11/04/21 18:33:54:31] The branch 'develop' has a matching commit.
INFO [11/04/21 18:33:54:31] Searching for commits reachable from 'origin/hotfix/2.9.7'.
INFO [11/04/21 18:33:54:47] The branch 'origin/hotfix/2.9.7' has no matching commits.
INFO [11/04/21 18:33:54:47] Searching for commits reachable from 'origin/master'.
INFO [11/04/21 18:33:54:64] The branch 'origin/master' has no matching commits.
INFO [11/04/21 18:33:54:64] Searching for commits reachable from 'origin/release/2.4.0'.
INFO [11/04/21 18:33:54:69] The branch 'origin/release/2.4.0' has no matching commits.
INFO [11/04/21 18:33:54:69] Searching for commits reachable from 'origin/release/2.5.0'.
INFO [11/04/21 18:33:54:76] The branch 'origin/release/2.5.0' has no matching commits.
INFO [11/04/21 18:33:54:76] Searching for commits reachable from 'origin/release/2.6.0'.
INFO [11/04/21 18:33:54:82] The branch 'origin/release/2.6.0' has no matching commits.
INFO [11/04/21 18:33:54:82] Searching for commits reachable from 'origin/release/2.7.0'.
INFO [11/04/21 18:33:54:91] The branch 'origin/release/2.7.0' has no matching commits.
INFO [11/04/21 18:33:54:91] End: Getting branches containing the commit '3d6f3ea'. (Took: 604.65ms)
INFO [11/04/21 18:33:54:91] Found possible parent branches: develop
INFO [11/04/21 18:33:54:91] End: Attempting to inherit branch configuration from parent branch (Took: 811.45ms)
INFO [11/04/21 18:34:01:64] Running against branch: (no branch) (8c55d5a Add logging to GitVersion)
WARN [11/04/21 18:34:01:64] An error occurred:
It looks like the branch being examined is a detached Head pointing to commit '8c55d5a'. Without a proper branch name GitVersion cannot determine the build version.
INFO [11/04/21 18:34:01:66] Done writing

I tried other versions of gitversion 5.x but with no success. I also tried adding the git branch name as an argument ("-b ") but also without luck.

In the log I can see "The branch 'develop' has a matching commit." Could this not be used to create the version number from there?

Thanks

@smuellener smuellener added the bug label Nov 4, 2021
@asbjornu
Copy link
Member

asbjornu commented Nov 4, 2021

Sounds like you may have a shallow repository checked out without an explicit branch or something like that. What happens if you do git fetch --unshallow before executing GitVersion?

@smuellener
Copy link
Author

This results in the following error:

fatal: --unshallow on a complete repository does not make sense

@asbjornu
Copy link
Member

asbjornu commented Nov 4, 2021

Hm, ok. Does adding the environment variable Git_Branch with the value of the branch being built help?

@smuellener
Copy link
Author

This does also not change the result. I see two mechanisms that are both not available in this case:

  1. From the log: "The branch 'develop' has a matching commit" -> apparently this is detected by GitVersion but then not used as a fallback. Could be a good solution in the first place?

  2. Passing the branch name actively using the "-b" argument seems to not be considered either.

@asbjornu
Copy link
Member

Strange. I agree that develop should have been chosen due to:

INFO [11/04/21 18:33:54:31] Multiple source branches have been found, picking the first one (develop).

I'm not sure why GitVersion doesn't pick develop even though it says so.

@fnadeau
Copy link

fnadeau commented Mar 11, 2022

Just want to pitch in that I have the same issue.

Easy way to reproduce without bitbucket

git clone https://github.com/GitTools/GitVersion.git
cd GitVersion
git checkout $(git rev-parse HEAD)
docker run --volume $(pwd):/repo gittools/gitversion:5.9.0-alpine.3.12-3.1 /repo -nofetch -nonormalize -diag

Redacted output:

INFO [03/11/22 20:57:49:46] Working directory: /repo
INFO [03/11/22 20:57:49:48] Branch from build environment: 
INFO [03/11/22 20:57:49:48] Project root is: /repo/
INFO [03/11/22 20:57:49:48] DotGit directory is: /repo/.git
INFO [03/11/22 20:57:49:53] Using latest commit on specified branch
INFO [03/11/22 20:57:49:62] Begin: Getting branches containing the commit 'c0b1509'.
  INFO [03/11/22 20:57:49:62] Trying to find direct branches.
  INFO [03/11/22 20:57:49:62] No direct branches found, searching through all branches.
  INFO [03/11/22 20:57:49:62] Searching for commits reachable from 'support/5.x'.
  INFO [03/11/22 20:57:49:63] The branch 'support/5.x' has a matching commit.
  INFO [03/11/22 20:57:49:63] Searching for commits reachable from 'origin/HEAD'.
  INFO [03/11/22 20:57:49:63] The branch 'origin/HEAD' has a matching commit.
  INFO [03/11/22 20:57:49:63] End: Getting branches containing the commit 'c0b1509'. (Took: 6.53ms)
  INFO [03/11/22 20:57:49:64] No branch configuration found for branch (no branch), falling back to default configuration
  INFO [03/11/22 20:57:49:64] Begin: Attempting to inherit branch configuration from parent branch
    INFO [03/11/22 20:57:49:65] Begin: Finding branch source of '(no branch)'
      INFO [03/11/22 20:57:49:65] Begin: Finding merge base between '(no branch)' and 'support/5.x'.
        INFO [03/11/22 20:57:49:66] Found merge base of c0b1509 added 'support' branch configuration
        INFO [03/11/22 20:57:49:66] Merge base of (no branch)' and 'support/5.x is c0b1509 added 'support' branch configuration
      INFO [03/11/22 20:57:49:66] End: Finding merge base between '(no branch)' and 'support/5.x'. (Took: 9.11ms)
      INFO [03/11/22 20:57:49:66] Begin: Finding merge base between '(no branch)' and 'origin/main'.
        INFO [03/11/22 20:57:49:66] Found merge base of 3a79a49 (build) renamed the release to support branch
        INFO [03/11/22 20:57:49:66] Merge base was due to a forward merge, next merge base is 20b5895 (build) renamed the release to support branch
        INFO [03/11/22 20:57:49:66] Merge base was due to a forward merge, next merge base is 5078f28 (deps): Bump Microsoft.Build from 16.9.0 to 17.1.0
        INFO [03/11/22 20:57:49:66] Merge base of (no branch)' and 'origin/main is 5078f28 (deps): Bump Microsoft.Build from 16.9.0 to 17.1.0
      INFO [03/11/22 20:57:49:66] End: Finding merge base between '(no branch)' and 'origin/main'. (Took: 1.71ms)
      INFO [03/11/22 20:57:49:66] Begin: Finding merge base between '(no branch)' and 'origin/support/5.x'.
        INFO [03/11/22 20:57:49:66] Found merge base of c0b1509 added 'support' branch configuration
        INFO [03/11/22 20:57:49:66] Merge base of (no branch)' and 'origin/support/5.x is c0b1509 added 'support' branch configuration
      INFO [03/11/22 20:57:49:66] End: Finding merge base between '(no branch)' and 'origin/support/5.x'. (Took: 0.21ms)
      INFO [03/11/22 20:57:49:67] Multiple source branches have been found, picking the first one (support/5.x).
This may result in incorrect commit counting.
Options were:
support/5.x, origin/support/5.x, origin/main
    INFO [03/11/22 20:57:49:67] End: Finding branch source of '(no branch)' (Took: 22.55ms)
    INFO [03/11/22 20:57:49:67] Begin: Getting branches containing the commit 'c0b1509'.
      INFO [03/11/22 20:57:49:67] Trying to find direct branches.
      INFO [03/11/22 20:57:49:67] No direct branches found, searching through all branches.
      INFO [03/11/22 20:57:49:67] Searching for commits reachable from 'support/5.x'.
      INFO [03/11/22 20:57:49:67] The branch 'support/5.x' has a matching commit.
      INFO [03/11/22 20:57:49:67] Searching for commits reachable from 'origin/main'.
      INFO [03/11/22 20:57:49:76] The branch 'origin/main' has no matching commits.
    INFO [03/11/22 20:57:49:76] End: Getting branches containing the commit 'c0b1509'. (Took: 89.68ms)
    INFO [03/11/22 20:57:49:76] Found possible parent branches: support/5.x
  INFO [03/11/22 20:57:49:76] End: Attempting to inherit branch configuration from parent branch (Took: 119.44ms)
  INFO [03/11/22 20:57:49:78] Running against branch: (no branch) (c0b1509 added 'support' branch configuration)
  WARN [03/11/22 20:57:49:79] An error occurred:
It looks like the branch being examined is a detached Head pointing to commit 'c0b1509'. Without a proper branch name GitVersion cannot determine the build version.
  INFO [03/11/22 20:57:49:80] Done writing

@asbjornu
Copy link
Member

Detached HEAD is not supported. GitVersion needs to know which branch is being built in order to generate a sensible version number. I retract my previous comment that it should have chosen develop, because even though the checked out commit exists in develop, there is no guarantee that develop is the branch being built unless GitVersion has explicit knowledge about it.

I don't think it's a good idea to introduce heuristics that choose the first branch the checked out commit is found on to generate a version number.

@smuellener
Copy link
Author

@asbjornu agreed. What about explicitely passing the name as an argument?

asbjornu added a commit to asbjornu/GitVersion that referenced this issue Mar 15, 2022
Don't just use the environment variable GIT_BRANCH in GitPreparer
to find the current branch; allow GIT_BRANCH to specify which branch
is being built in the GitVersionContext as well, so it can be used
throughout the codebase.

Fixes GitToolsGH-2900.
@asbjornu
Copy link
Member

@smuellener, that's what the environment variable GIT_BRANCH is supposed to do. Or so I thought. After investigating, I found that GIT_BRANCH only affects normalization and not version calculation. I've submitted #3049 to correct that. Fingers crossed it turns green. 🤞🏼

@asbjornu
Copy link
Member

May be fixed or at least improved by #3338. Closing due to inactivity, please let me know how this fares after the next v5 release.

@asbjornu asbjornu added this to the 5.x milestone Jan 20, 2023
@arturcic arturcic modified the milestones: 5.x, 5.12.0 Jan 20, 2023
@arturcic
Copy link
Member

🎉 This issue has been resolved in version 5.12.0 🎉
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