Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix paging of file commit logs #14831

Merged
merged 11 commits into from
Mar 4, 2021
36 changes: 34 additions & 2 deletions modules/git/repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package git
import (
"bytes"
"container/list"
"io"
"io/ioutil"
"strconv"
"strings"
)
Expand Down Expand Up @@ -232,8 +234,38 @@ func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {

// CommitsByFileAndRange return the commits according revison file and the page
func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (*list.List, error) {
stdout, err := NewCommand("log", revision, "--follow", "--skip="+strconv.Itoa((page-1)*50),
"--max-count="+strconv.Itoa(CommitsRangeSize), prettyLogFormat, "--", file).RunInDirBytes(repo.Path)
skip := (page - 1) * CommitsRangeSize

stdoutReader, stdoutWriter := io.Pipe()
defer func() {
_ = stdoutReader.Close()
_ = stdoutWriter.Close()
}()
go func() {
stderr := strings.Builder{}
err := NewCommand("log", revision, "--follow",
"--max-count="+strconv.Itoa(CommitsRangeSize*page),
prettyLogFormat, "--", file).
RunInDirPipeline(repo.Path, stdoutWriter, &stderr)
if err != nil {
_ = stdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
} else {
_ = stdoutWriter.Close()
}
}()

if skip > 0 {
_, err := io.CopyN(ioutil.Discard, stdoutReader, int64(skip*41))
if err != nil {
if err == io.EOF {
return list.New(), nil
}
_ = stdoutReader.CloseWithError(err)
return nil, err
}
}

stdout, err := ioutil.ReadAll(stdoutReader)
if err != nil {
return nil, err
}
Expand Down