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