From 27f4f9a51f9ac0605c323f177d4a57d1635f75a2 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Sun, 24 Jan 2016 09:50:30 +0100 Subject: [PATCH] Add validation script and update travis config. Signed-off-by: Vincent Demeester --- .travis.yml | 7 ++++--- hack/.validate | 33 +++++++++++++++++++++++++++++++ hack/validate-git-marks | 44 +++++++++++++++++++++++++++++++++++++++++ hack/validate-gofmt | 30 ++++++++++++++++++++++++++++ hack/validate-lint | 34 +++++++++++++++++++++++++++++++ hack/validate-vet | 32 ++++++++++++++++++++++++++++++ 6 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 hack/.validate create mode 100755 hack/validate-git-marks create mode 100755 hack/validate-gofmt create mode 100755 hack/validate-lint create mode 100755 hack/validate-vet diff --git a/.travis.yml b/.travis.yml index 022d6cab..dc3d23f5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,8 +12,9 @@ install: - go get github.com/golang/lint/golint script: - - go vet . - - test -z "$(golint . | tee /dev/stderr)" - - test -z "$(gofmt -s -l . | tee /dev/stderr)" + - hack/validate-vet + - hack/validate-lint + - hack/validate-gofmt + - hack/validate-git-marks - go build -o skopeo . - go test -v . diff --git a/hack/.validate b/hack/.validate new file mode 100644 index 00000000..31d7164b --- /dev/null +++ b/hack/.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/runcom/skopeo.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/hack/validate-git-marks b/hack/validate-git-marks new file mode 100755 index 00000000..daa6950e --- /dev/null +++ b/hack/validate-git-marks @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +source "$(dirname "$BASH_SOURCE")/.validate" + +# folders=$(find * -type d | egrep -v '^Godeps|bundles|.git') + +IFS=$'\n' +files=( $(validate_diff --diff-filter=ACMR --name-only -- '*' | grep -v '^vendor/' || true) ) +unset IFS + +badFiles=() +for f in "${files[@]}"; do + if [ $(grep -r "^<<<<<<<" $f) ]; then + badFiles+=( "$f" ) + continue + fi + + if [ $(grep -r "^>>>>>>>" $f) ]; then + badFiles+=( "$f" ) + continue + fi + + if [ $(grep -r "^=======$" $f) ]; then + badFiles+=( "$f" ) + continue + fi + set -e +done + + +if [ ${#badFiles[@]} -eq 0 ]; then + echo 'Congratulations! There is no conflict.' +else + { + echo "There is trace of conflict(s) in the following files :" + for f in "${badFiles[@]}"; do + echo " - $f" + done + echo + echo 'Please fix the conflict(s) commit the result.' + echo + } >&2 + false +fi diff --git a/hack/validate-gofmt b/hack/validate-gofmt new file mode 100755 index 00000000..8fc88cc5 --- /dev/null +++ b/hack/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 '^vendor/' || 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 diff --git a/hack/validate-lint b/hack/validate-lint new file mode 100755 index 00000000..f8cb9c9f --- /dev/null +++ b/hack/validate-lint @@ -0,0 +1,34 @@ +#!/bin/bash + +source "$(dirname "$BASH_SOURCE")/.validate" + +# We will eventually get to the point where packages should be the complete list +# of subpackages, vendoring excluded, as given by: +# +IFS=$'\n' +files=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^vendor/\|^integration' || true) ) +unset IFS + +errors=() +for f in "${files[@]}"; do + # we use "git show" here to validate that what's committed passes go lint + failedLint=$(golint "$f") + if [ "$failedLint" ]; then + errors+=( "$failedLint" ) + fi +done + +if [ ${#errors[@]} -eq 0 ]; then + echo 'Congratulations! All Go source files have been linted.' +else + { + echo "Errors from golint:" + for err in "${errors[@]}"; do + echo "$err" + done + echo + echo 'Please fix the above errors. You can test via "golint" and commit the result.' + echo + } >&2 + false +fi diff --git a/hack/validate-vet b/hack/validate-vet new file mode 100755 index 00000000..e88f7549 --- /dev/null +++ b/hack/validate-vet @@ -0,0 +1,32 @@ +#!/bin/bash + +source "$(dirname "$BASH_SOURCE")/.validate" + +IFS=$'\n' +files=( $(validate_diff --diff-filter=ACMR --name-only -- '*.go' | grep -v '^vendor/' || true) ) +unset IFS + +errors=() +for f in "${files[@]}"; do + # we use "git show" here to validate that what's committed passes go vet + failedVet=$(go vet "$f") + if [ "$failedVet" ]; then + errors+=( "$failedVet" ) + fi +done + + +if [ ${#errors[@]} -eq 0 ]; then + echo 'Congratulations! All Go source files have been vetted.' +else + { + echo "Errors from go vet:" + for err in "${errors[@]}"; do + echo " - $err" + done + echo + echo 'Please fix the above errors. You can test via "go vet" and commit the result.' + echo + } >&2 + false +fi