From 4867c420cae0abdf8990b1c198a6ae4937eee79b Mon Sep 17 00:00:00 2001 From: Gildas Cherruel Date: Mon, 23 Jan 2023 18:34:27 +0900 Subject: [PATCH] Pre-commit should run go fmt on go files --- hooks/common/pre-commit | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/hooks/common/pre-commit b/hooks/common/pre-commit index cd62f4c..5eef9f9 100755 --- a/hooks/common/pre-commit +++ b/hooks/common/pre-commit @@ -26,4 +26,17 @@ if ! get_config_bool gitflow.branch.allow-conflict-commit; then [[ ${#files_to_merge[@]} > 0 ]] && die "Resolve these merges before committing! ${files_to_merge[@]}" fi +STAGED_GO_FILES=$(git diff --cached --name-only | grep "\.go$") +[[ -z $STAGED_GO_FILES ]] && exit 0 + +GOFMT=$(command -v gofmt) +if (( $? )); then + GOFMT=$GOROOT/bin/gofmt + [[ ! -e $GOFMT ]] && die "The gofmt tool is missing, aborting" +fi + +for file in $STAGED_GO_FILES ; do + $GOFMT -w $file +done + exit 0