-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.py
More file actions
83 lines (66 loc) · 2.42 KB
/
install.py
File metadata and controls
83 lines (66 loc) · 2.42 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Installation script for commit message generator git hook.
"""
import os
import sys
import shutil
import subprocess
from pathlib import Path
def find_git_root():
"""Find the git root directory starting from current directory."""
current = Path.cwd()
while current != current.parent:
if (current / ".git").exists():
return current
current = current.parent
return None
def install_hook():
"""Install the commit message generator as a git hook."""
git_root = find_git_root()
if not git_root:
print("Error: Not in a git repository. Please run this from within a git repo.", file=sys.stderr)
return False
hooks_dir = git_root / ".git" / "hooks"
script_dir = Path(__file__).parent.absolute()
print(f"Installing hook in: {hooks_dir}")
print(f"Script location: {script_dir}")
hooks_dir.mkdir(exist_ok=True)
target_dir = hooks_dir / "commit-msg-gen"
if target_dir.exists():
print("Removing existing installation...")
shutil.rmtree(target_dir)
print("Copying files...")
shutil.copytree(script_dir, target_dir)
hook_file = hooks_dir / "prepare-commit-msg"
if os.name == 'nt': # Windows
hook_content = f'''@echo off
"%~dp0commit-msg-gen\\venv\\Scripts\\python.exe" "%~dp0commit-msg-gen\\main.py" %*
'''
else: # Unix-like
# Create a shell script that calls our wrapper
hook_content = f'''#!/bin/bash
SCRIPT_DIR="$(cd "$(dirname "${{BASH_SOURCE[0]}}")" && pwd)"
"$SCRIPT_DIR/commit-msg-gen/venv/bin/python" "$SCRIPT_DIR/commit-msg-gen/main.py" "$@"
'''
with open(hook_file, 'w') as f:
f.write(hook_content)
if os.name != 'nt':
os.chmod(hook_file, 0o755)
print("Installation complete!")
print("\nMake sure you have:")
print("1. Set up your OpenAI API key in commit-msg-gen/.env")
print("2. The virtual environment is properly set up (run setup.py if needed)")
return True
def main():
"""Main installation function."""
if len(sys.argv) > 1 and sys.argv[1] in ['--help', '-h']:
print("Usage: python install.py")
print("Installs the commit message generator as a git hook in the current repository.")
return
if install_hook():
print("\n Hook installed successfully!")
else:
print("\n Installation failed!")
sys.exit(1)
if __name__ == "__main__":
main()