| 59 | |
| 60 | |
| 61 | def install_hooks(start: Path | str | None = None, *, force: bool = False) -> HookStatus: |
| 62 | repo = find_git_repository(start) |
| 63 | hooks_dir = repo.git_dir / "hooks" |
| 64 | hooks_dir.mkdir(parents=True, exist_ok=True) |
| 65 | |
| 66 | script = _hook_script(repo.root) |
| 67 | for hook_name in HOOK_NAMES: |
| 68 | hook_path = hooks_dir / hook_name |
| 69 | if hook_path.exists() and not _is_managed_hook(hook_path) and not force: |
| 70 | raise HookError( |
| 71 | f"{hook_name} already exists and is not managed by CGC. " |
| 72 | "Re-run with --force to replace it." |
| 73 | ) |
| 74 | hook_path.write_text(script, encoding="utf-8") |
| 75 | mode = hook_path.stat().st_mode |
| 76 | hook_path.chmod(mode | stat.S_IXUSR | stat.S_IXGRP) |
| 77 | |
| 78 | _ensure_gitattributes(repo.root) |
| 79 | _configure_merge_driver(repo.root) |
| 80 | return get_hook_status(repo.root) |
| 81 | |
| 82 | |
| 83 | def uninstall_hooks(start: Path | str | None = None) -> HookStatus: |