diff --git a/go.mod b/go.mod index 5d229e61d..65e1a28f6 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( k8s.io/client-go v0.32.2 k8s.io/code-generator v0.32.2 k8s.io/kube-openapi v0.0.0-20241212222426-2c72e554b1e7 - knative.dev/hack v0.0.0-20250314171439-742e1e50da78 + knative.dev/hack v0.0.0-20250318155814-8f599b7a828c knative.dev/pkg v0.0.0-20250312035536-b7bbf4be5dbd ) diff --git a/go.sum b/go.sum index 63d64e10e..27ee00711 100644 --- a/go.sum +++ b/go.sum @@ -685,8 +685,8 @@ k8s.io/kube-openapi v0.0.0-20241212222426-2c72e554b1e7 h1:hcha5B1kVACrLujCKLbr8X k8s.io/kube-openapi v0.0.0-20241212222426-2c72e554b1e7/go.mod h1:GewRfANuJ70iYzvn+i4lezLDAFzvjxZYK1gn1lWcfas= k8s.io/utils v0.0.0-20241210054802-24370beab758 h1:sdbE21q2nlQtFh65saZY+rRM6x6aJJI8IUa1AmH/qa0= k8s.io/utils v0.0.0-20241210054802-24370beab758/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= -knative.dev/hack v0.0.0-20250314171439-742e1e50da78 h1:K3cI9khmEm63uwnm2S6ZFq1r/nsJFDXtUHqPnyKdnCM= -knative.dev/hack v0.0.0-20250314171439-742e1e50da78/go.mod h1:R0ritgYtjLDO9527h5vb5X6gfvt5LCrJ55BNbVDsWiY= +knative.dev/hack v0.0.0-20250318155814-8f599b7a828c h1:WbSdxtzL02rpfF6KNbAFnWLMCE9TFfrfD39VqgsCuOA= +knative.dev/hack v0.0.0-20250318155814-8f599b7a828c/go.mod h1:R0ritgYtjLDO9527h5vb5X6gfvt5LCrJ55BNbVDsWiY= knative.dev/pkg v0.0.0-20250312035536-b7bbf4be5dbd h1:KXG6bACwjKSZcT0JxyQDVYLcDPSip+7l6sVULeITi7k= knative.dev/pkg v0.0.0-20250312035536-b7bbf4be5dbd/go.mod h1:OuszA8pcsXmO+Pp4QCtD10ph6tjRPFN+LrF/XgAMDb8= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= diff --git a/hack/boilerplate/add-boilerplate.sh b/hack/add-boilerplate.sh similarity index 53% rename from hack/boilerplate/add-boilerplate.sh rename to hack/add-boilerplate.sh index 3c926a2c0..3bfce673c 100755 --- a/hack/boilerplate/add-boilerplate.sh +++ b/hack/add-boilerplate.sh @@ -14,6 +14,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +set -o errexit +set -o nounset +set -o pipefail + +# shellcheck disable=SC1090 +source "$(GOFLAGS='-mod=mod' go run knative.dev/hack/cmd/script codegen-library.sh)" + USAGE=$(cat <.txt to all . files missing it in a directory. @@ -22,17 +29,34 @@ Usage: (from repository root) Example: (from repository root) ./hack/boilerplate/add-boilerplate.sh go cmd + +As of now, only .go files are supported. EOF ) -set -e - -if [[ -z $1 || -z $2 ]]; then - echo "${USAGE}" +if [ -z "${1:-}" ] || [ -z "${2:-}" ]; then + error Invalid arguments + echo "${USAGE}" 1>&2 exit 1 fi -grep -r -L -P "Copyright \d+ The Knative Authors" $2 \ - | grep -P "\.$1\$" \ - | xargs -I {} sh -c \ - "cat hack/boilerplate/boilerplate.$1.txt {} > /tmp/boilerplate && mv /tmp/boilerplate {}" +if ! [[ "$1" = "go" ]]; then + error Unsupported file extension + echo "${USAGE}" 1>&2 + exit 2 +fi + +cnt=0 +while read -r file; do + if grep -q -E "^Copyright [[:digit:]]+ The Knative Authors$" "$file"; then + continue + fi + cat "$(boilerplate)" > "$file".bck + echo '' >> "$file".bck + cat "$file" >> "$file".bck + mv "$file".bck "$file" + log License added to "$file" + cnt=$(( cnt + 1)) +done < <(find "$2" -type f -name "*.$1") + +log License added to $cnt files diff --git a/hack/boilerplate/boilerplate.go.txt b/hack/boilerplate/boilerplate.go.txt deleted file mode 100644 index 6f818683b..000000000 --- a/hack/boilerplate/boilerplate.go.txt +++ /dev/null @@ -1,15 +0,0 @@ -/* -Copyright 2020 The Knative Authors - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ diff --git a/hack/boilerplate/boilerplate.sh.txt b/hack/boilerplate/boilerplate.sh.txt deleted file mode 100755 index f2b4c078a..000000000 --- a/hack/boilerplate/boilerplate.sh.txt +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bash - -# Copyright 2020 The Knative Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. diff --git a/hack/release.sh b/hack/release.sh index 2ac660618..dd95634e6 100755 --- a/hack/release.sh +++ b/hack/release.sh @@ -15,7 +15,8 @@ # limitations under the License. -source $(dirname $0)/../vendor/knative.dev/hack/release.sh +# shellcheck disable=SC1090 +source "$(GOFLAGS='-mod=mod' go run knative.dev/hack/cmd/script release.sh)" declare -A COMPONENTS COMPONENTS=( @@ -43,4 +44,4 @@ function build_release() { ARTIFACTS_TO_PUBLISH="${all_yamls[@]}" } -main $@ +main "$@" diff --git a/hack/update-codegen.sh b/hack/update-codegen.sh index aa5d3f6e5..e0e4bd3ac 100755 --- a/hack/update-codegen.sh +++ b/hack/update-codegen.sh @@ -18,8 +18,8 @@ set -o errexit set -o nounset set -o pipefail -source $(dirname $0)/../vendor/knative.dev/hack/codegen-library.sh -export PATH="$GOBIN:$PATH" +# shellcheck disable=SC1090 +source "$(GOFLAGS='-mod=mod' go run knative.dev/hack/cmd/script codegen-library.sh)" echo "=== Update Codegen for ${MODULE_NAME}" @@ -28,23 +28,23 @@ group "Kubernetes Codegen" source "${CODEGEN_PKG}/kube_codegen.sh" kube::codegen::gen_client \ - --boilerplate "${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt" \ + --boilerplate "$(boilerplate)" \ --output-dir "${REPO_ROOT_DIR}/pkg/client" \ --output-pkg "knative.dev/sample-controller/pkg/client" \ --with-watch \ "${REPO_ROOT_DIR}/pkg/apis" kube::codegen::gen_helpers \ - --boilerplate "${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt" \ + --boilerplate "$(boilerplate)" \ "${REPO_ROOT_DIR}/pkg" group "Knative Codegen" # Knative Injection -${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh "injection" \ +"${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh" "injection" \ knative.dev/sample-controller/pkg/client knative.dev/sample-controller/pkg/apis \ "samples:v1alpha1" \ - --go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt + --go-header-file "$(boilerplate)" group "Update CRD Schema" @@ -55,4 +55,4 @@ go run sigs.k8s.io/controller-tools/cmd/controller-gen@v0.17.1 \ group "Update deps post-codegen" # Make sure our dependencies are up-to-date -${REPO_ROOT_DIR}/hack/update-deps.sh +"${REPO_ROOT_DIR}/hack/update-deps.sh" diff --git a/hack/update-deps.sh b/hack/update-deps.sh index 5c9988b5b..259221032 100755 --- a/hack/update-deps.sh +++ b/hack/update-deps.sh @@ -18,6 +18,7 @@ set -o errexit set -o nounset set -o pipefail -source $(dirname "$0")/../vendor/knative.dev/hack/library.sh +# shellcheck disable=SC1090 +source "$(GOFLAGS='-mod=mod' go run knative.dev/hack/cmd/script library.sh)" go_update_deps "$@" diff --git a/hack/verify-codegen.sh b/hack/verify-codegen.sh index e4553d9a9..fe80e093b 100755 --- a/hack/verify-codegen.sh +++ b/hack/verify-codegen.sh @@ -18,38 +18,18 @@ set -o errexit set -o nounset set -o pipefail -readonly REPO_ROOT_DIR="$(git rev-parse --show-toplevel)" -readonly TMP_DIFFROOT="$(mktemp -d -p ${REPO_ROOT_DIR})" - -cleanup() { - rm -rf "${TMP_DIFFROOT}" -} - -trap "cleanup" EXIT SIGINT - -cleanup - -# Save working tree state -mkdir -p "${TMP_DIFFROOT}/pkg" -cp -aR "${REPO_ROOT_DIR}/go.sum" "${REPO_ROOT_DIR}/pkg" "${REPO_ROOT_DIR}/vendor" "${TMP_DIFFROOT}" - -# TODO(mattmoor): We should be able to rm -rf pkg/client/ and vendor/ - -"${REPO_ROOT_DIR}/hack/update-codegen.sh" -echo "Diffing ${REPO_ROOT_DIR} against freshly generated codegen" -ret=0 -diff -Naupr "${REPO_ROOT_DIR}/pkg" "${TMP_DIFFROOT}/pkg" || ret=1 -diff -Naupr --no-dereference "${REPO_ROOT_DIR}/vendor" "${TMP_DIFFROOT}/vendor" || ret=1 - -# Restore working tree state -rm -fr "${TMP_DIFFROOT}/config" -rm -fr "${REPO_ROOT_DIR}/go.sum" "${REPO_ROOT_DIR}/pkg" "${REPO_ROOT_DIR}/vendor" -cp -aR "${TMP_DIFFROOT}"/* "${REPO_ROOT_DIR}" - -if [[ $ret -eq 0 ]] -then - echo "${REPO_ROOT_DIR} up to date." -else - echo "ERROR: ${REPO_ROOT_DIR} is out of date. Please run ./hack/update-codegen.sh" - exit 1 +# shellcheck disable=SC1090 +source "$(GOFLAGS='-mod=mod' go run knative.dev/hack/cmd/script library.sh)" + +"${REPO_ROOT_DIR}"/hack/update-codegen.sh + +if ! git diff --exit-code --name-only > /dev/null; then + error 'Modified files found:' + git diff --name-only + error 'Difference:' + git --no-pager diff + abort "${MODULE_NAME} is out of date!" "" \ + "Please, run ./hack/update-codegen.sh and commit." fi + +header "${MODULE_NAME} is up to date." diff --git a/test/presubmit-tests.sh b/test/presubmit-tests.sh index 9ad3d3486..542333940 100755 --- a/test/presubmit-tests.sh +++ b/test/presubmit-tests.sh @@ -24,9 +24,9 @@ # Markdown linting failures don't show up properly in Gubernator resulting # in a net-negative contributor experience. export DISABLE_MD_LINTING=1 -export GO111MODULE=on -source $(dirname $0)/../vendor/knative.dev/hack/presubmit-tests.sh +# shellcheck disable=SC1090 +source "$(GOFLAGS='-mod=mod' go run knative.dev/hack/cmd/script presubmit-tests.sh)" # TODO(mattmoor): integration tests diff --git a/vendor/knative.dev/hack/boilerplate.go.txt b/vendor/knative.dev/hack/boilerplate.go.txt index 8fb5f9cdf..01300b6ac 100644 --- a/vendor/knative.dev/hack/boilerplate.go.txt +++ b/vendor/knative.dev/hack/boilerplate.go.txt @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, diff --git a/vendor/knative.dev/hack/codegen-library.sh b/vendor/knative.dev/hack/codegen-library.sh index 42ece681c..46528a03f 100644 --- a/vendor/knative.dev/hack/codegen-library.sh +++ b/vendor/knative.dev/hack/codegen-library.sh @@ -138,8 +138,8 @@ function restore-changes-if-its-copyright-year-only() { git diff --name-only > "$difflist" while read -r file; do # check if the file contains just the change in the boilerplate year - if [ "$(LANG=C git diff --exit-code --shortstat -- "$file")" = ' 1 file changed, 1 insertion(+), 1 deletion(-)' ] && \ - [[ "$(git diff --exit-code -U1 -- "$file" | grep -Ec '^[+-]\s*[*#]?\s*Copyright 2[0-9]{3}')" -eq 2 ]]; then + if [ "$(LANG=C git diff --ignore-space-change --exit-code --shortstat -- "$file")" = ' 1 file changed, 1 insertion(+), 1 deletion(-)' ] && \ + [[ "$(git diff --ignore-space-change --exit-code -U1 -- "$file" | grep -Ec '^[+-]\s*[*#]?\s*Copyright 2[0-9]{3}')" -eq 2 ]]; then # restore changes to that file git checkout -- "$file" fi diff --git a/vendor/modules.txt b/vendor/modules.txt index 748db8396..a970f9d41 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -873,7 +873,7 @@ k8s.io/utils/net k8s.io/utils/pointer k8s.io/utils/ptr k8s.io/utils/trace -# knative.dev/hack v0.0.0-20250314171439-742e1e50da78 +# knative.dev/hack v0.0.0-20250318155814-8f599b7a828c ## explicit; go 1.21 knative.dev/hack # knative.dev/pkg v0.0.0-20250312035536-b7bbf4be5dbd