github.com/hyperledger/fabric-ca@v2.0.0-alpha.0.20201120210307-7b4f34729db1+incompatible/scripts/check_license (about)

     1  #!/bin/bash
     2  #
     3  # Copyright IBM Corp. All Rights Reserved.
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  #
     7  
     8  function filterGeneratedFiles {
     9      for f in "$@"; do
    10          if [ -f "$f" ]; then
    11              head -n2 "$f" | grep -qE 'Code generated by.*DO NOT EDIT' || echo "$f"
    12          fi
    13      done
    14  }
    15  
    16  function filterExcludedFiles {
    17    CHECK=`echo "$CHECK" \
    18  		| grep -v "^\.git/" \
    19  		| grep -v "^\.build/" \
    20  		| grep -v "^vendor/" \
    21  		| grep -v "testdata/" \
    22  		| grep -v "swagger/" \
    23  		| grep -v "^LICENSE$" \
    24  		| grep -v "\.png$" \
    25  		| grep -v "\.rst$" \
    26  		| grep -v "\.txt$" \
    27  		| grep -v "\.pem$" \
    28  		| grep -v "_sk$" \
    29  		| grep -v "\.key$" \
    30  		| grep -v "\.gen\.go$" \
    31  		| grep -v "^Gopkg\.lock$" \
    32  		| grep -v "\.md$" \
    33  		| grep -v "\.pb\.go$" \
    34  		| grep -v ".gitignore" \
    35          | grep -v "\.xml$" \
    36  		| grep -v "\.mod$" \
    37  		| grep -v "\.sum" \
    38  		| sort -u`
    39  
    40    CHECK=$(filterGeneratedFiles "$CHECK")
    41  }
    42  
    43  CHECK=$(git diff --name-only --diff-filter=ACMRTUXB HEAD)
    44  filterExcludedFiles
    45  if [[ -z "$CHECK" ]]; then
    46    LAST_COMMITS=($(git log -2 --pretty=format:"%h"))
    47    CHECK=$(git diff-tree --no-commit-id --name-only --diff-filter=ACMRTUXB -r ${LAST_COMMITS[1]} ${LAST_COMMITS[0]})
    48    filterExcludedFiles
    49  fi
    50  
    51  if [[ -z "$CHECK" ]]; then
    52     echo "All files are excluded from having license headers"
    53     exit 0
    54  fi
    55  
    56  echo "Checking Go files for license headers ..."
    57  missing=`echo "$CHECK" | xargs ls -d 2>/dev/null | xargs grep -L "SPDX-License-Identifier"`
    58  if [[ -z "$missing" ]]; then
    59     echo "All files have SPDX-License-Identifier headers"
    60     exit 0
    61  fi
    62  echo "The following files are missing SPDX-License-Identifier headers:"
    63  echo "$missing"
    64  echo
    65  echo "Please replace the Apache license header comment text with:"
    66  echo "SPDX-License-Identifier: Apache-2.0"
    67  
    68  missing=`echo "$missing" | xargs ls -d 2>/dev/null | xargs grep -L "Apache License"`
    69  if [ -z "$missing" ]; then
    70     echo "All remaining files have Apache 2.0 headers"
    71     exit 0
    72  fi
    73  
    74  echo "The following files are missing license headers:"
    75  echo "$missing"
    76  exit 1