aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/email/patch.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-06-30 16:20:19 +0200
committerDmitry Vyukov <dvyukov@google.com>2017-06-30 16:20:19 +0200
commit1b20342f25dda771055fe93749190719733c4d0a (patch)
tree981fd01791e12d0a5297ddb5744cd6b24e8136d5 /pkg/email/patch.go
parent7f03d6d5530800e41cef613f38779dc3698e6e74 (diff)
pkg/email: move patch parsing from pkg/kernel
ParsePatch is used by appengine app. Appengine apps can't depend on syscall/unsafe, but pkg/kernel currently does. Move patch parsing to pkg/email which does not depend on syscall/unsafe.
Diffstat (limited to 'pkg/email/patch.go')
-rw-r--r--pkg/email/patch.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/pkg/email/patch.go b/pkg/email/patch.go
new file mode 100644
index 000000000..46a31a05d
--- /dev/null
+++ b/pkg/email/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 email
+
+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
+}