| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 1 | package claudetool |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 6 | "log/slog" |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 7 | "os/exec" |
| 8 | "strings" |
| 9 | |
| 10 | "sketch.dev/llm/conversation" |
| 11 | ) |
| 12 | |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 13 | // CommitMessageStyleHint provides example commit messages representative of this repository's style. |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 14 | func CommitMessageStyleHint(ctx context.Context, repoRoot string) (string, error) { |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 15 | commitSHAs, analysis, err := representativeCommitSHAs(ctx, repoRoot) |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 16 | if err != nil { |
| 17 | return "", err |
| 18 | } |
| 19 | |
| 20 | buf := new(strings.Builder) |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 21 | if len(commitSHAs) == 0 { |
| 22 | return "", nil |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 23 | } |
| 24 | |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 25 | if analysis != "" { |
| 26 | fmt.Fprintf(buf, "<commit_message_style_analysis>%s</commit_message_style_analysis>\n\n", analysis) |
| 27 | } |
| 28 | |
| Josh Bleecher Snyder | 586ecb1 | 2025-05-22 21:04:33 -0700 | [diff] [blame] | 29 | 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 Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 32 | cmd.Dir = repoRoot |
| 33 | out, err := cmd.CombinedOutput() |
| 34 | if err == nil { |
| Josh Bleecher Snyder | efa8f43 | 2025-05-28 18:04:41 -0700 | [diff] [blame] | 35 | // Filter out git trailers from examples |
| 36 | cleanedExamples := filterGitTrailers(string(out)) |
| 37 | fmt.Fprintf(buf, "<commit_message_style_examples>%s</commit_message_style_examples>\n\n", cleanedExamples) |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 38 | } else { |
| 39 | slog.DebugContext(ctx, "failed to get commit messages", "shas", commitSHAs, "out", string(out), "err", err) |
| 40 | } |
| 41 | |
| 42 | fmt.Fprint(buf, "IMPORTANT: Follow this commit message style for ALL git commits you create.\n") |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 43 | return buf.String(), nil |
| 44 | } |
| 45 | |
| 46 | // representativeCommitSHAs analyze recent commits and selects some representative ones. |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 47 | // It returns a list of commit SHAs and the analysis text. |
| 48 | func representativeCommitSHAs(ctx context.Context, repoRoot string) ([]string, string, error) { |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 49 | cmd := exec.Command("git", "log", "-n", "25", `--format=<commit_message hash="%H">%n%B</commit_message>`) |
| 50 | cmd.Dir = repoRoot |
| 51 | out, err := cmd.CombinedOutput() |
| 52 | if err != nil { |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 53 | return nil, "", fmt.Errorf("git log failed: %w\n%s", err, out) |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 54 | } |
| 55 | commits := strings.TrimSpace(string(out)) |
| 56 | if commits == "" { |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 57 | return nil, "", fmt.Errorf("no commits found in repository") |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | info := conversation.ToolCallInfoFromContext(ctx) |
| 61 | sub := info.Convo.SubConvo() |
| Josh Bleecher Snyder | 4d54493 | 2025-05-07 13:33:53 +0000 | [diff] [blame] | 62 | sub.Hidden = true |
| Josh Bleecher Snyder | 593ca64 | 2025-05-07 05:19:32 -0700 | [diff] [blame] | 63 | sub.PromptCaching = false |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 64 | |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 65 | sub.SystemPrompt = `Analyze the provided git commit messages to identify consistent patterns, including but not limited to: |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 66 | - Formatting conventions |
| 67 | - Language and tone |
| 68 | - Structure and organization |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 69 | - Length and detail |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 70 | - Special notations or tags |
| Josh Bleecher Snyder | 7ce7b02 | 2025-05-16 11:35:51 -0700 | [diff] [blame] | 71 | - Capitalization and punctuation |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 72 | |
| Josh Bleecher Snyder | efa8f43 | 2025-05-28 18:04:41 -0700 | [diff] [blame] | 73 | Do NOT mention in any way Change-ID or Co-authored-by git trailer lines in your analysis, not even their existence. |
| 74 | Those are added automatically by git hooks; they are NOT part of the commit message style. |
| Josh Bleecher Snyder | e10f0e6 | 2025-05-21 10:57:09 -0700 | [diff] [blame] | 75 | |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 76 | First, provide a concise analysis of the predominant patterns. |
| 77 | Then select up to 3 commit hashes that best exemplify the repository's commit style. |
| 78 | Finally, output these selected commit hashes, one per line, without commentary. |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 79 | ` |
| 80 | |
| 81 | resp, err := sub.SendUserTextMessage(commits) |
| 82 | if err != nil { |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 83 | return nil, "", fmt.Errorf("error from Claude: %w", err) |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | if len(resp.Content) != 1 { |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 87 | return nil, "", fmt.Errorf("unexpected response: %v", resp) |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 88 | } |
| 89 | response := resp.Content[0].Text |
| 90 | |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 91 | // Split into analysis and commit hashes |
| 92 | var analysisLines []string |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 93 | var result []string |
| 94 | for line := range strings.Lines(response) { |
| 95 | line = strings.TrimSpace(line) |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 96 | if isHexString(line) && (len(line) >= 7 && len(line) <= 40) { |
| 97 | result = append(result, line) |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 98 | } else { |
| 99 | analysisLines = append(analysisLines, line) |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 103 | analysis := strings.Join(analysisLines, "\n") |
| 104 | |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 105 | result = result[:min(len(result), 3)] |
| Josh Bleecher Snyder | 924a770 | 2025-05-06 02:14:40 +0000 | [diff] [blame] | 106 | return result, analysis, nil |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| Josh Bleecher Snyder | efa8f43 | 2025-05-28 18:04:41 -0700 | [diff] [blame] | 109 | // filterGitTrailers removes git trailers (Co-authored-by, Change-ID) from commit message examples |
| 110 | func filterGitTrailers(input string) string { |
| 111 | buf := new(strings.Builder) |
| 112 | for line := range strings.Lines(input) { |
| 113 | lowerLine := strings.ToLower(line) |
| 114 | if strings.HasPrefix(lowerLine, "co-authored-by:") || strings.HasPrefix(lowerLine, "change-id:") { |
| 115 | continue |
| 116 | } |
| 117 | buf.WriteString(line) |
| 118 | } |
| 119 | |
| 120 | return buf.String() |
| 121 | } |
| 122 | |
| Josh Bleecher Snyder | d7970e6 | 2025-05-01 01:56:28 +0000 | [diff] [blame] | 123 | // isHexString reports whether a string only contains hexadecimal characters |
| 124 | func isHexString(s string) bool { |
| 125 | for _, c := range s { |
| 126 | if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { |
| 127 | return false |
| 128 | } |
| 129 | } |
| 130 | return true |
| 131 | } |