@@ -26,6 +26,7 @@ import (
26
26
"log"
27
27
"os"
28
28
"path/filepath"
29
+ "regexp"
29
30
"strings"
30
31
"time"
31
32
@@ -110,12 +111,12 @@ func main() {
110
111
return nil
111
112
}
112
113
// Check if file has a license
113
- isMissingLicenseHeader , err := fileHasLicense (f .path )
114
+ hasLicense , err := fileHasLicense (f .path )
114
115
if err != nil {
115
116
log .Printf ("%s: %v" , f .path , err )
116
117
return err
117
118
}
118
- if isMissingLicenseHeader {
119
+ if ! hasLicense {
119
120
fmt .Printf ("%s\n " , f .path )
120
121
return errors .New ("missing license header" )
121
122
}
@@ -165,6 +166,9 @@ func walk(ch chan<- *file, start string) {
165
166
})
166
167
}
167
168
169
+ // addLicense add a license to the file if missing.
170
+ //
171
+ // It returns true if the file was updated.
168
172
func addLicense (path string , fmode os.FileMode , tmpl * template.Template , data * copyrightData ) (bool , error ) {
169
173
var lic []byte
170
174
var err error
@@ -174,7 +178,10 @@ func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data *c
174
178
}
175
179
176
180
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 ) {
178
185
return false , err
179
186
}
180
187
@@ -193,10 +200,11 @@ func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data *c
193
200
// fileHasLicense reports whether the file at path contains a license header.
194
201
func fileHasLicense (path string ) (bool , error ) {
195
202
b , err := ioutil .ReadFile (path )
196
- if err != nil || hasLicense ( b ) {
203
+ if err != nil {
197
204
return false , err
198
205
}
199
- return true , nil
206
+ // If generated, we count it as if it has a license.
207
+ return hasLicense (b ) || isGenerated (b ), nil
200
208
}
201
209
202
210
func licenseHeader (path string , tmpl * template.Template , data * copyrightData ) ([]byte , error ) {
@@ -262,6 +270,14 @@ func hashBang(b []byte) []byte {
262
270
return nil
263
271
}
264
272
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
+
265
281
func hasLicense (b []byte ) bool {
266
282
n := 1000
267
283
if len (b ) < 1000 {
0 commit comments