- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcommit_app.py
More file actions
Latest commit
163 lines (138 loc) · 6.31 KB
/
Copy pathgcommit_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
"""
importsubprocess
importsys
fromtypingimportList, Tuple
fromrich.consoleimportConsole
fromrich.themeimportTheme
fromrich.tableimportTable
fromrich.panelimportPanel
fromrich.progressimporttrack
fromrich.promptimportPrompt, Confirm
fromrich.textimportText
fromgit_helperimportGitHelper
fromollama_clientimportOllamaClient
classGCommit:
"""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)
defcheck_untracked_files(self) ->None:
"""Display warning for untracked files"""
has_untracked, untracked_files=self.git.has_untracked_files()
ifhas_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")
forfileinuntracked_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")
defrun(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)
exceptsubprocess.CalledProcessError:
self.console.print("[danger]Error: Not in a git repository[/danger]")
return1
# Check for untracked files
self.check_untracked_files()
# Get staged files
staged_files=self.git.get_staged_files()
ifnotstaged_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]")
return0
# Check Ollama availability
ifnotself.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]")
return1
# Process each file
self.console.rule("[header]Analyzing Staged Files[/header]")
file_summaries= []
forfilepathintrack(staged_files, description="Processing files..."):
diff=self.git.get_file_diff(filepath)
ifdiff:
withself.console.status(f"[info]Analyzing[/info] [filename]{filepath}[/filename]...") asstatus:
summary=self.ollama.summarize_file_changes(filepath, diff, self.hint)
ifsummary:
file_summaries.append((filepath, summary))
ifnotfile_summaries:
self.console.print("[danger]Error: Could not summarize any file changes[/danger]")
return1
# 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")
forfilepath, summaryinfile_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]")
withself.console.status("[info]Creating commit message...[/info]") asstatus:
commit_message=self.ollama.generate_commit_message(file_summaries, self.hint)
ifnotcommit_message:
self.console.print("[danger]Error: Failed to generate commit message[/danger]")
return1
# 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"
)
ifchoice=="accept":
final_message=commit_message
elifchoice=="reject":
self.console.print("[warning]Commit cancelled.[/warning]")
return0
elifchoice=="edit":
final_message=Prompt.ask("Enter your commit message", default=commit_message)
# Commit changes
self.console.rule("[header]Committing Changes[/header]")
withself.console.status("[info]Committing...[/info]") asstatus:
ifself.git.commit_changes(final_message):
self.console.print("[success]✅ Changes committed successfully![/success]")
return0
else:
return1