Git-getpull: Quickly find the pull request that merged your commit to master

Ideally git blame would give you all the context you need to determine why some code was written. But the reality is that no team is perfectly disciplined, and sometimes you're going to run across commits with cryptic or ambiguous messages ("bugfix," anyone?).

When a git commit message doesn't provide enough context, or provides context at the wrong level (too much what, too little why), it's useful to be able to find the GitHub pull request that merged that commit. Pull requests can contain code review comments, discussion over implementation details, before/after pictures of the UI, and more. Pull requests may also have many commits associated with them, which together give a clearer sense of the intended change.

The git-getpull utility allows you to find out which pull request merged a commit into master.

Installation

$ curl -o /usr/local/bin/git-getpull https://raw.githubusercontent.com/a-warner/git-getpull/master/git-getpull && chmod +x /usr/local/bin/git-getpull 

Usage

$ git getpull 42a3817c
https://github.com/rails/rails/pull/11195

Annotated Code

#!/bin/sh

if [ -z "$1" ]; then
  echo "Usage: git getpull <SHA>"
  exit 1
elif [ -z "$(git rev-parse --git-dir 2>/dev/null)" ]; then
  echo "Not in a git directory"
  exit 1
else
  repository_path=$(git config --get remote.origin.url 2>/dev/null | \
   perl -lne 'print $1 if /(?:(?:https?:\/\/github.com\/)|:)(.*?).git/')
  pull_base_url=https://github.com/$repository_path/pull
  pull_id=$(git log $1..master --ancestry-path --merges --oneline 2>/dev/null \
  | tail -n 1 | perl -nle 'print $1 if /#(\d+)/')

  if [ -n "$pull_id" ]; then
    echo "$pull_base_url/$pull_id"
  else
    echo "Sorry, couldn't find that pull"
    exit 1
  fi
fi

Share on: ,