Skip to content

Instantly share code, notes, and snippets.

@tobiasbueschel
Created September 19, 2018 16:17
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 tobiasbueschel/ba385f25432c6b75f63f31eb2edf77b5 to your computer and use it in GitHub Desktop.
Save tobiasbueschel/ba385f25432c6b75f63f31eb2edf77b5 to your computer and use it in GitHub Desktop.

Alias: Git Push & Open PR using Chrome

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.

Step 1: Ensure git_current_branch is present in your shell

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

Step 2: Ensure stopIfProtectedBranch is present in your shell (optional)

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
}

Step 3: Add gppr function to your shell

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 🎉.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment