Skip to content

Instantly share code, notes, and snippets.

@andersonvom

andersonvom/greview

Last active Jun 12, 2017
Embed
What would you like to do?
#!/bin/bash
# This script will submit all pending commits to gerrit and will automatically
# add reviewers to the associated change. Reviewers can be added by simply
# listing them after greview. You can either enter their full email addresses
# or just partially enter a unique combination of letters that will be
# fuzy-matched to either the email or the name of the commiters.
#
# Usage: greview <reviewer-1> <reviewer-2> ...
reviewers=${*:1}
function list_reviewers {
git log --format="%aE %aN" -n 1000 | sort | uniq
}
default_branch=$(cat .gitreview | grep -e 'defaultbranch' | cut -d '=' -f2)
default_branch=${default_branch:-"master"}
topic_branch=$(git rev-parse --abbrev-ref HEAD)
push_ref="HEAD:refs/for/$default_branch"
if [ "$topic_branch" != "$default_branch" ]; then
push_ref="$push_ref/$topic_branch"
fi
url=$(git remote show -n gerrit |
grep Fetch |
cut -d ':' -f2- |
sed -e 's/ //g')
for rev in $reviewers
do
pattern="$(echo "$rev" | sed 's/\(.\)/[^ ]*\1/g')"
matches="$(list_reviewers | grep "$pattern" | cut -d ' ' -f1)"
all_reviewers=$(echo $matches | sed -e "s/ .*//g")
if [ "$all_reviewers" = "" ]; then
all_reviewers="$rev"
fi
reviewer_opts="$reviewer_opts --reviewer $all_reviewers"
done
git push --receive-pack="git-receive-pack $reviewer_opts" "$url" "$push_ref"
@Lekensteyn

This comment has been minimized.

Copy link

@Lekensteyn Lekensteyn commented Oct 10, 2015

I think that the last lines (git config and git push) can be simplified to avoid a new remote:

git push --receivepack="git-receive-pack $reviewer_opts" "$url" "$push_ref"

Simplification of matches that is also case sensitive now:

pattern="$(echo "$rev" | sed 's/./[^ ]*./')"
matches="$(list_reviewers | grep "$pattern" | cut -d ' ' -f1)

... now I also see that this script retrieves all authors from the commit log as reviewers which could a bit slow for large repos.

@andersonvom

This comment has been minimized.

Copy link
Owner Author

@andersonvom andersonvom commented Nov 3, 2015

Thanks for the tips! The sed expression needed a bit of change to work properly, but it's working great now. I'm also limiting the number of commits to 1000 so that there's no performance hit in large repos. =D

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