Skip to content

Commit 4295bd5

Browse files
authored
Merge pull request #41 from maruel/ignored
Skip generated files
2 parents df58aca + 541722a commit 4295bd5

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

main.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"log"
2727
"os"
2828
"path/filepath"
29+
"regexp"
2930
"strings"
3031
"time"
3132

@@ -110,12 +111,12 @@ func main() {
110111
return nil
111112
}
112113
// Check if file has a license
113-
isMissingLicenseHeader, err := fileHasLicense(f.path)
114+
hasLicense, err := fileHasLicense(f.path)
114115
if err != nil {
115116
log.Printf("%s: %v", f.path, err)
116117
return err
117118
}
118-
if isMissingLicenseHeader {
119+
if !hasLicense {
119120
fmt.Printf("%s\n", f.path)
120121
return errors.New("missing license header")
121122
}
@@ -165,6 +166,9 @@ func walk(ch chan<- *file, start string) {
165166
})
166167
}
167168

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

176180
b, err := ioutil.ReadFile(path)
177-
if err != nil || hasLicense(b) {
181+
if err != nil {
182+
return false, err
183+
}
184+
if hasLicense(b) || isGenerated(b) {
178185
return false, err
179186
}
180187

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

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

273+
var reGenerated = regexp.MustCompile(`(?m)^.{1,2} Code generated .* DO NOT EDIT\.$`)
274+
275+
// isGenerated returns true if it contains a string that implies the file was
276+
// generated.
277+
func isGenerated(b []byte) bool {
278+
return reGenerated.Match(b)
279+
}
280+
265281
func hasLicense(b []byte) bool {
266282
n := 1000
267283
if len(b) < 1000 {

testdata/expected/file_generated.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/initial/file_generated.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)