(
target: string,
options: RevertOptions = {},
)
| 217 | * @param options Additional options for the revert |
| 218 | */ |
| 219 | export async function revertChanges( |
| 220 | target: string, |
| 221 | options: RevertOptions = {}, |
| 222 | ): Promise<void> { |
| 223 | // Check if the target is a valid date or commit |
| 224 | try { |
| 225 | // Try to parse as a date |
| 226 | new Date(target); |
| 227 | } catch (error) { |
| 228 | // If not a date, check if it's a valid commit |
| 229 | const revParseCmd = new Deno.Command("git", { |
| 230 | args: ["rev-parse", "--verify", target], |
| 231 | stdout: "piped", |
| 232 | stderr: "piped", |
| 233 | }); |
| 234 | |
| 235 | const revParseOutput = await revParseCmd.output(); |
| 236 | if (!revParseOutput.success) { |
| 237 | const errorOutput = new TextDecoder().decode(revParseOutput.stderr); |
| 238 | await logMessage("error", `Invalid target: ${errorOutput}`, { target }); |
| 239 | throw new Error(`Invalid target: ${errorOutput}`); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // Get commits since the target date |
| 244 | const logCmd = new Deno.Command("git", { |
| 245 | args: ["log", "--since", target, "--format=%H"], |
| 246 | stdout: "piped", |
| 247 | stderr: "piped", |
| 248 | }); |
| 249 | |
| 250 | const logOutput = await logCmd.output(); |
| 251 | if (!logOutput.success) { |
| 252 | const errorOutput = new TextDecoder().decode(logOutput.stderr); |
| 253 | await logMessage("error", `Git log failed: ${errorOutput}`, { target }); |
| 254 | throw new Error(`Git log failed: ${errorOutput}`); |
| 255 | } |
| 256 | |
| 257 | const logText = new TextDecoder().decode(logOutput.stdout); |
| 258 | |
| 259 | const commits = logText.trim().split("\n").filter(Boolean); |
| 260 | |
| 261 | if (commits.length === 0) { |
| 262 | await logMessage("info", `No commits found since ${target}`); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | // Create a new branch if requested |
| 267 | if (options.newBranch) { |
| 268 | const branchCmd = new Deno.Command("git", { |
| 269 | args: ["checkout", "-b", options.newBranch], |
| 270 | stdout: "piped", |
| 271 | stderr: "piped", |
| 272 | }); |
| 273 | |
| 274 | const branchOutput = await branchCmd.output(); |
| 275 | if (!branchOutput.success) { |
| 276 | const errorOutput = new TextDecoder().decode(branchOutput.stderr); |
nothing calls this directly
no test coverage detected