Test if branch is behind for merge request

Describe your question in as much detail as possible:
When a merge request is created, I’d like for my pipeline script to test if the branch that is going to be merged is behind the branch being merged to and if it is, fail the test to help people to remember to rebase. I am running on gitlab.com inside a docker image of my own with git and rockylinux:8 in a private project.

  • What are you seeing, and how does that differ from what you expect to see?
    It seems like inside the docker image, I am just fetching the one last commit, but I’d like to compare the branches. So, if I’m merging develop into main, I want to make sure develop isn’t behind the main branch.
    This command in my terminal returns me the commits behind:

git rev-list --left-only --count origin/main..@

but gives me an error on gitlab

ambiguous argument 'main': unknown revision or path not in the working tree

  • What troubleshooting steps have you already taken? Can you link to any docs or other resources so we know where you have been?
    I’ve tried changing the GIT_STRATEGY: to clone and also just manually running git fetch but there still seems to be no information pertaining to the develop or main branches available.

I apologize in advance if I am doing something completely wrong or inherently bad, I am pretty new to both git and gitlab. Thank you.

Are you definitely running Git from the root of your project?

Also, can you add git remote -vv to your pipeline, and just check that the remote is correct?

Hello! Thank you for the reply! I just double checked to make sure and yes I’m at the root of my project and git remote -vv and everything seemed correct. It looked like this (I redacted a few items):

git remote -vv
origin https://gitlab-ci-token:[MASKED]@gitlab.com/redacted/pipeline/internal/test.git (fetch)
origin https://gitlab-ci-token:[MASKED]@gitlab.com/redacted/pipeline/internal/test.git (push)

Does that look correct to you? Anything else maybe? Thank you again!

That looks OK.

I’ve had a dig around, and in my pipelines I see something like this:

Checking out abcde0123 as branch-name

and I think that’s the clue – the repo is in a detached HEAD state.

I think you need to switch back to the branch using $CI_COMMIT_BRANCH and then run fetch.

This is what worked for me:

$ git branch -a
* (HEAD detached at 6042d00)

$ git checkout $CI_COMMIT_BRANCH
Switched to a new branch 'test-regexp'
Branch 'test-regexp' set up to track remote branch 'test-regexp' from 'origin'.

$ git fetch -a
From https://gitlab.com/NAMESPACE/REPO
 * [new branch]      add-releases -> origin/add-releases
 * [new branch]      develop      -> origin/develop
 * [new branch]      master       -> origin/master
 * [new branch]      test-branch  -> origin/test-branch
 * [new branch]      test/branch  -> origin/test/branch

$ git branch -a
* test-regexp
1 Like

Ah, thank you! Yes, that was it. It was checking out a hash directly and after checking out the branch I’m able to run my commands. Thank you again!

1 Like