-
-
Save adnils/a5749ea1688f586f1d43d9f70edfff97 to your computer and use it in GitHub Desktop.
A pre-commit git hook that prompts you to automatically `gofmt` Go files staged to commit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Modified 2015, Austin Dizzy | |
# 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. | |
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '.go$') | |
[ -z "$gofiles" ] && exit 0 | |
unformatted=$(gofmt -l $gofiles) | |
[ -z "$unformatted" ] && exit 0 | |
# Some files are not gofmt'd. Print message and fail. | |
echo >&2 "The following Go files are not gofmt'd:" | |
for fn in $unformatted; do | |
echo >&2 " [*] $fn" | |
done | |
exec < /dev/tty | |
read -p "Would you like to gofmt them? (Y/n) " yn | |
if [ "$yn" = "" ]; then | |
yn='Y' | |
fi | |
case $yn in | |
[Yy]) | |
for fn in $unformatted; do | |
eval "gofmt -w $PWD/$fn" | |
eval "git add $PWD/$fn" | |
done | |
;; | |
[Nn]) exit 0 | |
;; | |
* ) echo "Please answer y or n for yes or no.";; | |
esac | |
exec <&- | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment