Git Blame: List a Particular Author’s Files Across a Code Base

My goal: to have git tell me which files across the breadth of our current code base have lines of code in them that are attributed to a particular author.

My googling for a script came up with bubkes, so I decided to write my own and share that on the internet. No doubt there’s a better way of doing this, and no doubt there’s a better bash script possible (my bash scripting skills are rudimentary at best), but what I did does do the job.

Here’s the bash script:

#!/bin/bash

matchGitBlame() {

	if [[ "$1" == *$2* ]]
	then
		local count=`grep -o "$2" <<< "$1" | wc -l`

		if [ $count -gt 0 ]
		then
			echo "$count : $2 : $3"
		fi
	fi
}

files=`find . -type f`
name=$1
name2=$2

for file in $files
do
	blame=`git blame $file`
	matchGitBlame "$blame" "$name" "$file"
	matchGitBlame "$blame" "$name2" "$file"
done

If you execute the script like so (the script works with one or two author names as attributes):
./blame-script.sh authorName1 authorName2 > blame-script-results.txt && sort blame-script-results.txt -rVo blame-script-results.txt

you should find in the file named blame-script-results.txt something like the following:

180 : authorName1 : ./Breadcrumb.java
43 : authorName1 : ./ManagementTrail.java
41 : authorName1 : ./DashboardTrail.java
24 : authorName2 : ./NavigationTabsPanel.java
17 : authorName1 : ./DropDownMenuPanel.java
5 : authorName2 : ./ManageReportDropDownMenuPanel.java
1 : authorName1 : ./CrumbItem.java

Leave a Reply