I’m working on a large project now with many other developers. Some of my teammates have been on the project for over a year, while others have joined in the past few weeks. I want to promote a strong code review culture on our team, so getting pull requests reviewed quickly and thoroughly matters. Occassionally, a pull request will sit around for a while waiting for a review. Some of our new developers, however, may not feel comfortable reviewing it because they have not had much experience with the codebase. Other times, it lingers because other developers are busy working on their own features and haven’t been notified that there is something for them to review.

I wanted to find a way to help new developers find teammates that could pair with them. I also wanted the author of a PR to be able to identify quickly a few people who might be best equipped to review new changes. Besides encouraging people to pair with each other through a code review, I developed a simple shell script that naively suggests reviewers.

First, the script compares HEAD (typically a feature or bugfix branch) with develop, our “base” branch to collect the file names of changed files:

collect_file_names() {
  git diff --name-only HEAD $BASE_BRANCH
}

It then pipes this output to a function blame_files that git blames each file, parses the author of each line, and finally outputs the three authors of the most lines:

blame_files() {
  xargs -L 1 git blame --line-porcelain | count_lines
}

count_lines() {
  sed -n 's/^author //p' | authors_of_most_lines
}

authors_of_most_lines() {
  sort | uniq -c | sort -rn | head -3
}

The --line-porcelain flag is especially useful for scripting. It outputs full commit information for each line of the file. This output can then be piped to sed to get a list of authors. Finally, sort, uniq, and head, sort and count the list of author-lines before printing the top three.

We can now run this script before creating a new pull request so that we can tag one or more of these teammates in our PR. Check out the full script in this Gist.