|
| 1 | +import { exec } from "child_process" |
| 2 | +import { promisify } from "util" |
| 3 | + |
| 4 | +const execAsync = promisify(exec) |
| 5 | +const GIT_OUTPUT_LINE_LIMIT = 500 |
| 6 | + |
| 7 | +export interface GitCommit { |
| 8 | + hash: string |
| 9 | + shortHash: string |
| 10 | + subject: string |
| 11 | + author: string |
| 12 | + date: string |
| 13 | +} |
| 14 | + |
| 15 | +async function checkGitRepo(cwd: string): Promise<boolean> { |
| 16 | + try { |
| 17 | + await execAsync("git rev-parse --git-dir", { cwd }) |
| 18 | + return true |
| 19 | + } catch (error) { |
| 20 | + return false |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +async function checkGitInstalled(): Promise<boolean> { |
| 25 | + try { |
| 26 | + await execAsync("git --version") |
| 27 | + return true |
| 28 | + } catch (error) { |
| 29 | + return false |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +export async function searchCommits(query: string, cwd: string): Promise<GitCommit[]> { |
| 34 | + try { |
| 35 | + const isInstalled = await checkGitInstalled() |
| 36 | + if (!isInstalled) { |
| 37 | + console.error("Git is not installed") |
| 38 | + return [] |
| 39 | + } |
| 40 | + |
| 41 | + const isRepo = await checkGitRepo(cwd) |
| 42 | + if (!isRepo) { |
| 43 | + console.error("Not a git repository") |
| 44 | + return [] |
| 45 | + } |
| 46 | + |
| 47 | + // Search commits by hash or message, limiting to 10 results |
| 48 | + const { stdout } = await execAsync( |
| 49 | + `git log -n 10 --format="%H%n%h%n%s%n%an%n%ad" --date=short ` + `--grep="${query}" --regexp-ignore-case`, |
| 50 | + { cwd }, |
| 51 | + ) |
| 52 | + |
| 53 | + let output = stdout |
| 54 | + if (!output.trim() && /^[a-f0-9]+$/i.test(query)) { |
| 55 | + // If no results from grep search and query looks like a hash, try searching by hash |
| 56 | + const { stdout: hashStdout } = await execAsync( |
| 57 | + `git log -n 10 --format="%H%n%h%n%s%n%an%n%ad" --date=short ` + `--author-date-order ${query}`, |
| 58 | + { cwd }, |
| 59 | + ).catch(() => ({ stdout: "" })) |
| 60 | + |
| 61 | + if (!hashStdout.trim()) { |
| 62 | + return [] |
| 63 | + } |
| 64 | + |
| 65 | + output = hashStdout |
| 66 | + } |
| 67 | + |
| 68 | + const commits: GitCommit[] = [] |
| 69 | + const lines = output |
| 70 | + .trim() |
| 71 | + .split("\n") |
| 72 | + .filter((line) => line !== "--") |
| 73 | + |
| 74 | + for (let i = 0; i < lines.length; i += 5) { |
| 75 | + commits.push({ |
| 76 | + hash: lines[i], |
| 77 | + shortHash: lines[i + 1], |
| 78 | + subject: lines[i + 2], |
| 79 | + author: lines[i + 3], |
| 80 | + date: lines[i + 4], |
| 81 | + }) |
| 82 | + } |
| 83 | + |
| 84 | + return commits |
| 85 | + } catch (error) { |
| 86 | + console.error("Error searching commits:", error) |
| 87 | + return [] |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export async function getCommitInfo(hash: string, cwd: string): Promise<string> { |
| 92 | + try { |
| 93 | + const isInstalled = await checkGitInstalled() |
| 94 | + if (!isInstalled) { |
| 95 | + return "Git is not installed" |
| 96 | + } |
| 97 | + |
| 98 | + const isRepo = await checkGitRepo(cwd) |
| 99 | + if (!isRepo) { |
| 100 | + return "Not a git repository" |
| 101 | + } |
| 102 | + |
| 103 | + // Get commit info, stats, and diff separately |
| 104 | + const { stdout: info } = await execAsync(`git show --format="%H%n%h%n%s%n%an%n%ad%n%b" --no-patch ${hash}`, { |
| 105 | + cwd, |
| 106 | + }) |
| 107 | + const [fullHash, shortHash, subject, author, date, body] = info.trim().split("\n") |
| 108 | + |
| 109 | + const { stdout: stats } = await execAsync(`git show --stat --format="" ${hash}`, { cwd }) |
| 110 | + |
| 111 | + const { stdout: diff } = await execAsync(`git show --format="" ${hash}`, { cwd }) |
| 112 | + |
| 113 | + const summary = [ |
| 114 | + `Commit: ${shortHash} (${fullHash})`, |
| 115 | + `Author: ${author}`, |
| 116 | + `Date: ${date}`, |
| 117 | + `\nMessage: ${subject}`, |
| 118 | + body ? `\nDescription:\n${body}` : "", |
| 119 | + "\nFiles Changed:", |
| 120 | + stats.trim(), |
| 121 | + "\nFull Changes:", |
| 122 | + ].join("\n") |
| 123 | + |
| 124 | + const output = summary + "\n\n" + diff.trim() |
| 125 | + return truncateOutput(output) |
| 126 | + } catch (error) { |
| 127 | + console.error("Error getting commit info:", error) |
| 128 | + return `Failed to get commit info: ${error instanceof Error ? error.message : String(error)}` |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +export async function getWorkingState(cwd: string): Promise<string> { |
| 133 | + try { |
| 134 | + const isInstalled = await checkGitInstalled() |
| 135 | + if (!isInstalled) { |
| 136 | + return "Git is not installed" |
| 137 | + } |
| 138 | + |
| 139 | + const isRepo = await checkGitRepo(cwd) |
| 140 | + if (!isRepo) { |
| 141 | + return "Not a git repository" |
| 142 | + } |
| 143 | + |
| 144 | + // Get status of working directory |
| 145 | + const { stdout: status } = await execAsync("git status --short", { cwd }) |
| 146 | + if (!status.trim()) { |
| 147 | + return "No changes in working directory" |
| 148 | + } |
| 149 | + |
| 150 | + // Get all changes (both staged and unstaged) compared to HEAD |
| 151 | + const { stdout: diff } = await execAsync("git diff HEAD", { cwd }) |
| 152 | + const output = `Working directory changes:\n\n${status}\n\n${diff}`.trim() |
| 153 | + return truncateOutput(output) |
| 154 | + } catch (error) { |
| 155 | + console.error("Error getting working state:", error) |
| 156 | + return `Failed to get working state: ${error instanceof Error ? error.message : String(error)}` |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +function truncateOutput(content: string): string { |
| 161 | + if (!GIT_OUTPUT_LINE_LIMIT) { |
| 162 | + return content |
| 163 | + } |
| 164 | + |
| 165 | + const lines = content.split("\n") |
| 166 | + if (lines.length <= GIT_OUTPUT_LINE_LIMIT) { |
| 167 | + return content |
| 168 | + } |
| 169 | + |
| 170 | + const beforeLimit = Math.floor(GIT_OUTPUT_LINE_LIMIT * 0.2) // 20% of lines before |
| 171 | + const afterLimit = GIT_OUTPUT_LINE_LIMIT - beforeLimit // remaining 80% after |
| 172 | + return [ |
| 173 | + ...lines.slice(0, beforeLimit), |
| 174 | + `\n[...${lines.length - GIT_OUTPUT_LINE_LIMIT} lines omitted...]\n`, |
| 175 | + ...lines.slice(-afterLimit), |
| 176 | + ].join("\n") |
| 177 | +} |
0 commit comments