Skip to content

Commit 59d1cc4

Browse files
zeripath6543
andauthored
Fix paging of file commit logs (#14831)
Unfortunately `git log revision ... --skip=x -- path` skips the number of commits not the number of commits relating to the path. This PR changes the function to have a reader that reads and skips the necessary number of commits by hand instead. Fix #8716 Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 0bdeb2b commit 59d1cc4

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

modules/git/repo_commit.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ package git
88
import (
99
"bytes"
1010
"container/list"
11+
"io"
12+
"io/ioutil"
1113
"strconv"
1214
"strings"
1315
)
@@ -232,8 +234,38 @@ func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
232234

233235
// CommitsByFileAndRange return the commits according revison file and the page
234236
func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (*list.List, error) {
235-
stdout, err := NewCommand("log", revision, "--follow", "--skip="+strconv.Itoa((page-1)*50),
236-
"--max-count="+strconv.Itoa(CommitsRangeSize), prettyLogFormat, "--", file).RunInDirBytes(repo.Path)
237+
skip := (page - 1) * CommitsRangeSize
238+
239+
stdoutReader, stdoutWriter := io.Pipe()
240+
defer func() {
241+
_ = stdoutReader.Close()
242+
_ = stdoutWriter.Close()
243+
}()
244+
go func() {
245+
stderr := strings.Builder{}
246+
err := NewCommand("log", revision, "--follow",
247+
"--max-count="+strconv.Itoa(CommitsRangeSize*page),
248+
prettyLogFormat, "--", file).
249+
RunInDirPipeline(repo.Path, stdoutWriter, &stderr)
250+
if err != nil {
251+
_ = stdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
252+
} else {
253+
_ = stdoutWriter.Close()
254+
}
255+
}()
256+
257+
if skip > 0 {
258+
_, err := io.CopyN(ioutil.Discard, stdoutReader, int64(skip*41))
259+
if err != nil {
260+
if err == io.EOF {
261+
return list.New(), nil
262+
}
263+
_ = stdoutReader.CloseWithError(err)
264+
return nil, err
265+
}
266+
}
267+
268+
stdout, err := ioutil.ReadAll(stdoutReader)
237269
if err != nil {
238270
return nil, err
239271
}

0 commit comments

Comments
 (0)