@@ -84,6 +84,8 @@ func findGitRoot(dir string) (string, error) {
84
84
}
85
85
}
86
86
87
+ const styleGuideFilename = "COMMITS.md"
88
+
87
89
// findRepoStyleGuide searches for "COMMITS.md" in the repository root of dir
88
90
// and returns its contents.
89
91
func findRepoStyleGuide (dir string ) (string , error ) {
@@ -92,7 +94,7 @@ func findRepoStyleGuide(dir string) (string, error) {
92
94
return "" , fmt .Errorf ("find git root: %w" , err )
93
95
}
94
96
95
- styleGuide , err := os .ReadFile (filepath .Join (root , "COMMITS.md" ))
97
+ styleGuide , err := os .ReadFile (filepath .Join (root , styleGuideFilename ))
96
98
if err != nil {
97
99
if os .IsNotExist (err ) {
98
100
return "" , nil
@@ -102,6 +104,22 @@ func findRepoStyleGuide(dir string) (string, error) {
102
104
return string (styleGuide ), nil
103
105
}
104
106
107
+ func findUserStyleGuide () (string , error ) {
108
+ home , err := os .UserHomeDir ()
109
+ if err != nil {
110
+ return "" , fmt .Errorf ("find user home dir: %w" , err )
111
+ }
112
+ styleGuide , err := os .ReadFile (filepath .Join (home , styleGuideFilename ))
113
+ if err != nil {
114
+ if os .IsNotExist (err ) {
115
+ return "" , nil
116
+ }
117
+ return "" , fmt .Errorf ("read user style guide: %w" , err )
118
+ }
119
+
120
+ return string (styleGuide ), nil
121
+ }
122
+
105
123
func BuildPrompt (
106
124
log io.Writer ,
107
125
dir string ,
@@ -217,16 +235,27 @@ func BuildPrompt(
217
235
)
218
236
219
237
// Add style guide after commit messages so it takes priority.
220
- styleGuide , err := findRepoStyleGuide (dir )
238
+ repoStyleGuide , err := findRepoStyleGuide (dir )
221
239
if err != nil {
222
240
return nil , fmt .Errorf ("find style guide: %w" , err )
223
241
}
224
- if styleGuide != "" {
242
+ if repoStyleGuide != "" {
225
243
resp = append (resp , openai.ChatCompletionMessage {
226
244
Role : openai .ChatMessageRoleSystem ,
227
245
Content : "This repository has a style guide. Follow it even when " +
228
- "it diverges from the norm.\n " + styleGuide ,
246
+ "it diverges from the norm.\n " + repoStyleGuide ,
229
247
})
248
+ } else {
249
+ userStyleGuide , err := findUserStyleGuide ()
250
+ if err != nil {
251
+ return nil , fmt .Errorf ("find user style guide: %w" , err )
252
+ }
253
+ if userStyleGuide != "" {
254
+ resp = append (resp , openai.ChatCompletionMessage {
255
+ Role : openai .ChatMessageRoleSystem ,
256
+ Content : "This user has a preferred style guide:\n " + userStyleGuide ,
257
+ })
258
+ }
230
259
}
231
260
232
261
resp = append (resp , openai.ChatCompletionMessage {
@@ -266,7 +295,7 @@ func generateDiff(w io.Writer, dir string, refName string, amend bool) error {
266
295
// Run the git command and return any execution errors
267
296
err := cmd .Run ()
268
297
if err != nil {
269
- return fmt .Errorf ("Running %s %s: %w\n %s" ,
298
+ return fmt .Errorf ("running %s %s: %w\n %s" ,
270
299
cmd .Args [0 ], strings .Join (cmd .Args [1 :], " " ), err , errBuf .String ())
271
300
}
272
301
0 commit comments