Fix #25326 ---------
Showing the changes to services/gitdiff/gitdiff.go (1 of 4 files). Show all
Unified · Split
| @@ -1229,6 +1229,42 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 1228 unmodified lines | ||
| 1229 | 1229 | return diff, nil |
| 1230 | 1230 | } |
| 1231 | 1231 | |
| 1232 | +type PullDiffStats struct { | |
| 1233 | + TotalAddition, TotalDeletion int | |
| 1234 | +} | |
| 1235 | + | |
| 1236 | +// GetPullDiffStats | |
| 1237 | +func GetPullDiffStats(gitRepo *git.Repository, opts *DiffOptions) (*PullDiffStats, error) { | |
| 1238 | + repoPath := gitRepo.Path | |
| 1239 | + | |
| 1240 | + diff := &PullDiffStats{} | |
| 1241 | + | |
| 1242 | + separator := "..." | |
| 1243 | + if opts.DirectComparison { | |
| 1244 | + separator = ".." | |
| 1245 | + } | |
| 1246 | + | |
| 1247 | + diffPaths := []string{opts.BeforeCommitID + separator + opts.AfterCommitID} | |
| 1248 | + if len(opts.BeforeCommitID) == 0 || opts.BeforeCommitID == git.EmptySHA { | |
| 1249 | + diffPaths = []string{git.EmptyTreeSHA, opts.AfterCommitID} | |
| 1250 | + } | |
| 1251 | + | |
| 1252 | + var err error | |
| 1253 | + | |
| 1254 | + _, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(gitRepo.Ctx, repoPath, nil, diffPaths...) | |
| 1255 | + if err != nil && strings.Contains(err.Error(), "no merge base") { | |
| 1256 | + // git >= 2.28 now returns an error if base and head have become unrelated. | |
| 1257 | + // previously it would return the results of git diff --shortstat base head so let's try that... | |
| 1258 | + diffPaths = []string{opts.BeforeCommitID, opts.AfterCommitID} | |
| 1259 | + _, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(gitRepo.Ctx, repoPath, nil, diffPaths...) | |
| 1260 | + } | |
| 1261 | + if err != nil { | |
| 1262 | + return nil, err | |
| 1263 | + } | |
| 1264 | + | |
| 1265 | + return diff, nil | |
| 1266 | +} | |
| 1267 | + | |
| 1232 | 1268 | // SyncAndGetUserSpecificDiff is like GetDiff, except that user specific data such as which files the given user has already viewed on the given PR will also be set |
| 1233 | 1269 | // Additionally, the database asynchronously is updated if files have changed since the last review |
| 1234 | 1270 | func SyncAndGetUserSpecificDiff(ctx context.Context, userID int64, pull *issues_model.PullRequest, gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff, error) { |