[COPYRIGHT] add copyright pre-commit#481
Conversation
There was a problem hiding this comment.
Code Review
This pull request integrates automated copyright header management by adding local pre-commit hooks, updating the tools/copyright.py script with a --check option, and applying copyright headers to various files. The review feedback suggests critical improvements: ensuring the --check flag verifies the exact expected header content rather than just matching a generic pattern, expanding the pre-commit hooks to cover all supported file types (not just Python), and optimizing the file extension checks in the copyright script using Python's native endswith tuple support.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if args.check: | ||
| return bool(pattern.match(code)) |
There was a problem hiding this comment.
The check-copyright hook is intended to verify that the copyright header is correct and up-to-date (e.g., checking the correct year and authors). However, using pattern.match(code) only checks if any copyright header matching the general structure exists. It does not verify if the header content (such as the year or author list) matches the expected header. For example, an outdated year like 2025 would still pass the check.
To fix this, we should check if the file starts with the expected header generated by build_header_fn(file).
| if args.check: | |
| return bool(pattern.match(code)) | |
| if args.check: | |
| header = build_header_fn(file) | |
| return code.startswith(header) |
| name: add copyright header | ||
| language: python | ||
| entry: python tools/copyright.py --repo . --header "Copyright (c) 2026, Mayank Mishra" --no-contributors | ||
| types: [python] |
There was a problem hiding this comment.
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_or with all supported types.
types_or: [python, c, c++, cuda, yaml, html, markdown]| name: check copyright year | ||
| language: python | ||
| entry: python tools/copyright.py --repo . --header "Copyright (c) 2026, Mayank Mishra" --no-contributors --check | ||
| types: [python] |
There was a problem hiding this comment.
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_or with all supported types.
types_or: [python, c, c++, cuda, yaml, html, markdown]| 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) |
There was a problem hiding this comment.
Using any([file.endswith(i) for i in ...]) creates an unnecessary list comprehension and iterates manually. In Python, str.endswith() natively accepts a tuple of strings and performs this check efficiently. We can convert the extension lists to tuples and pass them directly to endswith.
| 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) | |
| if file.endswith(tuple(_CPP_LIKE_EXTENSIONS)): | |
| ok = _check_and_add_copyright_header(file, _build_cpp_header, _CPP_PATTERN) | |
| elif file.endswith(tuple(_PYTHON_LIKE_EXTENSIONS)): | |
| ok = _check_and_add_copyright_header(file, _build_python_header, _PYTHON_PATTERN) | |
| elif file.endswith(tuple(_HTML_LIKE_EXTENSIONS)): | |
| ok = _check_and_add_copyright_header(file, _build_html_header, _HTML_PATTERN) |
No description provided.