ParseDiffText splits the unified diff text into per-file Diff structs. ref, if non-empty, is a git ref used to read new-file content via git show instead of reading from the working tree. runner, if non-nil, is used to execute git subprocesses through a shared concurrency limiter.
(ctx context.Context, diffText string, repoDir string, ref string, runner *gitcmd.Runner)
| 96 | // runner, if non-nil, is used to execute git subprocesses through a |
| 97 | // shared concurrency limiter. |
| 98 | func ParseDiffText(ctx context.Context, diffText string, repoDir string, ref string, runner *gitcmd.Runner) ([]model.Diff, error) { |
| 99 | lines := splitDiffLines(diffText) |
| 100 | var diffs []model.Diff |
| 101 | var current *model.Diff |
| 102 | var buf strings.Builder |
| 103 | // inHunk tracks whether the current line sits inside a "@@" hunk of the |
| 104 | // current file's section. Only hunk content lines carry a leading |
| 105 | // "+"/"-"/" " marker, so insertion/deletion counting and the binary |
| 106 | // marker must look at hunk state: outside a hunk, "+++ b/file" and |
| 107 | // "--- a/file" are headers, not content; inside a hunk, an added line |
| 108 | // like "++i" renders as "+++i" and still counts as an insertion. |
| 109 | inHunk := false |
| 110 | |
| 111 | ctx, cancel := context.WithTimeout(ctx, 2*time.Minute) |
| 112 | defer cancel() |
| 113 | |
| 114 | for _, line := range lines { |
| 115 | if oldPath, newPath, ok := parseDiffHeaderLine(line); ok { |
| 116 | // Flush previous diff |
| 117 | if current != nil { |
| 118 | current.Diff = strings.TrimSuffix(buf.String(), "\n") |
| 119 | finalizeDiff(ctx, current, repoDir, ref, runner) |
| 120 | diffs = append(diffs, *current) |
| 121 | buf.Reset() |
| 122 | } |
| 123 | current = &model.Diff{ |
| 124 | OldPath: oldPath, |
| 125 | NewPath: newPath, |
| 126 | } |
| 127 | inHunk = false |
| 128 | } |
| 129 | if current == nil { |
| 130 | continue |
| 131 | } |
| 132 | |
| 133 | switch { |
| 134 | case strings.HasPrefix(line, "@@"): |
| 135 | inHunk = true |
| 136 | // The object IDs and mode in Git's extended "index" header are not |
| 137 | // useful review context. Keep index text in hunks, where it is file |
| 138 | // content and therefore carries a diff prefix. |
| 139 | case !inHunk && strings.HasPrefix(line, "index "): |
| 140 | continue |
| 141 | case !inHunk && binaryRe.MatchString(line): |
| 142 | current.IsBinary = true |
| 143 | // Extended header lines (unambiguous: content lines always carry a |
| 144 | // leading "+", "-" or " " prefix, so a bare prefix match is safe). |
| 145 | case strings.HasPrefix(line, "new file mode "): |
| 146 | current.IsNew = true |
| 147 | case strings.HasPrefix(line, "deleted file mode "): |
| 148 | current.IsDeleted = true |
| 149 | case strings.HasPrefix(line, "rename from "): |
| 150 | // Authoritative old path for renames; more reliable than the |
| 151 | // "diff --git" header when paths contain spaces. |
| 152 | current.OldPath = unquoteRenamePath(strings.TrimPrefix(line, "rename from ")) |
| 153 | current.IsRenamed = true |
| 154 | case strings.HasPrefix(line, "rename to "): |
| 155 | current.NewPath = unquoteRenamePath(strings.TrimPrefix(line, "rename to ")) |