Skip to content

Instantly share code, notes, and snippets.

@paolobrasolin
Last active March 17, 2018 15:47
Show Gist options
  • Save paolobrasolin/793960a53f2fe1f7ff5d40012e870fd4 to your computer and use it in GitHub Desktop.
Save paolobrasolin/793960a53f2fe1f7ff5d40012e870fd4 to your computer and use it in GitHub Desktop.
git locstat

git locstat

The alias computes per-user lines of code.

Parameters are passed on to git ls-files: the main intended use is tallying a certain section of a project.

E.g. to examine the backend of a Rails' project you can run

git locstat :**.rb :^lib

Note the alias is defined as a command, so it's always run from the root of the project.

The script additionally computes total and percentages.

[alias]
locstat = "!git ls-files -z \"$@\" | parallel --no-notice --null git blame -w -M -C -C --line-porcelain | sed -n 's/^author //p' | sort -i | uniq -ic | sort -nr #"
read -r -d '' AWK_SCRIPT <<-'EOF'
{
loc[NR] = $1;
usr[NR] = $2;
tot += loc[NR];
}END{
while (++i <= NR) {
printf "%s%\t%d%%\t%s\n", loc[i], 100*loc[i]/tot, usr[i];
}
printf "%s%\t%d%%\t%s\n", tot, 100, "TOTAL";
}
EOF
git ls-files -z "$@" |
parallel --no-notice --null git blame -w -M -C -C --line-porcelain |
sed -n 's/^author //p' |
sort --ignore-case |
uniq -ic |
sort --numeric-sort --reverse |
sed -e 's/^ *//;s/ / /' |
awk -F" " "$AWK_SCRIPT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment