#!/bin/bash

# prstat -- Report the lines of code added or changed in the current tree vs
# upstream/master, excluding files that are known not to count against the line
# limit for platform review.

set -e

# Exclude data files and files that are in a /dev/ folder.
excludes=""
excludes="$excludes --exclude=*.mtl"
excludes="$excludes --exclude=*.obj"
excludes="$excludes --exclude=*.sdf"
excludes="$excludes --exclude=*.urdf"
excludes="$excludes --exclude=*.xml"
excludes="$excludes --exclude=*/dev/*"

# Also exclude files that contain the reviewable.io phrase that indicates
# generated code.  See https://github.com/Reviewable/Reviewable/wiki/FAQ.
marker="GENERATED FILE ""DO NOT EDIT"
toplevel=$(git rev-parse --show-toplevel)
git_base_path=$(git merge-base upstream/master HEAD)
for relpath in $(git diff $git_base_path | lsdiff $excludes --strip=1); do
    if fgrep -s -q -e"$marker" $toplevel/$relpath; then
        excludes="$excludes --exclude=$relpath"
    fi
done

# Display a final summary.
git diff $git_base_path |
    filterdiff --strip-match=1 --strip=1 $excludes |
    diffstat -p 0
