Skip to content

Commit d33bae4

Browse files
committed
copyright: test that all files in the repo have copyright notices
Adapted the script that I used to automatically add copyright notices into a test that all files in the repository currently have appropriate copyrights. Caught a few typos and extra spaces, which I decided to fix rather than adjust regular expression. Change-Id: Ifdbad969eca482e25c89afc5a2ddd5968c6661a6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/282592 Trust: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 1b1bb64 commit d33bae4

File tree

25 files changed

+160
-23
lines changed

25 files changed

+160
-23
lines changed

blog/atom/atom.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2009 The Go Authors. All rights reserved.
1+
// Copyright 2009 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

blog/blog.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Go Authors. All rights reserved.
1+
// Copyright 2013 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

cmd/compilebench/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 The Go Authors. All rights reserved.
1+
// Copyright 2015 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

cmd/cover/cover_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Go Authors. All rights reserved.
1+
// Copyright 2013 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

cmd/godoc/godoc_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Go Authors. All rights reserved.
1+
// Copyright 2013 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

cmd/gomvpkg/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2015 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
3-
// licence that can be found in the LICENSE file.
3+
// license that can be found in the LICENSE file.
44

55
// The gomvpkg command moves go packages, updating import declarations.
66
// See the -help message or Usage constant for details.

cmd/html2article/conv.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Go Authors. All rights reserved.
1+
// Copyright 2013 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

cmd/present/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Go Authors. All rights reserved.
1+
// Copyright 2013 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

cmd/present/play.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Go Authors. All rights reserved.
1+
// Copyright 2012 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

cmd/toolstash/cmp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 The Go Authors. All rights reserved.
1+
// Copyright 2015 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

cmd/toolstash/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 The Go Authors. All rights reserved.
1+
// Copyright 2015 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

container/intsets/popcnt_gccgo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 The Go Authors. All rights reserved.
1+
// Copyright 2015 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

copyright/copyright.go

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

copyright/copyright_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package copyright
6+
7+
import (
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
"testing"
12+
)
13+
14+
func TestToolsCopyright(t *testing.T) {
15+
cwd, err := os.Getwd()
16+
if err != nil {
17+
t.Fatal(err)
18+
}
19+
tools := filepath.Dir(cwd)
20+
if !strings.HasSuffix(filepath.Base(tools), "tools") {
21+
t.Fatalf("current working directory is %s, expected tools", tools)
22+
}
23+
files, err := checkCopyright(tools)
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
if len(files) > 0 {
28+
t.Errorf("The following files are missing copyright notices:\n%s", strings.Join(files, "\n"))
29+
}
30+
}

go/ssa/interp/external.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Go Authors. All rights reserved.
1+
// Copyright 2013 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

go/vcs/discovery.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Go Authors. All rights reserved.
1+
// Copyright 2012 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

go/vcs/env.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Go Authors. All rights reserved.
1+
// Copyright 2013 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

go/vcs/http.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Go Authors. All rights reserved.
1+
// Copyright 2012 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

go/vcs/vcs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Go Authors. All rights reserved.
1+
// Copyright 2012 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

go/vcs/vcs_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Go Authors. All rights reserved.
1+
// Copyright 2013 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

playground/playground.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Go Authors. All rights reserved.
1+
// Copyright 2013 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

playground/socket/socket.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Go Authors. All rights reserved.
1+
// Copyright 2012 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

playground/socket/socket_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2015 The Go Authors. All rights reserved.
1+
// Copyright 2015 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

refactor/rename/mvpkg.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2015 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
3-
// licence that can be found in the LICENSE file.
3+
// license that can be found in the LICENSE file.
44

55
// This file contains the implementation of the 'gomvpkg' command
66
// whose main function is in golang.org/x/tools/cmd/gomvpkg.

refactor/rename/mvpkg_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2015 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
3-
// licence that can be found in the LICENSE file.
3+
// license that can be found in the LICENSE file.
44

55
package rename
66

0 commit comments

Comments
 (0)