#!/bin/sh

active_branches () {
    git branch --remote --list --no-merged HEAD \
        | grep -v -- -\> \
        | while read branch; do
        commit=`git log -n1 --since='3 months' $branch`
        if [ x"$commit" != "x" ]; then
            echo $branch
        fi
    done
}

first="1"
for file in $@; do
    if [ $first = "1" ]; then
        first="0"
    else
        echo
    fi
    echo "Changed versions of $file"
    echo
    printf "%11s | %6s%% | %-30s | %s\n" 'LOC changed' '' 'Branch' 'Last commit by'
    for branch in `active_branches`; do
        changed_lines=`git diff HEAD...$branch -- $file | grep '^[+-][^+-]' | wc -l`
        if [ "$changed_lines" != "0" ]; then
            file_lines=`cat $file | wc -l`
            percentage=`awk "BEGIN {printf \"%.2f\", $changed_lines/$file_lines*100}"`
            last_commit_info=`git log $branch -n1 --format='format:%ae, %ar'`
            printf "%11s | %6s%% | %-30s | %s\n" \
                   "$changed_lines/$file_lines" "$percentage" \
                   "$branch" "$last_commit_info"
        fi
    done
done
