forked from golang/tools
/
copyright.go
116 lines (108 loc) · 2.95 KB
/
copyright.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.18
// +build go1.18
// Package copyright checks that files have the correct copyright notices.
package copyright
import (
"go/ast"
"go/parser"
"go/token"
"io/fs"
"io/ioutil"
"path/filepath"
"regexp"
"strings"
)
func checkCopyright(dir string) ([]string, error) {
var files []string
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
// Skip directories like ".git".
if strings.HasPrefix(d.Name(), ".") {
return filepath.SkipDir
}
// Skip any directory that starts with an underscore, as the go
// command would.
if strings.HasPrefix(d.Name(), "_") {
return filepath.SkipDir
}
return nil
}
needsCopyright, err := checkFile(dir, path)
if err != nil {
return err
}
if needsCopyright {
files = append(files, path)
}
return nil
})
return files, err
}
var copyrightRe = regexp.MustCompile(`Copyright \d{4} The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.`)
func checkFile(toolsDir, filename string) (bool, error) {
// Only check Go files.
if !strings.HasSuffix(filename, "go") {
return false, nil
}
// Don't check testdata files.
normalized := strings.TrimPrefix(filepath.ToSlash(filename), filepath.ToSlash(toolsDir))
if strings.Contains(normalized, "/testdata/") {
return false, nil
}
// goyacc is the only file with a different copyright header.
if strings.HasSuffix(normalized, "cmd/goyacc/yacc.go") {
return false, nil
}
content, err := ioutil.ReadFile(filename)
if err != nil {
return false, err
}
fset := token.NewFileSet()
parsed, err := parser.ParseFile(fset, filename, content, parser.ParseComments)
if err != nil {
return false, err
}
// Don't require headers on generated files.
if isGenerated(fset, parsed) {
return false, nil
}
shouldAddCopyright := true
for _, c := range parsed.Comments {
// The copyright should appear before the package declaration.
if c.Pos() > parsed.Package {
break
}
if copyrightRe.MatchString(c.Text()) {
shouldAddCopyright = false
break
}
}
return shouldAddCopyright, nil
}
// Copied from golang.org/x/tools/gopls/internal/lsp/source/util.go.
// Matches cgo generated comment as well as the proposed standard:
//
// https://golang.org/s/generatedcode
var generatedRx = regexp.MustCompile(`// .*DO NOT EDIT\.?`)
func isGenerated(fset *token.FileSet, file *ast.File) bool {
for _, commentGroup := range file.Comments {
for _, comment := range commentGroup.List {
if matched := generatedRx.MatchString(comment.Text); !matched {
continue
}
// Check if comment is at the beginning of the line in source.
if pos := fset.Position(comment.Slash); pos.Column == 1 {
return true
}
}
}
return false
}