blob: d5b589183238bca8c634a41314972fb59b423e85 [file] [log] [blame]
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +00001package claudetool
2
3import (
4 "context"
5 "fmt"
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +00006 "log/slog"
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +00007 "os/exec"
8 "strings"
9
10 "sketch.dev/llm/conversation"
11)
12
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000013// CommitMessageStyleHint provides example commit messages representative of this repository's style.
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000014func CommitMessageStyleHint(ctx context.Context, repoRoot string) (string, error) {
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000015 commitSHAs, analysis, err := representativeCommitSHAs(ctx, repoRoot)
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000016 if err != nil {
17 return "", err
18 }
19
20 buf := new(strings.Builder)
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000021 if len(commitSHAs) == 0 {
22 return "", nil
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000023 }
24
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000025 if analysis != "" {
26 fmt.Fprintf(buf, "<commit_message_style_analysis>%s</commit_message_style_analysis>\n\n", analysis)
27 }
28
Josh Bleecher Snyder586ecb12025-05-22 21:04:33 -070029 args := []string{"show", "-s", "--format='<commit_message_style_example>%n%B</commit_message_style_example>'"}
30 args = append(args, commitSHAs...)
31 cmd := exec.Command("git", args...)
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000032 cmd.Dir = repoRoot
33 out, err := cmd.CombinedOutput()
34 if err == nil {
35 fmt.Fprintf(buf, "<commit_message_style_examples>%s</commit_message_style_examples>\n\n", out)
36 } else {
37 slog.DebugContext(ctx, "failed to get commit messages", "shas", commitSHAs, "out", string(out), "err", err)
38 }
39
40 fmt.Fprint(buf, "IMPORTANT: Follow this commit message style for ALL git commits you create.\n")
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000041 return buf.String(), nil
42}
43
44// representativeCommitSHAs analyze recent commits and selects some representative ones.
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000045// It returns a list of commit SHAs and the analysis text.
46func representativeCommitSHAs(ctx context.Context, repoRoot string) ([]string, string, error) {
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000047 cmd := exec.Command("git", "log", "-n", "25", `--format=<commit_message hash="%H">%n%B</commit_message>`)
48 cmd.Dir = repoRoot
49 out, err := cmd.CombinedOutput()
50 if err != nil {
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000051 return nil, "", fmt.Errorf("git log failed: %w\n%s", err, out)
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000052 }
53 commits := strings.TrimSpace(string(out))
54 if commits == "" {
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000055 return nil, "", fmt.Errorf("no commits found in repository")
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000056 }
57
58 info := conversation.ToolCallInfoFromContext(ctx)
59 sub := info.Convo.SubConvo()
Josh Bleecher Snyder4d544932025-05-07 13:33:53 +000060 sub.Hidden = true
Josh Bleecher Snyder593ca642025-05-07 05:19:32 -070061 sub.PromptCaching = false
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000062
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000063 sub.SystemPrompt = `Analyze the provided git commit messages to identify consistent patterns, including but not limited to:
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000064- Formatting conventions
65- Language and tone
66- Structure and organization
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000067- Length and detail
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000068- Special notations or tags
Josh Bleecher Snyder7ce7b022025-05-16 11:35:51 -070069- Capitalization and punctuation
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000070
Josh Bleecher Snydere10f0e62025-05-21 10:57:09 -070071Ignore Change-ID and Co-authored-by lines in your analysis.
72
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000073First, provide a concise analysis of the predominant patterns.
74Then select up to 3 commit hashes that best exemplify the repository's commit style.
75Finally, output these selected commit hashes, one per line, without commentary.
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000076`
77
78 resp, err := sub.SendUserTextMessage(commits)
79 if err != nil {
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000080 return nil, "", fmt.Errorf("error from Claude: %w", err)
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000081 }
82
83 if len(resp.Content) != 1 {
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000084 return nil, "", fmt.Errorf("unexpected response: %v", resp)
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000085 }
86 response := resp.Content[0].Text
87
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000088 // Split into analysis and commit hashes
89 var analysisLines []string
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000090 var result []string
91 for line := range strings.Lines(response) {
92 line = strings.TrimSpace(line)
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000093 if isHexString(line) && (len(line) >= 7 && len(line) <= 40) {
94 result = append(result, line)
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000095 } else {
96 analysisLines = append(analysisLines, line)
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000097 }
98 }
99
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +0000100 analysis := strings.Join(analysisLines, "\n")
101
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +0000102 result = result[:min(len(result), 3)]
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +0000103 return result, analysis, nil
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +0000104}
105
106// isHexString reports whether a string only contains hexadecimal characters
107func isHexString(s string) bool {
108 for _, c := range s {
109 if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
110 return false
111 }
112 }
113 return true
114}