|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright The containerd Authors. |
| 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 | +# |
| 18 | +# verifies if the require and replace directives for two go.mod files are in sync |
| 19 | +# |
| 20 | +set -eu -o pipefail |
| 21 | + |
| 22 | +ROOT=$(dirname "${BASH_SOURCE}")/.. |
| 23 | + |
| 24 | +if [ "$#" -ne 1 ]; then |
| 25 | + echo "Usage: $0 dir-for-second-go-mod" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +if ! command -v jq &> /dev/null ; then |
| 30 | + echo Please install jq |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +# Load the requires and replaces section in the root go.mod file |
| 35 | +declare -A map_requires_1 |
| 36 | +declare -A map_replaces_1 |
| 37 | +pushd "${ROOT}" > /dev/null |
| 38 | +while IFS='#' read -r key value |
| 39 | +do |
| 40 | + map_requires_1[$key]="$value" |
| 41 | +done<<<$(go mod edit -json | jq -r '.Require[] | .Path + " # " + .Version') |
| 42 | +while IFS='#' read -r key value |
| 43 | +do |
| 44 | + map_replaces_1[$key]="$value" |
| 45 | +done<<<$(go mod edit -json | jq -r '.Replace[] | .Old.Path + " # " + .New.Path + " : " + .New.Version') |
| 46 | +popd > /dev/null |
| 47 | + |
| 48 | +# Load the requires and replaces section in the other go.mod file |
| 49 | +declare -A map_requires_2 |
| 50 | +declare -A map_replaces_2 |
| 51 | +pushd "${ROOT}/$1" > /dev/null |
| 52 | +while IFS='#' read -r key value |
| 53 | +do |
| 54 | + map_requires_2[$key]="$value" |
| 55 | +done<<<$(go mod edit -json | jq -r '.Require[] | .Path + " # " + .Version') |
| 56 | +while IFS='#' read -r key value |
| 57 | +do |
| 58 | + map_replaces_2[$key]="$value" |
| 59 | +done<<<$(go mod edit -json | jq -r '.Replace[] | .Old.Path + " # " + .New.Path + " : " + .New.Version') |
| 60 | +popd > /dev/null |
| 61 | + |
| 62 | +# signal for errors later |
| 63 | +ERRORS=0 |
| 64 | + |
| 65 | +# iterate through the second go.mod's require section and ensure that all items |
| 66 | +# have the same values in the root go.mod replace section |
| 67 | +for k in "${!map_requires_2[@]}" |
| 68 | +do |
| 69 | + if [ -v "map_requires_1[$k]" ]; then |
| 70 | + if [ "${map_requires_2[$k]}" != "${map_requires_1[$k]}" ]; then |
| 71 | + echo "${k} has different values in the go.mod files require section:" \ |
| 72 | + "${map_requires_1[$k]} in root go.mod ${map_requires_2[$k]} in $1/go.mod" |
| 73 | + ERRORS=$(( ERRORS + 1 )) |
| 74 | + fi |
| 75 | + fi |
| 76 | +done |
| 77 | + |
| 78 | +# iterate through the second go.mod's replace section and ensure that all items |
| 79 | +# have the same values in the root go.mod's replace section. Except for the |
| 80 | +# containerd/containerd which we know will be different |
| 81 | +for k in "${!map_replaces_2[@]}" |
| 82 | +do |
| 83 | + if [[ "${k}" == "github.com/containerd/containerd"* ]]; then |
| 84 | + continue |
| 85 | + fi |
| 86 | + if [ -v "map_replaces_1[$k]" ]; then |
| 87 | + if [ "${map_replaces_2[$k]}" != "${map_replaces_1[$k]}" ]; then |
| 88 | + echo "${k} has different values in the go.mod files replace section:" \ |
| 89 | + "${map_replaces_1[$k]} in root go.mod ${map_replaces_2[$k]} in $1/go.mod" |
| 90 | + ERRORS=$(( ERRORS + 1 )) |
| 91 | + fi |
| 92 | + fi |
| 93 | +done |
| 94 | + |
| 95 | +# iterate through the root go.mod's replace section and ensure that all the |
| 96 | +# same items are present in the second go.mod's replace section and nothing is missing |
| 97 | +for k in "${!map_replaces_1[@]}" |
| 98 | +do |
| 99 | + if [[ "${k}" == "github.com/containerd/containerd"* ]]; then |
| 100 | + continue |
| 101 | + fi |
| 102 | + if [ ! -v "map_replaces_2[$k]" ]; then |
| 103 | + echo "${k} has an entry in root go.mod replace section, but is missing from" \ |
| 104 | + " replace section in $1/go.mod" |
| 105 | + ERRORS=$(( ERRORS + 1 )) |
| 106 | + fi |
| 107 | +done |
| 108 | + |
| 109 | +if [ "$ERRORS" -ne 0 ]; then |
| 110 | + echo "Found $ERRORS error(s)." |
| 111 | + exit 1 |
| 112 | +fi |
0 commit comments