diff --git a/Makefile b/Makefile index e12cac5d..d576d089 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,15 @@ RUNC_TEST_IMAGE=runc_test PROJECT=github.com/opencontainers/runc -TEST_DOCKERFILE=test_Dockerfile +TEST_DOCKERFILE=script/test_Dockerfile export GOPATH:=$(CURDIR)/Godeps/_workspace:$(GOPATH) all: go build -o runc . -lint: +vet: go get golang.org/x/tools/cmd/vet + +lint: vet go vet ./... go fmt ./... @@ -25,3 +27,9 @@ install: clean: rm runc + +validate: vet + script/validate-gofmt + go vet ./... + +ci: validate localtest diff --git a/script/.validate b/script/.validate new file mode 100644 index 00000000..170d6747 --- /dev/null +++ b/script/.validate @@ -0,0 +1,33 @@ +#!/bin/bash + +if [ -z "$VALIDATE_UPSTREAM" ]; then + # this is kind of an expensive check, so let's not do this twice if we + # are running more than one validate bundlescript + + VALIDATE_REPO='https://github.com/opencontainers/runc.git' + VALIDATE_BRANCH='master' + + if [ "$TRAVIS" = 'true' -a "$TRAVIS_PULL_REQUEST" != 'false' ]; then + VALIDATE_REPO="https://github.com/${TRAVIS_REPO_SLUG}.git" + VALIDATE_BRANCH="${TRAVIS_BRANCH}" + fi + + VALIDATE_HEAD="$(git rev-parse --verify HEAD)" + + git fetch -q "$VALIDATE_REPO" "refs/heads/$VALIDATE_BRANCH" + VALIDATE_UPSTREAM="$(git rev-parse --verify FETCH_HEAD)" + + VALIDATE_COMMIT_LOG="$VALIDATE_UPSTREAM..$VALIDATE_HEAD" + VALIDATE_COMMIT_DIFF="$VALIDATE_UPSTREAM...$VALIDATE_HEAD" + + validate_diff() { + if [ "$VALIDATE_UPSTREAM" != "$VALIDATE_HEAD" ]; then + git diff "$VALIDATE_COMMIT_DIFF" "$@" + fi + } + validate_log() { + if [ "$VALIDATE_UPSTREAM" != "$VALIDATE_HEAD" ]; then + git log "$VALIDATE_COMMIT_LOG" "$@" + fi + } +fi diff --git a/test_Dockerfile b/script/test_Dockerfile similarity index 100% rename from test_Dockerfile rename to script/test_Dockerfile diff --git a/script/validate-gofmt b/script/validate-gofmt new file mode 100755 index 00000000..c565976b --- /dev/null +++ b/script/validate-gofmt @@ -0,0 +1,30 @@ +#!/bin/bash + +source "$(dirname "$BASH_SOURCE")/.validate" + +IFS=$'\n' +files=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^Godeps/' || true) ) +unset IFS + +badFiles=() +for f in "${files[@]}"; do + # we use "git show" here to validate that what's committed is formatted + if [ "$(git show "$VALIDATE_HEAD:$f" | gofmt -s -l)" ]; then + badFiles+=( "$f" ) + fi +done + +if [ ${#badFiles[@]} -eq 0 ]; then + echo 'Congratulations! All Go source files are properly formatted.' +else + { + echo "These files are not properly gofmt'd:" + for f in "${badFiles[@]}"; do + echo " - $f" + done + echo + echo 'Please reformat the above files using "gofmt -s -w" and commit the result.' + echo + } >&2 + false +fi