|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright 2022 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Run this linter from the root of the Git working tree to find files that |
| 18 | +# might be missing copyright headers. |
| 19 | + |
| 20 | +# Prints the path of every file that is versioned by Git. |
| 21 | +print-all-versioned-files() { git ls-tree -r --name-only HEAD ; } |
| 22 | + |
| 23 | +# Input: none. |
| 24 | +# |
| 25 | +# Arguments: FORMAT PATTERN... |
| 26 | +# FORMAT: a printf format string containing '%s'. |
| 27 | +# PATTERN: a regular expression. |
| 28 | +# |
| 29 | +# Output: a regular expression matching any PATTERN, formatted by FORMAT |
| 30 | +regexp(){ local fmt="${1}" IFS='|'; shift; printf "$fmt" "(${*})" ; } |
| 31 | + |
| 32 | +full-path() { regexp "^%s$" "${@}" ; } |
| 33 | +top-level-directory() { regexp "^%s/" "${@}" ; } |
| 34 | +subdirectory() { regexp "/%s/" "${@}" ; } |
| 35 | +filename() { regexp "/%s$" "${@}" ; } |
| 36 | +extension() { regexp "\.%s$" "${@}" ; } |
| 37 | + |
| 38 | +filter-by-path() { |
| 39 | + local patterns=( |
| 40 | + # The LICENSE file is ok by definition, no need to check it for a header. |
| 41 | + "$( full-path LICENSE )" |
| 42 | + |
| 43 | + # Third-party code comes with its own license. |
| 44 | + "$( top-level-directory third_party )" |
| 45 | + |
| 46 | + # Automatically generated files don't need a copyright header. |
| 47 | + "$( subdirectory bindata )" |
| 48 | + |
| 49 | + "$( filename Appfile Fastfile Gemfile Podfile Snapfile 'deps\.nix' )" |
| 50 | + "$( regexp "%s" example generated '\.g\.' )" |
| 51 | + |
| 52 | + # Any file that is probably a generic configuration-only or data-only file. |
| 53 | + "$( extension entitlements firebaserc )" |
| 54 | + "$( extension xml ya?ml )" # correct in... approximately all cases |
| 55 | + |
| 56 | + # Special purpose configuration files that are just trivial. |
| 57 | + "$( filename envrc )" |
| 58 | + |
| 59 | + # Ignore .gitignore and similar files. |
| 60 | + "$( extension 'git[^/]*' )" |
| 61 | + |
| 62 | + # TODO: add proper explanations and/or sort into groups above: |
| 63 | + "$( extension gradle iml jpg json lock md metadata mod modulemap 'pb\.go' plist )" |
| 64 | + "$( extension properties png storyboard sum toml 'xc[^.]*' xib '[^./]*xproj' )" |
| 65 | + ) |
| 66 | + local pattern="$( regexp "%s" "${patterns[@]}" )" |
| 67 | + grep --invert-match --extended "${pattern}" |
| 68 | + echo "${pattern}" |
| 69 | + #cat |
| 70 | +} |
| 71 | + |
| 72 | +filter-by-content() { |
| 73 | + while read path |
| 74 | + do |
| 75 | + if [ -f "${path}" ] && ! grep --quiet 'Copyright' "${path}" && ! ( head -n 5 "${path}" | grep --quiet --ignore-case --extended ' (re)?generated|Created by ' ) |
| 76 | + then |
| 77 | + echo "${path}" |
| 78 | + fi |
| 79 | + done |
| 80 | +} |
| 81 | + |
| 82 | +print-all-versioned-files | filter-by-path | filter-by-content |
0 commit comments