Skip to content

Instantly share code, notes, and snippets.

@andersonvom
Last active June 12, 2017 08:40
Show Gist options
  • Save andersonvom/924fdc5f92aefa5eca9c to your computer and use it in GitHub Desktop.
Save andersonvom/924fdc5f92aefa5eca9c to your computer and use it in GitHub Desktop.
#!/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
Copy link

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
Copy link
Author

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