Browse code

hack/validate/vendor: split tidy from vendor

Signed-off-by: Bjorn Neergaard <bneergaard@mirantis.com>

Bjorn Neergaard authored on 2022/11/15 06:56:53
Showing 1 changed files
... ...
@@ -1,53 +1,54 @@
1 1
 #!/usr/bin/env bash
2 2
 
3
+set -e
4
+
3 5
 SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4 6
 source "${SCRIPTDIR}/.validate"
5 7
 
8
+tidy_files=('vendor.mod' 'vendor.sum')
9
+vendor_files=("${tidy_files[@]}" 'vendor/')
10
+
11
+validate_vendor_tidy() {
12
+	# run mod tidy
13
+	./hack/vendor.sh tidy
14
+	# check if any files have changed
15
+	git diff --quiet HEAD -- "${tidy_files[@]}"
16
+}
17
+
6 18
 validate_vendor_diff() {
7
-	IFS=$'\n'
8
-	check_files=('vendor.sum' 'vendor.mod' 'vendor/')
9
-	# shellcheck disable=SC2207
10
-	changed_files=($(validate_diff --diff-filter=ACMR --name-only -- "${check_files[@]}" || true))
11
-	unset IFS
19
+	mapfile -t changed_files < <(validate_diff --diff-filter=ACMR --name-only -- "${vendor_files[@]}")
12 20
 
13 21
 	if [ -n "${TEST_FORCE_VALIDATE:-}" ] || [ "${#changed_files[@]}" -gt 0 ]; then
14 22
 		# recreate vendor/
15
-		./hack/vendor.sh
23
+		./hack/vendor.sh vendor
16 24
 		# check if any files have changed
17
-		diffs="$(git status --porcelain -- "${check_files[@]}" 2> /dev/null)"
18
-		mfiles="$(echo "$diffs" | awk '/^ M / {print $2}')"
19
-		if [ "$diffs" ]; then
20
-			{
21
-				echo 'The result of go mod vendor differs'
22
-				echo
23
-				echo "$diffs"
24
-				echo
25
-				echo 'Please vendor your package with hack/vendor.sh.'
26
-				echo
27
-				if [ -n "$mfiles" ]; then
28
-					git diff -- "$mfiles"
29
-				fi
30
-			} >&2
31
-			false
32
-		else
33
-			echo 'Congratulations! All vendoring changes are done the right way.'
34
-		fi
25
+		git diff --quiet HEAD -- "${vendor_files[@]}"
35 26
 	else
36
-		echo 'No vendor changes in diff.'
27
+		echo >&2 'No vendor changes in diff; skipping vendor check.'
37 28
 	fi
38 29
 }
39 30
 
40
-# 1. make sure all the vendored packages are used
41
-# 2. make sure all the packages contain license information (just warning, because it can cause false-positive)
42
-validate_vendor_used() {
31
+validate_vendor_license() {
43 32
 	for f in $(mawk '$1 = "#" { print $2 }' 'vendor/modules.txt'); do
44 33
 		if [ -d "vendor/$f" ]; then
45 34
 			if ! echo "vendor/$f"/* | grep -qiEc '/(LICENSE|COPYING)'; then
46
-				echo "WARNING: could not find copyright information for $f"
35
+				echo >&2 "WARNING: could not find copyright information for $f"
47 36
 			fi
48 37
 		fi
49 38
 	done
50 39
 }
51 40
 
52
-validate_vendor_diff
53
-validate_vendor_used
41
+if validate_vendor_tidy && validate_vendor_diff && validate_vendor_license; then
42
+	echo >&2 'Vendoring has been performed correctly!'
43
+else
44
+	{
45
+		echo 'Vendoring was not performed correctly; the following files changed during re-vendor:'
46
+		echo
47
+		git diff --name-status HEAD -- "${vendor_files[@]}"
48
+		echo
49
+		echo 'Please revendor with hack/vendor.sh'
50
+		echo
51
+		git diff --diff-filter=M -- "${vendor_files[@]}"
52
+	} >&2
53
+	exit 1
54
+fi