Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add .todosignore file support. #135

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ linters-settings:
- "github.com/ianlewis/todos"

# Dependencies.
- "github.com/denormal/go-gitignore"
- "github.com/fatih/color"
- "github.com/google/go-github"
- "github.com/ianlewis/linguist"
Expand Down
1 change: 1 addition & 0 deletions .todosignore
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- A new `github-issue-reopener` binary was added. This tool will scan a
directory for TODOs referencing a GitHub issue and reopen issues that have
been prematurely closed.
- Support for a `.todosignore` file was added. It functions the same as a
`.gitignore` file and uses the same format. Files matching the `.todosignore`
file are ignored when searching for TODOs.

## [0.2.0] - 2023-06-30

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/ianlewis/todos
go 1.20

require (
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817
github.com/fatih/color v1.15.0
github.com/google/go-cmp v0.5.9
github.com/google/go-github/v52 v52.0.0
Expand All @@ -19,6 +20,7 @@ require (
github.com/cloudflare/circl v1.3.3 // indirect
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-github/v53 v53.0.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be/go.mod
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ=
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/dayvonjersen/git4go v0.0.0-20150730160921-060dbfa3f1a1/go.mod h1:d4Czjd9u5QQ3gdds3c5TeNFtQdC5mrZzOqH3QHjmUIc=
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817 h1:0nsrg//Dc7xC74H/TZ5sYR8uk4UQRNjsw8zejqH5a4Q=
github.com/denormal/go-gitignore v0.0.0-20180930084346-ae8ad1d07817/go.mod h1:C/+sI4IFnEpCn6VQ3GIPEp+FrQnQw+YQP3+n+GdGq7o=
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
Expand Down
19 changes: 19 additions & 0 deletions internal/walker/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"path/filepath"
"strings"

"github.com/denormal/go-gitignore"
"github.com/ianlewis/linguist"

"github.com/ianlewis/todos/internal/scanner"
Expand Down Expand Up @@ -88,6 +89,9 @@
// path is the currently walked path.
path string

// ignore is the ignorefile config for the currently walked path.
ignore gitignore.GitIgnore

// The last error encountered.
err error
}
Expand Down Expand Up @@ -116,6 +120,14 @@
}

if fInfo.IsDir() {
ignore, err := gitignore.NewRepositoryWithFile(path, ".todosignore")
ianlewis marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
if herr := w.handleErr(path, err); herr != nil {
break

Check warning on line 126 in internal/walker/walker.go

View check run for this annotation

Codecov / codecov/patch

internal/walker/walker.go#L125-L126

Added lines #L125 - L126 were not covered by tests
}
}
w.ignore = ignore

// Walk the directory
w.walkDir(path)
} else {
Expand Down Expand Up @@ -147,6 +159,13 @@
return w.handleErr(path, err)
}

if match := w.ignore.Relative(path, d.IsDir()); match != nil && !match.Include() {
if d.IsDir() {
return fs.SkipDir
}
return nil
}

fullPath, err := filepath.EvalSymlinks(filepath.Join(w.path, path))
if err != nil {
// NOTE: If the symbolic link couldn't be evaluated just skip it.
Expand Down
102 changes: 102 additions & 0 deletions internal/walker/walker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,108 @@ var testCases = []struct {
},
},
},
{
name: ".todosignore ignore file",
files: []*testutils.File{
{
Path: ".todosignore",
Contents: []byte(`ignored.go`),
Mode: 0o600,
},
{
Path: "ignored.go",
Contents: []byte(`package foo
// package comment

// TODO is a function.
// TODO: some task.
func TODO() {
return // Random comment
}`),
Mode: 0o600,
},
{
Path: "line_comments.go",
Contents: []byte(`package foo
// package comment

// TODO is a function.
// TODO: some task.
func TODO() {
return // Random comment
}`),
Mode: 0o600,
},
},
opts: &Options{
Config: &todos.Config{
Types: []string{"TODO"},
},
},
expected: []*TODORef{
{
FileName: "line_comments.go",
TODO: &todos.TODO{
Type: "TODO",
Text: "// TODO: some task.",
Message: "some task.",
Line: 5,
CommentLine: 5,
},
},
},
},
{
name: ".todosignore ignore directory",
files: []*testutils.File{
{
Path: ".todosignore",
Contents: []byte(`ignored/`),
Mode: 0o600,
},
{
Path: "ignored/ignored.go",
Contents: []byte(`package foo
// package comment

// TODO is a function.
// TODO: some task.
func TODO() {
return // Random comment
}`),
Mode: 0o600,
},
{
Path: "line_comments.go",
Contents: []byte(`package foo
// package comment

// TODO is a function.
// TODO: some task.
func TODO() {
return // Random comment
}`),
Mode: 0o600,
},
},
opts: &Options{
Config: &todos.Config{
Types: []string{"TODO"},
},
},
expected: []*TODORef{
{
FileName: "line_comments.go",
TODO: &todos.TODO{
Type: "TODO",
Text: "// TODO: some task.",
Message: "some task.",
Line: 5,
CommentLine: 5,
},
},
},
},
}

type fixture struct {
Expand Down
Loading