Skip to content

Commit 12ae7fb

Browse files
committed
Add support for user style guide
- Implement `findUserStyleGuide` to locate `~/COMMITS.md`. - Adjust `BuildPrompt` to prefer repo style guide, fall back to user style guide. - Update README with instructions on user style guide. Resolves #4
1 parent 418474e commit 12ae7fb

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,7 @@ aicommit --save-key
7373

7474
`aicommit` will read the `COMMITS.md` file in the root of the repository to
7575
determine the style guide. It is optional, but if it exists, it will be followed
76-
even if the rules there diverge from the norm.
76+
even if the rules there diverge from the norm.
77+
78+
If there is no repo style guide, `aicommit` will look for a user style guide
79+
in `~/COMMITS.md`.

prompt.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ func findGitRoot(dir string) (string, error) {
8484
}
8585
}
8686

87+
const styleGuideFilename = "COMMITS.md"
88+
8789
// findRepoStyleGuide searches for "COMMITS.md" in the repository root of dir
8890
// and returns its contents.
8991
func findRepoStyleGuide(dir string) (string, error) {
@@ -92,7 +94,7 @@ func findRepoStyleGuide(dir string) (string, error) {
9294
return "", fmt.Errorf("find git root: %w", err)
9395
}
9496

95-
styleGuide, err := os.ReadFile(filepath.Join(root, "COMMITS.md"))
97+
styleGuide, err := os.ReadFile(filepath.Join(root, styleGuideFilename))
9698
if err != nil {
9799
if os.IsNotExist(err) {
98100
return "", nil
@@ -102,6 +104,22 @@ func findRepoStyleGuide(dir string) (string, error) {
102104
return string(styleGuide), nil
103105
}
104106

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+
105123
func BuildPrompt(
106124
log io.Writer,
107125
dir string,
@@ -217,16 +235,27 @@ func BuildPrompt(
217235
)
218236

219237
// Add style guide after commit messages so it takes priority.
220-
styleGuide, err := findRepoStyleGuide(dir)
238+
repoStyleGuide, err := findRepoStyleGuide(dir)
221239
if err != nil {
222240
return nil, fmt.Errorf("find style guide: %w", err)
223241
}
224-
if styleGuide != "" {
242+
if repoStyleGuide != "" {
225243
resp = append(resp, openai.ChatCompletionMessage{
226244
Role: openai.ChatMessageRoleSystem,
227245
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,
229247
})
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+
}
230259
}
231260

232261
resp = append(resp, openai.ChatCompletionMessage{
@@ -266,7 +295,7 @@ func generateDiff(w io.Writer, dir string, refName string, amend bool) error {
266295
// Run the git command and return any execution errors
267296
err := cmd.Run()
268297
if err != nil {
269-
return fmt.Errorf("Running %s %s: %w\n%s",
298+
return fmt.Errorf("running %s %s: %w\n%s",
270299
cmd.Args[0], strings.Join(cmd.Args[1:], " "), err, errBuf.String())
271300
}
272301

0 commit comments

Comments
 (0)