Skip to content

Commit

Permalink
fixed namespace cleanup when filename contains namespace (e.g. _cq_, …
Browse files Browse the repository at this point in the history
…_jcr_, _sling_)
  • Loading branch information
Dominik Przybyl committed Sep 19, 2024
1 parent 67fb491 commit 1ff7fd5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
21 changes: 15 additions & 6 deletions pkg/content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
JCRRootPrefix = "<jcr:root"
PropPattern = "^\\s*([^ =]+)=\"([^\"]+)\"(.*)$"
NamespacePattern = "^\\w+:(\\w+)=\"[^\"]+\"$"
NamespaceFilePattern = "\\\\|/_([a-zA-Z0-9]+)_"
ParentsBackupSuffix = ".bak"
ParentsBackupDirIndicator = ".bakdir"
JCRContentNode = "jcr:content"
Expand All @@ -33,13 +34,15 @@ const (
)

var (
propPatternRegex *regexp.Regexp
namespacePatternRegex *regexp.Regexp
propPatternRegex *regexp.Regexp
namespacePatternRegex *regexp.Regexp
namespaceFilePatternRegex *regexp.Regexp
)

func init() {
propPatternRegex = regexp.MustCompile(PropPattern)
namespacePatternRegex = regexp.MustCompile(NamespacePattern)
namespaceFilePatternRegex = regexp.MustCompile(NamespaceFilePattern)
}

type Manager struct {
Expand Down Expand Up @@ -187,25 +190,31 @@ func (c Manager) filterLines(path string, lines []string) []string {
result = result[:len(result)-1]
}
}
return c.cleanNamespaces(result)
return c.cleanNamespaces(path, result)
}

func (c Manager) cleanNamespaces(lines []string) []string {
func (c Manager) cleanNamespaces(path string, lines []string) []string {
if !c.NamespacesSkipped {
return lines
}

var fileNamespace string
groups := namespaceFilePatternRegex.FindStringSubmatch(path)
if groups != nil {
fileNamespace = groups[1]
}

var result []string
for _, line := range lines {
if strings.HasPrefix(line, JCRRootPrefix) {
var rootResult []string
for _, part := range strings.Split(line, " ") {
groups := namespacePatternRegex.FindStringSubmatch(part)
groups = namespacePatternRegex.FindStringSubmatch(part)
if groups == nil {
rootResult = append(rootResult, part)
} else {
flag := lo.SomeBy(lines, func(line string) bool {
return strings.Contains(line, groups[1]+":")
return strings.Contains(line, groups[1]+":") || groups[1] == fileNamespace
})
if flag {
rootResult = append(rootResult, part)
Expand Down
2 changes: 1 addition & 1 deletion pkg/content_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
NamespacePattern = "(\\\\|/)_([a-zA-Z0-9]+)_"
NamespacePattern = "\\\\|/_[a-zA-Z0-9]+_"
)

var (
Expand Down

0 comments on commit 1ff7fd5

Please sign in to comment.