From 9f58db799f4a1b379fa132cfee3b9c1833feceb8 Mon Sep 17 00:00:00 2001 From: Ryan Richard Date: Thu, 31 Oct 2024 11:05:34 -0700 Subject: [PATCH] update pre-commit hooks for ci branch --- .pre-commit-config.yaml | 31 ++++++++++++++++++++-------- hack/check-copyright-year.sh | 29 ++++++++++++++++++++++++++ hack/update-copyright-year.sh | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 9 deletions(-) create mode 100755 hack/check-copyright-year.sh create mode 100755 hack/update-copyright-year.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fd16ba2dc..e57f862e6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,10 +1,23 @@ -# See https://pre-commit.com for more information -# See https://pre-commit.com/hooks.html for more hooks +# This is a configuration for https://pre-commit.com/. +# On macOS, try `brew install pre-commit` and then run `pre-commit install`. repos: -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.2.0 - hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-yaml - - id: check-added-large-files + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 + hooks: + # TODO: find a version of this to validate ytt templates? + # - id: check-yaml + # args: ['--allow-multiple-documents'] + - id: check-json + - id: end-of-file-fixer + - id: trailing-whitespace + - id: check-merge-conflict + - id: check-added-large-files + - id: check-byte-order-marker + - id: detect-private-key + - id: mixed-line-ending + - repo: local + hooks: + - id: validate-copyright-year + name: Validate copyright year + entry: hack/check-copyright-year.sh + language: script diff --git a/hack/check-copyright-year.sh b/hack/check-copyright-year.sh new file mode 100755 index 000000000..d800ae30d --- /dev/null +++ b/hack/check-copyright-year.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Copyright 2021-2024 the Pinniped contributors. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +set -euo pipefail + +files=$(git diff --cached --name-only) +year=$(date +"%Y") + +missing_copyright_files=() + +for f in $files; do + head -10 "$f" | grep -i 'Copyright.*the Pinniped contributors' 2>&1 1>/dev/null || continue + + if ! head -10 "$f" | grep -i -e "Copyright.*$year.*the Pinniped contributors" 2>&1 1>/dev/null; then + missing_copyright_files+=("$f") + fi +done + +if [[ "${#missing_copyright_files[@]}" -gt "0" ]]; then + echo "Copyright notice should include the year the file was created and the year the file was last modified." + echo "$year is missing in the copyright notice of the following files:" + for f in "${missing_copyright_files[@]}"; do + echo " $f" + done + echo "Try using hack/update-copyright-year.sh to update the copyright automatically in staged files." + exit 1 +fi diff --git a/hack/update-copyright-year.sh b/hack/update-copyright-year.sh new file mode 100755 index 000000000..cde6561c2 --- /dev/null +++ b/hack/update-copyright-year.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +# Copyright 2021-2024 the Pinniped contributors. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +set -euo pipefail + +if [[ "$OSTYPE" != "darwin"* ]]; then + echo "This script was only written for MacOS (due to differences with Linux sed flags)" + exit 1 +fi + +files=$(git diff --cached --name-only) +year=$(date +"%Y") + +missing_copyright_files=() + +for f in $files; do + head -10 "$f" | grep -i 'Copyright.*the Pinniped contributors' 2>&1 1>/dev/null || continue + + if ! head -10 "$f" | grep -i -e "Copyright.*$year.*the Pinniped contributors" 2>&1 1>/dev/null; then + missing_copyright_files+=("$f") + fi +done + +if [[ "${#missing_copyright_files[@]}" -gt "0" ]]; then + echo "Fixing copyright notice in the following files:" + for f in "${missing_copyright_files[@]}"; do + echo " $f" + # The rule when updating copyrights is to always keep the starting year, + # and to replace the ending year with the current year. + # This uses MacOS sed flags to replace "XXXX-YYYY" with "XXXX-year" in the copyright notice. + sed -E -e 's/Copyright ([0-9]{4})-([0-9]{4}) the Pinniped contributors/Copyright \1-'"$year"' the Pinniped contributors/' -i '' "$f" + # This uses MacOS sed flags to replace "XXXX" with "XXXX-year" in the copyright notice. + sed -E -e 's/Copyright ([0-9]{4}) the Pinniped contributors/Copyright \1-'"$year"' the Pinniped contributors/' -i '' "$f" + done + echo "Done!" + exit 1 +fi