#!/bin/bash

bash -c "cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON"

TOTAL_ERRORS=0

# The trick to remove deleted files: https://stackoverflow.com/a/2413151
for file in $(git diff --cached --name-status | awk '$1 != "D" {print $2}' | grep "src/" | grep -v ".pb." | grep -v ".h"); do
    echo "clang-tidy check $file";
    clang-tidy $file --fix --fix-errors --header-filter=.* 
    TOTAL_ERRORS=$(expr $TOTAL_ERRORS + $?);
    echo "clang-tidy error TOTAL_ERRORS = $TOTAL_ERRORS . "
done

rm -f compile_commands.json

exit $TOTAL_ERRORS

