Skip to content

Instantly share code, notes, and snippets.

@hajimehoshi
Created July 1, 2020 12:24
Show Gist options
  • Save hajimehoshi/590bcd4a7656910cc5394137a43e4bd2 to your computer and use it in GitHub Desktop.
Save hajimehoshi/590bcd4a7656910cc5394137a43e4bd2 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Copyright 2012 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# git gofmt pre-commit hook
#
# To use, store as .git/hooks/pre-commit inside your repository and make sure
# it has execute permissions.
#
# This script does not handle file names that contain spaces.
#authors_cmd="git log --format='%aN <%aE>' | sort -f | uniq | grep -v '<[email protected]>'"
#authors=$(eval "$authors_cmd")
#authors_diff=$(echo "$authors" | diff AUTHORS -)
#if [ -n "$authors_diff" ]; then
# echo >&2 "AUTHORS should be updated."
# echo >&2 "Command:"
# echo >&2 " $authors_cmd > AUTHORS"
# echo >&2 ""
# echo >&2 "Diff:"
# echo >&2 "$authors_diff"
# exit 0
#fi
clang_format_diff=$(git clang-format --diff -q ./internal/graphicsdriver)
if [ "$clang_format_diff" != 'no modified files to format' ] &&
[ "$clang_format_diff" != 'clang-format did not modify any files' ] &&
[ "$clang_format_diff" != '' ]; then
echo >&2 "clang-format must be done."
echo >&2 "Command:"
echo >&2 " git clang-format"
echo >&2 ""
echo >&2 "Diff:"
echo >&2 "$clang_format_diff"
exit 1
fi
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$')
[ -z "$gofiles" ] && exit 0
unformatted=$(gofmt -s -w -l $gofiles)
[ -z "$unformatted" ] && exit 0
# Some files are not gofmt'd. Print message and fail.
echo >&2 "Go files must be formatted with gofmt. Please run:"
for fn in $unformatted; do
echo >&2 " gofmt -s -w $PWD/$fn"
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment