Last active
June 28, 2018 19:56
-
-
Save jplitza/5e1d2380377728089b8f to your computer and use it in GitHub Desktop.
Do a git diffstat based on words (using git diff --word-diff and wc -w). Might break on merge commits!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright (c) 2014 Jan-Philipp Litza <[email protected]> | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
set -eu | |
width=$(($(tput cols) - 49)) | |
function scale_linear(){ | |
if [ $1 -eq 0 ]; then | |
echo 0 | |
else | |
echo $((1 + $1 * ($2 - 1) / $3 )) | |
fi | |
} | |
GIT_ROOT="$(git rev-parse --show-toplevel)" | |
cd "$GIT_ROOT" | |
git diff --numstat "$@" | awk '{print $3}' | ( | |
declare -i removed=0 removed_file=0 | |
declare -i added=0 added_file=0 | |
declare -i filecount=0 | |
while read file; do | |
removed_file=$(git diff --word-diff=porcelain "$@" -- "$file" | grep -Po '(?<=^-).([^-].*)?$' | tr -cd 'a-zA-Z ' | wc -w) | |
added_file=$( git diff --word-diff=porcelain "$@" -- "$file" | grep -Po '(?<=^\+).([^\+].*)?$' | tr -cd 'a-zA-Z ' | wc -w) | |
removed=$(($removed + $removed_file)) | |
added=$(($added + $added_file)) | |
filecount=$(($filecount + 1)) | |
printf " %-40s | %4i " $file $(($removed_file + $added_file)) | |
if [ $(($added_file + $removed_file)) -ge $width ]; then | |
total=$(scale_linear $(($added_file + $removed_file)) $width $(($added_file + $removed_file))) | |
if [ $total -lt 2 ]; then | |
total=2 | |
fi | |
if [ $added_file -lt $removed_file ]; then | |
added_file=$(scale_linear $added_file $width $(($added_file + $removed_file))) | |
removed_file=$(($total - $added_file)) | |
else | |
removed_file=$(scale_linear $removed_file $width $(($added_file + $removed_file))) | |
added_file=$(($total - $removed_file)) | |
fi | |
fi | |
echo -en "\e[32m" | |
for i in $(seq 1 $added_file); do | |
echo -n "+" | |
done | |
echo -en "\e[31m" | |
for i in $(seq 1 $removed_file); do | |
echo -n "-" | |
done | |
echo -e "\e[m" | |
done | |
echo " ${filecount} file(s) changed, ${added} word insertions(+), ${removed} word deletions(-)" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment