blob: 306c7b95170404134089a3cf1fbd310ced745d0a [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
29 cmd := exec.Command("git", "show", "-s", "--format='<commit_message_style_example>%n%B</commit_message_style_example>'", strings.Join(commitSHAs, " "))
30 cmd.Dir = repoRoot
31 out, err := cmd.CombinedOutput()
32 if err == nil {
33 fmt.Fprintf(buf, "<commit_message_style_examples>%s</commit_message_style_examples>\n\n", out)
34 } else {
35 slog.DebugContext(ctx, "failed to get commit messages", "shas", commitSHAs, "out", string(out), "err", err)
36 }
37
38 fmt.Fprint(buf, "IMPORTANT: Follow this commit message style for ALL git commits you create.\n")
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000039 return buf.String(), nil
40}
41
42// representativeCommitSHAs analyze recent commits and selects some representative ones.
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000043// It returns a list of commit SHAs and the analysis text.
44func representativeCommitSHAs(ctx context.Context, repoRoot string) ([]string, string, error) {
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000045 cmd := exec.Command("git", "log", "-n", "25", `--format=<commit_message hash="%H">%n%B</commit_message>`)
46 cmd.Dir = repoRoot
47 out, err := cmd.CombinedOutput()
48 if err != nil {
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000049 return nil, "", fmt.Errorf("git log failed: %w\n%s", err, out)
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000050 }
51 commits := strings.TrimSpace(string(out))
52 if commits == "" {
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000053 return nil, "", fmt.Errorf("no commits found in repository")
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000054 }
55
56 info := conversation.ToolCallInfoFromContext(ctx)
57 sub := info.Convo.SubConvo()
Josh Bleecher Snyder593ca642025-05-07 05:19:32 -070058 sub.PromptCaching = false
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000059
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000060 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 +000061- Formatting conventions
62- Language and tone
63- Structure and organization
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000064- Length and detail
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000065- Special notations or tags
66
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000067First, provide a concise analysis of the predominant patterns.
68Then select up to 3 commit hashes that best exemplify the repository's commit style.
69Finally, output these selected commit hashes, one per line, without commentary.
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000070`
71
72 resp, err := sub.SendUserTextMessage(commits)
73 if err != nil {
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000074 return nil, "", fmt.Errorf("error from Claude: %w", err)
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000075 }
76
77 if len(resp.Content) != 1 {
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000078 return nil, "", fmt.Errorf("unexpected response: %v", resp)
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000079 }
80 response := resp.Content[0].Text
81
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000082 // Split into analysis and commit hashes
83 var analysisLines []string
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000084 var result []string
85 for line := range strings.Lines(response) {
86 line = strings.TrimSpace(line)
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000087 if isHexString(line) && (len(line) >= 7 && len(line) <= 40) {
88 result = append(result, line)
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000089 } else {
90 analysisLines = append(analysisLines, line)
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000091 }
92 }
93
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000094 analysis := strings.Join(analysisLines, "\n")
95
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000096 result = result[:min(len(result), 3)]
Josh Bleecher Snyder924a7702025-05-06 02:14:40 +000097 return result, analysis, nil
Josh Bleecher Snyderd7970e62025-05-01 01:56:28 +000098}
99
100// isHexString reports whether a string only contains hexadecimal characters
101func isHexString(s string) bool {
102 for _, c := range s {
103 if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
104 return false
105 }
106 }
107 return true
108}