1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 03:36:16 +00:00

Fix the notorious renaming bug

Fixes: #226 #220
This commit is contained in:
Timur Ismagilov 2024-06-01 23:10:09 +03:00
parent 96820305f2
commit a0945b1210

View File

@ -7,8 +7,10 @@ import (
"github.com/bouncepaw/mycorrhiza/categories"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/files"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/hyphae"
@ -41,8 +43,10 @@ func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, leave
var (
re = regexp.MustCompile(`(?i)` + oldHypha.CanonicalName())
replaceName = func(str string) string {
// Can we drop that util.CanonicalName?
return re.ReplaceAllString(util.CanonicalName(str), newName)
namepart := strings.TrimPrefix(str, files.HyphaeDir())
// Can we drop that util.CanonicalName?:
replaced := re.ReplaceAllString(util.CanonicalName(namepart), newName)
return path.Join(files.HyphaeDir(), replaced)
}
hyphaeToRename = findHyphaeToRename(oldHypha, recursive)
renameMap, err = renamingPairs(hyphaeToRename, replaceName)
@ -75,7 +79,7 @@ func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, leave
for _, h := range hyphaeToRename {
var (
oldName = h.CanonicalName()
newName = replaceName(oldName)
newName = re.ReplaceAllString(oldName, newName)
)
hyphae.RenameHyphaTo(h, newName, replaceName)
backlinks.UpdateBacklinksAfterRename(h, oldName)