2
2
from rich .console import Console
3
3
from rich .panel import Panel
4
4
from pathlib import Path
5
- from typing import List
6
5
from ..core import config , git_utils , llm , editor
7
6
8
7
console = Console ()
9
8
9
+
10
10
def commit (ctx : typer .Context ):
11
11
"""
12
12
Generates a commit message for staged changes, accepting git commit options.
13
-
13
+
14
14
All unknown options will be passed directly to the underlying 'git commit' command.
15
15
"""
16
16
cfg = config .load_config ()
17
17
gemini_api_key = cfg .get ("api_keys" , {}).get ("gemini" , "" )
18
18
if "YOUR_GEMINI_API_KEY" in gemini_api_key or not gemini_api_key :
19
- console .print ("[red]Error: Gemini API key not found or not set in config.toml[/red]" )
19
+ console .print (
20
+ "[red]Error: Gemini API key not found or not set in config.toml[/red]"
21
+ )
20
22
raise typer .Exit (1 )
21
23
22
24
repo = git_utils .get_repo ()
@@ -25,7 +27,7 @@ def commit(ctx: typer.Context):
25
27
raise typer .Exit (1 )
26
28
27
29
passthrough_args = ctx .args
28
-
30
+
29
31
use_all = "--all" in passthrough_args or "-a" in passthrough_args
30
32
use_amend = "--amend" in passthrough_args
31
33
@@ -35,31 +37,37 @@ def commit(ctx: typer.Context):
35
37
diff = git_utils .get_staged_diff (repo )
36
38
37
39
if not diff .strip () and not use_amend :
38
- console .print ("[yellow]No changes to commit. Please stage your changes or use --all.[/yellow]" )
40
+ console .print (
41
+ "[yellow]No changes to commit. Please stage your changes or use --all.[/yellow]"
42
+ )
39
43
raise typer .Exit ()
40
44
41
45
try :
42
- prompt_paths = cfg [' prompt_paths' ]
43
- system_prompt_path = Path (prompt_paths [' system_commit' ]).expanduser ()
44
- user_prompt_path = Path (prompt_paths [' user_commit' ]).expanduser ()
46
+ prompt_paths = cfg [" prompt_paths" ]
47
+ system_prompt_path = Path (prompt_paths [" system_commit" ]).expanduser ()
48
+ user_prompt_path = Path (prompt_paths [" user_commit" ]).expanduser ()
45
49
46
- with open (system_prompt_path , 'r' , encoding = ' utf-8' ) as f :
50
+ with open (system_prompt_path , "r" , encoding = " utf-8" ) as f :
47
51
system_prompt = f .read ()
48
- with open (user_prompt_path , 'r' , encoding = ' utf-8' ) as f :
52
+ with open (user_prompt_path , "r" , encoding = " utf-8" ) as f :
49
53
user_prompt = f .read ()
50
54
except (KeyError , FileNotFoundError ) as e :
51
55
console .print (f"[red]Error loading prompt files: { e } [/red]" )
52
56
raise typer .Exit (1 )
53
57
54
58
final_user_prompt = f"{ user_prompt } \n \n --- Git Diff ---\n { diff } "
55
-
59
+
56
60
if use_amend :
57
61
try :
58
62
last_message = git_utils .get_last_commit_message (repo )
59
- final_user_prompt += f"\n \n --- Previous Commit Message (to amend) ---\n { last_message } "
63
+ final_user_prompt += (
64
+ f"\n \n --- Previous Commit Message (to amend) ---\n { last_message } "
65
+ )
60
66
system_prompt += "\n \n You are amending a previous commit. Refine the provided message based on the new diff."
61
67
except Exception :
62
- console .print ("[yellow]Could not find previous commit to amend. Proceeding without it.[/yellow]" )
68
+ console .print (
69
+ "[yellow]Could not find previous commit to amend. Proceeding without it.[/yellow]"
70
+ )
63
71
64
72
console .print ("[cyan]Generating commit message from LLM...[/cyan]" )
65
73
try :
@@ -69,21 +77,28 @@ def commit(ctx: typer.Context):
69
77
raise typer .Exit (1 )
70
78
71
79
while True :
72
- console .print (Panel (commit_msg , title = "[bold green]Generated Commit Message[/bold green]" , border_style = "green" , expand = False ))
80
+ console .print (
81
+ Panel (
82
+ commit_msg ,
83
+ title = "[bold green]Generated Commit Message[/bold green]" ,
84
+ border_style = "green" ,
85
+ expand = False ,
86
+ )
87
+ )
73
88
action = typer .prompt ("Adopt this message? (y/n/e) [e]dit" , default = "y" ).lower ()
74
89
75
- if action == 'y' :
90
+ if action == "y" :
76
91
try :
77
92
git_utils .commit (commit_msg , passthrough_args )
78
93
console .print ("[bold green]Successfully committed.[/bold green]" )
79
94
except Exception as e :
80
95
console .print (f"[bold red]Failed to commit: { e } [/red]" )
81
96
raise typer .Exit (1 )
82
97
break
83
- elif action == 'e' :
98
+ elif action == "e" :
84
99
editor_command = editor .get_editor (cfg )
85
100
commit_msg = editor .edit_content (commit_msg , editor_command )
86
101
console .print ("[cyan]Content updated. Please review.[/cyan]" )
87
102
else :
88
103
console .print ("[yellow]Operation cancelled.[/yellow]" )
89
- break
104
+ break
0 commit comments