-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously we only formatted without adding to git index. Also made the script a little leaner (only format changed files).
- Loading branch information
Tobias Laving
committed
Jul 18, 2020
1 parent
51e5a84
commit f81de17
Showing
1 changed file
with
24 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,28 @@ | ||
#!/bin/bash | ||
function log { | ||
echo "Pre-commit: $@" | ||
echo "Pre-commit: $1" | ||
} | ||
# Function found https://github.com/edsrzf/gofmt-git-hook/blob/master/fmt-fix | ||
fix_fmt() { | ||
hash gofmt 2>&- || { echo >&2 "gofmt not in PATH."; exit 1; } | ||
OLDIFS=$IFS | ||
IFS=' | ||
' | ||
exitcode=0 | ||
for file in $(git diff --cached --name-only --diff-filter=ACM | grep '\.go$') | ||
do | ||
output=$(gofmt -w "$file") | ||
if test -n "$output" | ||
then | ||
# any output is a syntax error | ||
echo >&2 "$output" | ||
exitcode=1 | ||
fi | ||
git add "$file" | ||
done | ||
IFS=$OLDIFS | ||
exit $exitcode | ||
} | ||
|
||
log "Formatting" | ||
go fmt ./... | ||
fix_fmt |