@@ -6,8 +6,10 @@ import (
6
6
"io"
7
7
"os"
8
8
"os/exec"
9
+ "path/filepath"
9
10
"strings"
10
11
12
+ "al.essio.dev/pkg/shellescape"
11
13
"github.com/coder/aicommit"
12
14
"github.com/coder/pretty"
13
15
"github.com/coder/serpent"
@@ -30,7 +32,7 @@ func debugf(format string, args ...any) {
30
32
}
31
33
// Gray
32
34
c := pretty .FgColor (colorProfile .Color ("#808080" ))
33
- pretty .Fprintf (os .Stderr , c , format , args ... )
35
+ pretty .Fprintf (os .Stderr , c , "debug: " + format + " \n " , args ... )
34
36
}
35
37
36
38
func getLastCommitHash () (string , error ) {
@@ -51,6 +53,16 @@ func resolveRef(ref string) (string, error) {
51
53
return strings .TrimSpace (string (output )), nil
52
54
}
53
55
56
+ func formatShellCommand (cmd * exec.Cmd ) string {
57
+ buf := & strings.Builder {}
58
+ buf .WriteString (filepath .Base (cmd .Path ))
59
+ for _ , arg := range cmd .Args [1 :] {
60
+ buf .WriteString (" " )
61
+ buf .WriteString (shellescape .Quote (arg ))
62
+ }
63
+ return buf .String ()
64
+ }
65
+
54
66
func run (inv * serpent.Invocation , opts runOptions ) error {
55
67
workdir , err := os .Getwd ()
56
68
if err != nil {
@@ -84,7 +96,7 @@ func run(inv *serpent.Invocation, opts runOptions) error {
84
96
ctx := inv .Context ()
85
97
if debugMode {
86
98
for _ , msg := range msgs {
87
- debugf ("%s: (%v tokens)\n %s\n \n " , msg .Role , aicommit .CountTokens (msg ), aicommit . Ellipse ( msg .Content , 100 ) )
99
+ debugf ("%s: (%v tokens)\n %s\n \n " , msg .Role , aicommit .CountTokens (msg ), msg .Content )
88
100
}
89
101
debugf ("prompt includes %d commits\n " , len (msgs )/ 2 )
90
102
}
@@ -118,15 +130,23 @@ func run(inv *serpent.Invocation, opts runOptions) error {
118
130
}
119
131
// Usage is only sent in the last message.
120
132
if resp .Usage != nil {
121
- debugf ("total tokens: %d" , resp .Usage .TotalTokens )
133
+ debugf ("\n total tokens: %d" , resp .Usage .TotalTokens )
122
134
break
123
135
}
124
136
c := resp .Choices [0 ].Delta .Content
125
137
msg .WriteString (c )
126
138
pretty .Fprintf (inv .Stdout , color , "%s" , c )
127
139
}
140
+ inv .Stdout .Write ([]byte ("\n " ))
141
+
142
+ cmd := exec .Command ("git" , "commit" , "-m" , msg .String ())
143
+ if opts .amend {
144
+ cmd .Args = append (cmd .Args , "--amend" )
145
+ }
128
146
129
147
if opts .dryRun {
148
+ fmt .Fprintf (inv .Stdout , "Run the following command to commit:\n " )
149
+ inv .Stdout .Write ([]byte ("" + formatShellCommand (cmd ) + "\n " ))
130
150
return nil
131
151
}
132
152
if opts .ref != "" {
@@ -137,10 +157,6 @@ func run(inv *serpent.Invocation, opts runOptions) error {
137
157
inv .Stdout .Write ([]byte ("\n " ))
138
158
139
159
inv .Stdout .Write ([]byte ("\n " ))
140
- cmd := exec .Command ("git" , "commit" , "-m" , msg .String ())
141
- if opts .amend {
142
- cmd .Args = append (cmd .Args , "--amend" )
143
- }
144
160
145
161
cmd .Stderr = os .Stderr
146
162
cmd .Stdout = os .Stdout
0 commit comments