Skip to content

Commit 13abff4

Browse files
committed
Implement auto-commit and push with improved AI requests
1 parent 49c9aa2 commit 13abff4

File tree

13 files changed

+123
-61
lines changed

13 files changed

+123
-61
lines changed

cmd/ask.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/chand1012/ottodocs/pkg/ai"
1616
"github.com/chand1012/ottodocs/pkg/config"
17+
"github.com/chand1012/ottodocs/pkg/git"
1718
"github.com/chand1012/ottodocs/pkg/utils"
1819
)
1920

@@ -66,7 +67,7 @@ Requires a path to a repository or file as a positional argument.`,
6667
}
6768

6869
// index the repo
69-
repo, err := utils.GetRepo(repoPath, ignoreFilePath, ignoreGitignore)
70+
repo, err := git.GetRepo(repoPath, ignoreFilePath, ignoreGitignore)
7071
if err != nil {
7172
log.Errorf("Error processing repo: %s", err)
7273
os.Exit(1)

cmd/commit.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/chand1012/ottodocs/pkg/ai"
1212
"github.com/chand1012/ottodocs/pkg/config"
13+
"github.com/chand1012/ottodocs/pkg/git"
1314
"github.com/spf13/cobra"
1415
)
1516

@@ -27,6 +28,8 @@ var commitCmd = &cobra.Command{
2728
os.Exit(1)
2829
}
2930

31+
log.Info("Generating commit message...")
32+
3033
c := exec.Command("git", "diff")
3134
diffBytes, err := c.Output()
3235
if err != nil {
@@ -42,7 +45,36 @@ var commitCmd = &cobra.Command{
4245
os.Exit(1)
4346
}
4447

45-
fmt.Println("Commit message:", msg)
48+
if auto || push {
49+
log.Info("Adding and committing...")
50+
output, err := git.AddAll()
51+
if err != nil {
52+
log.Error(err)
53+
os.Exit(1)
54+
}
55+
fmt.Println(output)
56+
output, err = git.Commit(msg)
57+
if err != nil {
58+
log.Error(err)
59+
os.Exit(1)
60+
}
61+
fmt.Println(output)
62+
if push {
63+
log.Info("Pushing...")
64+
output, err = git.Push()
65+
if err != nil {
66+
log.Error(err)
67+
os.Exit(1)
68+
}
69+
fmt.Println(output)
70+
}
71+
}
72+
73+
if plain {
74+
fmt.Println(msg)
75+
} else {
76+
fmt.Println("Commit message:", msg)
77+
}
4678
},
4779
}
4880

@@ -51,4 +83,6 @@ func init() {
5183

5284
commitCmd.Flags().BoolVarP(&conventional, "conventional", "c", false, "use conventional commits")
5385
commitCmd.Flags().BoolVarP(&plain, "plain", "p", false, "no output formatting")
86+
commitCmd.Flags().BoolVarP(&auto, "auto", "a", false, "automatically add all and commit with the generated message")
87+
commitCmd.Flags().BoolVar(&push, "push", false, "automatically push to the current branch")
5488
}

cmd/docs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/chand1012/ottodocs/pkg/ai"
1212
"github.com/chand1012/ottodocs/pkg/config"
13+
"github.com/chand1012/ottodocs/pkg/git"
1314
"github.com/chand1012/ottodocs/pkg/utils"
1415
"github.com/spf13/cobra"
1516
)
@@ -63,7 +64,7 @@ search for files in the directory and document them. If a single file is specifi
6364
}
6465

6566
if info.IsDir() {
66-
repo, err := utils.GetRepo(repoPath, ignoreFilePath, ignoreGitignore)
67+
repo, err := git.GetRepo(repoPath, ignoreFilePath, ignoreGitignore)
6768
if err != nil {
6869
log.Errorf("Error: %s", err)
6970
os.Exit(1)

cmd/setModel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
2+
Copyright © 2023 Chandler <chandler@chand1012.dev>
33
*/
44
package cmd
55

cmd/vars.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var overwriteOriginal bool
2222

2323
var conventional bool // use conventional commits
2424
var plain bool
25+
var auto bool
26+
var push bool
2527

2628
var log = l.NewWithOptions(os.Stderr, l.Options{
2729
Level: l.InfoLevel,

cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
2+
Copyright © 2023 Chandler <chandler@chand1012.dev>
33
*/
44
package cmd
55

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ require (
5858
github.com/pjbgf/sha1cd v0.3.0 // indirect
5959
github.com/rivo/uniseg v0.2.0 // indirect
6060
github.com/russross/blackfriday/v2 v2.1.0 // indirect
61+
github.com/sashabaranov/go-openai v1.9.0 // indirect
6162
github.com/sergi/go-diff v1.3.1 // indirect
6263
github.com/skeema/knownhosts v1.1.0 // indirect
6364
github.com/spf13/pflag v1.0.5 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
204204
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
205205
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
206206
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
207+
github.com/sashabaranov/go-openai v1.9.0 h1:NoiO++IISxxJ1pRc0n7uZvMGMake0G+FJ1XPwXtprsA=
208+
github.com/sashabaranov/go-openai v1.9.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
207209
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
208210
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
209211
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=

pkg/ai/req.go

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,40 @@
11
package ai
22

33
import (
4+
"context"
45
"errors"
56

6-
gopenai "github.com/CasualCodersProjects/gopenai"
7-
ai_types "github.com/CasualCodersProjects/gopenai/types"
8-
"github.com/chand1012/ottodocs/pkg/calc"
97
"github.com/chand1012/ottodocs/pkg/config"
8+
"github.com/sashabaranov/go-openai"
109
)
1110

1211
func request(systemMsg, userMsg string, conf *config.Config) (string, error) {
1312

14-
openai := gopenai.NewOpenAI(&gopenai.OpenAIOpts{
15-
APIKey: conf.APIKey,
16-
})
17-
18-
messages := []ai_types.ChatMessage{
19-
{
20-
Content: systemMsg,
21-
Role: "system",
22-
},
23-
{
24-
Content: userMsg,
25-
Role: "user",
13+
c := openai.NewClient(conf.APIKey)
14+
ctx := context.Background()
15+
16+
req := openai.ChatCompletionRequest{
17+
Model: conf.Model,
18+
Messages: []openai.ChatCompletionMessage{
19+
{
20+
Content: systemMsg,
21+
Role: openai.ChatMessageRoleSystem,
22+
},
23+
{
24+
Content: userMsg,
25+
Role: openai.ChatMessageRoleUser,
26+
},
2627
},
2728
}
2829

29-
tokens, err := calc.PreciseTokens(messages[0].Content, messages[1].Content)
30-
if err != nil {
31-
return "", err
32-
}
33-
34-
req := ai_types.NewDefaultChatRequest("")
35-
req.Messages = messages
36-
req.MaxTokens = calc.GetMaxTokens(conf.Model) - tokens
37-
req.Model = conf.Model
38-
39-
if req.MaxTokens < 0 {
40-
return "", errors.New("the prompt is too long")
41-
}
42-
43-
resp, err := openai.CreateChat(req)
30+
resp, err := c.CreateChatCompletion(ctx, req)
4431
if err != nil {
4532
return "", err
4633
}
4734

4835
if len(resp.Choices) == 0 {
49-
return "", errors.New("no choices returned. Check your OpenAI API key")
36+
return "", errors.New("no choices returned")
5037
}
5138

52-
message := resp.Choices[0].Message.Content
53-
54-
return message, nil
39+
return resp.Choices[0].Message.Content, nil
5540
}

pkg/git/commit.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package git
2+
3+
// git add -A
4+
func AddAll() (string, error) {
5+
return git("add", "-A")
6+
}
7+
8+
// git commit -am <message>
9+
func Commit(message string) (string, error) {
10+
return git("commit", "-am", message)
11+
}
12+
13+
// git push
14+
func Push() (string, error) {
15+
return git("push")
16+
}

0 commit comments

Comments
 (0)