Last active
September 19, 2022 21:18
-
-
Save garno/b9e02113f638d928e2be0a76dbf5b8d1 to your computer and use it in GitHub Desktop.
Small bash script to automatically open Pull Request page with branches correctly selected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Retrieve branch name | |
branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') | |
# Retrieve destination branch | |
destination_branch=${branch%--*} | |
# Determine the default origin branch | |
if [[ $branch == $destination_branch ]]; then | |
master_exists=$(git branch | grep 'master') | |
if [[ -z $master_exists ]]; then | |
destination_branch=develop | |
else | |
destination_branch=master | |
fi | |
fi | |
# Retrieve repository URL | |
repository_url=$(git remote get-url origin | sed -e 's/git@//' -e 's/.git//' -e 's/:/\//') | |
if [[ $repository_url == github* ]]; then | |
pr_url=https://$repository_url/compare/$destination_branch...$branch | |
elif [[ $repository_url == gitlab* ]]; then | |
pr_url="https://$repository_url/merge_requests/new?merge_request%5Bsource_branch%5D=$branch&merge_request%5Btarget_branch%5D=$destination_branch" | |
fi | |
# Open the new Pull Request URL | |
open $pr_url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice.
A couple changes required to make it run on my machine (Ubuntu):
open
->xdg-open
on last line#!/bin/sh
->#!/usr/bin/env bash
(bin/sh
was symlinked todash
on my installation...)