Skip to content

Commit 5cb201d

Browse files
Fix numbr of files, total additions, and deletions (#11614)
Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent b97917a commit 5cb201d

File tree

8 files changed

+106
-15
lines changed

8 files changed

+106
-15
lines changed

modules/git/repo_compare.go

+87-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
package git
77

88
import (
9+
"bytes"
910
"container/list"
1011
"fmt"
1112
"io"
13+
"regexp"
1214
"strconv"
1315
"strings"
1416
"time"
@@ -84,14 +86,97 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string)
8486
}
8587

8688
// Count number of changed files.
87-
stdout, err := NewCommand("diff", "--name-only", remoteBranch+"..."+headBranch).RunInDir(repo.Path)
89+
// This probably should be removed as we need to use shortstat elsewhere
90+
// Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly
91+
compareInfo.NumFiles, err = repo.GetDiffNumChangedFiles(remoteBranch, headBranch)
8892
if err != nil {
8993
return nil, err
9094
}
91-
compareInfo.NumFiles = len(strings.Split(stdout, "\n")) - 1
9295
return compareInfo, nil
9396
}
9497

98+
type lineCountWriter struct {
99+
numLines int
100+
}
101+
102+
// Write counts the number of newlines in the provided bytestream
103+
func (l *lineCountWriter) Write(p []byte) (n int, err error) {
104+
n = len(p)
105+
l.numLines += bytes.Count(p, []byte{'\000'})
106+
return
107+
}
108+
109+
// GetDiffNumChangedFiles counts the number of changed files
110+
// This is substantially quicker than shortstat but...
111+
func (repo *Repository) GetDiffNumChangedFiles(base, head string) (int, error) {
112+
// Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly
113+
w := &lineCountWriter{}
114+
stderr := new(bytes.Buffer)
115+
116+
if err := NewCommand("diff", "-z", "--name-only", base+"..."+head).
117+
RunInDirPipeline(repo.Path, w, stderr); err != nil {
118+
return 0, fmt.Errorf("%v: Stderr: %s", err, stderr)
119+
}
120+
return w.numLines, nil
121+
}
122+
123+
// GetDiffShortStat counts number of changed files, number of additions and deletions
124+
func (repo *Repository) GetDiffShortStat(base, head string) (numFiles, totalAdditions, totalDeletions int, err error) {
125+
return GetDiffShortStat(repo.Path, base+"..."+head)
126+
}
127+
128+
// GetDiffShortStat counts number of changed files, number of additions and deletions
129+
func GetDiffShortStat(repoPath string, args ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
130+
// Now if we call:
131+
// $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875
132+
// we get:
133+
// " 9902 files changed, 2034198 insertions(+), 298800 deletions(-)\n"
134+
args = append([]string{
135+
"diff",
136+
"--shortstat",
137+
}, args...)
138+
139+
stdout, err := NewCommand(args...).RunInDir(repoPath)
140+
if err != nil {
141+
return 0, 0, 0, err
142+
}
143+
144+
return parseDiffStat(stdout)
145+
}
146+
147+
var shortStatFormat = regexp.MustCompile(
148+
`\s*(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?`)
149+
150+
func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int, err error) {
151+
if len(stdout) == 0 || stdout == "\n" {
152+
return 0, 0, 0, nil
153+
}
154+
groups := shortStatFormat.FindStringSubmatch(stdout)
155+
if len(groups) != 4 {
156+
return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s groups: %s", stdout, groups)
157+
}
158+
159+
numFiles, err = strconv.Atoi(groups[1])
160+
if err != nil {
161+
return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumFiles %v", stdout, err)
162+
}
163+
164+
if len(groups[2]) != 0 {
165+
totalAdditions, err = strconv.Atoi(groups[2])
166+
if err != nil {
167+
return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumAdditions %v", stdout, err)
168+
}
169+
}
170+
171+
if len(groups[3]) != 0 {
172+
totalDeletions, err = strconv.Atoi(groups[3])
173+
if err != nil {
174+
return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumDeletions %v", stdout, err)
175+
}
176+
}
177+
return
178+
}
179+
95180
// GetDiffOrPatch generates either diff or formatted patch data between given revisions
96181
func (repo *Repository) GetDiffOrPatch(base, head string, w io.Writer, formatted bool) error {
97182
if formatted {

modules/repofiles/diff_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func TestGetDiffPreview(t *testing.T) {
108108
},
109109
IsIncomplete: false,
110110
}
111+
expectedDiff.NumFiles = len(expectedDiff.Files)
111112

112113
t.Run("with given branch", func(t *testing.T) {
113114
diff, err := GetDiffPreview(ctx.Repo.Repository, branch, treePath, content)

modules/repofiles/temp_repo.go

+5
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ func (t *TemporaryUploadRepository) DiffIndex() (*gitdiff.Diff, error) {
299299
t.repo.FullName(), err, stderr)
300300
}
301301

302+
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(t.basePath, "--cached", "HEAD")
303+
if err != nil {
304+
return nil, err
305+
}
306+
302307
return diff, nil
303308
}
304309

routers/repo/commit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func Diff(ctx *context.Context) {
291291
ctx.Data["Author"] = models.ValidateCommitWithEmail(commit)
292292
ctx.Data["Diff"] = diff
293293
ctx.Data["Parents"] = parents
294-
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
294+
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
295295

296296
if err := models.CalculateTrustStatus(verification, ctx.Repo.Repository, nil); err != nil {
297297
ctx.ServerError("CalculateTrustStatus", err)

routers/repo/compare.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func PrepareCompareDiff(
437437
return false
438438
}
439439
ctx.Data["Diff"] = diff
440-
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
440+
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
441441

442442
headCommit, err := headGitRepo.GetCommit(headCommitID)
443443
if err != nil {

routers/repo/editor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ func DiffPreviewPost(ctx *context.Context, form auth.EditPreviewDiffForm) {
339339
return
340340
}
341341

342-
if diff.NumFiles() == 0 {
342+
if diff.NumFiles == 0 {
343343
ctx.PlainText(200, []byte(ctx.Tr("repo.editor.no_changes_to_show")))
344344
return
345345
}

routers/repo/pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ func ViewPullFiles(ctx *context.Context) {
611611
}
612612

613613
ctx.Data["Diff"] = diff
614-
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
614+
ctx.Data["DiffNotAvailable"] = diff.NumFiles == 0
615615

616616
baseCommit, err := ctx.Repo.GitRepo.GetCommit(startCommitID)
617617
if err != nil {

services/gitdiff/gitdiff.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ func getCommitFileLineCount(commit *git.Commit, filePath string) int {
367367

368368
// Diff represents a difference between two git trees.
369369
type Diff struct {
370-
TotalAddition, TotalDeletion int
371-
Files []*DiffFile
372-
IsIncomplete bool
370+
NumFiles, TotalAddition, TotalDeletion int
371+
Files []*DiffFile
372+
IsIncomplete bool
373373
}
374374

375375
// LoadComments loads comments into each line
@@ -398,11 +398,6 @@ func (diff *Diff) LoadComments(issue *models.Issue, currentUser *models.User) er
398398
return nil
399399
}
400400

401-
// NumFiles returns number of files changes in a diff.
402-
func (diff *Diff) NumFiles() int {
403-
return len(diff.Files)
404-
}
405-
406401
const cmdDiffHead = "diff --git "
407402

408403
// ParsePatch builds a Diff object from a io.Reader and some
@@ -639,7 +634,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
639634
}
640635
}
641636
}
642-
637+
diff.NumFiles = len(diff.Files)
643638
return diff, nil
644639
}
645640

@@ -716,6 +711,11 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
716711
return nil, fmt.Errorf("Wait: %v", err)
717712
}
718713

714+
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, beforeCommitID+"..."+afterCommitID)
715+
if err != nil {
716+
return nil, err
717+
}
718+
719719
return diff, nil
720720
}
721721

0 commit comments

Comments
 (0)