MCPcopy Create free account
hub / github.com/alibaba/open-code-review / GetDiff

Method GetDiff

internal/diff/git.go:174–236  ·  view source on GitHub ↗

GetDiff returns all changes as parsed model.Diff structs.

(ctx context.Context)

Source from the content-addressed store, hash-verified

172
173// GetDiff returns all changes as parsed model.Diff structs.
174func (p *Provider) GetDiff(ctx context.Context) ([]model.Diff, error) {
175 var combined strings.Builder
176
177 switch p.mode {
178 case ModeRange:
179 base := p.MergeBase(ctx)
180 if base == "" {
181 return nil, fmt.Errorf("cannot find merge-base between %s and %s", p.from, p.to)
182 }
183 out, stderr, err := p.runGitSplit(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, "--")
184 if err != nil {
185 return nil, gitFailure("git diff", stderr, err)
186 }
187 combined.WriteString(out)
188
189 case ModeCommit:
190 // --diff-merges=first-parent: for merge commits, plain `git show`
191 // emits a combined diff ("diff --cc"), which ParseDiffText cannot
192 // parse — the commit would silently yield zero reviewable diffs.
193 // Diffs against the first parent instead, in regular unified format.
194 out, stderr, err := p.runGitSplit(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)
195 if err != nil {
196 return nil, gitFailure("git show", stderr, err)
197 }
198 combined.WriteString(out)
199
200 case ModeWorkspace:
201 // The stderr returned here is the fallback's (`git diff --staged`), and
202 // that is the one worth surfacing. Reaching the fallback at all means
203 // `git diff HEAD` already failed, and in the case the fallback exists
204 // for — a repository with no commits — it failed with "bad revision
205 // 'HEAD'", which is expected rather than diagnostic. Only the second
206 // failure describes what actually blocked the review.
207 tracked, stderr, err := p.workspaceTrackedDiff(ctx)
208 if err != nil {
209 return nil, gitFailure("workspace tracked diff", stderr, err)
210 }
211 combined.WriteString(tracked)
212
213 untracked, err := p.untrackedFileDiffs(ctx)
214 if err != nil {
215 return nil, fmt.Errorf("untracked file diff failed: %w", err)
216 }
217 for _, ud := range untracked {
218 combined.WriteString(ud)
219 combined.WriteString("\n\n")
220 }
221 }
222
223 var ref string
224 switch p.mode {
225 case ModeRange:
226 ref = p.to
227 case ModeCommit:
228 ref = p.commit
229 }
230
231 diffs, err := ParseDiffText(ctx, combined.String(), p.repoDir, ref, p.runner)

Calls 8

MergeBaseMethod · 0.95
runGitSplitMethod · 0.95
workspaceTrackedDiffMethod · 0.95
untrackedFileDiffsMethod · 0.95
filterDiffsMethod · 0.95
gitFailureFunction · 0.85
ParseDiffTextFunction · 0.85
StringMethod · 0.45