-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcommit_app.py
More file actions
163 lines (138 loc) · 6.31 KB
/
gcommit_app.py
File metadata and controls
163 lines (138 loc) · 6.31 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
#!/usr/bin/env python3
"""
GCommit - Main application class for AI-powered git commit message generation
"""
import subprocess
import sys
from typing import List, Tuple
from rich.console import Console
from rich.theme import Theme
from rich.table import Table
from rich.panel import Panel
from rich.progress import track
from rich.prompt import Prompt, Confirm
from rich.text import Text
from git_helper import GitHelper
from ollama_client import OllamaClient
class GCommit:
"""Main application class"""
def __init__(self, ollama_url: str = "http://localhost:11434", model: str = "llama3", hint: str = ""):
self.git = GitHelper()
self.ollama = OllamaClient(ollama_url, model)
self.hint = hint
# Set up Rich console with custom theme
custom_theme = Theme({
"info": "dim cyan",
"warning": "yellow",
"danger": "bold red",
"success": "bold green",
"header": "bold magenta",
"filename": "cyan",
"highlight": "bold yellow"
})
self.console = Console(theme=custom_theme)
def check_untracked_files(self) -> None:
"""Display warning for untracked files"""
has_untracked, untracked_files = self.git.has_untracked_files()
if has_untracked:
self.console.print(Panel(
"[warning]⚠️ Warning: Untracked files detected[/warning]",
title="Git Status",
border_style="yellow"
))
table = Table(show_header=True, header_style="warning")
table.add_column("Untracked Files", style="filename")
for file in untracked_files:
table.add_row(file)
self.console.print(table)
self.console.print("[info]These files will not be included in the commit.[/info]")
self.console.print("[info]Use [highlight]git add <files>[/highlight] to track them.[/info]\n")
def run(self) -> int:
"""Main application entry point"""
# Display welcome header
self.console.print(Panel(
"[header]🤖 gcommit[/header] - AI-powered Git commit message generator",
title="Welcome",
border_style="magenta"
))
# Check if we're in a git repository
try:
subprocess.run(['git', 'rev-parse', '--git-dir'],
capture_output=True, check=True)
except subprocess.CalledProcessError:
self.console.print("[danger]Error: Not in a git repository[/danger]")
return 1
# Check for untracked files
self.check_untracked_files()
# Get staged files
staged_files = self.git.get_staged_files()
if not staged_files:
self.console.print(Panel(
"[warning]No staged changes to commit[/warning] 😥",
title="Git Status",
border_style="yellow"
))
self.console.print("[info]Use [highlight]git add <files>[/highlight] to stage your changes first.[/info]")
return 0
# Check Ollama availability
if not self.ollama.is_available():
self.console.print("[danger]Error: Ollama is not running or not accessible.[/danger]")
self.console.print("[info]Please start Ollama with: [highlight]ollama serve[/highlight][/info]")
return 1
# Process each file
self.console.rule("[header]Analyzing Staged Files[/header]")
file_summaries = []
for filepath in track(staged_files, description="Processing files..."):
diff = self.git.get_file_diff(filepath)
if diff:
with self.console.status(f"[info]Analyzing[/info] [filename]{filepath}[/filename]...") as status:
summary = self.ollama.summarize_file_changes(filepath, diff, self.hint)
if summary:
file_summaries.append((filepath, summary))
if not file_summaries:
self.console.print("[danger]Error: Could not summarize any file changes[/danger]")
return 1
# Display file summaries table
self.console.rule("[header]File Analysis Results[/header]")
summary_table = Table(show_header=True, header_style="header")
summary_table.add_column("File", style="filename", width=30)
summary_table.add_column("Summary", style="white")
for filepath, summary in file_summaries:
summary_table.add_row(filepath, summary)
self.console.print(summary_table)
# Generate commit message from summaries
self.console.rule("[header]Generating Commit Message[/header]")
with self.console.status("[info]Creating commit message...[/info]") as status:
commit_message = self.ollama.generate_commit_message(file_summaries, self.hint)
if not commit_message:
self.console.print("[danger]Error: Failed to generate commit message[/danger]")
return 1
# Display and get confirmation
self.console.rule("[header]Commit Message Preview[/header]")
self.console.print(Panel(
commit_message,
title="Generated Commit Message",
border_style="green",
expand=False
))
choices = ["Accept and commit", "Reject and cancel", "Edit message"]
choice = Prompt.ask(
"\nWhat would you like to do?",
choices=["accept", "reject", "edit"],
default="accept"
)
if choice == "accept":
final_message = commit_message
elif choice == "reject":
self.console.print("[warning]Commit cancelled.[/warning]")
return 0
elif choice == "edit":
final_message = Prompt.ask("Enter your commit message", default=commit_message)
# Commit changes
self.console.rule("[header]Committing Changes[/header]")
with self.console.status("[info]Committing...[/info]") as status:
if self.git.commit_changes(final_message):
self.console.print("[success]✅ Changes committed successfully![/success]")
return 0
else:
return 1