@@ -372,18 +372,62 @@ export async function generateRebase(
372372
373373 let generateType : 'commits' | 'rebase' = 'rebase' ;
374374 let headRefSlug = head . ref ;
375+ let generatedBranchName : string | undefined ;
376+ let previousHeadRef : GitReference | undefined ;
377+ let generatedHeadRef : GitReference | undefined ;
378+ let generatedStashRef : GitStashReference | undefined ;
379+ let stashCommit : GitStashCommit | undefined ;
380+ let previousStashCommit : GitStashCommit | undefined ;
381+
375382 const shas = await repo . git . patch ?. createUnreachableCommitsFromPatches ( base . ref , diffInfo ) ;
376383 if ( shas ?. length ) {
377384 if ( head . ref === uncommitted ) {
378385 generateType = 'commits' ;
379386 headRefSlug = 'uncommitted' ;
387+
388+ // Capture the current HEAD before making changes
389+ const log = await svc . commits . getLog ( undefined , { limit : 1 } ) ;
390+ if ( log ?. commits . size ) {
391+ const currentCommit = log . commits . values ( ) . next ( ) . value ;
392+ if ( currentCommit ) {
393+ previousHeadRef = createReference ( currentCommit . sha , svc . path , { refType : 'revision' } ) ;
394+ }
395+ }
396+
397+ let stash = await svc . stash ?. getStash ( ) ;
398+ if ( stash ?. stashes . size ) {
399+ const latestStash = stash . stashes . values ( ) . next ( ) . value ;
400+ if ( latestStash ) {
401+ previousStashCommit = latestStash ;
402+ }
403+ }
404+
380405 // stash the working changes
381406 await svc . stash ?. saveStash ( undefined , undefined , { includeUntracked : true } ) ;
382- // await repo.git.checkout?.(shas[shas.length - 1]);
407+
408+ // Get the latest stash reference
409+ stash = await svc . stash ?. getStash ( ) ;
410+ if ( stash ?. stashes . size ) {
411+ stashCommit = stash . stashes . values ( ) . next ( ) . value ;
412+ if ( stashCommit ) {
413+ generatedStashRef = createReference ( stashCommit . ref , svc . path , {
414+ refType : 'stash' ,
415+ name : stashCommit . stashName ,
416+ number : stashCommit . stashNumber ,
417+ message : stashCommit . message ,
418+ stashOnRef : stashCommit . stashOnRef ,
419+ } ) ;
420+ }
421+ }
422+
383423 // reset the current branch to the new shas
384424 await svc . reset ( shas [ shas . length - 1 ] , { hard : true } ) ;
425+
426+ // Capture the new HEAD after reset
427+ generatedHeadRef = createReference ( shas [ shas . length - 1 ] , svc . path , { refType : 'revision' } ) ;
385428 } else {
386- await svc . branches . createBranch ?.( `rebase/${ head . ref } -${ Date . now ( ) } ` , shas [ shas . length - 1 ] ) ;
429+ generatedBranchName = `rebase/${ head . ref } -${ Date . now ( ) } ` ;
430+ await svc . branches . createBranch ?.( generatedBranchName , shas [ shas . length - 1 ] ) ;
387431 }
388432 }
389433
@@ -395,6 +439,40 @@ export async function generateRebase(
395439 ) ;
396440
397441 showMarkdownPreview ( documentUri ) ;
442+
443+ // Show success notification with Undo button
444+ const undoButton = { title : 'Undo' } ;
445+ const resultNotification = await window . showInformationMessage (
446+ generateType === 'commits'
447+ ? 'Successfully generated commits from your working changes.'
448+ : 'Successfully generated rebase branch.' ,
449+ undoButton ,
450+ ) ;
451+
452+ if ( resultNotification === undoButton ) {
453+ if ( generateType === 'commits' ) {
454+ // Undo GenerateCommitsCommand
455+ void executeCommand ( 'gitlens.ai.undoGenerateRebase' , {
456+ undoCommand : 'gitlens.ai.generateCommits' ,
457+ repoPath : svc . path ,
458+ generatedHeadRef : generatedHeadRef ,
459+ previousHeadRef : previousHeadRef ,
460+ generatedStashRef :
461+ stashCommit != null && stashCommit . ref !== previousStashCommit ?. ref
462+ ? generatedStashRef
463+ : undefined ,
464+ source : source ,
465+ } satisfies UndoGenerateRebaseCommandArgs ) ;
466+ } else {
467+ // Undo GenerateRebaseCommand
468+ void executeCommand ( 'gitlens.ai.undoGenerateRebase' , {
469+ undoCommand : 'gitlens.ai.generateRebase' ,
470+ repoPath : svc . path ,
471+ generatedBranchName : generatedBranchName ,
472+ source : source ,
473+ } satisfies UndoGenerateRebaseCommandArgs ) ;
474+ }
475+ }
398476 } catch ( ex ) {
399477 Logger . error ( ex , 'GenerateRebaseCommand' , 'execute' ) ;
400478 void showGenericErrorMessage ( 'Unable to parse rebase result' ) ;
0 commit comments