Skip to content

Commit

Permalink
Add support for user style guide
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
ammario committed Sep 3, 2024
1 parent 418474e commit 12ae7fb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,7 @@ aicommit --save-key

`aicommit` will read the `COMMITS.md` file in the root of the repository to
determine the style guide. It is optional, but if it exists, it will be followed
even if the rules there diverge from the norm.
even if the rules there diverge from the norm.

If there is no repo style guide, `aicommit` will look for a user style guide
in `~/COMMITS.md`.
39 changes: 34 additions & 5 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func findGitRoot(dir string) (string, error) {
}
}

const styleGuideFilename = "COMMITS.md"

// findRepoStyleGuide searches for "COMMITS.md" in the repository root of dir
// and returns its contents.
func findRepoStyleGuide(dir string) (string, error) {
Expand All @@ -92,7 +94,7 @@ func findRepoStyleGuide(dir string) (string, error) {
return "", fmt.Errorf("find git root: %w", err)
}

styleGuide, err := os.ReadFile(filepath.Join(root, "COMMITS.md"))
styleGuide, err := os.ReadFile(filepath.Join(root, styleGuideFilename))
if err != nil {
if os.IsNotExist(err) {
return "", nil
Expand All @@ -102,6 +104,22 @@ func findRepoStyleGuide(dir string) (string, error) {
return string(styleGuide), nil
}

func findUserStyleGuide() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("find user home dir: %w", err)
}
styleGuide, err := os.ReadFile(filepath.Join(home, styleGuideFilename))
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", fmt.Errorf("read user style guide: %w", err)
}

return string(styleGuide), nil
}

func BuildPrompt(
log io.Writer,
dir string,
Expand Down Expand Up @@ -217,16 +235,27 @@ func BuildPrompt(
)

// Add style guide after commit messages so it takes priority.
styleGuide, err := findRepoStyleGuide(dir)
repoStyleGuide, err := findRepoStyleGuide(dir)
if err != nil {
return nil, fmt.Errorf("find style guide: %w", err)
}
if styleGuide != "" {
if repoStyleGuide != "" {
resp = append(resp, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
Content: "This repository has a style guide. Follow it even when " +
"it diverges from the norm.\n" + styleGuide,
"it diverges from the norm.\n" + repoStyleGuide,
})
} else {
userStyleGuide, err := findUserStyleGuide()
if err != nil {
return nil, fmt.Errorf("find user style guide: %w", err)
}
if userStyleGuide != "" {
resp = append(resp, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleSystem,
Content: "This user has a preferred style guide:\n" + userStyleGuide,
})
}
}

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

Expand Down

0 comments on commit 12ae7fb

Please sign in to comment.