1#!/bin/bash 2# Owner: gchips-productivity-team@google.com 3# VERSION: 2019-12-16 4# Pre commit hook to auto format code using git clang-format 5 6# change the default style to Google 7git config --global clangformat.style Google 8 9# default PRE_COMMIT_CLANG_FORMAT to true 10PRE_COMMIT_CLANG_FORMAT="${PRE_COMMIT_CLANG_FORMAT:-true}" 11 12check_clang_format() { 13 if ! [ -x "$(command -v clang-format)" ]; then 14 echo "Warning: clang-format not found. Unable to format source code before commit. Please check go/clang-format-setup for setup instructions." 15 fi 16} 17 18if [ "$PRE_COMMIT_CLANG_FORMAT" = true ] ; then 19 check_clang_format 20 FILES=$(git clang-format) 21 counter=0 22 for file in $FILES ; do 23 if [[ -f "$file" ]]; then 24 git add ${file} 25 echo -e "[Info] Changes in ${file} have been formatted" 26 ((counter++)) 27 fi 28 done 29 30 if (( counter > 0 )); then 31 echo "[Info] $counter file(s) have been formatted" 32 # Track Usage 33 MY_PATH=${PWD//\//"%2F"} 34 curl "https://us-central1-si-sw-eng-prod-team.cloudfunctions.net/trackAutoFormatUsage?user=${USER}&pwd=${MY_PATH}×tamp=$(date +%s)&type=FORMAT&filesFormatted=${counter}" > /dev/null 2>&1 35 fi 36 37fi 38 39# Run google's default pre-commit 40if [ -x /usr/lib/git-core/google_hook ]; then 41 /usr/lib/git-core/google_hook pre-commit "$@" 42else 43 echo 'warning: Cannot run /usr/lib/git-core/google_hook.' \ 44 'If this is unexpected, please file a go/git-bug' >&2 45fi 46