2

I have two branches, master and turtles, with turtles being ahead of master by one commit: 'I like turtles'.
In GitLab I have the following .yml file, which runs whenever a Merge Request is created, or updated by pushing the branch to merge:

update-doc:
    stage: deploy
    script:
        - echo $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
        - 'echo $(git log --abbrev-commit remotes/origin/master)'
        - 'echo $(git log --abbrev-commit remotes/origin/master..remotes/origin/${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME})'
        - 'echo $(git cherry -v remotes/origin/master remotes/origin/turtles --abbrev=1)'
    only:
        - merge_requests

Running git log --abbrev-commit remotes/origin/master..remotes/origin/turtles or git cherry -v remotes/origin/master remotes/origin/turtles in Git Bash on my Windows machine and on the Linux VM where we are hosting GitLab returns the commit message 'I like turtles', as expected. But when the .yml file runs it cannot find the branch remotes/origin/turtles and I get the following output:

$ echo $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
turtles
$ echo $(git log --abbrev-commit remotes/origin/master)
8406e4d Update .gitlab-ci.yml
$ echo $(git log --abbrev-commit remotes/origin/master..remotes/origin/${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME})
fatal: ambiguous argument 'remotes/origin/master..remotes/origin/turtles': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git [...] -- [...]'
$ echo $(git cherry -v remotes/origin/master remotes/origin/turtles --abbrev=1)
fatal: Unknown commit remotes/origin/turtles

So GitLab clearly knows that there is the turtles branch as it's in the $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME variable, but can't seem to resolve the remotes/origin/turtles. I've tried without the remotes/origin/ part too but still no luck.

How can I get the GitLab runner to recognise that remote path of the merge request branch? Or is there another git command I could try that shows just the commits on the turtles branch?

JChristen
  • 588
  • 4
  • 21
  • 1
    I suspect Gitlab CI fetched only single branch `master`. Try to fetch the branch `turtles`: `git fetch origin $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME` – phd Feb 19 '20 at 11:43
  • @phd fetching `$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME` fixed the error but then gave me all the commits on the branch (including those on `master`), but fetching `master` too fixed that issue. What is the meaning of the `:` between the branches in your comment? Not seen that notation before – JChristen Feb 19 '20 at 13:09
  • See the docs https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt-ltrefspecgt. With `git fetch/pull`left to `:` is the remote ref, right to is the local ref (you fetch remote refs and update local refs). In `git push` it's the other way around (you push local refs to remote refs). – phd Feb 19 '20 at 13:17
  • Thanks. Do you know why the GitLab job runner wouldn't be able to see the branches on the repo? – JChristen Feb 19 '20 at 14:28
  • 1
    Becuase it uses [`git clone --depth`](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---depthltdepthgt) which implies `--single-branch`. – phd Feb 19 '20 at 15:12

1 Answers1

5

In GitLab CI/CD, the default strategy for checking out code is to fetch the current branch with a shallow merge, e.g. git fetch --depth 50 origin $CI_COMMIT_BRANCH. This explains why you see only the one branch.

You can fix this:

  • By setting "Git shallow clone" to 0 in the web UI, or
  • by setting GIT_DEPTH: 0 in your .gitlab-ci.yml to disable shallow clones, or
  • by pulling/fetching any other branches you need, e.g. git fetch origin master

Refer to the docs on shallow cloning.

Ben Whaley
  • 32,811
  • 7
  • 87
  • 85