aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSaeed Rasooli <saeed.gnu@gmail.com>2019-11-29 13:39:29 +0330
committerSaeed Rasooli <saeed.gnu@gmail.com>2019-11-29 16:46:44 +0330
commit224f047f4f95b44434f84303746c37439929e02b (patch)
treec5e20545bedd8da274e8dff25b5b0d71771ab05c
parentf54ee6d2b1f92d67ed567027f7a8ec1f2139c532 (diff)
downloadgo-git-224f047f4f95b44434f84303746c37439929e02b.tar.gz
add `PathFilter func(string) bool` to LogOptions
Signed-off-by: Saeed Rasooli <saeed.gnu@gmail.com>
-rw-r--r--options.go7
-rw-r--r--repository.go11
-rw-r--r--repository_test.go15
3 files changed, 33 insertions, 0 deletions
diff --git a/options.go b/options.go
index cc8f9c5..9a03560 100644
--- a/options.go
+++ b/options.go
@@ -343,8 +343,15 @@ type LogOptions struct {
// Show only those commits in which the specified file was inserted/updated.
// It is equivalent to running `git log -- <file-name>`.
+ // this field is kept for compatility, it can be replaced with PathFilter
FileName *string
+ // Filter commits based on the path of files that are updated
+ // takes file path as argument and should return true if the file is desired
+ // It can be used to implement `git log -- <path>`
+ // either <path> is a file path, or directory path, or a regexp of file/directory path
+ PathFilter func(string) bool
+
// Pretend as if all the refs in refs/, along with HEAD, are listed on the command line as <commit>.
// It is equivalent to running `git log --all`.
// If set on true, the From option will be ignored.
diff --git a/repository.go b/repository.go
index 2bb2afb..1e3e339 100644
--- a/repository.go
+++ b/repository.go
@@ -1067,6 +1067,9 @@ func (r *Repository) Log(o *LogOptions) (object.CommitIter, error) {
// for `git log --all` also check parent (if the next commit comes from the real parent)
it = r.logWithFile(*o.FileName, it, o.All)
}
+ if o.PathFilter != nil {
+ it = r.logWithPathFilter(o.PathFilter, it, o.All)
+ }
if o.Since != nil || o.Until != nil {
limitOptions := object.LogLimitOptions{Since: o.Since, Until: o.Until}
@@ -1108,6 +1111,14 @@ func (*Repository) logWithFile(fileName string, commitIter object.CommitIter, ch
)
}
+func (*Repository) logWithPathFilter(pathFilter func(string) bool, commitIter object.CommitIter, checkParent bool) object.CommitIter {
+ return object.NewCommitPathIterFromIter(
+ pathFilter,
+ commitIter,
+ checkParent,
+ )
+}
+
func (*Repository) logWithLimit(commitIter object.CommitIter, limitOptions object.LogLimitOptions) object.CommitIter {
return object.NewCommitLimitIterFromIter(commitIter, limitOptions)
}
diff --git a/repository_test.go b/repository_test.go
index f9e3ca4..2bca078 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
+ "regexp"
"strings"
"testing"
"time"
@@ -1679,6 +1680,20 @@ func (s *RepositorySuite) TestLogFileWithError(c *C) {
c.Assert(err, NotNil)
}
+func (s *RepositorySuite) TestLogPathRegexpWithError(c *C) {
+ pathRE := regexp.MustCompile("R.*E")
+ pathIter := func(path string) bool {
+ return pathRE.MatchString(path)
+ }
+ cIter := object.NewCommitPathIterFromIter(pathIter, &mockErrCommitIter{}, false)
+ defer cIter.Close()
+
+ err := cIter.ForEach(func(commit *object.Commit) error {
+ return nil
+ })
+ c.Assert(err, NotNil)
+}
+
func (s *RepositorySuite) TestLogLimitNext(c *C) {
r, _ := Init(memory.NewStorage(), nil)
err := r.clone(context.Background(), &CloneOptions{