Skip to content

Commit 6cccd67

Browse files
committed
Limit undo to last non-restore operation
Ensure "but undo" only reverts the most recent non-restore command instead of stepping back multiple times. Previously the code always inspected the second snapshot which could point to a restore operation and cause undo to jump back further; now we fetch more snapshots, skip the current one, and select the first snapshot whose operation is not a RestoreFromSnapshot. If no such snapshot exists, we print a message and exit. This prevents undo from reverting more than the last actual user operation. Undo: restore oplog head as undo target Simplify the undo command to use the oplog head (the last operation before the current state) as the target to restore. The previous logic fetched many snapshots and attempted to skip restore operations to find a prior non-restore operation; this was more complex than necessary. Fetch only the last snapshot and treat it as the target, remove unused OperationKind import and the verbose current-operation printout. This makes undo behavior clearer and reduces snapshot traversal complexity. Restore second-most recent snapshot instead of latest The undo command was intended to restore the second-most recent operation (i.e., step back one more change), but it only listed one snapshot and selected the most recent. Change to request two snapshots and validate that at least two exist, then select snapshots[1] as the target. This ensures the command restores the previous oplog entry instead of the current head.
1 parent 6be14ce commit 6cccd67

File tree

1 file changed

+1
-10
lines changed

1 file changed

+1
-10
lines changed

crates/but/src/undo/mod.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,16 @@ pub(crate) fn undo_last_operation(repo_path: &Path, _json: bool) -> anyhow::Resu
99
let project = Project::from_path(repo_path)?;
1010
let ctx = CommandContext::open(&project, AppSettings::load_from_default_path_creating()?)?;
1111

12-
// Get the last two snapshots to find the one to restore to
12+
// Get the last two snapshots - restore to the second one back
1313
let snapshots = ctx.list_snapshots(2, None, vec![])?;
1414

1515
if snapshots.len() < 2 {
1616
println!("{}", "No previous operations to undo.".yellow());
1717
return Ok(());
1818
}
1919

20-
// Get the current (most recent) and previous snapshots
21-
let current_snapshot = &snapshots[0];
2220
let target_snapshot = &snapshots[1];
2321

24-
let current_operation = current_snapshot
25-
.details
26-
.as_ref()
27-
.map(|d| d.title.as_str())
28-
.unwrap_or("Unknown operation");
29-
3022
let target_operation = target_snapshot
3123
.details
3224
.as_ref()
@@ -39,7 +31,6 @@ pub(crate) fn undo_last_operation(repo_path: &Path, _json: bool) -> anyhow::Resu
3931
.to_string();
4032

4133
println!("{}", "Undoing operation...".blue().bold());
42-
println!(" Current: {}", current_operation.yellow());
4334
println!(
4435
" Reverting to: {} ({})",
4536
target_operation.green(),

0 commit comments

Comments
 (0)