Skip to content

Commit d4c7f35

Browse files
zeripath6543
andcommitted
Fix paging of file commit logs (go-gitea#14831)
Backport go-gitea#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 go-gitea#8716 Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 7e85cba commit d4c7f35

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

modules/git/repo_commit.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"bytes"
1010
"container/list"
1111
"fmt"
12+
"io"
13+
"io/ioutil"
1214
"strconv"
1315
"strings"
1416

@@ -327,8 +329,41 @@ func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
327329

328330
// CommitsByFileAndRange return the commits according revison file and the page
329331
func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (*list.List, error) {
330-
stdout, err := NewCommand("log", revision, "--follow", "--skip="+strconv.Itoa((page-1)*50),
331-
"--max-count="+strconv.Itoa(CommitsRangeSize), prettyLogFormat, "--", file).RunInDirBytes(repo.Path)
332+
skip := (page - 1) * CommitsRangeSize
333+
334+
stdoutReader, stdoutWriter := io.Pipe()
335+
defer func() {
336+
_ = stdoutReader.Close()
337+
_ = stdoutWriter.Close()
338+
}()
339+
go func() {
340+
stderr := strings.Builder{}
341+
err := NewCommand("log", revision, "--follow",
342+
"--max-count="+strconv.Itoa(CommitsRangeSize*page),
343+
prettyLogFormat, "--", file).
344+
RunInDirPipeline(repo.Path, stdoutWriter, &stderr)
345+
if err != nil {
346+
if stderr.Len() > 0 {
347+
err = fmt.Errorf("%v - %s", err, stderr.String())
348+
}
349+
_ = stdoutWriter.CloseWithError(err)
350+
} else {
351+
_ = stdoutWriter.Close()
352+
}
353+
}()
354+
355+
if skip > 0 {
356+
_, err := io.CopyN(ioutil.Discard, stdoutReader, int64(skip*41))
357+
if err != nil {
358+
if err == io.EOF {
359+
return list.New(), nil
360+
}
361+
_ = stdoutReader.CloseWithError(err)
362+
return nil, err
363+
}
364+
}
365+
366+
stdout, err := ioutil.ReadAll(stdoutReader)
332367
if err != nil {
333368
return nil, err
334369
}

0 commit comments

Comments
 (0)