|
| 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()) |
0 commit comments