-
Notifications
You must be signed in to change notification settings - Fork 30
[COPYRIGHT] add copyright pre-commit #481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # ************************************************** | ||
| # Copyright (c) 2026, Mayank Mishra | ||
| # ************************************************** | ||
|
|
||
| one_date_re: '\bCopyright \(c\) (?P<year>[0-9]{4}), Mayank Mishra\b' | ||
| two_date_re: '\bCopyright \(c\) (?P<from>[0-9]{4})-(?P<to>[0-9]{4}), Mayank Mishra\b' | ||
| one_date_format: 'Copyright (c) {year}, Mayank Mishra' | ||
| two_date_format: 'Copyright (c) {from}-{to}, Mayank Mishra' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,20 @@ | |
| # ************************************************** | ||
|
|
||
| repos: | ||
| - repo: local | ||
| hooks: | ||
| - id: add-copyright | ||
| name: add copyright header | ||
| language: python | ||
| entry: python tools/copyright.py --repo . --header "Copyright (c) 2026, Mayank Mishra" --no-contributors | ||
| types: [python] | ||
| pass_filenames: false | ||
| - id: check-copyright | ||
| name: check copyright year | ||
| language: python | ||
| entry: python tools/copyright.py --repo . --header "Copyright (c) 2026, Mayank Mishra" --no-contributors --check | ||
| types: [python] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since To ensure the hook runs when any supported file type is modified, use types_or: [python, c, c++, cuda, yaml, html, markdown] |
||
| pass_filenames: false | ||
| - repo: https://github.com/PyCQA/autoflake | ||
| rev: v2.3.1 | ||
| hooks: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |||||||||||||||||||||||||||||||
| parser.add_argument("--header", type=str, required=True) | ||||||||||||||||||||||||||||||||
| parser.add_argument("--extra-name", type=str, required=False) | ||||||||||||||||||||||||||||||||
| parser.add_argument("--no-contributors", action="store_true", required=False) | ||||||||||||||||||||||||||||||||
| parser.add_argument("--check", action="store_true", required=False) | ||||||||||||||||||||||||||||||||
| args = parser.parse_args() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -121,11 +122,14 @@ def _build_html_header(file: str) -> str: | |||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def _check_and_add_copyright_header(file: str, build_header_fn, pattern: re.Pattern) -> None: | ||||||||||||||||||||||||||||||||
| def _check_and_add_copyright_header(file: str, build_header_fn, pattern: re.Pattern) -> bool: | ||||||||||||||||||||||||||||||||
| code = open(file, "r").read() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if len(code) == 0: | ||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if args.check: | ||||||||||||||||||||||||||||||||
| return bool(pattern.match(code)) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+131
to
+132
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The To fix this, we should check if the file starts with the expected header generated by
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| header = build_header_fn(file) | ||||||||||||||||||||||||||||||||
| code_stripped = pattern.sub("", code) | ||||||||||||||||||||||||||||||||
|
|
@@ -135,6 +139,7 @@ def _check_and_add_copyright_header(file: str, build_header_fn, pattern: re.Patt | |||||||||||||||||||||||||||||||
| code = f"{header}{code}" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| open(file, "w").writelines([code]) | ||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def _is_banned(path: str) -> bool: | ||||||||||||||||||||||||||||||||
|
|
@@ -150,6 +155,7 @@ def _is_banned(path: str) -> bool: | |||||||||||||||||||||||||||||||
| directory = os.path.realpath(args.repo) | ||||||||||||||||||||||||||||||||
| _AUTHOR_MAP = {} if args.no_contributors else _build_author_map(directory) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| missing = [] | ||||||||||||||||||||||||||||||||
| for root, dirs, files in os.walk(directory): | ||||||||||||||||||||||||||||||||
| if _is_banned(root): | ||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||
|
|
@@ -160,9 +166,18 @@ def _is_banned(path: str) -> bool: | |||||||||||||||||||||||||||||||
| if _is_banned(file): | ||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| ok = True | ||||||||||||||||||||||||||||||||
| if any([file.endswith(i) for i in _CPP_LIKE_EXTENSIONS]): | ||||||||||||||||||||||||||||||||
| _check_and_add_copyright_header(file, _build_cpp_header, _CPP_PATTERN) | ||||||||||||||||||||||||||||||||
| ok = _check_and_add_copyright_header(file, _build_cpp_header, _CPP_PATTERN) | ||||||||||||||||||||||||||||||||
| elif any([file.endswith(i) for i in _PYTHON_LIKE_EXTENSIONS]): | ||||||||||||||||||||||||||||||||
| _check_and_add_copyright_header(file, _build_python_header, _PYTHON_PATTERN) | ||||||||||||||||||||||||||||||||
| ok = _check_and_add_copyright_header(file, _build_python_header, _PYTHON_PATTERN) | ||||||||||||||||||||||||||||||||
| elif any([file.endswith(i) for i in _HTML_LIKE_EXTENSIONS]): | ||||||||||||||||||||||||||||||||
| _check_and_add_copyright_header(file, _build_html_header, _HTML_PATTERN) | ||||||||||||||||||||||||||||||||
| ok = _check_and_add_copyright_header(file, _build_html_header, _HTML_PATTERN) | ||||||||||||||||||||||||||||||||
|
Comment on lines
170
to
+175
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if not ok: | ||||||||||||||||||||||||||||||||
| missing.append(os.path.relpath(file, directory)) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if missing: | ||||||||||||||||||||||||||||||||
| for f in sorted(missing): | ||||||||||||||||||||||||||||||||
| print(f"No copyright found on '{f}'.") | ||||||||||||||||||||||||||||||||
| raise SystemExit(1) | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
types: [python]is specified, this pre-commit hook will only be triggered if a Python file is staged in the commit. If a developer only modifies C++ (.cpp,.h), YAML, or Markdown files, the hook will be skipped entirely, allowing those files to be committed without copyright headers.To ensure the hook runs when any supported file type is modified, use
types_orwith all supported types.