(
branch: string,
filePath: string | string[],
commitMessage: string,
options: CommitOptions = {},
)
| 26 | * @returns The commit hash |
| 27 | */ |
| 28 | export async function createCommit( |
| 29 | branch: string, |
| 30 | filePath: string | string[], |
| 31 | commitMessage: string, |
| 32 | options: CommitOptions = {}, |
| 33 | ): Promise<string> { |
| 34 | // Convert filePath to array if it's a string |
| 35 | const filePaths = Array.isArray(filePath) ? filePath : [filePath]; |
| 36 | |
| 37 | // Stage the files |
| 38 | for (const path of filePaths) { |
| 39 | const addCmd = new Deno.Command("git", { |
| 40 | args: ["add", path], |
| 41 | stdout: "piped", |
| 42 | stderr: "piped", |
| 43 | }); |
| 44 | |
| 45 | const addOutput = await addCmd.output(); |
| 46 | if (!addOutput.success) { |
| 47 | const errorOutput = new TextDecoder().decode(addOutput.stderr); |
| 48 | await logMessage("error", `Git add failed for ${path}: ${errorOutput}`, { path }); |
| 49 | throw new Error(`Git add failed for ${path}: ${errorOutput}`); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Create the commit |
| 54 | const commitArgs = ["commit", "-m", commitMessage]; |
| 55 | |
| 56 | // Add any additional options |
| 57 | if (options.options) { |
| 58 | commitArgs.push(...options.options); |
| 59 | } |
| 60 | |
| 61 | const commitCmd = new Deno.Command("git", { |
| 62 | args: commitArgs, |
| 63 | stdout: "piped", |
| 64 | stderr: "piped", |
| 65 | }); |
| 66 | |
| 67 | const commitOutput = await commitCmd.output(); |
| 68 | if (!commitOutput.success) { |
| 69 | const errorOutput = new TextDecoder().decode(commitOutput.stderr); |
| 70 | await logMessage("error", `Git commit failed: ${errorOutput}`, { branch, filePaths }); |
| 71 | throw new Error(`Git commit failed: ${errorOutput}`); |
| 72 | } |
| 73 | |
| 74 | const commitText = new TextDecoder().decode(commitOutput.stdout); |
| 75 | const commitHashMatch = commitText.match(/\[([^\]]+)\s+([a-f0-9]+)\]/); |
| 76 | const commitHash = commitHashMatch ? commitHashMatch[2] : ""; |
| 77 | |
| 78 | await logMessage("info", `Commit created for ${filePaths.join(", ")}`, { |
| 79 | commitHash, |
| 80 | output: commitText.trim(), |
| 81 | }); |
| 82 | |
| 83 | // Push the commit if requested |
| 84 | if (options.push) { |
| 85 | const remote = options.remote || "origin"; |
no test coverage detected