Last active
April 15, 2023 05:51
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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
You should turn this into a repo! :)
Automating this install with
brew install git-reviewers
would be awesome.