From f1e177fee7f747115c4f47c05c4d421133934b95 Mon Sep 17 00:00:00 2001 From: Margo Crawford Date: Tue, 5 Jan 2021 14:02:28 -0800 Subject: [PATCH] Copyright year precommit hook Signed-off-by: Andrew Keesler --- .pre-commit-config.yaml | 4 ++++ .pre-commit-hooks.yaml | 4 ++++ hack/check-copyright-year.sh | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 .pre-commit-hooks.yaml create mode 100644 hack/check-copyright-year.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 50581ddd..869045d4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,3 +15,7 @@ repos: - id: detect-private-key exclude: testdata - id: mixed-line-ending +- repo: . + rev: main + hooks: + - id: validate-copyright-year diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 00000000..b8af8e46 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,4 @@ +- 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 100644 index 00000000..5d282486 --- /dev/null +++ b/hack/check-copyright-year.sh @@ -0,0 +1,21 @@ +# +# Check if copyright statements include the current year +# +files=git diff --cached --name-only +year=date +"%Y" + +for f in $files; do + head -10 $f | grep -i copyright 2>&1 1>/dev/null || continue + + if ! grep -i -e "copyright.*$year" $f 2>&1 1>/dev/null; then + missing_copyright_files="$missing_copyright_files $f" + fi +done + +if [ -n "$missing_copyright_files" ]; then + echo "$year is missing in the copyright notice of the following files:" + for f in $missing_copyright_files; do + echo " $f" + done + exit 1 +fi