Skip to content

Commit

Permalink
[#674] Handle VCS errors in introspectiveRevert
Browse files Browse the repository at this point in the history
- refactor simple and introspective Reverts to simplify error handling
- add a test to make sure we deal with VCS errors in introspectiveRevert
  • Loading branch information
philou authored and mengdaming committed Oct 1, 2024
1 parent 8101dbd commit 7f8b117
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/engine/tcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,39 +596,42 @@ func (tcr *TCREngine) commit(event events.TCREvent) {
}

func (tcr *TCREngine) revert(e events.TCREvent) {
var err error

switch *tcr.variant {
case variant.Introspective:
_ = tcr.introspectiveRevert(e)
err = tcr.introspectiveRevert(e)
default:
tcr.simpleRevert()
err = tcr.simpleRevert()
}

tcr.handleError(err, false, status.VCSError)
}

func (tcr *TCREngine) simpleRevert() {
func (tcr *TCREngine) simpleRevert() error {
diffs, err := tcr.vcs.Diff()
tcr.handleError(err, false, status.VCSError)
if err != nil {
return
return err
}
var reverted int
for _, diff := range diffs {
if tcr.shouldRevertFile(diff.Path) {
err := tcr.revertFile(diff.Path)
tcr.handleError(err, false, status.VCSError)
if err == nil {
reverted++
if err != nil {
return err
}
reverted++
}
}
if reverted > 0 {
report.PostWarning(reverted, " file(s) reverted")
} else {
report.PostInfo(tcr.noFilesRevertedMessage())
}
return nil
}

func (tcr *TCREngine) introspectiveRevert(event events.TCREvent) (err error) {
// Commit changes with failure message into VCS index
err = tcr.vcs.Add()
if err != nil {
return err
Expand All @@ -637,12 +640,10 @@ func (tcr *TCREngine) introspectiveRevert(event events.TCREvent) (err error) {
if err != nil {
return err
}
// Rollback changes (both in VCS index and working tree)
err = tcr.vcs.RollbackLastCommit()
if err != nil {
return err
}
// Amend commit message on revert operation in VCS index
err = tcr.vcs.Commit(tcr.wrapCommitMessages(commitMessageRevert, nil)...)
return err
}
Expand Down
9 changes: 9 additions & 0 deletions src/engine/tcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ func Test_tcr_operation_end_state(t *testing.T) {
},
status.VCSError,
},
{
"introspective revert with VCS rollback last commit failure",
func() {
tcr, _ := initTCREngineWithFakes(nil, nil, fake.Commands{fake.RollbackLastCommitCommand}, nil)
tcr.SetVariant(variant.Introspective.Name())
tcr.revert(*events.ATcrEvent())
},
status.VCSError,
},
}

for _, tt := range testFlags {
Expand Down

0 comments on commit 7f8b117

Please sign in to comment.