diff options
Diffstat (limited to 'internal/admin/undo.go')
| -rw-r--r-- | internal/admin/undo.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/internal/admin/undo.go b/internal/admin/undo.go index 24ce9c1..c2ea61d 100644 --- a/internal/admin/undo.go +++ b/internal/admin/undo.go @@ -115,6 +115,47 @@ func (us *UndoStack) SetPlayerFileOnlineCheck(fn func(relPath string) bool) { us.playerFileOnlineCheck = fn } +// RewritePaths re-prefixes every stored history path under oldPrefix so a +// directory rename does not leave stale absolute paths behind. Without this +// a later undo recreates the old directory and writes a duplicate file while +// the live file stays in the new directory. newPrefix may be empty to leave +// paths untouched. +func (us *UndoStack) RewritePaths(oldPrefix, newPrefix string) { + if oldPrefix == "" || newPrefix == "" || oldPrefix == newPrefix { + return + } + us.mu.Lock() + defer us.mu.Unlock() + sep := string(os.PathSeparator) + rewrite := func(p string) string { + if p == "" { + return p + } + if p == oldPrefix { + return newPrefix + } + if strings.HasPrefix(p, oldPrefix+sep) { + return filepath.Join(newPrefix, strings.TrimPrefix(p, oldPrefix+sep)) + } + return p + } + rewriteDesc := func(c *ChangeDesc) { + c.FilePath = rewrite(c.FilePath) + c.NewFilePath = rewrite(c.NewFilePath) + for i := range c.ExtraFiles { + c.ExtraFiles[i].FilePath = rewrite(c.ExtraFiles[i].FilePath) + c.ExtraFiles[i].NewFilePath = rewrite(c.ExtraFiles[i].NewFilePath) + } + } + for i := range us.history { + rewriteDesc(&us.history[i]) + } + for i := range us.redo { + rewriteDesc(&us.redo[i]) + } + us.save() +} + func (us *UndoStack) Push(desc ChangeDesc) { us.mu.Lock() defer us.mu.Unlock() |
