diff options
author | zeripath <art27@cantab.net> | 2020-05-26 06:58:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-26 01:58:07 -0400 |
commit | 5cb201dc93bf41556556f2154ea28755907fd550 (patch) | |
tree | eefbd70bb80a2b2204a18b776f9d0198855e087b /modules | |
parent | b97917a6e7b7a7852d1c0f39a96e06eeb7aab6de (diff) | |
download | gitea-5cb201dc93bf41556556f2154ea28755907fd550.tar.gz gitea-5cb201dc93bf41556556f2154ea28755907fd550.zip |
Fix numbr of files, total additions, and deletions (#11614)
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/git/repo_compare.go | 89 | ||||
-rw-r--r-- | modules/repofiles/diff_test.go | 1 | ||||
-rw-r--r-- | modules/repofiles/temp_repo.go | 5 |
3 files changed, 93 insertions, 2 deletions
diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index 5bc7f9ca5a..5faadcf3f0 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -6,9 +6,11 @@ package git import ( + "bytes" "container/list" "fmt" "io" + "regexp" "strconv" "strings" "time" @@ -84,14 +86,97 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string) } // Count number of changed files. - stdout, err := NewCommand("diff", "--name-only", remoteBranch+"..."+headBranch).RunInDir(repo.Path) + // This probably should be removed as we need to use shortstat elsewhere + // Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly + compareInfo.NumFiles, err = repo.GetDiffNumChangedFiles(remoteBranch, headBranch) if err != nil { return nil, err } - compareInfo.NumFiles = len(strings.Split(stdout, "\n")) - 1 return compareInfo, nil } +type lineCountWriter struct { + numLines int +} + +// Write counts the number of newlines in the provided bytestream +func (l *lineCountWriter) Write(p []byte) (n int, err error) { + n = len(p) + l.numLines += bytes.Count(p, []byte{'\000'}) + return +} + +// GetDiffNumChangedFiles counts the number of changed files +// This is substantially quicker than shortstat but... +func (repo *Repository) GetDiffNumChangedFiles(base, head string) (int, error) { + // Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly + w := &lineCountWriter{} + stderr := new(bytes.Buffer) + + if err := NewCommand("diff", "-z", "--name-only", base+"..."+head). + RunInDirPipeline(repo.Path, w, stderr); err != nil { + return 0, fmt.Errorf("%v: Stderr: %s", err, stderr) + } + return w.numLines, nil +} + +// GetDiffShortStat counts number of changed files, number of additions and deletions +func (repo *Repository) GetDiffShortStat(base, head string) (numFiles, totalAdditions, totalDeletions int, err error) { + return GetDiffShortStat(repo.Path, base+"..."+head) +} + +// GetDiffShortStat counts number of changed files, number of additions and deletions +func GetDiffShortStat(repoPath string, args ...string) (numFiles, totalAdditions, totalDeletions int, err error) { + // Now if we call: + // $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875 + // we get: + // " 9902 files changed, 2034198 insertions(+), 298800 deletions(-)\n" + args = append([]string{ + "diff", + "--shortstat", + }, args...) + + stdout, err := NewCommand(args...).RunInDir(repoPath) + if err != nil { + return 0, 0, 0, err + } + + return parseDiffStat(stdout) +} + +var shortStatFormat = regexp.MustCompile( + `\s*(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?`) + +func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int, err error) { + if len(stdout) == 0 || stdout == "\n" { + return 0, 0, 0, nil + } + groups := shortStatFormat.FindStringSubmatch(stdout) + if len(groups) != 4 { + return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s groups: %s", stdout, groups) + } + + numFiles, err = strconv.Atoi(groups[1]) + if err != nil { + return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumFiles %v", stdout, err) + } + + if len(groups[2]) != 0 { + totalAdditions, err = strconv.Atoi(groups[2]) + if err != nil { + return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumAdditions %v", stdout, err) + } + } + + if len(groups[3]) != 0 { + totalDeletions, err = strconv.Atoi(groups[3]) + if err != nil { + return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumDeletions %v", stdout, err) + } + } + return +} + // GetDiffOrPatch generates either diff or formatted patch data between given revisions func (repo *Repository) GetDiffOrPatch(base, head string, w io.Writer, formatted bool) error { if formatted { diff --git a/modules/repofiles/diff_test.go b/modules/repofiles/diff_test.go index 4e1d5b13eb..5c09e180f3 100644 --- a/modules/repofiles/diff_test.go +++ b/modules/repofiles/diff_test.go @@ -108,6 +108,7 @@ func TestGetDiffPreview(t *testing.T) { }, IsIncomplete: false, } + expectedDiff.NumFiles = len(expectedDiff.Files) t.Run("with given branch", func(t *testing.T) { diff, err := GetDiffPreview(ctx.Repo.Repository, branch, treePath, content) diff --git a/modules/repofiles/temp_repo.go b/modules/repofiles/temp_repo.go index 89f9b0b208..2b03db8b4a 100644 --- a/modules/repofiles/temp_repo.go +++ b/modules/repofiles/temp_repo.go @@ -299,6 +299,11 @@ func (t *TemporaryUploadRepository) DiffIndex() (*gitdiff.Diff, error) { t.repo.FullName(), err, stderr) } + diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(t.basePath, "--cached", "HEAD") + if err != nil { + return nil, err + } + return diff, nil } |