Skip to content

Commit 224f047

Browse files
committed
add PathFilter func(string) bool to LogOptions
Signed-off-by: Saeed Rasooli <[email protected]>
1 parent f54ee6d commit 224f047

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

options.go

+7
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,15 @@ type LogOptions struct {
343343

344344
// Show only those commits in which the specified file was inserted/updated.
345345
// It is equivalent to running `git log -- <file-name>`.
346+
// this field is kept for compatility, it can be replaced with PathFilter
346347
FileName *string
347348

349+
// Filter commits based on the path of files that are updated
350+
// takes file path as argument and should return true if the file is desired
351+
// It can be used to implement `git log -- <path>`
352+
// either <path> is a file path, or directory path, or a regexp of file/directory path
353+
PathFilter func(string) bool
354+
348355
// Pretend as if all the refs in refs/, along with HEAD, are listed on the command line as <commit>.
349356
// It is equivalent to running `git log --all`.
350357
// If set on true, the From option will be ignored.

repository.go

+11
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,9 @@ func (r *Repository) Log(o *LogOptions) (object.CommitIter, error) {
10671067
// for `git log --all` also check parent (if the next commit comes from the real parent)
10681068
it = r.logWithFile(*o.FileName, it, o.All)
10691069
}
1070+
if o.PathFilter != nil {
1071+
it = r.logWithPathFilter(o.PathFilter, it, o.All)
1072+
}
10701073

10711074
if o.Since != nil || o.Until != nil {
10721075
limitOptions := object.LogLimitOptions{Since: o.Since, Until: o.Until}
@@ -1108,6 +1111,14 @@ func (*Repository) logWithFile(fileName string, commitIter object.CommitIter, ch
11081111
)
11091112
}
11101113

1114+
func (*Repository) logWithPathFilter(pathFilter func(string) bool, commitIter object.CommitIter, checkParent bool) object.CommitIter {
1115+
return object.NewCommitPathIterFromIter(
1116+
pathFilter,
1117+
commitIter,
1118+
checkParent,
1119+
)
1120+
}
1121+
11111122
func (*Repository) logWithLimit(commitIter object.CommitIter, limitOptions object.LogLimitOptions) object.CommitIter {
11121123
return object.NewCommitLimitIterFromIter(commitIter, limitOptions)
11131124
}

repository_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"os/exec"
1212
"path/filepath"
13+
"regexp"
1314
"strings"
1415
"testing"
1516
"time"
@@ -1679,6 +1680,20 @@ func (s *RepositorySuite) TestLogFileWithError(c *C) {
16791680
c.Assert(err, NotNil)
16801681
}
16811682

1683+
func (s *RepositorySuite) TestLogPathRegexpWithError(c *C) {
1684+
pathRE := regexp.MustCompile("R.*E")
1685+
pathIter := func(path string) bool {
1686+
return pathRE.MatchString(path)
1687+
}
1688+
cIter := object.NewCommitPathIterFromIter(pathIter, &mockErrCommitIter{}, false)
1689+
defer cIter.Close()
1690+
1691+
err := cIter.ForEach(func(commit *object.Commit) error {
1692+
return nil
1693+
})
1694+
c.Assert(err, NotNil)
1695+
}
1696+
16821697
func (s *RepositorySuite) TestLogLimitNext(c *C) {
16831698
r, _ := Init(memory.NewStorage(), nil)
16841699
err := r.clone(context.Background(), &CloneOptions{

0 commit comments

Comments
 (0)