diff --git a/hack/validate/vendor b/hack/validate/vendor index 8a7992c24f..9aa78ff889 100755 --- a/hack/validate/vendor +++ b/hack/validate/vendor @@ -5,38 +5,30 @@ set -e SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${SCRIPTDIR}/.validate" -tidy_files=('go.mod' 'go.sum') +modules_files=('api/go.mod' 'client/go.mod' 'man/go.mod' 'go.mod') +tidy_files=("${modules_files[@]}" 'api/go.sum' 'client/go.sum' 'man/go.sum' 'go.sum') vendor_files=("${tidy_files[@]}" 'vendor/') -validate_vendor_files_exist() { - local file - for file in "${vendor_files[@]}"; do - if [ ! -e "$file" ]; then - echo >&2 "ERROR: required file '$file' does not exist" +validate_tidy_modules() { + # check that all go.mod files exist in HEAD; go.sum files are generated by 'go mod tidy' + # so we don't need to check for their existence beforehand + for f in "${modules_files[@]}"; do + if [ ! -f "$f" ]; then + echo >&2 "ERROR: missing $f" return 1 fi done - return 0 -} - -validate_vendor_tidy() { # run mod tidy ./hack/vendor.sh tidy # check if any files have changed - git diff --quiet HEAD -- "${tidy_files[@]}" + git diff --quiet HEAD -- "${tidy_files[@]}" && [ -z "$(git ls-files --others --exclude-standard)" ] } validate_vendor_diff() { - 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 vendor - # check if any files have changed - git diff --quiet HEAD -- "${vendor_files[@]}" - else - echo >&2 'INFO: no vendor changes in diff; skipping vendor check.' - fi + # recreate vendor/ + ./hack/vendor.sh vendor + # check if any files have changed + git diff --quiet HEAD -- "${vendor_files[@]}" && [ -z "$(git ls-files --others --exclude-standard)" ] } validate_vendor_license() { @@ -48,26 +40,22 @@ validate_vendor_license() { done < <(awk '/^# /{ print $2 }' vendor/modules.txt) } -# First check the required files exist as git diff will not report missing files. -if ! validate_vendor_files_exist; then - { - echo 'FAIL: Vendoring was not performed correctly!' - echo - echo 'Please revendor with hack/vendor.sh' - } >&2 - exit 1 -fi - -if validate_vendor_tidy && validate_vendor_diff && validate_vendor_license; then +if validate_tidy_modules && 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 + if [ -n "$(git ls-files --others --exclude-standard)" ]; then + echo 'The following files are missing:' + git ls-files --others --exclude-standard + echo + fi + if [ -n "$(git diff --name-status HEAD -- "${vendor_files[@]}")" ]; then + echo 'The following files changed during re-vendor:' + git diff --name-status HEAD -- "${vendor_files[@]}" + echo + fi echo 'Please revendor with hack/vendor.sh' echo git diff --diff-filter=M -- "${vendor_files[@]}" diff --git a/hack/vendor.sh b/hack/vendor.sh index ef54835676..c43ef967c7 100755 --- a/hack/vendor.sh +++ b/hack/vendor.sh @@ -7,15 +7,31 @@ set -e SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PROJECT_DIR="$(cd "$SCRIPTDIR/.." && pwd)" + +# All module paths are relative to PROJECT_DIR +# Currently only a subset of modules are vendored. +vendor_modules_paths=(. man) +modules_paths=(api client "${vendor_modules_paths[@]}") tidy() ( - set -x - go mod tidy + for module_path in "${modules_paths[@]}"; do + ( + set -x + cd "$PROJECT_DIR/$module_path" + go mod tidy + ) + done ) vendor() ( - set -x - go mod vendor + for module_path in "${vendor_modules_paths[@]}"; do + ( + set -x + cd "$PROJECT_DIR/$module_path" + go mod vendor + ) + done ) replace() (