github.com/opencontainers/umoci@v0.4.8-0.20240508124516-656e4836fb0d/hack/test-vendor.sh (about) 1 #!/bin/bash 2 # umoci: Umoci Modifies Open Containers' Images 3 # Copyright (C) 2016-2020 SUSE LLC 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 set -Eeuxo pipefail 18 source "$(dirname "$BASH_SOURCE")/readlinkf.sh" 19 20 # Generate a hash-of-hashes for the entire vendor/ tree. 21 function gethash() { 22 ( 23 cd "$1" 24 find . -type f -not -path "./modules.txt" | \ 25 xargs sha256sum | sort -k2 | sha256sum | awk '{ print $1 }' 26 ) 27 } 28 29 # Figure out root directory. 30 ROOT="$(readlinkf_posix "$(dirname "$BASH_SOURCE")/..")" 31 STASHED_ROOT="$(mktemp -dt umoci-vendor.XXXXXX)" 32 33 # Stash away old vendor tree, and restore it on-exit. 34 mv "$ROOT/vendor" "$STASHED_ROOT/vendor" 35 trap 'rm -rf "$ROOT/vendor" ; mv "$STASHED_ROOT/vendor" "$ROOT/vendor" ; rm -rf "$STASHED_ROOT"' ERR EXIT 36 37 # Try to re-generate vendor/. 38 go clean -modcache 39 go mod verify 40 go mod vendor 41 42 # Make sure that none of the packages we have listed are unused. 43 if (go mod tidy -v 2>&1 | grep '^unused') 44 then 45 echo "Unused modules found in go.mod." 46 exit 1 47 fi 48 49 # See whether something has changed. 50 oldhash="$(gethash "$STASHED_ROOT/vendor")" 51 newhash="$(gethash "$ROOT/vendor")" 52 53 # Verify the hashes match. 54 diff -qr "$STASHED_ROOT/vendor" "$ROOT/vendor" || : 55 [[ "$oldhash" == "$newhash" ]] || ( echo "ERROR: vendor/ does not match go.mod -- run 'go mod vendor'" ; exit 1 )