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 <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Rebecca Stambler 2021-01-07 22:36:26 -05:00
Родитель 1b1bb6453d
Коммит d33bae4414
25 изменённых файлов: 160 добавлений и 23 удалений

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Copyright 2009 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2013 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Copyright 2015 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2013 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2013 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.

Просмотреть файл

@ -1,6 +1,6 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// licence that can be found in the LICENSE file.
// license that can be found in the LICENSE file.
// The gomvpkg command moves go packages, updating import declarations.
// See the -help message or Usage constant for details.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2013 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2013 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Copyright 2012 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Copyright 2015 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Copyright 2015 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Copyright 2015 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.

107
copyright/copyright.go Normal file
Просмотреть файл

@ -0,0 +1,107 @@
// 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.
// Package copyright checks that files have the correct copyright notices.
package copyright
import (
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
func checkCopyright(dir string) ([]string, error) {
var files []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
// Skip directories like ".git".
if strings.HasPrefix(info.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/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
}

Просмотреть файл

@ -0,0 +1,30 @@
// 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.
package copyright
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestToolsCopyright(t *testing.T) {
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
tools := filepath.Dir(cwd)
if !strings.HasSuffix(filepath.Base(tools), "tools") {
t.Fatalf("current working directory is %s, expected tools", tools)
}
files, err := checkCopyright(tools)
if err != nil {
t.Fatal(err)
}
if len(files) > 0 {
t.Errorf("The following files are missing copyright notices:\n%s", strings.Join(files, "\n"))
}
}

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2013 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Copyright 2012 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2013 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Copyright 2012 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Copyright 2012 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2013 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2013 The Go Authors. All rights reserved.
// Copyright 2013 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2012 The Go Authors. All rights reserved.
// Copyright 2012 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.

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Copyright 2015 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.

Просмотреть файл

@ -1,6 +1,6 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// licence that can be found in the LICENSE file.
// license that can be found in the LICENSE file.
// This file contains the implementation of the 'gomvpkg' command
// whose main function is in golang.org/x/tools/cmd/gomvpkg.

Просмотреть файл

@ -1,6 +1,6 @@
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// licence that can be found in the LICENSE file.
// license that can be found in the LICENSE file.
package rename