-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathverify-diff.sh
executable file
·71 lines (61 loc) · 2.18 KB
/
verify-diff.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env bash
# Define the files to exclude
readonly EXCLUDE_FILES=(
'olm-catalog/serverless-operator/manifests/serverless-operator.clusterserviceversion.yaml'
'olm-catalog/serverless-operator-index/Dockerfile'
'test/images-rekt.yaml'
'.konflux-release/*'
)
# Define the patterns to exclude
readonly EXCLUDE_PATTERNS=(
'*sha256:*'
'*revision: *'
'url: \"https://github.com/openshift-knative/.*(\.git)?\"' # some repos in the override-snapshot have the .git suffix, some not ¯\_(ツ)_/¯
'name: serverless-operator-.*-override-snapshot-.*'
)
# Function to check if a file should be excluded
function should_exclude() {
local file="$1"
diff="$(git --no-pager -c color.ui=never diff --unified=0 "$file" | grep '^[+-][\ a-z]')"
while IFS= read -r line; do
line_matched_pattern=false
for pattern in "${EXCLUDE_PATTERNS[@]}"; do
# shellcheck disable=SC2053
if [[ $line == $pattern || $line =~ $pattern ]]; then
echo "Excluding line $line since it matches pattern $pattern"
line_matched_pattern=true
break
fi
done
if [[ "$line_matched_pattern" == "false" ]]; then
echo "line '$line' doesn't match any of the patterns. Failing the exclude check"
return 1
fi
done <<< "$diff"
return 0
}
# shellcheck disable=SC2016
function debug_log_fail() {
echo '::debug::Running `git status`'
git -c color.status=always status
echo '::debug::Running `git diff`'
git --no-pager -c color.ui=always diff
echo '::error::Not all generated files are committed. Run `make generated-files` and commit files.'
echo '::warning::`make generated-files` needs to be run on GOPATH due to https://github.com/knative/pkg/issues/1287'
}
# shellcheck disable=SC2143
if [ -n "$(git status --porcelain | grep -v -E "$(IFS=\|; echo "${EXCLUDE_FILES[*]}")")" ]; then
debug_log_fail
exit 33
fi
# shellcheck disable=SC2143
if [ -n "$(git status --porcelain | grep -E "$(IFS=\|; echo "${EXCLUDE_FILES[*]}")")" ]; then
echo 'Excluded files are different'
git diff --name-only | while read -r file; do
if ! should_exclude "$file"; then
git --no-pager -c color.ui=always diff "$file"
debug_log_fail
exit 33
fi
done
fi