diff --git a/hack/go-mod-prepare.sh b/hack/go-mod-prepare.sh index f1fb655a5c..e5552aaa5f 100755 --- a/hack/go-mod-prepare.sh +++ b/hack/go-mod-prepare.sh @@ -1,8 +1,13 @@ #!/usr/bin/env bash -ROOTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +set -e -cat > "${ROOTDIR}/go.mod" << EOF +SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +ROOTDIR="$(git -C "$SCRIPTDIR" rev-parse --show-toplevel)" + +set -x + +tee "${ROOTDIR}/go.mod" << EOF module github.com/docker/docker go 1.18 diff --git a/hack/validate/vendor b/hack/validate/vendor index 851bf6e249..a0b35d3f25 100755 --- a/hack/validate/vendor +++ b/hack/validate/vendor @@ -1,53 +1,55 @@ #!/usr/bin/env bash +set -e + SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${SCRIPTDIR}/.validate" +tidy_files=('vendor.mod' 'vendor.sum') +vendor_files=("${tidy_files[@]}" 'vendor/') + +validate_vendor_tidy() { + # run mod tidy + ./hack/vendor.sh tidy + # check if any files have changed + git diff --quiet HEAD -- "${tidy_files[@]}" +} + validate_vendor_diff() { - IFS=$'\n' - check_files=('vendor.sum' 'vendor.mod' 'vendor/') - # shellcheck disable=SC2207 - changed_files=($(validate_diff --diff-filter=ACMR --name-only -- "${check_files[@]}" || true)) - unset IFS + mapfile -t changed_files < <(validate_diff --diff-filter=ACMR --name-only -- "${vendor_files[@]}") if [ -n "${TEST_FORCE_VALIDATE:-}" ] || [ "${#changed_files[@]}" -gt 0 ]; then # recreate vendor/ - ./hack/vendor.sh + ./hack/vendor.sh vendor # check if any files have changed - diffs="$(git status --porcelain -- "${check_files[@]}" 2> /dev/null)" - mfiles="$(echo "$diffs" | awk '/^ M / {print $2}')" - if [ "$diffs" ]; then - { - echo 'The result of go mod vendor differs' - echo - echo "$diffs" - echo - echo 'Please vendor your package with hack/vendor.sh.' - echo - if [ -n "$mfiles" ]; then - git diff -- "$mfiles" - fi - } >&2 - false - else - echo 'Congratulations! All vendoring changes are done the right way.' - fi + git diff --quiet HEAD -- "${vendor_files[@]}" else - echo 'No vendor changes in diff.' + echo >&2 'INFO: no vendor changes in diff; skipping vendor check.' fi } -# 1. make sure all the vendored packages are used -# 2. make sure all the packages contain license information (just warning, because it can cause false-positive) -validate_vendor_used() { - for f in $(mawk '$1 = "#" { print $2 }' 'vendor/modules.txt'); do - if [ -d "vendor/$f" ]; then - if ! echo "vendor/$f"/* | grep -qiEc '/(LICENSE|COPYING)'; then - echo "WARNING: could not find copyright information for $f" - fi +validate_vendor_license() { + while IFS= read -r module; do + test -d "vendor/$module" || continue + if ! compgen -G "vendor/$module/*" | grep -qEi '/(LICENSE|COPYING)[^/]*$'; then + echo >&2 "WARNING: could not find copyright information for $module" fi - done + done < <(awk '/^# /{ print $2 }' vendor/modules.txt) } -validate_vendor_diff -validate_vendor_used +if validate_vendor_tidy && validate_vendor_diff && validate_vendor_license; then + echo >&2 'PASS: Vendoring has been performed correctly!' +else + { + echo 'FAIL: Vendoring was not performed correctly!' + echo + echo 'The following files changed during re-vendor:' + echo + git diff --name-status HEAD -- "${vendor_files[@]}" + echo + echo 'Please revendor with hack/vendor.sh' + echo + git diff --diff-filter=M -- "${vendor_files[@]}" + } >&2 + exit 1 +fi diff --git a/hack/vendor.sh b/hack/vendor.sh index 52eeda2460..d106494be9 100755 --- a/hack/vendor.sh +++ b/hack/vendor.sh @@ -1,14 +1,37 @@ #!/usr/bin/env bash - -# This file is just wrapper around 'go mod vendor' tool. +# +# This file is just a wrapper around the 'go mod vendor' tool. # For updating dependencies you should change `vendor.mod` file in root of the # project. set -e -set -x SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" "${SCRIPTDIR}"/go-mod-prepare.sh -GO111MODULE=auto go mod tidy -modfile 'vendor.mod' -compat 1.18 -GO111MODULE=auto go mod vendor -modfile vendor.mod +export GO111MODULE=on + +tidy() ( + set -x + go mod tidy -modfile vendor.mod -compat 1.18 +) + +vendor() ( + set -x + go mod vendor -modfile vendor.mod +) + +help() { + printf "%s:\n" "$(basename "$0")" + echo " - tidy: run go mod tidy" + echo " - vendor: run go mod vendor" + echo " - all: run tidy && vendor" + echo " - help: show this help" +} + +case "$1" in + tidy) tidy ;; + vendor) vendor ;; + ""|all) tidy && vendor ;; + *) help ;; +esac