@@ -122,11 +122,11 @@ pub(crate) fn restore_to_oplog(
122
122
oplog_sha : & str ,
123
123
) -> anyhow:: Result < ( ) > {
124
124
let project = Project :: find_by_path ( repo_path) ?;
125
+ let snapshots = but_api:: undo:: list_snapshots ( project. id , 100 , None , None ) ?;
125
126
126
127
// Parse the oplog SHA (support partial SHAs)
127
128
let commit_sha_string = if oplog_sha. len ( ) >= 7 {
128
129
// Try to find a snapshot that starts with this SHA
129
- let snapshots = but_api:: undo:: list_snapshots ( project. id , 100 , None , None ) ?;
130
130
131
131
let matching_snapshot = snapshots
132
132
. iter ( )
@@ -139,7 +139,6 @@ pub(crate) fn restore_to_oplog(
139
139
} ;
140
140
141
141
// Get information about the target snapshot
142
- let snapshots = but_api:: undo:: list_snapshots ( project. id , 100 , None , None ) ?;
143
142
let target_snapshot = snapshots
144
143
. iter ( )
145
144
. find ( |snapshot| snapshot. commit_id . to_string ( ) == commit_sha_string)
@@ -196,3 +195,54 @@ pub(crate) fn restore_to_oplog(
196
195
197
196
Ok ( ( ) )
198
197
}
198
+
199
+ pub ( crate ) fn undo_last_operation ( repo_path : & Path , _json : bool ) -> anyhow:: Result < ( ) > {
200
+ let project = Project :: find_by_path ( repo_path) ?;
201
+
202
+ // Get the last two snapshots - restore to the second one back
203
+ let snapshots = but_api:: undo:: list_snapshots ( project. id , 2 , None , None ) ?;
204
+
205
+ if snapshots. len ( ) < 2 {
206
+ println ! ( "{}" , "No previous operations to undo." . yellow( ) ) ;
207
+ return Ok ( ( ) ) ;
208
+ }
209
+
210
+ let target_snapshot = & snapshots[ 1 ] ;
211
+
212
+ let target_operation = target_snapshot
213
+ . details
214
+ . as_ref ( )
215
+ . map ( |d| d. title . as_str ( ) )
216
+ . unwrap_or ( "Unknown operation" ) ;
217
+
218
+ let target_time = chrono:: DateTime :: from_timestamp ( target_snapshot. created_at . seconds ( ) , 0 )
219
+ . ok_or ( anyhow:: anyhow!( "Could not parse timestamp" ) ) ?
220
+ . format ( "%Y-%m-%d %H:%M:%S" )
221
+ . to_string ( ) ;
222
+
223
+ println ! ( "{}" , "Undoing operation..." . blue( ) . bold( ) ) ;
224
+ println ! (
225
+ " Reverting to: {} ({})" ,
226
+ target_operation. green( ) ,
227
+ target_time. dimmed( )
228
+ ) ;
229
+
230
+ // Restore to the previous snapshot using the but_api
231
+ but_api:: undo:: restore_snapshot ( project. id , target_snapshot. commit_id . to_string ( ) ) ?;
232
+
233
+ let restore_commit_short = format ! (
234
+ "{}{}" ,
235
+ & target_snapshot. commit_id. to_string( ) [ ..7 ]
236
+ . blue( )
237
+ . underline( ) ,
238
+ & target_snapshot. commit_id. to_string( ) [ 7 ..12 ] . blue( ) . dimmed( )
239
+ ) ;
240
+
241
+ println ! (
242
+ "{} Undo completed successfully! Restored to snapshot: {}" ,
243
+ "✓" . green( ) . bold( ) ,
244
+ restore_commit_short
245
+ ) ;
246
+
247
+ Ok ( ( ) )
248
+ }
0 commit comments