diff options
author | hiifong <i@hiif.ong> | 2023-07-03 09:00:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-03 01:00:28 +0000 |
commit | 36f1fa779213695db249d2215233e9cec2d5da7c (patch) | |
tree | 5a204eb2c34cf2b7b23a26dbb74c2241696630fb /services | |
parent | eab011db58a3cfdedc6a4d88c354829c38fd84c0 (diff) | |
download | gitea-36f1fa779213695db249d2215233e9cec2d5da7c.tar.gz gitea-36f1fa779213695db249d2215233e9cec2d5da7c.zip |
Support displaying diff stats in PR tab bar (#25387)
Fix #25326
---------
Co-authored-by: silverwind <me@silverwind.io>
Diffstat (limited to 'services')
-rw-r--r-- | services/gitdiff/gitdiff.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 9adf3b9400..38283680ae 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -1229,6 +1229,42 @@ func GetDiff(gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff return diff, nil } +type PullDiffStats struct { + TotalAddition, TotalDeletion int +} + +// GetPullDiffStats +func GetPullDiffStats(gitRepo *git.Repository, opts *DiffOptions) (*PullDiffStats, error) { + repoPath := gitRepo.Path + + diff := &PullDiffStats{} + + separator := "..." + if opts.DirectComparison { + separator = ".." + } + + diffPaths := []string{opts.BeforeCommitID + separator + opts.AfterCommitID} + if len(opts.BeforeCommitID) == 0 || opts.BeforeCommitID == git.EmptySHA { + diffPaths = []string{git.EmptyTreeSHA, opts.AfterCommitID} + } + + var err error + + _, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(gitRepo.Ctx, repoPath, nil, diffPaths...) + if err != nil && strings.Contains(err.Error(), "no merge base") { + // git >= 2.28 now returns an error if base and head have become unrelated. + // previously it would return the results of git diff --shortstat base head so let's try that... + diffPaths = []string{opts.BeforeCommitID, opts.AfterCommitID} + _, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(gitRepo.Ctx, repoPath, nil, diffPaths...) + } + if err != nil { + return nil, err + } + + return diff, nil +} + // 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 // Additionally, the database asynchronously is updated if files have changed since the last review func SyncAndGetUserSpecificDiff(ctx context.Context, userID int64, pull *issues_model.PullRequest, gitRepo *git.Repository, opts *DiffOptions, files ...string) (*Diff, error) { |