(
commit: string,
message: string = `Rollback to ${commit}`,
options: RollbackOptions = {},
)
| 122 | * @returns The new commit hash |
| 123 | */ |
| 124 | export async function rollbackChanges( |
| 125 | commit: string, |
| 126 | message: string = `Rollback to ${commit}`, |
| 127 | options: RollbackOptions = {}, |
| 128 | ): Promise<string> { |
| 129 | // Check if the commit exists |
| 130 | const revParseCmd = new Deno.Command("git", { |
| 131 | args: ["rev-parse", "--verify", commit], |
| 132 | stdout: "piped", |
| 133 | stderr: "piped", |
| 134 | }); |
| 135 | |
| 136 | const revParseOutput = await revParseCmd.output(); |
| 137 | if (!revParseOutput.success) { |
| 138 | const errorOutput = new TextDecoder().decode(revParseOutput.stderr); |
| 139 | await logMessage("error", `Commit ${commit} not found: ${errorOutput}`, { commit }); |
| 140 | throw new Error(`Commit ${commit} not found: ${errorOutput}`); |
| 141 | } |
| 142 | |
| 143 | // Create a new branch if requested |
| 144 | if (options.newBranch) { |
| 145 | const branchCmd = new Deno.Command("git", { |
| 146 | args: ["checkout", "-b", options.newBranch], |
| 147 | stdout: "piped", |
| 148 | stderr: "piped", |
| 149 | }); |
| 150 | |
| 151 | const branchOutput = await branchCmd.output(); |
| 152 | if (!branchOutput.success) { |
| 153 | const errorOutput = new TextDecoder().decode(branchOutput.stderr); |
| 154 | await logMessage("error", `Creating new branch failed: ${errorOutput}`, { |
| 155 | branch: options.newBranch, |
| 156 | }); |
| 157 | throw new Error(`Creating new branch failed: ${errorOutput}`); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // Reset to the specified commit |
| 162 | const resetCmd = new Deno.Command("git", { |
| 163 | args: ["reset", "--hard", commit], |
| 164 | stdout: "piped", |
| 165 | stderr: "piped", |
| 166 | }); |
| 167 | |
| 168 | const resetOutput = await resetCmd.output(); |
| 169 | if (!resetOutput.success) { |
| 170 | const errorOutput = new TextDecoder().decode(resetOutput.stderr); |
| 171 | await logMessage("error", `Git reset failed: ${errorOutput}`, { commit }); |
| 172 | throw new Error(`Git reset failed: ${errorOutput}`); |
| 173 | } |
| 174 | |
| 175 | const resetText = new TextDecoder().decode(resetOutput.stdout); |
| 176 | await logMessage("info", `Reset to commit ${commit}`, { output: resetText.trim() }); |
| 177 | |
| 178 | // Push the changes if requested |
| 179 | if (options.push) { |
| 180 | const remote = options.remote || "origin"; |
| 181 | const branch = options.newBranch || await getCurrentBranch(); |
no test coverage detected