Nitishkumar-ai commited on
Commit
09fe077
·
1 Parent(s): 8f4e44a

Feat (Phase 3): Implement Git hook integration and pre-commit framework support

Browse files
.pre-commit-hooks.yaml ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ - id: commitguard
2
+ name: CommitGuard vulnerability scan
3
+ entry: commitguard scan --staged --format text --fail-on-vulnerable
4
+ language: python
5
+ types: [python, c, cpp]
commitguard_env/cli.py CHANGED
@@ -67,6 +67,19 @@ def cmd_eval(args):
67
  subprocess.run(cmd, check=True)
68
 
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  def main():
71
  parser = argparse.ArgumentParser(description="CommitGuard AI-paced security review")
72
  subparsers = parser.add_subparsers(dest="command", required=True)
@@ -94,6 +107,12 @@ def main():
94
  eval_parser = subparsers.add_parser("eval", help="Run the evaluation harness")
95
  eval_parser.add_argument("eval_args", nargs=argparse.REMAINDER, help="Arguments passed to evaluate.py")
96
 
 
 
 
 
 
 
97
  args = parser.parse_args()
98
 
99
  if args.command == "scan":
@@ -102,6 +121,8 @@ def main():
102
  cmd_server(args)
103
  elif args.command == "eval":
104
  cmd_eval(args)
 
 
105
 
106
  if __name__ == "__main__":
107
  main()
 
67
  subprocess.run(cmd, check=True)
68
 
69
 
70
+ def cmd_hook(args):
71
+ from .hooks import install_hook
72
+
73
+ if args.action == "install":
74
+ if args.pre_commit:
75
+ install_hook("pre-commit")
76
+ elif args.pre_push:
77
+ install_hook("pre-push")
78
+ else:
79
+ print("Please specify a hook type to install (e.g., --pre-commit or --pre-push)")
80
+ sys.exit(1)
81
+
82
+
83
  def main():
84
  parser = argparse.ArgumentParser(description="CommitGuard AI-paced security review")
85
  subparsers = parser.add_subparsers(dest="command", required=True)
 
107
  eval_parser = subparsers.add_parser("eval", help="Run the evaluation harness")
108
  eval_parser.add_argument("eval_args", nargs=argparse.REMAINDER, help="Arguments passed to evaluate.py")
109
 
110
+ # 'hook' subcommand
111
+ hook_parser = subparsers.add_parser("hook", help="Manage git hooks")
112
+ hook_parser.add_argument("action", choices=["install"], help="Action to perform (e.g., install)")
113
+ hook_parser.add_argument("--pre-commit", action="store_true", help="Install pre-commit hook")
114
+ hook_parser.add_argument("--pre-push", action="store_true", help="Install pre-push hook")
115
+
116
  args = parser.parse_args()
117
 
118
  if args.command == "scan":
 
121
  cmd_server(args)
122
  elif args.command == "eval":
123
  cmd_eval(args)
124
+ elif args.command == "hook":
125
+ cmd_hook(args)
126
 
127
  if __name__ == "__main__":
128
  main()
commitguard_env/hooks.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import stat
3
+ import sys
4
+ from pathlib import Path
5
+
6
+ PRE_COMMIT_SCRIPT = """#!/bin/sh
7
+ # CommitGuard pre-commit hook
8
+ echo "Running CommitGuard scan on staged changes..."
9
+ commitguard scan --staged --format text --fail-on-vulnerable
10
+ if [ $? -ne 0 ]; then
11
+ echo "CommitGuard found vulnerabilities! Commit aborted."
12
+ exit 1
13
+ fi
14
+ """
15
+
16
+ PRE_PUSH_SCRIPT = """#!/bin/sh
17
+ # CommitGuard pre-push hook
18
+ echo "Running CommitGuard scan on commits to be pushed..."
19
+ while read local_ref local_sha remote_ref remote_sha
20
+ do
21
+ if [ "$local_sha" != "0000000000000000000000000000000000000000" ]; then
22
+ commitguard scan --commit $local_sha --format text --fail-on-vulnerable
23
+ if [ $? -ne 0 ]; then
24
+ echo "CommitGuard found vulnerabilities in $local_sha! Push aborted."
25
+ exit 1
26
+ fi
27
+ fi
28
+ done
29
+ """
30
+
31
+ def install_hook(hook_type: str):
32
+ git_dir = Path(".git")
33
+ if not git_dir.exists() or not git_dir.is_dir():
34
+ print("Error: .git directory not found. Please run this command from the root of a git repository.")
35
+ sys.exit(1)
36
+
37
+ hooks_dir = git_dir / "hooks"
38
+ hooks_dir.mkdir(exist_ok=True)
39
+
40
+ hook_path = hooks_dir / hook_type
41
+ script_content = PRE_COMMIT_SCRIPT if hook_type == "pre-commit" else PRE_PUSH_SCRIPT
42
+
43
+ with open(hook_path, "w", encoding="utf-8") as f:
44
+ f.write(script_content)
45
+
46
+ # Make it executable
47
+ st = os.stat(hook_path)
48
+ os.chmod(hook_path, st.st_mode | stat.S_IEXEC)
49
+
50
+ print(f"Successfully installed {hook_type} hook at {hook_path}")