From 2de953490d6a4ccba9668484fce1f028578c6f77 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Thu, 11 Apr 2013 11:30:35 -0700 Subject: [PATCH] Contrib post-commit hook for checking gofmt --- hack/README.md | 1 + hack/fmt-check.hook | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 hack/README.md create mode 100644 hack/fmt-check.hook diff --git a/hack/README.md b/hack/README.md new file mode 100644 index 0000000000..06cdd50854 --- /dev/null +++ b/hack/README.md @@ -0,0 +1 @@ +This directory contains material helpful for hacking on docker. diff --git a/hack/fmt-check.hook b/hack/fmt-check.hook new file mode 100644 index 0000000000..cd18a18bcb --- /dev/null +++ b/hack/fmt-check.hook @@ -0,0 +1,46 @@ +#!/bin/sh + +# This pre-commit hook will abort if a committed file doesn't pass gofmt. +# By Even Shaw +# http://github.com/edsrzf/gofmt-git-hook + +test_fmt() { + hash gofmt 2>&- || { echo >&2 "gofmt not in PATH."; exit 1; } + IFS=' +' + for file in `git diff --cached --name-only --diff-filter=ACM | grep '\.go$'` + do + output=`git cat-file -p :$file | gofmt -l 2>&1` + if test $? -ne 0 + then + output=`echo "$output" | sed "s,,$file,"` + syntaxerrors="${list}${output}\n" + elif test -n "$output" + then + list="${list}${file}\n" + fi + done + exitcode=0 + if test -n "$syntaxerrors" + then + echo >&2 "gofmt found syntax errors:" + printf "$syntaxerrors" + exitcode=1 + fi + if test -n "$list" + then + echo >&2 "gofmt needs to format these files (run gofmt -w and git add):" + printf "$list" + exitcode=1 + fi + exit $exitcode +} + +case "$1" in + --about ) + echo "Check Go code formatting" + ;; + * ) + test_fmt + ;; +esac