aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenedetta Delfino <benedetta.delfino@arm.com>2024-03-26 16:26:19 +0000
committerBenedetta Delfino <benedetta.delfino@arm.com>2024-04-23 14:15:17 +0100
commit4470381a084d2393b36776fcdba684dfd16c2cb6 (patch)
treee7389ab82b5e7e435cf636b23880fe3d6684087a
parent32405c279d2f98c2d40bdbbb7f7306ff12c86cd6 (diff)
downloadmlia-4470381a084d2393b36776fcdba684dfd16c2cb6.tar.gz
feat: Add custom pre-commit hook to check for updated Copyright header
Signed-off-by: Benedetta Delfino <benedetta.delfino@arm.com> Change-Id: Ic6a07ef12868673466a5db0cbce08015b9df4ffc
-rw-r--r--.pre-commit-config.yaml8
-rw-r--r--pre_commit_hooks/check_copyright_header.py34
2 files changed, 42 insertions, 0 deletions
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index b601b03..3788326 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -124,3 +124,11 @@ repos:
hooks:
- id: commitizen-branch
args: [--rev-range, HEAD~1..HEAD]
+
+- repo: local
+ hooks:
+ - id: check-copyright-header
+ name: Check Copyright header years
+ entry: python pre_commit_hooks/check_copyright_header.py
+ language: python
+ verbose: true
diff --git a/pre_commit_hooks/check_copyright_header.py b/pre_commit_hooks/check_copyright_header.py
new file mode 100644
index 0000000..ded7675
--- /dev/null
+++ b/pre_commit_hooks/check_copyright_header.py
@@ -0,0 +1,34 @@
+# SPDX-FileCopyrightText: Copyright 2024, Arm Limited and/or its affiliates.
+# SPDX-License-Identifier: Apache-2.0
+"""Pre-commit hook that checks the current year is in the Copyright header of a file.
+
+If the header is out of date it will print a warning.
+"""
+import datetime
+import subprocess # nosec
+
+
+class CopyrightHeaderChecker:
+ """Class that wraps the checker for the Copyright header."""
+
+ def check_files_have_updated_header(self, filenames: list) -> None:
+ """Check whether input files have the current year in the copyright string."""
+ current_year = str(datetime.datetime.now().year)
+ for filename in filenames:
+ with open(filename, encoding="utf-8") as file:
+ first_line = file.readline()
+ second_line = file.readline()
+ if filename.endswith(".md") and current_year not in second_line:
+ print(f"WARNING: The Copyright header of {filename} is out of date!")
+
+ if not filename.endswith(".md") and current_year not in first_line:
+ print(f"WARNING: The Copyright header of {filename} is out of date!")
+
+
+if __name__ == "__main__":
+ staged_files = (
+ subprocess.check_output(["git", "diff", "--cached", "--name-only"]) # nosec
+ .decode()
+ .splitlines()
+ )
+ CopyrightHeaderChecker().check_files_have_updated_header(filenames=staged_files)