GitHub and GitLab both give a very similar output after a git push
command has been executed. Hence, we can construct an alias that allows us to push and automatically open Chrome to create a PR.
This is what GitHub outputs after git push
has been executed:
Counting objects: 4, done.
Delta compression using up to 12 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 3.01 KiB | 3.01 MiB/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote:
remote: Create a pull request for 'feat/add-more-pokemons' on GitHub by visiting:
remote: https://github.com/tobiasbueschel/awesome-pokemon/pull/new/feat/add-more-pokemons
remote:
To github.com:tobiasbueschel/awesome-pokemon.git
* [new branch] feat/add-more-pokemons -> feat/add-more-pokemons
The following shows how to extract the url from the PR from git push
's output and open it in Chrome.
In case you are a Oh My ZSH user, this will already be available for you. If not, you can add the following to the top of your .zshrc or .bash_profile.
# Outputs the name of the current branch
# Usage example: git pull origin $(git_current_branch)
# Using '--quiet' with 'symbolic-ref' will not cause a fatal error (128) if
# it's not a symbolic ref, but in a Git repo.
function git_current_branch() {
local ref
ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null)
local ret=$?
if [[ $ret != 0 ]]; then
[[ $ret == 128 ]] && return # no git repo.
ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
fi
echo ${ref#refs/heads/}
}
Source: https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/git.zsh#L61
If you are following Vincent Driessen's branching model, it might be a good idea to protect your master
and develop
branch from accidental pushes. Hence, the following needs to be added to your .zshrc or .bash_profile.
stopIfProtectedBranch() {
if [ "$(git_current_branch)" == "master" ]
then
echo "Not allowed to push to master"
return 1;
fi
if [ "$(git_current_branch)" == "develop" ]
then
echo "Not allowed to push to develop"
return 1;
fi
}
Add the following snippet to your .zshrc or .bash_profile.
# git push that directly opens PR (works for GitHub & GitLab)
gppr() {
# optional check to ensure we don't push to master or develop
stopIfProtectedBranch && \
# push current branch to remote origin
gitOutput=$(git push origin $(git_current_branch) 2>&1) && \
# get URL for PR
prURL=$(echo $gitOutput | awk '/https/{print $2}') && \
# open new PR
open -a Google\ Chrome $prURL
}
Voilà, after running source ~/.zshrc
, source ~/.bash_profile
or simply restarting your shell, you can run gppr
🎉.