Skip to content

Commit 6304db1

Browse files
committed
Show candidate command-line invocation on dry run
1 parent b5de34e commit 6304db1

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

cmd/aicommit/main.go

+23-7
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"io"
77
"os"
88
"os/exec"
9+
"path/filepath"
910
"strings"
1011

12+
"al.essio.dev/pkg/shellescape"
1113
"github.com/coder/aicommit"
1214
"github.com/coder/pretty"
1315
"github.com/coder/serpent"
@@ -30,7 +32,7 @@ func debugf(format string, args ...any) {
3032
}
3133
// Gray
3234
c := pretty.FgColor(colorProfile.Color("#808080"))
33-
pretty.Fprintf(os.Stderr, c, format, args...)
35+
pretty.Fprintf(os.Stderr, c, "debug: "+format+"\n", args...)
3436
}
3537

3638
func getLastCommitHash() (string, error) {
@@ -51,6 +53,16 @@ func resolveRef(ref string) (string, error) {
5153
return strings.TrimSpace(string(output)), nil
5254
}
5355

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+
5466
func run(inv *serpent.Invocation, opts runOptions) error {
5567
workdir, err := os.Getwd()
5668
if err != nil {
@@ -84,7 +96,7 @@ func run(inv *serpent.Invocation, opts runOptions) error {
8496
ctx := inv.Context()
8597
if debugMode {
8698
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)
88100
}
89101
debugf("prompt includes %d commits\n", len(msgs)/2)
90102
}
@@ -118,15 +130,23 @@ func run(inv *serpent.Invocation, opts runOptions) error {
118130
}
119131
// Usage is only sent in the last message.
120132
if resp.Usage != nil {
121-
debugf("total tokens: %d", resp.Usage.TotalTokens)
133+
debugf("\ntotal tokens: %d", resp.Usage.TotalTokens)
122134
break
123135
}
124136
c := resp.Choices[0].Delta.Content
125137
msg.WriteString(c)
126138
pretty.Fprintf(inv.Stdout, color, "%s", c)
127139
}
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+
}
128146

129147
if opts.dryRun {
148+
fmt.Fprintf(inv.Stdout, "Run the following command to commit:\n")
149+
inv.Stdout.Write([]byte("" + formatShellCommand(cmd) + "\n"))
130150
return nil
131151
}
132152
if opts.ref != "" {
@@ -137,10 +157,6 @@ func run(inv *serpent.Invocation, opts runOptions) error {
137157
inv.Stdout.Write([]byte("\n"))
138158

139159
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-
}
144160

145161
cmd.Stderr = os.Stderr
146162
cmd.Stdout = os.Stdout

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require (
3434
)
3535

3636
require (
37+
al.essio.dev/pkg/shellescape v1.5.0
3738
cdr.dev/slog v1.6.2-0.20240126064726-20367d4aede6 // indirect
3839
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
3940
github.com/go-git/go-git/v5 v5.12.0

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
al.essio.dev/pkg/shellescape v1.5.0 h1:7oTvSsQ5kg9WksA9O58y9wjYnY4jP0CL82/Q8WLUGKk=
2+
al.essio.dev/pkg/shellescape v1.5.0/go.mod h1:6sIqp7X2P6mThCQ7twERpZTuigpr6KbZWtls1U8I890=
13
cdr.dev/slog v1.6.2-0.20240126064726-20367d4aede6 h1:KHblWIE/KHOwQ6lEbMZt6YpcGve2FEZ1sDtrW1Am5UI=
24
cdr.dev/slog v1.6.2-0.20240126064726-20367d4aede6/go.mod h1:NaoTA7KwopCrnaSb0JXTC0PTp/O/Y83Lndnq0OEV3ZQ=
35
cloud.google.com/go/compute v1.23.0 h1:tP41Zoavr8ptEqaW6j+LQOnyBBhO7OkOMAGrgLopTwY=
@@ -63,6 +65,8 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg
6365
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
6466
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
6567
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
68+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
69+
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
6670
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
6771
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
6872
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=

prompt.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ func BuildPrompt(log io.Writer, dir string,
7373
Content: "You are a helpful assistant that generates commit messages for git diffs." +
7474
"Generate nothing but the commit message. Do not include any other text." +
7575
"Commit messages should have a maximum column width of 100 characters." +
76-
"Extended descriptions go on a new line. Follow the style of the existing commit messages.",
76+
"Extended descriptions go on a new line. Follow the style of the existing commit messages." +
77+
"Each message is an individual diff for and individual commit. Do not describe changes from previous commits" +
78+
"in the new commit message.",
7779
},
7880
}
7981

0 commit comments

Comments
 (0)