| 10 | var hunkHeaderRegexp = regexp.MustCompile(`(?m)^@@ -(\d+)[^\+]+\+(\d+)[^@]+@@(.*)$`) |
| 11 | |
| 12 | func Parse(patchStr string) *Patch { |
| 13 | // ignore trailing newline. |
| 14 | lines := strings.Split(strings.TrimSuffix(patchStr, "\n"), "\n") |
| 15 | |
| 16 | hunks := []*Hunk{} |
| 17 | patchHeader := []string{} |
| 18 | |
| 19 | var currentHunk *Hunk |
| 20 | for _, line := range lines { |
| 21 | if strings.HasPrefix(line, "@@") { |
| 22 | oldStart, newStart, headerContext := headerInfo(line) |
| 23 | |
| 24 | currentHunk = &Hunk{ |
| 25 | oldStart: oldStart, |
| 26 | newStart: newStart, |
| 27 | headerContext: headerContext, |
| 28 | bodyLines: []*PatchLine{}, |
| 29 | } |
| 30 | hunks = append(hunks, currentHunk) |
| 31 | } else if currentHunk != nil { |
| 32 | currentHunk.bodyLines = append(currentHunk.bodyLines, newHunkLine(line)) |
| 33 | } else { |
| 34 | patchHeader = append(patchHeader, line) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return &Patch{ |
| 39 | hunks: hunks, |
| 40 | header: patchHeader, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func headerInfo(header string) (int, int, string) { |
| 45 | match := hunkHeaderRegexp.FindStringSubmatch(header) |