@@ -196,3 +196,54 @@ pub(crate) fn restore_to_oplog(
196
196
197
197
Ok ( ( ) )
198
198
}
199
+
200
+ pub ( crate ) fn undo_last_operation ( repo_path : & Path , _json : bool ) -> anyhow:: Result < ( ) > {
201
+ let project = Project :: find_by_path ( repo_path) ?;
202
+
203
+ // Get the last two snapshots - restore to the second one back
204
+ let snapshots = but_api:: undo:: list_snapshots ( project. id , 2 , None , None ) ?;
205
+
206
+ if snapshots. len ( ) < 2 {
207
+ println ! ( "{}" , "No previous operations to undo." . yellow( ) ) ;
208
+ return Ok ( ( ) ) ;
209
+ }
210
+
211
+ let target_snapshot = & snapshots[ 1 ] ;
212
+
213
+ let target_operation = target_snapshot
214
+ . details
215
+ . as_ref ( )
216
+ . map ( |d| d. title . as_str ( ) )
217
+ . unwrap_or ( "Unknown operation" ) ;
218
+
219
+ let target_time = chrono:: DateTime :: from_timestamp ( target_snapshot. created_at . seconds ( ) , 0 )
220
+ . ok_or ( anyhow:: anyhow!( "Could not parse timestamp" ) ) ?
221
+ . format ( "%Y-%m-%d %H:%M:%S" )
222
+ . to_string ( ) ;
223
+
224
+ println ! ( "{}" , "Undoing operation..." . blue( ) . bold( ) ) ;
225
+ println ! (
226
+ " Reverting to: {} ({})" ,
227
+ target_operation. green( ) ,
228
+ target_time. dimmed( )
229
+ ) ;
230
+
231
+ // Restore to the previous snapshot using the but_api
232
+ but_api:: undo:: restore_snapshot ( project. id , target_snapshot. commit_id . to_string ( ) ) ?;
233
+
234
+ let restore_commit_short = format ! (
235
+ "{}{}" ,
236
+ & target_snapshot. commit_id. to_string( ) [ ..7 ]
237
+ . blue( )
238
+ . underline( ) ,
239
+ & target_snapshot. commit_id. to_string( ) [ 7 ..12 ] . blue( ) . dimmed( )
240
+ ) ;
241
+
242
+ println ! (
243
+ "{} Undo completed successfully! Restored to snapshot: {}" ,
244
+ "✓" . green( ) . bold( ) ,
245
+ restore_commit_short
246
+ ) ;
247
+
248
+ Ok ( ( ) )
249
+ }
0 commit comments