Skip to content

Commit

Permalink
Automatically ignore anything in the top level .gitignore
Browse files Browse the repository at this point in the history
Signed-off-by: Liam White <[email protected]>
  • Loading branch information
liamawhite committed Jun 30, 2019
1 parent 60538ed commit 682f546
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.vscode
bin
vendor
dist
.vscode/
bin/
vendor/
dist/
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/liamawhite/licenser
go 1.12

require (
github.com/gobwas/glob v0.2.3
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.2.2
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down
7 changes: 7 additions & 0 deletions pkg/command/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ import (
var verifyCmd = &cobra.Command{
Use: "verify <directory>",
Short: "Verify licenses are present in files in your directory",
Long: `Verify licenses are present in files in your directory.
Verify will ignore the following files:
- Anything matched by the top level .gitignore
- .gitignore
- *.md, *.golden
`,

Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
Expand Down
51 changes: 41 additions & 10 deletions pkg/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
package processor

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"sync"

"github.com/gobwas/glob"
"github.com/liamawhite/licenser/pkg/file"
mutator "github.com/liamawhite/licenser/pkg/file"
"github.com/liamawhite/licenser/pkg/license"
Expand All @@ -34,13 +35,18 @@ type processor struct {
mutator *file.Mutator
success bool

skipListGlob []glob.Glob
skipListExtension map[string]bool

visitFunc func(path string, dryRun bool) bool
}

func New(startDirectory string, license license.Handler) *processor {
return &processor{
startDirectory: startDirectory,
mutator: mutator.New(license),
startDirectory: startDirectory,
mutator: mutator.New(license),
skipListGlob: buildGlobSkip(startDirectory),
skipListExtension: buildExtensionSkip(),
}
}

Expand Down Expand Up @@ -79,7 +85,7 @@ func (p *processor) run(recurse bool) bool {
}

func (p *processor) visit(path string, f os.FileInfo, err error) error {
if shouldSkip(path) {
if p.shouldSkip(path) {
return nil
}
if f.Mode().IsRegular() {
Expand All @@ -94,12 +100,37 @@ func (p *processor) visit(path string, f os.FileInfo, err error) error {
return nil
}

func shouldSkip(path string) bool {
if strings.Contains(path, ".git") {
return true
func (p *processor) shouldSkip(path string) bool {
for _, g := range p.skipListGlob {
if g.Match(path) {
return true
}
}
return p.skipListExtension[filepath.Ext(path)]
}

func buildGlobSkip(startDirectory string) []glob.Glob {
globList := []string{".git/**", ".gitignore"}
// TODO: handle multilevel gitignores
gitignore := filepath.Join(startDirectory, ".gitignore")
f, err := os.Open(gitignore)
if err != nil {
fmt.Fprintf(os.Stderr, "error opening .gitignore:%v", err)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
globList = append(globList, fmt.Sprintf("%v**", scanner.Text()))
}
if strings.Contains(path, ".md") {
return true
result := []glob.Glob{}
for _, pattern := range globList {
toGlob := filepath.Join(startDirectory, pattern)
result = append(result, glob.MustCompile(toGlob))
}
return result
}
func buildExtensionSkip() map[string]bool {
return map[string]bool{
".md": true,
".golden": true,
}
return false
}

0 comments on commit 682f546

Please sign in to comment.