Skip to content

Commit fa9ae2c

Browse files
committed
Add git hook for updating the copyright as part of pre-commit
Signed-off-by: Neil R. Spruit <neil.r.spruit@intel.com>
1 parent b3567ec commit fa9ae2c

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

.githooks/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Git Hooks
2+
3+
This directory contains git hooks for the repository.
4+
5+
## Setup
6+
7+
To enable the git hooks, run:
8+
9+
```bash
10+
python3 .githooks/setup.py
11+
```
12+
13+
Or on Windows:
14+
```cmd
15+
python .githooks\setup.py
16+
```
17+
18+
This configures git to use hooks from this directory instead of the default `.git/hooks`.
19+
20+
**Note:** The hooks are written in Python for cross-platform compatibility (Linux, macOS, Windows).
21+
22+
## Available Hooks
23+
24+
### pre-commit
25+
26+
Automatically updates copyright years in files being committed. The hook:
27+
- Runs `scripts/update_copyright.py` on modified files
28+
- Updates copyright year ranges (e.g., 2019-2024 → 2019-2025)
29+
- Adds single years where missing (e.g., 2019 → 2019-2025)
30+
- Automatically stages the copyright changes
31+
- Works across Linux, macOS, and Windows
32+
33+
The hook only modifies files already staged for commit and will not cause the commit to fail.

.githooks/pre-commit

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Pre-commit hook to update copyright years in modified files.
4+
This script is cross-platform compatible (Linux, macOS, Windows).
5+
"""
6+
7+
import subprocess
8+
import sys
9+
from pathlib import Path
10+
11+
def main():
12+
"""Run the copyright update script and stage changes."""
13+
try:
14+
# Get the root directory of the repo
15+
result = subprocess.run(
16+
['git', 'rev-parse', '--show-toplevel'],
17+
capture_output=True,
18+
text=True,
19+
check=True
20+
)
21+
repo_root = Path(result.stdout.strip())
22+
23+
# Run the update copyright script
24+
copyright_script = repo_root / 'scripts' / 'update_copyright.py'
25+
26+
if not copyright_script.exists():
27+
# Fallback to bash script if Python version doesn't exist
28+
copyright_script = repo_root / 'scripts' / 'update_copyright.sh'
29+
if copyright_script.exists():
30+
subprocess.run(['bash', str(copyright_script)], check=True)
31+
else:
32+
print("Warning: Copyright update script not found", file=sys.stderr)
33+
return 0
34+
else:
35+
# Run in interactive mode (will show preview and ask for confirmation)
36+
result = subprocess.run([sys.executable, str(copyright_script)])
37+
38+
# Check if there were any changes applied
39+
result = subprocess.run(
40+
['git', 'diff', '--quiet'],
41+
capture_output=True
42+
)
43+
44+
if result.returncode != 0:
45+
# There are changes, stage them
46+
print("\nStaging copyright updates...")
47+
subprocess.run(['git', 'add', '-u'], check=True)
48+
49+
return 0
50+
51+
except subprocess.CalledProcessError as e:
52+
print(f"Error running copyright update: {e}", file=sys.stderr)
53+
return 0 # Don't fail the commit
54+
except Exception as e:
55+
print(f"Unexpected error: {e}", file=sys.stderr)
56+
return 0 # Don't fail the commit
57+
58+
if __name__ == '__main__':
59+
sys.exit(main())

.githooks/setup.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#! /usr/bin/env python3
2+
"""
3+
Copyright (C) 2025-2026 Intel Corporation
4+
5+
SPDX-License-Identifier: MIT
6+
7+
"""
8+
"""
9+
Setup script to configure git hooks for this repository.
10+
This script is cross-platform compatible (Linux, macOS, Windows).
11+
"""
12+
13+
import subprocess
14+
import sys
15+
from pathlib import Path
16+
17+
def main():
18+
"""Configure git to use the .githooks directory."""
19+
try:
20+
# Get the root directory of the repo
21+
result = subprocess.run(
22+
['git', 'rev-parse', '--show-toplevel'],
23+
capture_output=True,
24+
text=True,
25+
check=True
26+
)
27+
repo_root = Path(result.stdout.strip())
28+
githooks_dir = repo_root / '.githooks'
29+
30+
# Configure git to use .githooks directory
31+
subprocess.run(
32+
['git', 'config', 'core.hooksPath', str(githooks_dir)],
33+
check=True
34+
)
35+
36+
print("Git hooks configured successfully!")
37+
print("The pre-commit hook will now automatically update copyright years.")
38+
39+
return 0
40+
41+
except subprocess.CalledProcessError as e:
42+
print(f"Error configuring git hooks: {e}", file=sys.stderr)
43+
return 1
44+
except Exception as e:
45+
print(f"Unexpected error: {e}", file=sys.stderr)
46+
return 1
47+
48+
if __name__ == '__main__':
49+
sys.exit(main())

CONTRIBUTING.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ We encourage anyone who wants to contribute to submit
66
review these for proper alignment with the
77
[Level Zero Specification](https://oneapi-src.github.io/level-zero-spec/level-zero/latest/index.html).
88

9+
## Git Hooks Setup
10+
11+
To enable automatic copyright year updates and other git hooks, run:
12+
13+
```bash
14+
python3 .githooks/setup.py
15+
```
16+
17+
Or on Windows:
18+
```cmd
19+
python .githooks\setup.py
20+
```
21+
22+
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.
23+
924
## C++ Coding Standards
1025

1126
* C++14 maximum support

0 commit comments

Comments
 (0)