Skip to content

Instantly share code, notes, and snippets.

@wesen
Created January 13, 2012 13:14
Show Gist options
  • Save wesen/1606058 to your computer and use it in GitHub Desktop.
Save wesen/1606058 to your computer and use it in GitHub Desktop.
Open a commit for a line of a file on github
#!/bin/sh
#
# Usage: git hub-blame file line
#
# Opens the commit where the given line originates in the commit view. Useful for
# code review on github.
FILENAME=$1
LINE=$2
# lookup original commit, file and line
set -- $(git blame -p -L$LINE,$LINE "$FILENAME" |
awk '/filename/ {match($0, /^filename /); origfile = substr($0, RLENGTH+1)};
NR == 1 { commit = $1; origline = $2 };
END { print commit " " origfile " " origline }')
COMMIT=$1
ORIGFILE=$2
ORIGLINE=$3
# get the line number of the original file in the original commit diff (so that we can generate the anchor tag for github)
set -- $(git diff-tree -r --name-only $COMMIT ${COMMIT}~1 |
awk -v origfile="$ORIGFILE" '$0 ~ origfile { print NR-1; exit } ')
ORIGNUM=$1
hub browse -- commit/${COMMIT}#L${ORIGNUM}R${ORIGLINE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment