Skip to content

Commit 3894409

Browse files
committed
Add revert cleanup on patch failure to avoid stale patch state
1 parent fef703d commit 3894409

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

util/patch.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,24 @@ func Patch(pathInput, option, rootDir, targetDir, targetLabel string, reverse bo
5454
if targetDir == "" {
5555
return fmt.Errorf("patch target %s has no working directory", targetLabel)
5656
}
57+
var revertRegistered bool
58+
var revertSucceeded bool
5759
if reverse {
5860
mutex.Lock()
5961
if patchedTargets[targetLabel] {
6062
mutex.Unlock()
6163
return nil
6264
}
6365
patchedTargets[targetLabel] = true
66+
revertRegistered = true
6467
mutex.Unlock()
68+
defer func() {
69+
if revertRegistered && !revertSucceeded {
70+
mutex.Lock()
71+
delete(patchedTargets, targetLabel)
72+
mutex.Unlock()
73+
}
74+
}()
6575
}
6676

6777
var individualPaths []string
@@ -112,5 +122,6 @@ func Patch(pathInput, option, rootDir, targetDir, targetLabel string, reverse bo
112122
return fmt.Errorf("failed to %s patch %s (options: %s): %w", strings.ToLower(logMsg), p, option, err)
113123
}
114124
}
125+
revertSucceeded = true
115126
return nil
116127
}

0 commit comments

Comments
 (0)