Skip to content

Instantly share code, notes, and snippets.

@bxt
Last active August 1, 2017 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bxt/e7905bdc5931cad79bf5274b3956c600 to your computer and use it in GitHub Desktop.
Save bxt/e7905bdc5931cad79bf5274b3956c600 to your computer and use it in GitHub Desktop.
Open the merge request for a merged commit in GitLab
#!/bin/bash
set -e
MAIN=${2:-development}
OPEN=open
MRS=$(git remote show -n origin | sed -n '/^ Fetch URL: git@\(.*\):\(.*\)\.git$/ s//https:\/\/\1\/\2\/merge_requests\//p')
if [ -z "$1" ]
then
MRN=
else
MRN=$(git when-merged -l $1 $MAIN | sed -n '/^ See merge request \!\([0-9]*\)$/ s//\1/p')
fi
$OPEN "$MRS$MRN"

GitLab open Merge Request for Commit

This is a handy script to open the Merge Request on which a commit was merged on GitLab, for example when you find out in what commit a certain piece of code was added via git blame and you don't know why this code was introduced from the commit message. You can then open the related merge request to find discussions, who reviewed the code and so on.

The script looks through the merge commits for the provided commit sha1 and looks for something like See merge request !42. GitLab puts this into merge commit messages by default. It will also parse your origin remote to find out where your GitLab repository is at. I then opens the merge request's URL (if found) using the open command, unless you configure it otherwise.

To install simply run:

curl -L https://git.io/gitlab-openmr-install | bash

Or manually put this script into ~/bin and run chmod +x ~/bin/gitlab-openmr.sh. You'll also need git-when-merged by Michael Haggerty, you can install it like this:

brew update
brew install git-when-merged

Run gitlab-openmr.sh it like this:

~/my-git-repo> gitlab-openmr.sh 4174ee6

You can optionally supply a branch name as a second argument, if the branch merged into is not named development like this:

~/my-git-repo> gitlab-openmr.sh 4174ee6 master

Enjoy! :)

#!/bin/sh
mkdir -p ~/bin
curl https://gist.githubusercontent.com/bxt/e7905bdc5931cad79bf5274b3956c600/raw/gitlab-openmr.sh > ~/bin/gitlab-openmr.sh
chmod +x ~/bin/gitlab-openmr.sh
echo gitlab-openmr.sh installed successfully.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment