Skip to content

Commit

Permalink
Updated pre-commit hook
Browse files Browse the repository at this point in the history
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.
26 changes: 24 additions & 2 deletions githooks/pre-commit
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

0 comments on commit f81de17

Please sign in to comment.