Skip to content

Instantly share code, notes, and snippets.

@alekstorm
Last active April 15, 2023 05:51
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alekstorm/4949628 to your computer and use it in GitHub Desktop.
Save alekstorm/4949628 to your computer and use it in GitHub Desktop.
Finds likely good reviewers for a commit or range of commits by getting a diff, then running `git blame` on the previous versions of each changed hunk. Outputs a sorted list of reviewer names, emails, and how many lines you've both touched. To use, name the file `git-reviewers`, put it somewhere in your $PATH, make it executable, and call it wit…
#!/usr/bin/env bash -ue
if [[ $# -lt 1 || $# -gt 2 ]]; then
echo "Usage: git $(basename "$0" | sed 's/^git-//') <end-commit> [<start-commit>]"
exit 1
fi
diff_range="$1^..$1"
end_commit="$1^"
if [[ $# -eq 2 ]]; then
diff_range="$2..$1"
end_commit="$2"
fi
function createBlameParts {
awk -v commit="$end_commit" '{
if ($1 == "@@") {
sub(",", ",+", $2)
print "git blame --line-porcelain -MCw -L " substr($2,2) " " commit
}
else
print "-- " substr($3,3)
}'
}
function concatBlameParts {
awk '{
if (match($0, /^--/) == 1)
file=$0
else
print $0 " " file
}'
}
function execBlame {
while read command; do
$command | egrep '^author |^author-mail ' | cut -d' ' -f2- | awk '{ if (NR % 2 == 1) o=$0; else print o " " $0 }'
done
}
git diff --diff-filter=DM "$diff_range" | egrep '^@|^diff' | createBlameParts | concatBlameParts | execBlame | sort | uniq -c | sort -nr
@jklmli
Copy link

jklmli commented Oct 4, 2016

You should turn this into a repo! :)
Automating this install with brew install git-reviewers would be awesome.

@hanz3356
Copy link

You should turn this into a repo! :) Automating this install with brew install git-reviewers would be awesome.

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