aboutsummaryrefslogtreecommitdiff
path: root/file.go
diff options
context:
space:
mode:
Diffstat (limited to 'file.go')
-rw-r--r--file.go165
1 files changed, 124 insertions, 41 deletions
diff --git a/file.go b/file.go
index 798790c..742290d 100644
--- a/file.go
+++ b/file.go
@@ -35,7 +35,8 @@ var reCopyrightText = regexp.MustCompile(`^(//+|#+|/\*+|<!--+)?\s?SPDX-FileCopyr
// REUSE-IgnoreEnd
type file struct {
- matchLicense *matchLicense
+ matchCopyright *matchCopyright
+ matchLicense *matchLicense
path string
@@ -226,7 +227,7 @@ func (f *file) apply(conv *SPDXConv) (err error) {
return err
}
- f.applyDelete()
+ f.applyDelete(&conv.cfg)
// REUSE-IgnoreStart
line := fmt.Sprintf("%sSPDX-License-Identifier: %s%s",
@@ -360,19 +361,18 @@ func (f *file) applyCopyrightText(conv *SPDXConv) (err error) {
}
// Verify that the line actually match with one of
// match-copyright pattern.
- var cmc *matchCopyright
- for _, cmc = range conv.cfg.MatchCopyright {
- if cmc.match(string(line)) {
- f.copyrightYear = cmc.year
- f.copyrightText = cmc.String()
+ for _, f.matchCopyright = range conv.cfg.MatchCopyright {
+ if f.matchCopyright.match(string(line)) {
+ f.copyrightYear = f.matchCopyright.year
+ f.copyrightText = f.matchCopyright.String()
break
}
}
- if cmc == nil {
- return fmt.Errorf(`%s: line %d does not match with any of match-license pattern`,
- f.path, f.idxLicenseID)
+ if f.matchCopyright == nil {
+ return fmt.Errorf(`%s: line %d does not match with any of match-copyright pattern`,
+ f.path, f.idxCopyrightText)
}
- if f.idxLicenseID >= 0 {
+ if f.idxCopyrightText >= 0 {
f.topLines[x] = nil
} else {
f.bottomLines[x] = nil
@@ -381,48 +381,131 @@ func (f *file) applyCopyrightText(conv *SPDXConv) (err error) {
return nil
}
-func (f *file) applyDelete() {
- var reDeleteLine []*regexp.Regexp
+func (f *file) applyDelete(cfg *config) {
+ var startat int
+ var lines [][]byte
if f.matchLicense != nil {
- reDeleteLine = f.matchLicense.reDeleteLine
+ if f.idxLicenseID >= 0 {
+ startat = f.idxLicenseID
+ lines = f.topLines
+ } else {
+ startat = f.idxLicenseID + cfg.MaxLineMatch
+ lines = f.bottomLines
+ }
+ f.deleteLineBefore(f.matchLicense.reDeleteLineBefore, lines, startat-1)
+ f.deleteLineAfter(f.matchLicense.reDeleteLineAfter, lines, startat+1)
}
- var lines [][]byte
- for _, line := range f.topLines {
- if line == nil {
- continue
+ if f.matchCopyright != nil {
+ if f.idxCopyrightText >= 0 {
+ startat = f.idxCopyrightText
+ lines = f.topLines
+ } else {
+ startat = f.idxCopyrightText + cfg.MaxLineMatch
+ lines = f.bottomLines
}
- var found bool
- for _, redel := range reDeleteLine {
- if redel.Match(line) {
- found = true
- break
- }
+ f.deleteLineBefore(f.matchCopyright.reDeleteLineBefore, lines, startat-1)
+ f.deleteLineAfter(f.matchCopyright.reDeleteLineAfter, lines, startat+1)
+ }
+ f.trimDeletedLines()
+}
+
+func (f *file) deleteLineBefore(listre []*regexp.Regexp, lines [][]byte, startat int) {
+ if len(listre) == 0 {
+ return
+ }
+ var idxre = 0
+ var re = listre[idxre]
+ var line []byte
+ for x := startat; x >= 0; x-- {
+ if lines[x] == nil {
+ return
}
- if found {
- continue
+ line = lines[x]
+ if !re.Match(line) {
+ // Once the regex does not match with line, return
+ // immediately.
+ return
+ }
+ lines[x] = nil
+ idxre++
+ if idxre == len(listre) {
+ break
}
- lines = append(lines, line)
+ re = listre[idxre]
}
- f.topLines = lines
+ // Continue deleting with the next lines.
+ for x := len(f.lines) - 1; x >= 0; x-- {
+ line = f.lines[x]
+ if !re.Match(line) {
+ return
+ }
+ f.lines[x] = nil
+ idxre++
+ if idxre == len(listre) {
+ break
+ }
+ re = listre[idxre]
+ }
+}
- lines = nil
- for _, line := range f.bottomLines {
- if line == nil {
- continue
+func (f *file) deleteLineAfter(listre []*regexp.Regexp, lines [][]byte, startat int) {
+ if len(listre) == 0 {
+ return
+ }
+ var idxre = 0
+ var re = listre[idxre]
+ var line []byte
+ for x := startat; x < len(lines); x++ {
+ if lines[x] == nil {
+ return
}
- var found bool
- for _, redel := range reDeleteLine {
- if redel.Match(line) {
- found = true
- break
- }
+ line = lines[x]
+ if !re.Match(line) {
+ // Once the regex does not match with line, return
+ // immediately.
+ return
+ }
+ lines[x] = nil
+ idxre++
+ if idxre == len(listre) {
+ break
}
- if found {
+ re = listre[idxre]
+ }
+ // Continue deleting with the next lines.
+ for x := 0; x < len(f.lines); x++ {
+ line = f.lines[x]
+ if !re.Match(line) {
+ return
+ }
+ f.lines[x] = nil
+ idxre++
+ if idxre == len(listre) {
+ break
+ }
+ re = listre[idxre]
+ }
+}
+
+func (f *file) trimDeletedLines() {
+ var newlines [][]byte
+ var lines [][]byte
+ if f.idxLicenseID >= 0 {
+ lines = f.topLines
+ } else {
+ lines = f.bottomLines
+ }
+ for x := 0; x < len(lines); x++ {
+ if lines[x] == nil {
continue
}
- lines = append(lines, line)
+ newlines = append(newlines, lines[x])
+ }
+ if f.idxLicenseID >= 0 {
+ f.topLines = newlines
+ } else {
+ f.bottomLines = newlines
}
- f.bottomLines = lines
}
// insertEmptyLine insert empty line after SPDX identifiers or any comments after it.