@@ -157,7 +157,11 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
157157
158158 originalRefs := make (map [string ]string )
159159 for _ , b := range s .Branches {
160- sha , _ := git .HeadSHA (b .Branch )
160+ sha , err := git .HeadSHA (b .Branch )
161+ if err != nil {
162+ cfg .Errorf ("failed to resolve HEAD SHA for %s: %s" , b .Branch , err )
163+ return nil
164+ }
161165 originalRefs [b .Branch ] = sha
162166 }
163167
@@ -213,7 +217,9 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
213217 UseOnto : true ,
214218 OntoOldBase : originalRefs [br .Branch ],
215219 }
216- saveRebaseState (gitDir , state )
220+ if err := saveRebaseState (gitDir , state ); err != nil {
221+ cfg .Warningf ("failed to save rebase state: %s" , err )
222+ }
217223
218224 printConflictDetails (cfg , newBase )
219225 cfg .Printf ("" )
@@ -261,7 +267,9 @@ func runRebase(cfg *config.Config, opts *rebaseOptions) error {
261267 OriginalBranch : currentBranch ,
262268 OriginalRefs : originalRefs ,
263269 }
264- saveRebaseState (gitDir , state )
270+ if err := saveRebaseState (gitDir , state ); err != nil {
271+ cfg .Warningf ("failed to save rebase state: %s" , err )
272+ }
265273
266274 printConflictDetails (cfg , base )
267275 cfg .Printf ("" )
@@ -408,7 +416,9 @@ func continueRebase(cfg *config.Config, gitDir string) error {
408416 state .CurrentBranchIndex = idx
409417 state .ConflictBranch = branchName
410418 state .OntoOldBase = state .OriginalRefs [branchName ]
411- saveRebaseState (gitDir , state )
419+ if err := saveRebaseState (gitDir , state ); err != nil {
420+ cfg .Warningf ("failed to save rebase state: %s" , err )
421+ }
412422
413423 cfg .Warningf ("Rebasing %s onto %s ... conflict" , branchName , newBase )
414424 printConflictDetails (cfg , newBase )
@@ -448,7 +458,9 @@ func continueRebase(cfg *config.Config, gitDir string) error {
448458 state .RemainingBranches = state .RemainingBranches [remainIdx + 1 :]
449459 state .CurrentBranchIndex = idx
450460 state .ConflictBranch = branchName
451- saveRebaseState (gitDir , state )
461+ if err := saveRebaseState (gitDir , state ); err != nil {
462+ cfg .Warningf ("failed to save rebase state: %s" , err )
463+ }
452464
453465 cfg .Warningf ("Rebasing %s onto %s ... conflict" , branchName , base )
454466 printConflictDetails (cfg , base )
@@ -509,21 +521,41 @@ func abortRebase(cfg *config.Config, gitDir string) error {
509521 _ = git .RebaseAbort ()
510522 }
511523
524+ var restoreErrors []string
512525 for branch , sha := range state .OriginalRefs {
513- _ = git .CheckoutBranch (branch )
514- _ = git .ResetHard (sha )
526+ if err := git .CheckoutBranch (branch ); err != nil {
527+ restoreErrors = append (restoreErrors , fmt .Sprintf ("checkout %s: %s" , branch , err ))
528+ continue
529+ }
530+ if err := git .ResetHard (sha ); err != nil {
531+ restoreErrors = append (restoreErrors , fmt .Sprintf ("reset %s: %s" , branch , err ))
532+ }
515533 }
516534
517535 _ = git .CheckoutBranch (state .OriginalBranch )
518536 clearRebaseState (gitDir )
519- cfg .Successf ("Rebase aborted and branches restored" )
520537
538+ if len (restoreErrors ) > 0 {
539+ cfg .Warningf ("Rebase aborted but some branches could not be fully restored:" )
540+ for _ , e := range restoreErrors {
541+ cfg .Printf (" %s" , e )
542+ }
543+ return nil
544+ }
545+
546+ cfg .Successf ("Rebase aborted and branches restored" )
521547 return nil
522548}
523549
524- func saveRebaseState (gitDir string , state * rebaseState ) {
525- data , _ := json .MarshalIndent (state , "" , " " )
526- _ = os .WriteFile (filepath .Join (gitDir , rebaseStateFile ), data , 0644 )
550+ func saveRebaseState (gitDir string , state * rebaseState ) error {
551+ data , err := json .MarshalIndent (state , "" , " " )
552+ if err != nil {
553+ return fmt .Errorf ("error serializing rebase state: %w" , err )
554+ }
555+ if err := os .WriteFile (filepath .Join (gitDir , rebaseStateFile ), data , 0644 ); err != nil {
556+ return fmt .Errorf ("error writing rebase state: %w" , err )
557+ }
558+ return nil
527559}
528560
529561func loadRebaseState (gitDir string ) (* rebaseState , error ) {
0 commit comments