Skip to content

Instantly share code, notes, and snippets.

@hajimehoshi
Last active November 13, 2018 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hajimehoshi/7f3a9cd4fab84d49a9ae1cbca84ded92 to your computer and use it in GitHub Desktop.
Save hajimehoshi/7f3a9cd4fab84d49a9ae1cbca84ded92 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 '<hajimehoshi@hajimehoshi.com>'"
#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)
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