Skip to content

Commit

Permalink
Merge pull request #41 from maruel/ignored
Browse files Browse the repository at this point in the history
Skip generated files
  • Loading branch information
mco-gh authored Aug 15, 2020
2 parents df58aca + 541722a commit 4295bd5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
26 changes: 21 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -110,12 +111,12 @@ func main() {
return nil
}
// Check if file has a license
isMissingLicenseHeader, err := fileHasLicense(f.path)
hasLicense, err := fileHasLicense(f.path)
if err != nil {
log.Printf("%s: %v", f.path, err)
return err
}
if isMissingLicenseHeader {
if !hasLicense {
fmt.Printf("%s\n", f.path)
return errors.New("missing license header")
}
Expand Down Expand Up @@ -165,6 +166,9 @@ func walk(ch chan<- *file, start string) {
})
}

// addLicense add a license to the file if missing.
//
// It returns true if the file was updated.
func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data *copyrightData) (bool, error) {
var lic []byte
var err error
Expand All @@ -174,7 +178,10 @@ func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data *c
}

b, err := ioutil.ReadFile(path)
if err != nil || hasLicense(b) {
if err != nil {
return false, err
}
if hasLicense(b) || isGenerated(b) {
return false, err
}

Expand All @@ -193,10 +200,11 @@ func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data *c
// fileHasLicense reports whether the file at path contains a license header.
func fileHasLicense(path string) (bool, error) {
b, err := ioutil.ReadFile(path)
if err != nil || hasLicense(b) {
if err != nil {
return false, err
}
return true, nil
// If generated, we count it as if it has a license.
return hasLicense(b) || isGenerated(b), nil
}

func licenseHeader(path string, tmpl *template.Template, data *copyrightData) ([]byte, error) {
Expand Down Expand Up @@ -262,6 +270,14 @@ func hashBang(b []byte) []byte {
return nil
}

var reGenerated = regexp.MustCompile(`(?m)^.{1,2} Code generated .* DO NOT EDIT\.$`)

// isGenerated returns true if it contains a string that implies the file was
// generated.
func isGenerated(b []byte) bool {
return reGenerated.Match(b)
}

func hasLicense(b []byte) bool {
n := 1000
if len(b) < 1000 {
Expand Down
7 changes: 7 additions & 0 deletions testdata/expected/file_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions testdata/initial/file_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4295bd5

Please sign in to comment.