cpplint_pre_commit.hook 841 B

123456789101112131415161718192021222324252627
  1. #!/bin/bash
  2. TOTAL_ERRORS=0
  3. if [[ ! $TRAVIS_BRANCH ]]; then
  4. # install cpplint on local machine.
  5. if [[ ! $(which cpplint) ]]; then
  6. pip install cpplint
  7. fi
  8. # diff files on local machine.
  9. files=$(git diff --cached --name-status | awk '$1 != "D" {print $2}')
  10. else
  11. # diff files between PR and latest commit on Travis CI.
  12. branch_ref=$(git rev-parse "$TRAVIS_BRANCH")
  13. head_ref=$(git rev-parse HEAD)
  14. files=$(git diff --name-status $branch_ref $head_ref | awk '$1 != "D" {print $2}')
  15. fi
  16. # The trick to remove deleted files: https://stackoverflow.com/a/2413151
  17. for file in $files; do
  18. if [[ $file =~ ^(patches/.*) ]]; then
  19. continue;
  20. else
  21. cpplint --filter=-readability/fn_size,-build/include_what_you_use,-build/c++11 $file;
  22. TOTAL_ERRORS=$(expr $TOTAL_ERRORS + $?);
  23. fi
  24. done
  25. exit $TOTAL_ERRORS