This is an automated email from the ASF dual-hosted git repository. kezhenxu94 pushed a commit to branch enhance in repository https://gitbox.apache.org/repos/asf/skywalking-eyes.git
commit b3725378f56a947cc84f22f96a6590064cf12064 Author: kezhenxu94 <[email protected]> AuthorDate: Tue Dec 22 19:39:01 2020 +0800 Append .gitignore content automatically, add `LicenseLocationThreshold` --- .licenserc.yaml | 32 +++++++++++++++++++++++++------- license-eye/assets/languages.yaml | 1 + license-eye/pkg/header/check.go | 23 +++++++++++++++++++++-- license-eye/pkg/header/config.go | 27 +++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 9 deletions(-) diff --git a/.licenserc.yaml b/.licenserc.yaml index 049ce1b..a6ab99c 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -1,3 +1,21 @@ +# +# Licensed to Apache Software Foundation (ASF) under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Apache Software Foundation (ASF) licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# header: # `header` section is configurations for source codes license header. license: | # `license` will be used as the content when `fix` command needs to insert a license header. Licensed to Apache Software Foundation (ASF) under one or more contributor @@ -16,7 +34,10 @@ header: # `header` section is configurations for source codes license header. KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - pattern: | # `pattern` is optional regexp if all the file headers are the same as `license` (linebreaks doesn't matter). + + # `pattern` is optional regexp if all the file headers are the same as `license` (linebreaks doesn't matter); + # In the `pattern`, all punctuations should be removed unless they are part of the regex; + pattern: | Licensed to( the)? Apache Software Foundation \(ASF\) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright @@ -34,14 +55,12 @@ header: # `header` section is configurations for source codes license header. specific language governing permissions and limitations under the License. - paths: # `paths` are the path list that will be checked (and fixed) by license-eye. + paths: # `paths` are the path list that will be checked (and fixed) by license-eye, default is ['**']. - '**' paths-ignore: # `paths-ignore` are the path list that will be ignored by license-eye. - - '.git/**' - - '.idea/**' - - 'dist/**' - - 'licenses/**' + - 'dist' + - 'licenses' - '**/bin/**' - '**/*.md' - '**/.DS_Store' @@ -50,5 +69,4 @@ header: # `header` section is configurations for source codes license header. - '**/go.sum' - 'LICENSE' - 'NOTICE' - - '**/assets.gen.go' - '**/assets/languages.yaml' diff --git a/license-eye/assets/languages.yaml b/license-eye/assets/languages.yaml index 6a249db..2d179ae 100644 --- a/license-eye/assets/languages.yaml +++ b/license-eye/assets/languages.yaml @@ -6401,6 +6401,7 @@ YAML: codemirror_mode: yaml codemirror_mime_type: text/x-yaml language_id: 407 + comment_style_id: Hashtag YANG: type: data extensions: diff --git a/license-eye/pkg/header/check.go b/license-eye/pkg/header/check.go index af187a0..45db2df 100644 --- a/license-eye/pkg/header/check.go +++ b/license-eye/pkg/header/check.go @@ -29,7 +29,13 @@ import ( "github.com/bmatcuk/doublestar/v2" ) -var Punctuations = regexp.MustCompile("[\\[\\]/*:;\\s#\\-!~'\"(){}?]+") // TODO: also trim stop words +// TODO: also trim stop words +var ( + // LicenseLocationThreshold specifies the index threshold where the license header can be located, + // after all, a "header" cannot be TOO far from the file start. + LicenseLocationThreshold = 80 + Punctuations = regexp.MustCompile("[\\[\\]/*:;\\s#\\-!~'\"(){}?]+") +) // Check checks the license headers of the specified paths/globs. func Check(config *ConfigHeader, result *Result) error { @@ -127,7 +133,7 @@ func CheckFile(file string, config *ConfigHeader, result *Result) error { content := Punctuations.ReplaceAllString(strings.Join(lines, " "), " ") license, pattern := config.NormalizedLicense(), config.NormalizedPattern() - if strings.Contains(content, license) || (pattern != nil && pattern.MatchString(content)) { + if satisfy(content, license, pattern) { result.Succeed(file) } else { logger.Log.Debugln("Content is:", content) @@ -140,3 +146,16 @@ func CheckFile(file string, config *ConfigHeader, result *Result) error { return nil } + +func satisfy(content string, license string, pattern *regexp.Regexp) bool { + if index := strings.Index(content, license); index >= 0 { + return index < LicenseLocationThreshold + } + + if pattern == nil { + return false + } + index := pattern.FindStringIndex(content) + + return index != nil && len(index) == 2 && index[0] < LicenseLocationThreshold +} diff --git a/license-eye/pkg/header/config.go b/license-eye/pkg/header/config.go index 82f4361..8911cfc 100644 --- a/license-eye/pkg/header/config.go +++ b/license-eye/pkg/header/config.go @@ -18,7 +18,9 @@ package header import ( + "bufio" "io/ioutil" + "os" "regexp" "strings" @@ -87,6 +89,16 @@ func (config *ConfigHeader) ShouldIgnore(path string) (bool, error) { return matched, err } } + + if stat, err := os.Stat(path); err == nil { + for _, ignorePattern := range config.PathsIgnore { + ignorePattern = strings.TrimRight(ignorePattern, "/") + if strings.HasPrefix(path, ignorePattern+"/") || stat.Name() == ignorePattern { + return true, nil + } + } + } + return false, nil } @@ -97,5 +109,20 @@ func (config *ConfigHeader) Finalize() error { config.Paths = []string{"**"} } + config.PathsIgnore = append(config.PathsIgnore, ".git") + + if file, err := os.Open(".gitignore"); err == nil { + defer func() { _ = file.Close() }() + + for scanner := bufio.NewScanner(file); scanner.Scan(); { + line := scanner.Text() + if strings.HasPrefix(line, "#") || strings.TrimSpace(line) == "" { + continue + } + logger.Log.Debugln("Add ignore path from .gitignore:", line) + config.PathsIgnore = append(config.PathsIgnore, strings.TrimSpace(line)) + } + } + return nil }
