-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_hook.py
More file actions
65 lines (51 loc) · 1.8 KB
/
Copy pathinstall_hook.py
File metadata and controls
65 lines (51 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Run this once to activate the SentinelCodeAI pre-commit hook.
Usage:
# install into current directory's repo
python install_hook.py
# install into any repo on your machine
python install_hook.py --path C:/Users/you/your-project
"""
import argparse
import shutil
import subprocess
import sys
from pathlib import Path
def get_repo_root(path: str) -> Path:
result = subprocess.run(
["git", "rev-parse", "--show-toplevel"],
capture_output=True, text=True, cwd=path
)
if result.returncode != 0:
print(f"ERROR: '{path}' is not inside a git repository.")
sys.exit(1)
return Path(result.stdout.strip())
def main():
parser = argparse.ArgumentParser(
description="Install SentinelCodeAI pre-commit hook into a git repository."
)
parser.add_argument(
"--path",
default=".",
help="Path to the git repository to protect (default: current directory).",
)
args = parser.parse_args()
target = str(Path(args.path).resolve())
repo_root = get_repo_root(target)
hooks_dir = repo_root / ".git" / "hooks"
hook_dest = hooks_dir / "pre-commit"
hook_src = Path(__file__).parent / "src" / "git_hooks" / "pre_commit_hook.sh"
if not hooks_dir.exists():
print(f"ERROR: .git/hooks not found in '{repo_root}'.")
sys.exit(1)
if not hook_src.exists():
print(f"ERROR: Hook source not found at '{hook_src}'.")
sys.exit(1)
if hook_dest.exists():
print(f"WARNING: Existing hook at '{hook_dest}' will be overwritten.")
shutil.copy(str(hook_src), str(hook_dest))
hook_dest.chmod(0o755)
print(f"SentinelCodeAI: hook installed into '{repo_root}'")
print(f"Every 'git commit' in that repo will now be scanned automatically.")
if __name__ == "__main__":
main()