GetDiff returns all changes as parsed model.Diff structs.
(ctx context.Context)
| 169 | |
| 170 | // GetDiff returns all changes as parsed model.Diff structs. |
| 171 | func (p *Provider) GetDiff(ctx context.Context) ([]model.Diff, error) { |
| 172 | var combined strings.Builder |
| 173 | |
| 174 | switch p.mode { |
| 175 | case ModeRange: |
| 176 | base := p.MergeBase(ctx) |
| 177 | if base == "" { |
| 178 | return nil, fmt.Errorf("cannot find merge-base between %s and %s", p.from, p.to) |
| 179 | } |
| 180 | out, err := p.runGit(ctx, "-c", "core.quotepath=false", "diff", "--no-ext-diff", "--no-textconv", "--find-renames", "--src-prefix=a/", "--dst-prefix=b/", "--no-color", "-U"+fmt.Sprint(DiffContextLines), "--end-of-options", base, p.to, "--") |
| 181 | if err != nil { |
| 182 | return nil, fmt.Errorf("git diff failed: %w", err) |
| 183 | } |
| 184 | combined.WriteString(out) |
| 185 | |
| 186 | case ModeCommit: |
| 187 | // --diff-merges=first-parent: for merge commits, plain `git show` |
| 188 | // emits a combined diff ("diff --cc"), which ParseDiffText cannot |
| 189 | // parse — the commit would silently yield zero reviewable diffs. |
| 190 | // Diffs against the first parent instead, in regular unified format. |
| 191 | out, err := p.runGit(ctx, "-c", "core.quotepath=false", "show", "--no-ext-diff", "--no-textconv", "--find-renames", "--src-prefix=a/", "--dst-prefix=b/", "--no-color", "--diff-merges=first-parent", "-U"+fmt.Sprint(DiffContextLines), "--end-of-options", p.commit) |
| 192 | if err != nil { |
| 193 | return nil, fmt.Errorf("git show failed: %w", err) |
| 194 | } |
| 195 | combined.WriteString(out) |
| 196 | |
| 197 | case ModeWorkspace: |
| 198 | tracked, err := p.workspaceTrackedDiff(ctx) |
| 199 | if err != nil { |
| 200 | return nil, fmt.Errorf("workspace tracked diff failed: %w", err) |
| 201 | } |
| 202 | combined.WriteString(tracked) |
| 203 | |
| 204 | untracked, err := p.untrackedFileDiffs(ctx) |
| 205 | if err != nil { |
| 206 | return nil, fmt.Errorf("untracked file diff failed: %w", err) |
| 207 | } |
| 208 | for _, ud := range untracked { |
| 209 | combined.WriteString(ud) |
| 210 | combined.WriteString("\n\n") |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | var ref string |
| 215 | switch p.mode { |
| 216 | case ModeRange: |
| 217 | ref = p.to |
| 218 | case ModeCommit: |
| 219 | ref = p.commit |
| 220 | } |
| 221 | |
| 222 | diffs, err := ParseDiffText(ctx, combined.String(), p.repoDir, ref, p.runner) |
| 223 | if err != nil { |
| 224 | return nil, err |
| 225 | } |
| 226 | return p.filterDiffs(diffs), nil |
| 227 | } |
| 228 |