GetDiff returns all changes as parsed model.Diff structs.
(ctx context.Context)
| 108 | |
| 109 | // GetDiff returns all changes as parsed model.Diff structs. |
| 110 | func (p *Provider) GetDiff(ctx context.Context) ([]model.Diff, error) { |
| 111 | var combined strings.Builder |
| 112 | |
| 113 | switch p.mode { |
| 114 | case ModeRange: |
| 115 | base := p.MergeBase(ctx) |
| 116 | if base == "" { |
| 117 | return nil, fmt.Errorf("cannot find merge-base between %s and %s", p.from, p.to) |
| 118 | } |
| 119 | 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, "--") |
| 120 | if err != nil { |
| 121 | return nil, fmt.Errorf("git diff failed: %w", err) |
| 122 | } |
| 123 | combined.WriteString(out) |
| 124 | |
| 125 | case ModeCommit: |
| 126 | // --diff-merges=first-parent: for merge commits, plain `git show` |
| 127 | // emits a combined diff ("diff --cc"), which ParseDiffText cannot |
| 128 | // parse — the commit would silently yield zero reviewable diffs. |
| 129 | // Diffs against the first parent instead, in regular unified format. |
| 130 | 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) |
| 131 | if err != nil { |
| 132 | return nil, fmt.Errorf("git show failed: %w", err) |
| 133 | } |
| 134 | combined.WriteString(out) |
| 135 | |
| 136 | case ModeWorkspace: |
| 137 | tracked, err := p.workspaceTrackedDiff(ctx) |
| 138 | if err != nil { |
| 139 | return nil, fmt.Errorf("workspace tracked diff failed: %w", err) |
| 140 | } |
| 141 | combined.WriteString(tracked) |
| 142 | |
| 143 | untracked, err := p.untrackedFileDiffs(ctx) |
| 144 | if err != nil { |
| 145 | return nil, fmt.Errorf("untracked file diff failed: %w", err) |
| 146 | } |
| 147 | for _, ud := range untracked { |
| 148 | combined.WriteString(ud) |
| 149 | combined.WriteString("\n\n") |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | var ref string |
| 154 | switch p.mode { |
| 155 | case ModeRange: |
| 156 | ref = p.to |
| 157 | case ModeCommit: |
| 158 | ref = p.commit |
| 159 | } |
| 160 | |
| 161 | diffs, err := ParseDiffText(ctx, combined.String(), p.repoDir, ref, p.runner) |
| 162 | if err != nil { |
| 163 | return nil, err |
| 164 | } |
| 165 | return p.filterDiffs(diffs), nil |
| 166 | } |
| 167 |