Concise Git Information (Bash Prompt #8)

Bash Prompt Index

This started when I decided I wanted to know not only whether or not the local git repository was changed, but also how much it was changed. The resulting code is, I'm forced to admit, mostly a product of StackOverflow (worth a read if you're at all interested in how this was achieved) but I did add at least a bit of knowledge of shell utilities and the Unix pipeline, and a certain joy in creating tiny but useful pieces of information for the Bash Prompt.

changecount=$(git diff --numstat --pretty="%H" | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("+%d-%d\n", plus, minus)}')
if ! [ "${changecount}" == "+0-0" ]
then
    echo "${changecount}"
fi

If run in a git repository folder, this produces something like +5-3 or +172-22, with the first part being the number of lines added, and the second being the number of lines removed. If there are no changes, the result is +0-0 and the above code doesn't print anything.

These changes can also be found in my dotbashprompt project in any prompt that shows git status (nearly all of them).