(query: string, cwd: string)
| 219 | } |
| 220 | |
| 221 | export async function searchCommits(query: string, cwd: string): Promise<GitCommit[]> { |
| 222 | try { |
| 223 | const isInstalled = await checkGitInstalled() |
| 224 | if (!isInstalled) { |
| 225 | console.error("Git is not installed") |
| 226 | return [] |
| 227 | } |
| 228 | |
| 229 | const isRepo = await checkGitRepo(cwd) |
| 230 | if (!isRepo) { |
| 231 | console.error("Not a git repository") |
| 232 | return [] |
| 233 | } |
| 234 | |
| 235 | // Search commits by hash or message, limiting to 10 results |
| 236 | const { stdout } = await execAsync( |
| 237 | `git log -n 10 --format="%H%n%h%n%s%n%an%n%ad" --date=short ` + `--grep="${query}" --regexp-ignore-case`, |
| 238 | { cwd }, |
| 239 | ) |
| 240 | |
| 241 | let output = stdout |
| 242 | if (!output.trim() && /^[a-f0-9]+$/i.test(query)) { |
| 243 | // If no results from grep search and query looks like a hash, try searching by hash |
| 244 | const { stdout: hashStdout } = await execAsync( |
| 245 | `git log -n 10 --format="%H%n%h%n%s%n%an%n%ad" --date=short ` + `--author-date-order ${query}`, |
| 246 | { cwd }, |
| 247 | ).catch(() => ({ stdout: "" })) |
| 248 | |
| 249 | if (!hashStdout.trim()) { |
| 250 | return [] |
| 251 | } |
| 252 | |
| 253 | output = hashStdout |
| 254 | } |
| 255 | |
| 256 | const commits: GitCommit[] = [] |
| 257 | const lines = output |
| 258 | .trim() |
| 259 | .split("\n") |
| 260 | .filter((line) => line !== "--") |
| 261 | |
| 262 | for (let i = 0; i < lines.length; i += 5) { |
| 263 | commits.push({ |
| 264 | hash: lines[i], |
| 265 | shortHash: lines[i + 1], |
| 266 | subject: lines[i + 2], |
| 267 | author: lines[i + 3], |
| 268 | date: lines[i + 4], |
| 269 | }) |
| 270 | } |
| 271 | |
| 272 | return commits |
| 273 | } catch (error) { |
| 274 | console.error("Error searching commits:", error) |
| 275 | return [] |
| 276 | } |
| 277 | } |
| 278 |
no test coverage detected