diff --git a/.githooks/README.md b/.githooks/README.md new file mode 100644 index 00000000..06a2f595 --- /dev/null +++ b/.githooks/README.md @@ -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. diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 00000000..c36f786b --- /dev/null +++ b/.githooks/pre-commit @@ -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()) diff --git a/.githooks/setup.py b/.githooks/setup.py new file mode 100755 index 00000000..c3a98ba4 --- /dev/null +++ b/.githooks/setup.py @@ -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()) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 79fa3e51..63d594a0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 +``` + +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