Skip to content

Commit

Permalink
(#31) --since filter (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
llorllale committed Mar 3, 2019
1 parent 50f14d8 commit 48bb99a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Lint your git!

## Usage
```
llorllale:~/dev/go-gitlint$ ./gitlint --help
$ ./gitlint --help
usage: gitlint [<flags>]
Flags:
Expand All @@ -23,6 +23,7 @@ Flags:
--subject-len=SUBJECT-LEN Filters commit subjects based on length.
--body-regex=BODY-REGEX Filters commit message bodies based on a regular expression.
--path="." Path to the git repo ("." by default).
--since="1970-01-01" A date in "yyyy-MM-dd" format starting from which commits will be analyzed (default: "1970-01-01")
```
Additionally, it will look for configurations in a file `.gitlint` in the current directory if it exists. This file's format is just the same command line flags but each on a separate line. *Flags passed through the command line take precedence.*

Expand Down
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

1 comment on commit 48bb99a

@0pdd
Copy link

@0pdd 0pdd commented on 48bb99a Mar 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 31-07b6963e discovered in internal/issues/filters.go and submitted as #34. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.