From f6b58964d70f7a66ab0ef9affc03a2320b4abd22 Mon Sep 17 00:00:00 2001
From: Sandro Santilli <strk@kbt.io>
Date: Tue, 6 Jun 2017 15:53:16 +0200
Subject: [PATCH] Include formatting check to the `make test` (and thus also
 `check`) rule (#1366)

* Include formatting check to the `make check` rule

... and give it its own standalone target too (make fmt-check)

Show diff on fmt-check failure
Do not allow running "fmt-check" with incompatible go version
Also simplify the `fmt` rule

* Forbid running "make fmt" with Go version != 1.7 or 1.8

* Ignore bindata.go for spelling and formatting checks

also remove duplicated variable assignment for GOFILES

* Run `make fmt`
---
 Makefile                   | 22 ++++++++++++++++++----
 models/repo_test.go        |  2 +-
 modules/setting/setting.go |  4 ++--
 3 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/Makefile b/Makefile
index 2566677d68..0b9935fc05 100644
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,8 @@ BINDATA := modules/{options,public,templates}/bindata.go
 STYLESHEETS := $(wildcard public/less/index.less public/less/_*.less)
 JAVASCRIPTS :=
 DOCKER_TAG := gitea/gitea:latest
+GOFILES := $(shell find . -name "*.go" -type f -not -path "./vendor/*" -not -path "*/bindata.go")
+GOFMT ?= gofmt -s
 
 GOFLAGS := -i -v
 EXTRA_GOFLAGS ?=
@@ -12,7 +14,6 @@ LDFLAGS := -X "main.Version=$(shell git describe --tags --always | sed 's/-/+/'
 
 PACKAGES ?= $(filter-out code.gitea.io/gitea/integrations,$(shell go list ./... | grep -v /vendor/))
 SOURCES ?= $(shell find . -name "*.go" -type f)
-GOFILES := $(shell find . -name "*.go" -type f -not -path "./vendor/*")
 
 TAGS ?=
 
@@ -42,9 +43,12 @@ clean:
 	go clean -i ./...
 	rm -rf $(EXECUTABLE) $(DIST) $(BINDATA)
 
+required-gofmt-version:
+	@go version  | grep -q '\(1.7\|1.8\)' || { echo "We require go version 1.7 or 1.8 to format code" >&2 && exit 1; }
+
 .PHONY: fmt
-fmt:
-	find . -name "*.go" -type f -not -path "./vendor/*" | xargs gofmt -s -w
+fmt: required-gofmt-version
+	$(GOFMT) -w $(GOFILES)
 
 .PHONY: vet
 vet:
@@ -88,8 +92,18 @@ misspell:
 	fi
 	misspell -w -i unknwon $(GOFILES)
 
+.PHONY: fmt-check
+fmt-check: required-gofmt-version
+	# get all go files and run go fmt on them
+	@diff=$$($(GOFMT) -d $(GOFILES)); \
+	if [ -n "$$diff" ]; then \
+		echo "Please run 'make fmt' and commit the result:"; \
+		echo "$${diff}"; \
+		exit 1; \
+	fi;
+
 .PHONY: test
-test:
+test: fmt-check
 	go test $(PACKAGES)
 
 .PHONY: test-coverage
diff --git a/models/repo_test.go b/models/repo_test.go
index 13ac272b6d..4b6fd440aa 100644
--- a/models/repo_test.go
+++ b/models/repo_test.go
@@ -11,8 +11,8 @@ import (
 	"code.gitea.io/gitea/modules/markdown"
 	"code.gitea.io/gitea/modules/setting"
 
-	"github.com/stretchr/testify/assert"
 	"github.com/Unknwon/com"
+	"github.com/stretchr/testify/assert"
 )
 
 func TestRepo(t *testing.T) {
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index cd2d4ab720..67018cf839 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -177,7 +177,7 @@ var (
 
 		// Repository local settings
 		Local struct {
-			LocalCopyPath       string
+			LocalCopyPath string
 		} `ini:"-"`
 	}{
 		AnsiCharset:            "",
@@ -214,7 +214,7 @@ var (
 
 		// Repository local settings
 		Local: struct {
-			LocalCopyPath  string
+			LocalCopyPath string
 		}{
 			LocalCopyPath: "tmp/local-repo",
 		},