| from pathlib import Path | |
| import stat | |
| import subprocess | |
| import sys | |
| root = Path(__file__).resolve().parents[2] | |
| hook_dir = root / ".githooks" | |
| hooks = [hook_dir / "pre-commit", hook_dir / "pre-push"] | |
| inside_repo = subprocess.run( | |
| ["git", "rev-parse", "--is-inside-work-tree"], | |
| cwd=root, | |
| text=True, | |
| capture_output=True, | |
| ) | |
| if inside_repo.returncode != 0 or inside_repo.stdout.strip() != "true": | |
| raise SystemExit(0) | |
| set_hooks = subprocess.run( | |
| ["git", "config", "core.hooksPath", ".githooks"], | |
| cwd=root, | |
| text=True, | |
| capture_output=True, | |
| ) | |
| if set_hooks.returncode != 0: | |
| sys.stderr.write(set_hooks.stderr) | |
| raise SystemExit(set_hooks.returncode) | |
| for hook in hooks: | |
| if hook.exists(): | |
| hook.chmod(hook.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) | |