Skip to content

Commit

Permalink
(#31) --since filter
Browse files Browse the repository at this point in the history
  • Loading branch information
llorllale committed Mar 3, 2019
1 parent 50f14d8 commit e5eff3a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cmd/go-gitlint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import (
// They promote coupling across different components inside the same package.
// Figure out a way to remove these global variables. Whatever command line
// parser we choose should be able to auto-generate usage.
var path = kingpin.Flag("path", `Path to the git repo ("." by default).`).Default(".").String() //nolint[gochecknoglobals]
var (
path = kingpin.Flag("path", `Path to the git repo ("." by default).`).Default(".").String() //nolint[gochecknoglobals]
since = kingpin.Flag("since", `A date in "yyyy-MM-dd" format starting from which commits will be analyzed (default: "1970-01-01")`).Default("1970-01-01").String() //nolint[gochecknoglobals]
)

func main() {
configure()
Expand All @@ -37,8 +40,11 @@ func main() {
os.Stdout, "\n",
issues.Collected(
issues.Filters(),
commits.In(
repo.Filesystem(*path),
commits.Since(
*since,
commits.In(
repo.Filesystem(*path),
),
),
),
)(),
Expand Down
20 changes: 20 additions & 0 deletions internal/commits/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package commits

import (
"strings"
"time"

"github.com/llorllale/go-gitlint/internal/repo"
git "gopkg.in/src-d/go-git.v4"
Expand All @@ -34,6 +35,7 @@ type Commits func() []*Commit
type Commit struct {
Hash string
Message string
Date time.Time
}

// ID is the commit's hash.
Expand Down Expand Up @@ -83,10 +85,28 @@ func In(repository repo.Repo) Commits {
&Commit{
Hash: c.Hash.String(),
Message: c.Message,
Date: c.Author.When,
},
)
return nil
})
return commits
}
}

// Since returns commits authored since time t (format: yyyy-MM-dd).
func Since(t string, cmts Commits) Commits {
return func() []*Commit {
start, err := time.Parse("2006-01-02", t)
if err != nil {
panic(err)
}
filtered := make([]*Commit, 0)
for _, c := range cmts() {
if !c.Date.Before(start) {
filtered = append(filtered, c)
}
}
return filtered
}
}
23 changes: 23 additions & 0 deletions internal/commits/commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/google/uuid"
"github.com/llorllale/go-gitlint/internal/repo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
Expand Down Expand Up @@ -75,6 +76,28 @@ func TestIn(t *testing.T) {
}
}

func TestSince(t *testing.T) {
before, err := time.Parse("2006-01-02", "2017-10-25")
require.NoError(t, err)
since, err := time.Parse("2006-01-02", "2019-01-01")
require.NoError(t, err)
after, err := time.Parse("2006-01-02", "2019-03-03")
require.NoError(t, err)
commits := Since(
"2019-01-01",
func() []*Commit {
return []*Commit{
{Date: before},
{Date: since},
{Date: after},
}
},
)()
assert.Len(t, commits, 2)
assert.Contains(t, commits, &Commit{Date: since})
assert.Contains(t, commits, &Commit{Date: after})
}

// A git repo initialized and with one commit per each of the messages provided.
// This repo is created in a temporary directory; use the cleanup function
// to delete it afterwards.
Expand Down
4 changes: 4 additions & 0 deletions internal/issues/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ var (
type Filter func(*commits.Commit) Issue

// Filters returns all filters configured by the user.
// @todo #31 Function issues.Filters() can be removed by providing
// default values for each filter that will effectively render them
// disabled. For example, OfSubjectRegex() can be effectively disabled
// by using ".*" as regex.
func Filters() []Filter {
filters := make([]Filter, 0)
if subjectRegex != nil {
Expand Down

0 comments on commit e5eff3a

Please sign in to comment.