-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstall.py
More file actions
60 lines (45 loc) · 1.72 KB
/
Install.py
File metadata and controls
60 lines (45 loc) · 1.72 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
import os
import sys
import shutil
import stat
REPO_ROOT = os.getcwd()
GIT_DIR = os.path.join(REPO_ROOT, ".git")
HOOKS_DIR = os.path.join(GIT_DIR, "hooks")
PRE_COMMIT_PATH = os.path.join(HOOKS_DIR, "pre-commit")
SOURCE_SCRIPT = os.path.join(REPO_ROOT, "git_sentinel.py")
SHEBANG = "#!/usr/bin/env python\n\n"
def fail(message):
print(f"[Git-Sentinel] ERROR: {message}")
sys.exit(1)
def main():
# 1. Verify Git repository
if not os.path.isdir(GIT_DIR):
fail("This directory is not a Git repository (.git not found).")
# 2. Verify source script exists
if not os.path.isfile(SOURCE_SCRIPT):
fail("git_sentinel.py not found in repository root.")
# 3. Ensure hooks directory exists
os.makedirs(HOOKS_DIR, exist_ok=True)
# 4. Prevent silent overwrite
if os.path.exists(PRE_COMMIT_PATH):
print("[Git-Sentinel] A pre-commit hook already exists.")
choice = input("Overwrite existing hook? (y/N): ").strip().lower()
if choice != "y":
print("[Git-Sentinel] Installation aborted.")
sys.exit(0)
# 5. Read scanner source
with open(SOURCE_SCRIPT, "r", encoding="utf-8") as f:
script_content = f.read()
# 6. Write pre-commit hook with shebang
with open(PRE_COMMIT_PATH, "w", encoding="utf-8") as f:
f.write(SHEBANG)
f.write(script_content)
# 7. Make executable (important for Git Bash / Unix)
st = os.stat(PRE_COMMIT_PATH)
os.chmod(PRE_COMMIT_PATH, st.st_mode | stat.S_IEXEC)
print("\n[Git-Sentinel] Installation successful.")
print("Pre-commit hook installed at:")
print(f" {PRE_COMMIT_PATH}\n")
print("Git-Sentinel is now protecting this repository.")
if __name__ == "__main__":
main()