diff --git a/blog/atom/atom.go b/blog/atom/atom.go index 542c50e6e..efea650fc 100644 --- a/blog/atom/atom.go +++ b/blog/atom/atom.go @@ -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. diff --git a/blog/blog.go b/blog/blog.go index a87401cb0..947c60e95 100644 --- a/blog/blog.go +++ b/blog/blog.go @@ -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. diff --git a/cmd/compilebench/main.go b/cmd/compilebench/main.go index 094a56c16..afce21817 100644 --- a/cmd/compilebench/main.go +++ b/cmd/compilebench/main.go @@ -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. diff --git a/cmd/cover/cover_test.go b/cmd/cover/cover_test.go index 54c4512bf..54d346540 100644 --- a/cmd/cover/cover_test.go +++ b/cmd/cover/cover_test.go @@ -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. diff --git a/cmd/godoc/godoc_test.go b/cmd/godoc/godoc_test.go index e61432f21..ac6bacd4f 100644 --- a/cmd/godoc/godoc_test.go +++ b/cmd/godoc/godoc_test.go @@ -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. diff --git a/cmd/gomvpkg/main.go b/cmd/gomvpkg/main.go index 043b0613a..20f6111c4 100644 --- a/cmd/gomvpkg/main.go +++ b/cmd/gomvpkg/main.go @@ -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. diff --git a/cmd/html2article/conv.go b/cmd/html2article/conv.go index 4ef4f6cc2..604bb1fd7 100644 --- a/cmd/html2article/conv.go +++ b/cmd/html2article/conv.go @@ -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. diff --git a/cmd/present/main.go b/cmd/present/main.go index cc04c4640..b89e11fe5 100644 --- a/cmd/present/main.go +++ b/cmd/present/main.go @@ -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. diff --git a/cmd/present/play.go b/cmd/present/play.go index 6650b9437..80627f268 100644 --- a/cmd/present/play.go +++ b/cmd/present/play.go @@ -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. diff --git a/cmd/toolstash/cmp.go b/cmd/toolstash/cmp.go index f5974e12b..a789d799b 100644 --- a/cmd/toolstash/cmp.go +++ b/cmd/toolstash/cmp.go @@ -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. diff --git a/cmd/toolstash/main.go b/cmd/toolstash/main.go index 6baf42ee5..b462b500d 100644 --- a/cmd/toolstash/main.go +++ b/cmd/toolstash/main.go @@ -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. diff --git a/container/intsets/popcnt_gccgo.go b/container/intsets/popcnt_gccgo.go index 82a8875c8..3fc5e8568 100644 --- a/container/intsets/popcnt_gccgo.go +++ b/container/intsets/popcnt_gccgo.go @@ -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. diff --git a/copyright/copyright.go b/copyright/copyright.go new file mode 100644 index 000000000..a20d6239c --- /dev/null +++ b/copyright/copyright.go @@ -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 +} diff --git a/copyright/copyright_test.go b/copyright/copyright_test.go new file mode 100644 index 000000000..bfab43ca0 --- /dev/null +++ b/copyright/copyright_test.go @@ -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")) + } +} diff --git a/go/ssa/interp/external.go b/go/ssa/interp/external.go index 735a14d9c..68ddee318 100644 --- a/go/ssa/interp/external.go +++ b/go/ssa/interp/external.go @@ -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. diff --git a/go/vcs/discovery.go b/go/vcs/discovery.go index 2428d8885..7d179bcc8 100644 --- a/go/vcs/discovery.go +++ b/go/vcs/discovery.go @@ -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. diff --git a/go/vcs/env.go b/go/vcs/env.go index e846f5b3b..189210cdf 100644 --- a/go/vcs/env.go +++ b/go/vcs/env.go @@ -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. diff --git a/go/vcs/http.go b/go/vcs/http.go index 96188185c..5836511d4 100644 --- a/go/vcs/http.go +++ b/go/vcs/http.go @@ -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. diff --git a/go/vcs/vcs.go b/go/vcs/vcs.go index 6e58ac749..1deb8137d 100644 --- a/go/vcs/vcs.go +++ b/go/vcs/vcs.go @@ -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. diff --git a/go/vcs/vcs_test.go b/go/vcs/vcs_test.go index b725f018b..a17b50d9d 100644 --- a/go/vcs/vcs_test.go +++ b/go/vcs/vcs_test.go @@ -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. diff --git a/playground/playground.go b/playground/playground.go index 14e984183..64f151924 100644 --- a/playground/playground.go +++ b/playground/playground.go @@ -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. diff --git a/playground/socket/socket.go b/playground/socket/socket.go index 7101ce4e8..17b6de3a1 100644 --- a/playground/socket/socket.go +++ b/playground/socket/socket.go @@ -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. diff --git a/playground/socket/socket_test.go b/playground/socket/socket_test.go index 0ceb2500f..b866e37af 100644 --- a/playground/socket/socket_test.go +++ b/playground/socket/socket_test.go @@ -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. diff --git a/refactor/rename/mvpkg.go b/refactor/rename/mvpkg.go index 91c00ff14..788971122 100644 --- a/refactor/rename/mvpkg.go +++ b/refactor/rename/mvpkg.go @@ -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. diff --git a/refactor/rename/mvpkg_test.go b/refactor/rename/mvpkg_test.go index aca3472b7..b8b4d85da 100644 --- a/refactor/rename/mvpkg_test.go +++ b/refactor/rename/mvpkg_test.go @@ -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