`git checkout $CI_COMMIT_REF_NAME` fails in Pipelines for merge requests

Problem to solve

This error happens only when git checkout $CI_COMMIT_REF_NAME is executed in .gitlab-ci.yml and GIT_DEPTH is specified (non-zero).

When users use Pipelines for merge requests (or Merged Results/Merge Train), GitLab-Rails composes the following refspecs:

Given an MR (IID: 1) is created on a feature branch example.

  • "+refs/heads/refs/merge-requests/1/head:refs/heads/refs/merge-requests/1/head"
  • "+refs/merge-requests/1/head:refs/merge-requests/1/head"

This is a bit messed up because build.ref stores the full-ref-path in this case, we should use pipeline.source_ref instead in order to compose the following refspecs:

  • "+refs/heads/example:refs/heads/example"
  • "+refs/merge-requests/1/head:refs/merge-requests/1/head"

This is correct and correctly allow users to execute git checkout $CI_COMMIT_REF_NAME