aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/kernel/patch.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-06-14 10:18:10 +0200
committerDmitry Vyukov <dvyukov@google.com>2017-06-14 10:18:10 +0200
commit54e36636a1909c1b0efdf11d80e168afdc045d9d (patch)
treeb30d2f58c88199d26a5a25750096c8e450993fab /pkg/kernel/patch.go
parentb41e96b421acda1761e9ba2ee17da16efad436cd (diff)
syz-dash: move patch parsing to pkg/kernel
Patch parsing is not dashboard-specific and can be reused by other programs.
Diffstat (limited to 'pkg/kernel/patch.go')
-rw-r--r--pkg/kernel/patch.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/pkg/kernel/patch.go b/pkg/kernel/patch.go
new file mode 100644
index 000000000..b8e30dbec
--- /dev/null
+++ b/pkg/kernel/patch.go
@@ -0,0 +1,69 @@
+// Copyright 2017 syzkaller project authors. All rights reserved.
+// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
+
+package kernel
+
+import (
+ "bufio"
+ "fmt"
+ "strings"
+)
+
+func ParsePatch(text string) (title string, diff string, err error) {
+ s := bufio.NewScanner(strings.NewReader(text))
+ parsingDiff := false
+ diffStarted := false
+ lastLine := ""
+ for s.Scan() {
+ ln := s.Text()
+ if strings.HasPrefix(ln, "--- a/") || strings.HasPrefix(ln, "--- /dev/null") {
+ parsingDiff = true
+ if title == "" {
+ title = lastLine
+ }
+ }
+ if parsingDiff {
+ if ln == "--" || ln == "-- " {
+ break
+ }
+ diff += ln + "\n"
+ continue
+ }
+ if strings.HasPrefix(ln, "diff --git") {
+ diffStarted = true
+ continue
+ }
+ if strings.HasPrefix(ln, "Subject: ") {
+ title = ln[len("Subject: "):]
+ continue
+ }
+ if ln == "" || title != "" || diffStarted {
+ continue
+ }
+ lastLine = ln
+ if strings.HasPrefix(ln, " ") {
+ title = ln[4:]
+ }
+ }
+ if err = s.Err(); err != nil {
+ return
+ }
+ if strings.Contains(strings.ToLower(title), "[patch") {
+ pos := strings.IndexByte(title, ']')
+ if pos == -1 {
+ err = fmt.Errorf("title contains '[patch' but not ']'")
+ return
+ }
+ title = title[pos+1:]
+ }
+ title = strings.TrimSpace(title)
+ if title == "" {
+ err = fmt.Errorf("failed to extract title")
+ return
+ }
+ if diff == "" {
+ err = fmt.Errorf("failed to extract diff")
+ return
+ }
+ return
+}