-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
172 lines (144 loc) · 4.52 KB
/
main.py
File metadata and controls
172 lines (144 loc) · 4.52 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
import sys
import argparse
from src.generator import CommitGenerator
from src.utils.display import print_banner, print_error
from src.utils.git import get_staged_diff, get_staged_files, is_git_repo
def main():
print_banner()
parser = argparse.ArgumentParser(
description="⚡ Generate perfect Git commit messages with AI",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Generate from staged changes (most common)
git add . && gcgen
# Generate with specific style
gcgen --style conventional
gcgen --style emoji
gcgen --style angular
# Generate multiple options to choose from
gcgen --count 5
# Commit automatically with generated message
gcgen --auto-commit
# Analyze diff from specific files
gcgen --files src/main.py src/utils.py
# Dry run — see what would be committed
gcgen --dry-run
"""
)
parser.add_argument(
"--style",
choices=["conventional", "emoji", "angular", "simple"],
default="conventional",
help="Commit message style (default: conventional)"
)
parser.add_argument(
"--count", "-n",
type=int, default=3,
help="Number of suggestions to generate (default: 3)"
)
parser.add_argument(
"--auto-commit", "-a",
action="store_true",
help="Automatically commit with the best suggestion"
)
parser.add_argument(
"--files", "-f",
nargs="+",
help="Specific files to analyze"
)
parser.add_argument(
"--provider",
choices=["openai", "ollama", "rule-based"],
default="rule-based",
help="AI provider (default: rule-based — works without API key)"
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Show what would be committed without committing"
)
parser.add_argument(
"--lang",
choices=["en", "fr", "es", "de", "ar"],
default="en",
help="Language for commit messages (default: en)"
)
parser.add_argument(
"--hook",
action="store_true",
help="Install as git prepare-commit-msg hook"
)
args = parser.parse_args()
# Install hook mode
if args.hook:
from src.utils.git import install_hook
install_hook()
return
# Validate git repo
if not is_git_repo():
print_error("Not a git repository. Run 'git init' first.")
sys.exit(1)
# Get diff
diff = get_staged_diff(args.files)
files = get_staged_files(args.files)
if not diff and not files:
print_error("No staged changes found. Run 'git add .' first.")
sys.exit(1)
# Generate messages
generator = CommitGenerator(provider=args.provider)
suggestions = generator.generate(
diff=diff,
files=files,
style=args.style,
count=args.count,
lang=args.lang
)
if not suggestions:
print_error("Could not generate commit messages.")
sys.exit(1)
# Display and handle
_handle_suggestions(suggestions, args)
def _handle_suggestions(suggestions: list, args):
"""Display suggestions and handle user choice."""
from src.utils.display import display_suggestions
from src.utils.git import do_commit
if args.dry_run:
print("\n📋 DRY RUN — These would be committed:\n")
display_suggestions(suggestions)
return
if args.auto_commit:
best = suggestions[0]
print(f"\n✅ Auto-committing with:\n {best['message']}\n")
do_commit(best['message'])
return
# Interactive selection
display_suggestions(suggestions)
print("\n💡 Options:")
print(" [1-{}] Select a suggestion".format(len(suggestions)))
print(" [e] Edit before committing")
print(" [r] Regenerate")
print(" [q] Quit\n")
choice = input("Your choice: ").strip().lower()
if choice == "q":
print("Cancelled.")
return
elif choice == "r":
main()
return
elif choice == "e":
import subprocess
msg = suggestions[0]['message']
edited = subprocess.check_output(
f'echo "{msg}" | vipe', shell=True
).decode().strip()
do_commit(edited)
elif choice.isdigit() and 1 <= int(choice) <= len(suggestions):
selected = suggestions[int(choice) - 1]
do_commit(selected['message'])
print(f"\n✅ Committed: {selected['message']}\n")
else:
print("Invalid choice.")
if __name__ == "__main__":
main()