From 91d6db578bc57919689392d04f0dc41224eadab0 Mon Sep 17 00:00:00 2001 From: "Henry D. Case" Date: Tue, 26 Jun 2018 09:32:47 +0100 Subject: [PATCH] CI: Fail build if code is wrongly formatted --- .travis.yml | 3 +++ README.md | 3 ++- _dev/Makefile | 2 ++ _dev/utils/fmtcheck.sh | 20 ++++++++++++++++++++ _dev/utils/pre-commit | 27 +++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100755 _dev/utils/fmtcheck.sh create mode 100755 _dev/utils/pre-commit diff --git a/.travis.yml b/.travis.yml index 2adca68..e36150e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,9 @@ env: matrix: fast_finish: true +before_install: + - make -f _dev/Makefile fmtcheck + install: - sudo pip install docker diff --git a/README.md b/README.md index 3c5780c..8dd877f 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,8 @@ There are number of things that need to be setup before running tests. Most impo ``` git clone https://github.com/cloudflare/tls-tris.git -cd tls-tris; make -f _dev/Makefile build-all +cd tls-tris; cp _dev/utils/pre-commit .git/hooks/ +make -f _dev/Makefile build-all ``` ### Testing diff --git a/_dev/Makefile b/_dev/Makefile index e532dce..5c582a8 100644 --- a/_dev/Makefile +++ b/_dev/Makefile @@ -112,5 +112,7 @@ clean: clean-all: clean rm -rf $(BUILD_DIR) +fmtcheck: + $(DEV_DIR)/utils/fmtcheck.sh .PHONY: $(BUILD_DIR) clean build build-test test test-unit test-bogo test-compat diff --git a/_dev/utils/fmtcheck.sh b/_dev/utils/fmtcheck.sh new file mode 100755 index 0000000..50e45e0 --- /dev/null +++ b/_dev/utils/fmtcheck.sh @@ -0,0 +1,20 @@ +#!/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. + +gofiles=$(find . -iname "*.go" ! -path "*/_dev/*") +#[ -z "$gofiles" ] && exit 0 + +unformatted=$(gofmt -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 -w $PWD/$fn" +done + +exit 1 + diff --git a/_dev/utils/pre-commit b/_dev/utils/pre-commit new file mode 100755 index 0000000..387f555 --- /dev/null +++ b/_dev/utils/pre-commit @@ -0,0 +1,27 @@ +#!/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. + +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 "Go files must be formatted with gofmt. Please run:" +for fn in $unformatted; do + echo >&2 " gofmt -w $PWD/$fn" +done + +exit 1 +