sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/hack/make-rules/verify/go-deps.sh (about) 1 #!/usr/bin/env bash 2 # Copyright 2021 The Kubernetes Authors. 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 set -o nounset 17 set -o errexit 18 set -o pipefail 19 20 REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd -P)" 21 cd $REPO_ROOT 22 23 echo "Ensuring go version." 24 source ./hack/build/setup-go.sh 25 26 # place to stick temp binaries 27 BINDIR="${REPO_ROOT}/_bin" 28 mkdir -p "${BINDIR}" 29 30 # TMP_REPO is used in make_temp_repo_copy 31 TMP_REPO="$(TMPDIR="${BINDIR}" mktemp -d "${BINDIR}/verify-deps.XXXXX")" 32 33 # exit trap cleanup for TMP_REPO 34 cleanup() { 35 if [[ -n "${TMP_REPO}" ]]; then 36 rm -rf "${TMP_REPO}" 37 fi 38 } 39 40 # copies repo into a temp root saved to TMP_REPO 41 make_temp_repo_copy() { 42 # we need to copy everything but bin (and the old _output) (which is .gitignore anyhow) 43 find . \ 44 -mindepth 1 -maxdepth 1 \ 45 \( \ 46 -type d -path "./.git" -o \ 47 -type d -path "./_bin" -o \ 48 -type d -path "./_output" \ 49 -type d -path "./bazel-bin" \ 50 -type d -path "./bazel-out" \ 51 -type d -path "./bazel-test-infra" \ 52 -type d -path "./bazel-testlogs" \ 53 \) -prune -o \ 54 -exec bash -c 'cp -r "${0}" "${1}/${0}" >/dev/null 2>&1' {} "${TMP_REPO}" \; 55 } 56 57 main() { 58 trap cleanup EXIT 59 60 # copy repo root into tempdir under ./_bin 61 make_temp_repo_copy 62 63 # run generated code update script 64 cd "${TMP_REPO}" 65 REPO_ROOT="${TMP_REPO}" make update-go-deps 66 67 # make sure the temp repo has no changes relative to the real repo 68 diff=$(diff -Nupr \ 69 -x ".git" \ 70 -x "_bin" \ 71 -x "_output" \ 72 -x "bazel-bin" \ 73 -x "bazel-out" \ 74 -x "bazel-test-infra" \ 75 -x "bazel-testlogs" \ 76 "${REPO_ROOT}" "${TMP_REPO}" 2>/dev/null || true) 77 if [[ -n "${diff}" ]]; then 78 echo "unexpectedly dirty working directory after ${0}" >&2 79 echo "" >&2 80 echo "${diff}" >&2 81 echo "" >&2 82 echo "please run make update-go-deps" >&2 83 exit 1 84 fi 85 } 86 87 main