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 20, 2024
1 parent 67fb491 commit d872dce
Showing 1 changed file with 15 additions and 6 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

0 comments on commit d872dce

Please sign in to comment.