-
Notifications
You must be signed in to change notification settings - Fork 123
Add git hook for updating the copyright as part of pre-commit #423
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
base: master
Are you sure you want to change the base?
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,33 @@ | ||
| # Git Hooks | ||
|
|
||
| This directory contains git hooks for the repository. | ||
|
|
||
| ## Setup | ||
|
|
||
| To enable the git hooks, run: | ||
|
|
||
| ```bash | ||
| python3 .githooks/setup.py | ||
| ``` | ||
|
|
||
| Or on Windows: | ||
| ```cmd | ||
| python .githooks\setup.py | ||
| ``` | ||
|
|
||
| This configures git to use hooks from this directory instead of the default `.git/hooks`. | ||
|
|
||
| **Note:** The hooks are written in Python for cross-platform compatibility (Linux, macOS, Windows). | ||
|
|
||
| ## Available Hooks | ||
|
|
||
| ### pre-commit | ||
|
|
||
| Automatically updates copyright years in files being committed. The hook: | ||
| - Runs `scripts/update_copyright.py` on modified files | ||
| - Updates copyright year ranges (e.g., 2019-2024 → 2019-2025) | ||
| - Adds single years where missing (e.g., 2019 → 2019-2025) | ||
| - Automatically stages the copyright changes | ||
| - Works across Linux, macOS, and Windows | ||
|
|
||
| The hook only modifies files already staged for commit and will not cause the commit to fail. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Pre-commit hook to update copyright years in modified files. | ||
| This script is cross-platform compatible (Linux, macOS, Windows). | ||
| """ | ||
|
|
||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| def main(): | ||
| """Run the copyright update script and stage changes.""" | ||
| try: | ||
| # Get the root directory of the repo | ||
| result = subprocess.run( | ||
| ['git', 'rev-parse', '--show-toplevel'], | ||
| capture_output=True, | ||
| text=True, | ||
| check=True | ||
| ) | ||
| repo_root = Path(result.stdout.strip()) | ||
|
|
||
| # Run the update copyright script | ||
| copyright_script = repo_root / 'scripts' / 'update_copyright.py' | ||
|
|
||
| if not copyright_script.exists(): | ||
| # Fallback to bash script if Python version doesn't exist | ||
| copyright_script = repo_root / 'scripts' / 'update_copyright.sh' | ||
| if copyright_script.exists(): | ||
| subprocess.run(['bash', str(copyright_script)], check=True) | ||
| else: | ||
| print("Warning: Copyright update script not found", file=sys.stderr) | ||
| return 0 | ||
| else: | ||
| # Run in interactive mode (will show preview and ask for confirmation) | ||
| result = subprocess.run([sys.executable, str(copyright_script)]) | ||
|
|
||
| # Check if there were any changes applied | ||
| result = subprocess.run( | ||
| ['git', 'diff', '--quiet'], | ||
| capture_output=True | ||
| ) | ||
|
|
||
| if result.returncode != 0: | ||
| # There are changes, stage them | ||
| print("\nStaging copyright updates...") | ||
| subprocess.run(['git', 'add', '-u'], check=True) | ||
|
|
||
| return 0 | ||
|
|
||
| except subprocess.CalledProcessError as e: | ||
| print(f"Error running copyright update: {e}", file=sys.stderr) | ||
| return 0 # Don't fail the commit | ||
| except Exception as e: | ||
| print(f"Unexpected error: {e}", file=sys.stderr) | ||
| return 0 # Don't fail the commit | ||
|
|
||
| if __name__ == '__main__': | ||
| sys.exit(main()) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #! /usr/bin/env python3 | ||
| """ | ||
| Copyright (C) 2025-2026 Intel Corporation | ||
|
|
||
| SPDX-License-Identifier: MIT | ||
|
|
||
| """ | ||
| """ | ||
| Setup script to configure git hooks for this repository. | ||
| This script is cross-platform compatible (Linux, macOS, Windows). | ||
| """ | ||
|
|
||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| def main(): | ||
| """Configure git to use the .githooks directory.""" | ||
| try: | ||
| # Get the root directory of the repo | ||
| result = subprocess.run( | ||
| ['git', 'rev-parse', '--show-toplevel'], | ||
| capture_output=True, | ||
| text=True, | ||
| check=True | ||
| ) | ||
| repo_root = Path(result.stdout.strip()) | ||
| githooks_dir = repo_root / '.githooks' | ||
|
|
||
| # Configure git to use .githooks directory | ||
| subprocess.run( | ||
| ['git', 'config', 'core.hooksPath', str(githooks_dir)], | ||
| check=True | ||
| ) | ||
|
|
||
| print("Git hooks configured successfully!") | ||
| print("The pre-commit hook will now automatically update copyright years.") | ||
|
|
||
| return 0 | ||
|
|
||
| except subprocess.CalledProcessError as e: | ||
| print(f"Error configuring git hooks: {e}", file=sys.stderr) | ||
| return 1 | ||
| except Exception as e: | ||
| print(f"Unexpected error: {e}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| if __name__ == '__main__': | ||
| sys.exit(main()) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,21 @@ We encourage anyone who wants to contribute to submit | |
| review these for proper alignment with the | ||
| [Level Zero Specification](https://oneapi-src.github.io/level-zero-spec/level-zero/latest/index.html). | ||
|
|
||
| ## Git Hooks Setup | ||
|
|
||
| To enable automatic copyright year updates and other git hooks, run: | ||
|
|
||
| ```bash | ||
| python3 .githooks/setup.py | ||
|
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. Pending your thoughts on my prior comments for git config options, we may want to change this to simply give instructions for them to copy/paste the pre-commit file to someplace they have enabled... And/or change the script to simply check if hooks are enabled, inform them, and then see if the pre-commit file is in their path. |
||
| ``` | ||
|
|
||
| Or on Windows: | ||
| ```cmd | ||
| python .githooks\setup.py | ||
| ``` | ||
|
|
||
| This configures your local repository to use the shared git hooks. The pre-commit hook will automatically update copyright years in modified files before each commit. | ||
|
|
||
| ## C++ Coding Standards | ||
|
|
||
| * C++14 maximum support | ||
|
|
||
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.
There are four levels of githooks...
--global --system --local --worktree
five if you consider the rare --blob ability (which probably not worrisome here).
The pathing , while I have no native concern, may be in conflict with a users pre-configured environment. Are we certain this stacks, or it is replacing the users existing environment, if so how is it restored?
Another thought
This I assume, is targeting only implied --local behavior since no option is provided.
However, if the user has their environment to use --global will this be ignored entirely...
Summary, is this something we want to get into, i.e. configuring for them, or simply put some docs in and leave it at that, if they wish to manage multiple hooks, folder, etc.. they probably need to copy/paste or something