History

Support displaying diff stats in PR tab bar (#25387)

Fix #25326 ---------

hiifong committed 3 years ago

Commit 36f1fa71 parent eab011d4 files · +94 −5Browse files

Showing the changes to services/gitdiff/gitdiff.go (1 of 4 files). Show all

Unified · Split

services/gitdiff/gitdiff.go+36−0
@@ -1229,6 +1229,42 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff · 1228 unmodified lines
1229 return diff, nil1229 return diff, nil
1230}1230}
1231 1231
1232type PullDiffStats struct {
1233 TotalAddition, TotalDeletion int
1234}
1235
1236// GetPullDiffStats
1237func 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// 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 set1268// 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// Additionally, the database asynchronously is updated if files have changed since the last review1269// Additionally, the database asynchronously is updated if files have changed since the last review
1234func SyncAndGetUserSpecificDiff(ctx context.Context, userID int64, pull *issues_model.PullRequest, gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff, error) {1270func SyncAndGetUserSpecificDiff(ctx context.Context, userID int64, pull *issues_model.PullRequest, gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff, error) {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t