Skip to content

Instantly share code, notes, and snippets.

@blech75
Created June 27, 2014 14:28
Show Gist options
  • Select an option

  • Save blech75/9fe5a5cf9e55cf58259b to your computer and use it in GitHub Desktop.

Select an option

Save blech75/9fe5a5cf9e55cf58259b to your computer and use it in GitHub Desktop.
git-blame-revs
#!/bin/bash
# git-blame-revs
#
# Generate a list of revisions that a file is currently composed of.
# Think of it as a high-level summary of a 'blame'.
#
# NOTE: It is currently hardcoded to work with SVN repos via git-svn.
#
# Author: justin@worksperfectly.net
#
#
# TODO
# ====
#
# * Refactor SVN-specific features so they can be enabled via an
# additional command line argument (-s/--svn).
if [ $# -lt 1 ] ; then
echo "Usage: "`basename "$0"` "FILE..." >&2
exit 1
fi
# allow DEBUG env varialbe to trigger simple bash debugging when it is set to
# anything but 'false'
if [ ${DEBUG:=false} != 'false' ] ; then
set -x
fi
# create var for newline char since it's a pain to embed in strings.
NL=$'\n'
for file in "$@"; do
# blame the file and then get the unique shas
#
# the -w is due to windows line endings and core.autocrlf
# http://stackoverflow.com/a/4653165/2284440
shas=`git blame -w ${file} | awk '{ print $1 }' | sort | uniq`
output=""
for sha in $shas; do
# get the revision info for each commit
# output the unix timestamp for sorting purposes. we'll remove it later
git_log=$(git --no-pager log -1 --pretty=format:"%ct %h [%ci] (%cn) %s%n" $sha)
# get the associated SVN info
git_svn=$(git svn find-rev $sha)
# format it nicely
svn_rev=$(printf "r%4s" "$git_svn")
# assemble the line for output
output="${output}$svn_rev / $git_log${NL}"
done
# remove trailing space for correct counting of lines
output=$(echo "$output" | sed -E -e 's/\n$//')
# count up the revs
count=$(echo "$output" | wc -l | tr -d ' ')
# TODO: add option to turn off count of revs
echo "$file (${count})"
# finally remove the unix timestamp. kind of messy. slash is part
# of the regexp to anchor us to ensure we get the timestamp and not
# the SHA (as that could be all numbers).
echo "${output}" | sort | sed -E -e 's/\/ [0-9]+ /\/ /'
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment