forked from RyuCode-Digital-Solution/AI-Commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_commit_gui.py
More file actions
886 lines (727 loc) · 33.8 KB
/
ai_commit_gui.py
File metadata and controls
886 lines (727 loc) · 33.8 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
import os
import subprocess
import threading
from pathlib import Path
from typing import Optional, List, Tuple, Dict
import tkinter as tk
from tkinter import ttk, scrolledtext, messagebox
try:
import google.generativeai as genai
GEMINI_AVAILABLE = True
except ImportError:
GEMINI_AVAILABLE = False
try:
from openai import OpenAI
OPENAI_AVAILABLE = True
except ImportError:
OPENAI_AVAILABLE = False
GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-2.5-flash")
OPENAI_MODEL = os.getenv("OPENAI_MODEL", "gpt-4o-mini")
class AICommitGUI:
def __init__(self, root):
self.root = root
self.root.title("🤖 AI Commit - GUI")
self.root.geometry("950x750")
self.root.resizable(True, True)
# Variables
self.selected_repo = tk.StringVar()
self.ai_provider = tk.StringVar(value="gemini")
self.commit_message = tk.StringVar()
self.auto_push = tk.BooleanVar(value=True)
self.dark_mode = tk.BooleanVar(value=False)
self.selected_files = []
self.repos = []
self.current_repo_path = None
self.raw_git_status = [] # Store raw git status data
self._is_generating = False # Flag to prevent multiple generations
# Color schemes
self.light_theme = {
'bg': '#f0f0f0',
'fg': '#000000',
'frame_bg': '#ffffff',
'text_bg': '#ffffff',
'text_fg': '#000000',
'button_bg': '#e0e0e0',
'accent': '#0078d4',
'log_bg': '#f9f9f9',
'log_fg': '#333333'
}
self.dark_theme = {
'bg': '#1e1e1e',
'fg': '#ffffff',
'frame_bg': '#2d2d2d',
'text_bg': '#252526',
'text_fg': '#d4d4d4',
'button_bg': '#3e3e42',
'accent': '#0e639c',
'log_bg': '#1e1e1e',
'log_fg': '#cccccc'
}
self.current_theme = self.light_theme
# Setup UI
self.setup_ui()
# Auto scan on start
self.root.after(500, self.scan_repositories)
def setup_ui(self):
"""Setup user interface"""
# Configure root
self.root.configure(bg=self.current_theme['bg'])
# Style
self.style = ttk.Style()
self.style.theme_use('clam')
self.apply_theme()
# Main container
main_frame = ttk.Frame(self.root, padding="10")
main_frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
self.root.columnconfigure(0, weight=1)
self.root.rowconfigure(0, weight=1)
# Configure grid weights for responsiveness
main_frame.columnconfigure(0, weight=1)
main_frame.columnconfigure(1, weight=1)
main_frame.columnconfigure(2, weight=1)
main_frame.rowconfigure(3, weight=2) # Files frame
main_frame.rowconfigure(6, weight=1) # Log frame
# Header Frame with Title and Theme Toggle
header_frame = ttk.Frame(main_frame)
header_frame.grid(row=0, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=(0, 10))
header_frame.columnconfigure(0, weight=1)
# Title
title_label = ttk.Label(
header_frame,
text="🤖 AI Commit",
font=('Helvetica', 18, 'bold')
)
title_label.grid(row=0, column=0, sticky=tk.W)
# Theme Toggle
theme_frame = ttk.Frame(header_frame)
theme_frame.grid(row=0, column=1, sticky=tk.E)
ttk.Label(theme_frame, text="🌙", font=('Helvetica', 14)).pack(side=tk.LEFT, padx=2)
theme_toggle = ttk.Checkbutton(
theme_frame,
text="Dark Mode",
variable=self.dark_mode,
command=self.toggle_theme
)
theme_toggle.pack(side=tk.LEFT, padx=5)
# Settings Frame
settings_frame = ttk.LabelFrame(main_frame, text="⚙️ Settings", padding="10")
settings_frame.grid(row=1, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=5)
settings_frame.columnconfigure(1, weight=1)
# AI Provider
ttk.Label(settings_frame, text="AI Provider:").grid(row=0, column=0, sticky=tk.W, padx=5)
ai_frame = ttk.Frame(settings_frame)
ai_frame.grid(row=0, column=1, sticky=tk.W, padx=5)
ttk.Radiobutton(ai_frame, text="Gemini (Free)", variable=self.ai_provider, value="gemini").pack(side=tk.LEFT, padx=5)
ttk.Radiobutton(ai_frame, text="ChatGPT", variable=self.ai_provider, value="chatgpt").pack(side=tk.LEFT, padx=5)
# Auto Push
ttk.Checkbutton(settings_frame, text="Auto Push to Origin", variable=self.auto_push).grid(row=0, column=2, padx=20, sticky=tk.E)
# Repository Selection Frame
repo_frame = ttk.LabelFrame(main_frame, text="📂 Select Repository", padding="10")
repo_frame.grid(row=2, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=5)
repo_frame.columnconfigure(1, weight=1)
# Scan button
ttk.Button(repo_frame, text="🔍 Scan Repositories", command=self.scan_repositories).grid(row=0, column=0, padx=5)
# Repository dropdown
self.repo_combo = ttk.Combobox(repo_frame, textvariable=self.selected_repo, state="readonly")
self.repo_combo.grid(row=0, column=1, padx=5, sticky=(tk.W, tk.E))
self.repo_combo.bind('<<ComboboxSelected>>', self.on_repo_selected)
# Files Frame
files_frame = ttk.LabelFrame(main_frame, text="📝 Changed Files", padding="10")
files_frame.grid(row=3, column=0, columnspan=3, sticky=(tk.W, tk.E, tk.N, tk.S), pady=5)
# File buttons - MOVED TO TOP
file_buttons = ttk.Frame(files_frame)
file_buttons.pack(fill=tk.X, pady=(0, 5))
ttk.Button(file_buttons, text="✅ Select All", command=self.select_all_files, width=15).pack(side=tk.LEFT, padx=2)
ttk.Button(file_buttons, text="❌ Clear Selection", command=self.clear_file_selection, width=15).pack(side=tk.LEFT, padx=2)
ttk.Button(file_buttons, text="➕ Add to Stage", command=self.add_selected_files, width=15).pack(side=tk.LEFT, padx=2)
# Info label
info_label = ttk.Label(
files_frame,
text="ℹ️ Select files and click 'Add to Stage' before generating commit message",
foreground=self.current_theme['accent'],
font=('Helvetica', 8, 'italic')
)
info_label.pack(fill=tk.X, pady=(0, 5))
self.info_label = info_label # Store reference for theme switching
# Files listbox with scrollbar
files_scroll_frame = ttk.Frame(files_frame)
files_scroll_frame.pack(fill=tk.BOTH, expand=True)
scrollbar = ttk.Scrollbar(files_scroll_frame)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.files_listbox = tk.Listbox(
files_scroll_frame,
selectmode=tk.MULTIPLE,
yscrollcommand=scrollbar.set,
height=8,
font=('Courier', 9),
bg=self.current_theme['text_bg'],
fg=self.current_theme['text_fg'],
selectbackground=self.current_theme['accent']
)
self.files_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.config(command=self.files_listbox.yview)
# Commit Message Frame
message_frame = ttk.LabelFrame(main_frame, text="💬 Commit Message", padding="10")
message_frame.grid(row=4, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=5)
# Message text area
self.message_text = scrolledtext.ScrolledText(
message_frame,
height=4,
wrap=tk.WORD,
font=('Courier', 9),
bg=self.current_theme['text_bg'],
fg=self.current_theme['text_fg'],
insertbackground=self.current_theme['text_fg']
)
self.message_text.pack(fill=tk.BOTH, expand=True, pady=5)
# Message buttons
msg_buttons = ttk.Frame(message_frame)
msg_buttons.pack(fill=tk.X)
ttk.Button(msg_buttons, text="🤖 Generate with AI", command=self.auto_add_and_generate, width=20).pack(side=tk.LEFT, padx=2)
ttk.Button(msg_buttons, text="🗑️ Clear", command=self.clear_message, width=12).pack(side=tk.LEFT, padx=2)
# Action Buttons
action_frame = ttk.Frame(main_frame)
action_frame.grid(row=5, column=0, columnspan=3, pady=10)
ttk.Button(action_frame, text="✅ Commit & Push", command=self.commit_and_push, width=18).pack(side=tk.LEFT, padx=5)
ttk.Button(action_frame, text="💾 Commit Only", command=self.commit_only, width=15).pack(side=tk.LEFT, padx=5)
ttk.Button(action_frame, text="❌ Cancel", command=self.root.quit, width=12).pack(side=tk.LEFT, padx=5)
# Log Frame
log_frame = ttk.LabelFrame(main_frame, text="📋 Activity Log", padding="5")
log_frame.grid(row=6, column=0, columnspan=3, sticky=(tk.W, tk.E, tk.N, tk.S), pady=5)
self.log_text = scrolledtext.ScrolledText(
log_frame,
height=6,
wrap=tk.WORD,
font=('Courier', 8),
bg=self.current_theme['log_bg'],
fg=self.current_theme['log_fg'],
state='disabled'
)
self.log_text.pack(fill=tk.BOTH, expand=True)
# Status bar
self.status_label = ttk.Label(main_frame, text="Ready", relief=tk.SUNKEN, anchor=tk.W)
self.status_label.grid(row=7, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=(5, 0))
def apply_theme(self):
"""Apply current theme to UI"""
theme = self.current_theme
# Configure ttk styles
self.style.configure('TFrame', background=theme['bg'])
self.style.configure('TLabel', background=theme['bg'], foreground=theme['fg'])
self.style.configure('TLabelframe', background=theme['bg'], foreground=theme['fg'])
self.style.configure('TLabelframe.Label', background=theme['bg'], foreground=theme['fg'])
self.style.configure('TButton', background=theme['button_bg'], foreground=theme['fg'])
self.style.configure('TCheckbutton', background=theme['bg'], foreground=theme['fg'])
self.style.configure('TRadiobutton', background=theme['bg'], foreground=theme['fg'])
self.style.configure('TCombobox', fieldbackground=theme['text_bg'], background=theme['bg'])
# Map for button states
self.style.map('TButton',
background=[('active', theme['accent'])],
foreground=[('active', '#ffffff')]
)
def toggle_theme(self):
"""Toggle between dark and light theme"""
if self.dark_mode.get():
self.current_theme = self.dark_theme
else:
self.current_theme = self.light_theme
# Apply theme
self.root.configure(bg=self.current_theme['bg'])
self.apply_theme()
# Update widgets that need manual color change
self.files_listbox.configure(
bg=self.current_theme['text_bg'],
fg=self.current_theme['text_fg'],
selectbackground=self.current_theme['accent']
)
self.message_text.configure(
bg=self.current_theme['text_bg'],
fg=self.current_theme['text_fg'],
insertbackground=self.current_theme['text_fg']
)
self.log_text.configure(
bg=self.current_theme['log_bg'],
fg=self.current_theme['log_fg']
)
self.info_label.configure(foreground=self.current_theme['accent'])
self.log("🎨 Theme changed to " + ("Dark Mode" if self.dark_mode.get() else "Light Mode"))
def log(self, message: str, level: str = "info"):
"""Add message to log"""
self.log_text.configure(state='normal')
self.log_text.insert(tk.END, f"{message}\n")
self.log_text.see(tk.END)
if level == "error":
# Get the line that was just inserted
last_line_start = self.log_text.index("end-2l")
last_line_end = self.log_text.index("end-1l")
self.log_text.tag_add("error", last_line_start, last_line_end)
self.log_text.tag_config("error", foreground="red")
elif level == "success":
last_line_start = self.log_text.index("end-2l")
last_line_end = self.log_text.index("end-1l")
self.log_text.tag_add("success", last_line_start, last_line_end)
self.log_text.tag_config("success", foreground="green")
self.log_text.configure(state='disabled')
def set_status(self, message: str):
"""Update status bar"""
self.status_label.config(text=message)
self.root.update_idletasks()
def run_git_command(self, command: List[str], cwd: Optional[str] = None) -> Tuple[bool, str]:
"""Execute git command"""
try:
result = subprocess.run(
command,
capture_output=True,
text=True,
check=True,
cwd=cwd,
timeout=30
)
return True, result.stdout
except subprocess.TimeoutExpired:
return False, "Command timed out after 30 seconds"
except subprocess.CalledProcessError as e:
return False, e.stderr
except FileNotFoundError:
return False, "Git command not found. Is Git installed?"
def is_git_repo(self, path: str) -> bool:
"""Check if path is a git repository"""
return (Path(path) / ".git").exists()
def check_has_changes(self, repo_path: str) -> bool:
"""Check if repository has changes"""
success, output = self.run_git_command(
["git", "status", "--porcelain"],
cwd=repo_path
)
return success and bool(output.strip())
def scan_repositories(self):
"""Scan for git repositories"""
self.log("🔍 Scanning for repositories...")
self.set_status("Scanning...")
current_path = Path('.').resolve()
parent_path = current_path.parent
self.repos = []
repo_names = []
for item in sorted(parent_path.iterdir()):
if item.is_dir() and not item.name.startswith('.'):
if self.is_git_repo(str(item)):
has_changes = self.check_has_changes(str(item))
indicator = "🔴" if has_changes else "⚪"
display_name = f"{indicator} {item.name}"
if item.name == current_path.name:
display_name += " (current)"
self.repos.append({
'name': item.name,
'path': str(item),
'has_changes': has_changes,
'display_name': display_name
})
repo_names.append(display_name)
if self.repos:
self.repo_combo['values'] = repo_names
self.log(f"✅ Found {len(self.repos)} repositories", "success")
# Auto-select first repo with changes
for idx, repo in enumerate(self.repos):
if repo['has_changes']:
self.repo_combo.current(idx)
self.on_repo_selected(None)
break
else:
self.log("❌ No git repositories found", "error")
messagebox.showwarning("No Repositories", "No git repositories found in parent folder!")
self.set_status("Ready")
def on_repo_selected(self, event):
"""Handle repository selection"""
selected_idx = self.repo_combo.current()
if selected_idx < 0:
return
repo = self.repos[selected_idx]
self.current_repo_path = repo['path']
self.log(f"📂 Selected: {repo['name']}")
self.load_changed_files()
def load_changed_files(self):
"""Load changed files from selected repository"""
if not self.current_repo_path:
return
self.files_listbox.delete(0, tk.END)
success, output = self.run_git_command(
["git", "status", "--porcelain"],
cwd=self.current_repo_path
)
if not success:
self.log("❌ Failed to get file status", "error")
return
if not output.strip():
self.log("ℹ️ No changes detected")
self.raw_git_status = []
return
# Build temp list first to avoid race conditions
temp_status = []
for line in output.strip().split('\n'):
if line and len(line) >= 3:
status = line[:2]
filename = line[3:].strip()
# Normalize path separators
filename = filename.replace('\\', '/')
# Handle renamed files: "R old.txt -> new.txt"
if 'R' in status and '->' in filename:
filename = filename.split('->')[-1].strip()
# Store raw data
temp_status.append({
'status': status,
'filename': filename
})
# Display with icon (visual only)
if "?" in status:
icon = "🆕"
elif "M" in status:
icon = "✏️"
elif "D" in status:
icon = "🗑️"
elif "A" in status:
icon = "➕"
elif "R" in status:
icon = "🔄"
else:
icon = "📝"
display_text = f"{icon} {filename}"
self.files_listbox.insert(tk.END, display_text)
# Atomic assignment to avoid race conditions
self.raw_git_status = temp_status
self.log(f"📝 Found {len(self.raw_git_status)} changed files")
def select_all_files(self):
"""Select all files in listbox"""
self.files_listbox.select_set(0, tk.END)
self.log(f"✅ Selected all {self.files_listbox.size()} files")
def clear_file_selection(self):
"""Clear file selection"""
self.files_listbox.selection_clear(0, tk.END)
self.log("❌ Cleared file selection")
def find_exact_file(self, filename: str) -> Optional[str]:
"""Find exact file match in git status - case sensitive"""
repo_path = Path(self.current_repo_path)
# Normalize path separator
filename = filename.replace('\\', '/')
# Direct path check first
if (repo_path / filename).exists():
return filename
# For files in folders, search recursively
if '/' in filename:
parts = filename.split('/')
target_filename = parts[-1]
folder_path = '/'.join(parts[:-1]) if len(parts) > 1 else ''
# Search in the specified folder
search_dir = repo_path / folder_path if folder_path else repo_path
if search_dir.exists() and search_dir.is_dir():
# Look for exact filename match (case sensitive)
for item in search_dir.iterdir():
if item.is_file() and item.name == target_filename:
# Return relative path from repo root
relative_path = item.relative_to(repo_path)
return str(relative_path).replace('\\', '/')
else:
# File in root, search root directory only
for item in repo_path.iterdir():
if item.is_file() and item.name == filename:
return filename
return None
def add_selected_files(self):
"""Add selected files to git"""
if not self.current_repo_path:
messagebox.showwarning("No Repository", "Please select a repository first!")
return
selected_indices = self.files_listbox.curselection()
if not selected_indices:
messagebox.showwarning("No Files", "Please select files to add!")
return
# Check if raw_git_status is available
if not self.raw_git_status:
self.log("⚠️ No cached file data, refreshing...", "error")
self.load_changed_files()
messagebox.showwarning("Please Try Again", "File list refreshed. Please select files and try again.")
return
self.set_status("Adding files...")
self.log(f"➕ Adding {len(selected_indices)} files to stage...")
files_to_add = []
deleted_files = []
for i in selected_indices:
if i < len(self.raw_git_status):
file_info = self.raw_git_status[i]
status_code = file_info['status']
filename = file_info['filename']
# Handle different file statuses
if 'D' in status_code:
deleted_files.append(filename)
else:
files_to_add.append(filename)
success_count = 0
error_messages = []
# Handle deleted files
for filename in deleted_files:
success_rm, output_rm = self.run_git_command(
["git", "rm", filename],
cwd=self.current_repo_path
)
if success_rm:
success_count += 1
self.log(f"🗑️ Staged deletion: {filename}", "success")
else:
error_messages.append(f"Failed to remove {filename}")
self.log(f"❌ Failed rm: {filename}", "error")
# Handle other files
for filename in files_to_add:
# Try to find exact file match
actual_file = self.find_exact_file(filename)
if actual_file:
success_add, output_add = self.run_git_command(
["git", "add", "--", actual_file],
cwd=self.current_repo_path
)
if success_add:
success_count += 1
self.log(f"✅ Staged: {actual_file}", "success")
else:
error_messages.append(f"Failed to add {actual_file}")
self.log(f"❌ Failed to add: {actual_file}\n{output_add}", "error")
else:
# File not found, log detailed error
self.log(f"❌ File not found: {filename}", "error")
error_messages.append(f"File not found: {filename}")
# Show results
if success_count > 0:
self.log(f"✅ Successfully staged {success_count} file(s)", "success")
if not error_messages:
messagebox.showinfo("Success", f"Staged {success_count} file(s) successfully!")
else:
messagebox.showwarning("Partial Success",
f"Staged {success_count} file(s), but {len(error_messages)} failed.\nCheck Activity Log for details.")
else:
self.log("❌ No files were staged", "error")
if error_messages:
messagebox.showerror("Error", f"Failed to stage files:\n" + "\n".join(error_messages[:3]))
else:
messagebox.showerror("Error", "Failed to stage files. Check Activity Log for details.")
self.set_status("Ready")
# Refresh file list
self.root.after(500, self.load_changed_files)
def auto_add_and_generate(self):
"""Auto add selected files (or all) then generate commit message"""
if self._is_generating:
self.log("⚠️ Already generating, please wait...", "error")
return
if not self.current_repo_path:
messagebox.showwarning("No Repository", "Please select a repository first!")
return
# Check if there are changes
if self.files_listbox.size() == 0:
messagebox.showinfo("No Changes", "No changed files detected in this repository.")
return
self.log("🔄 Auto-adding files before generating commit message...")
# Auto select all if nothing selected
selected_indices = self.files_listbox.curselection()
if not selected_indices:
self.select_all_files()
selected_indices = self.files_listbox.curselection()
# Use raw_git_status for file operations
if not self.raw_git_status:
self.log("⚠️ No file data available", "error")
return
files_to_add = []
deleted_files = []
for i in selected_indices:
if i < len(self.raw_git_status):
file_info = self.raw_git_status[i]
status_code = file_info['status']
filename = file_info['filename']
if 'D' in status_code:
deleted_files.append(filename)
else:
files_to_add.append(filename)
# Handle deleted files
for filename in deleted_files:
success_rm, _ = self.run_git_command(
["git", "rm", filename],
cwd=self.current_repo_path
)
if success_rm:
self.log(f"🗑️ Staged deletion: {filename}")
# Handle other files
for filename in files_to_add:
actual_file = self.find_exact_file(filename)
if actual_file:
success_add, _ = self.run_git_command(
["git", "add", "--", actual_file],
cwd=self.current_repo_path
)
if success_add:
self.log(f"✅ Staged: {actual_file}")
total_staged = len(files_to_add) + len(deleted_files)
if total_staged > 0:
self.log(f"✅ Total {total_staged} files staged", "success")
# Now generate commit message
self.root.after(200, self.generate_commit_message)
else:
messagebox.showwarning("No Files", "No files were staged")
self.set_status("Ready")
def generate_commit_message(self):
"""Generate commit message using AI"""
if self._is_generating:
return
if not self.current_repo_path:
messagebox.showwarning("No Repository", "Please select a repository first!")
return
# Get staged changes
success, diff = self.run_git_command(
["git", "diff", "--cached"],
cwd=self.current_repo_path
)
if not success or not diff.strip():
messagebox.showwarning("No Staged Changes", "No staged changes found. Files have been added, please try again.")
return
provider = self.ai_provider.get()
self.set_status(f"Generating message with {provider}...")
self.log(f"🤖 Generating commit message with {provider}...")
self._is_generating = True
# Run in thread to avoid blocking UI
thread = threading.Thread(target=self._generate_message_thread, args=(diff, provider))
thread.daemon = True
thread.start()
def _generate_message_thread(self, diff: str, provider: str):
"""Generate message in separate thread"""
try:
# Truncate diff to avoid token limits
diff_truncated = diff[:3000] if len(diff) > 3000 else diff
if provider == "gemini":
if not GEMINI_AVAILABLE:
raise ImportError("google-generativeai not installed")
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
raise ValueError("GEMINI_API_KEY not set")
genai.configure(api_key=api_key)
model = genai.GenerativeModel(GEMINI_MODEL)
prompt = f"""Generate a clear commit message following conventional commits format.
Git diff:
```
{diff_truncated}
```
Format: <type>(<scope>): <subject>
Types: feat, fix, docs, style, refactor, test, chore
Use English, be concise. Return only the commit message."""
response = model.generate_content(prompt)
message = response.text.strip()
else: # chatgpt
if not OPENAI_AVAILABLE:
raise ImportError("openai not installed")
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY not set")
client = OpenAI(api_key=api_key)
response = client.chat.completions.create(
model=OPENAI_MODEL,
messages=[
{"role": "system", "content": "You generate git commit messages."},
{"role": "user", "content": f"Generate commit message for:\n{diff_truncated}"}
],
temperature=0.7,
max_tokens=200,
timeout=30
)
message = response.choices[0].message.content.strip()
# Update UI in main thread
self.root.after(0, self._update_message, message)
except Exception as e:
self.root.after(0, self._show_error, str(e))
finally:
# Always reset the flag
self.root.after(0, lambda: setattr(self, '_is_generating', False))
self.root.after(0, lambda: self.set_status("Ready"))
def _update_message(self, message: str):
"""Update commit message (called from main thread)"""
self.message_text.delete(1.0, tk.END)
self.message_text.insert(1.0, message)
self.log("✅ Commit message generated successfully", "success")
self._is_generating = False
self.set_status("Ready")
def _show_error(self, error: str):
"""Show error message (called from main thread)"""
self.log(f"❌ Error: {error}", "error")
messagebox.showerror("Error", f"Failed to generate message:\n{error}")
self._is_generating = False
self.set_status("Ready")
def clear_message(self):
"""Clear commit message"""
self.message_text.delete(1.0, tk.END)
self.log("🗑️ Cleared commit message")
def commit_and_push(self):
"""Commit and push changes"""
self._commit(push=True)
def commit_only(self):
"""Commit without pushing"""
self._commit(push=False)
def _commit(self, push: bool = True):
"""Perform commit"""
if not self.current_repo_path:
messagebox.showwarning("No Repository", "Please select a repository!")
return
message = self.message_text.get(1.0, tk.END).strip()
if not message:
messagebox.showwarning("No Message", "Please enter or generate a commit message!")
return
# Validate message length (Git recommends 50 chars for subject)
first_line = message.split('\n')[0]
if len(first_line) > 72:
response = messagebox.askyesno(
"Long Subject Line",
f"The commit message subject is {len(first_line)} characters (recommended: 50-72).\n\nContinue anyway?"
)
if not response:
return
self.set_status("Committing...")
self.log(f"📝 Committing with message: {first_line[:50]}...")
# Commit
success, output = self.run_git_command(
["git", "commit", "-m", message],
cwd=self.current_repo_path
)
if not success:
self.log(f"❌ Commit failed: {output}", "error")
messagebox.showerror("Commit Failed", f"Failed to commit:\n{output}")
self.set_status("Ready")
return
self.log("✅ Commit successful!", "success")
if push and self.auto_push.get():
self.set_status("Pushing...")
# Get current branch
success, branch = self.run_git_command(
["git", "branch", "--show-current"],
cwd=self.current_repo_path
)
if not success:
self.log("❌ Failed to get branch name", "error")
self.set_status("Ready")
return
branch = branch.strip()
self.log(f"🚀 Pushing to origin/{branch}...")
# Push
success, output = self.run_git_command(
["git", "push", "origin", branch],
cwd=self.current_repo_path
)
if success:
self.log("✅ Push successful!", "success")
messagebox.showinfo("Success", "Commit and push completed successfully!")
else:
self.log(f"❌ Push failed: {output}", "error")
messagebox.showerror("Push Failed", f"Commit successful but push failed:\n{output}")
else:
messagebox.showinfo("Success", "Commit completed successfully!")
self.set_status("Ready")
# Clear message after successful commit
self.clear_message()
# Refresh file list
self.load_changed_files()
def main():
root = tk.Tk()
app = AICommitGUI(root)
root.mainloop()
if __name__ == "__main__":
main()