github.com/IBM-Blockchain/fabric-operator@v1.0.4/scripts/check-license.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright contributors to the Hyperledger Fabric Operator project
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  #
     7  # Licensed under the Apache License, Version 2.0 (the "License");
     8  # you may not use this file except in compliance with the License.
     9  # You may obtain a copy of the License at:
    10  #
    11  # 	  http://www.apache.org/licenses/LICENSE-2.0
    12  #
    13  # Unless required by applicable law or agreed to in writing, software
    14  # distributed under the License is distributed on an "AS IS" BASIS,
    15  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  # See the License for the specific language governing permissions and
    17  # limitations under the License.
    18  #
    19  
    20  cat << EOB > golang_copyright.txt
    21  /*
    22   * Copyright contributors to the Hyperledger Fabric Operator project
    23   *
    24   * SPDX-License-Identifier: Apache-2.0
    25   *
    26   * Licensed under the Apache License, Version 2.0 (the "License");
    27   * you may not use this file except in compliance with the License.
    28   * You may obtain a copy of the License at:
    29   *
    30   * 	  http://www.apache.org/licenses/LICENSE-2.0
    31   *
    32   * Unless required by applicable law or agreed to in writing, software
    33   * distributed under the License is distributed on an "AS IS" BASIS,
    34   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    35   * See the License for the specific language governing permissions and
    36   * limitations under the License.
    37   */
    38  
    39  EOB
    40  
    41  cat << EOB > shell_copyright.txt
    42  #
    43  # Copyright contributors to the Hyperledger Fabric Operator project
    44  #
    45  # SPDX-License-Identifier: Apache-2.0
    46  #
    47  # Licensed under the Apache License, Version 2.0 (the "License");
    48  # you may not use this file except in compliance with the License.
    49  # You may obtain a copy of the License at:
    50  #
    51  # 	  http://www.apache.org/licenses/LICENSE-2.0
    52  #
    53  # Unless required by applicable law or agreed to in writing, software
    54  # distributed under the License is distributed on an "AS IS" BASIS,
    55  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    56  # See the License for the specific language governing permissions and
    57  # limitations under the License.
    58  #
    59  
    60  EOB
    61  
    62  function filterGeneratedFiles {
    63      for f in $@; do
    64          head -n5 $f | grep -qE 'Code generated by.*DO NOT EDIT' || echo $f
    65      done
    66  }
    67  
    68  function filterExcludedFiles {
    69    CHECK=`echo "$CHECK" \
    70  		| grep -v "^\.build/" \
    71  		| grep -v "^\.git/" \
    72  		| grep -v "^\.gitignore" \
    73  		| grep -v "\.json$" \
    74  		| grep -v "\.pem$" \
    75  		| grep -v "\.crt$" \
    76  		| grep -v "\.txt$" \
    77  		| grep -v "\.md$" \
    78  		| grep -v "_sk$" \
    79  		| grep -v "\.key$" \
    80  		| grep -v "\.gen\.go$" \
    81  		| grep -v "tools/" \
    82  		| grep -v "testdata/" \
    83  		| grep -v "vendor/" \
    84  		| grep -v "go.mod" \
    85  		| grep -v "go.sum" \
    86  		| grep -v .secrets.baseline \
    87  		| grep -v .pre-commit-config.yaml \
    88  		| grep -v .ibp.com_*.yaml \
    89  		| grep -v .deepcopy.go \
    90  		| sort -u`
    91  
    92    CHECK=$(filterGeneratedFiles "$CHECK")
    93  }
    94  
    95  CHECK=$(git diff --name-only --diff-filter=ACMRTUXB HEAD)
    96  filterExcludedFiles
    97  if [[ -z "$CHECK" ]]; then
    98    CHECK=$(git diff-tree --no-commit-id --name-only --diff-filter=ACMRTUXB -r "HEAD^..HEAD")
    99    filterExcludedFiles
   100  fi
   101  
   102  if [[ -z "$CHECK" ]]; then
   103     echo "All files are excluded from having license headers"
   104     exit 0
   105  fi
   106  
   107  missing=`echo "$CHECK" | xargs ls -d 2>/dev/null | xargs grep -L "SPDX-License-Identifier: Apache-2.0"`
   108  if [[ -z "$missing" ]]; then
   109     echo "All files have SPDX-License-Identifier: Apache-2.0"
   110     exit 0
   111  fi
   112  
   113  TMPFILE="./tmpfile"
   114  
   115  for FILE in ${missing}; do
   116  	EXT="${FILE##*.}"
   117  	echo "Adding copyright notice to $FILE"
   118  	if [ "${EXT}" = "go" ]; then
   119  		cat golang_copyright.txt ${FILE} > ${TMPFILE}
   120  		cat ${TMPFILE} > ${FILE}
   121  		rm -f ${TMPFILE}
   122  		echo "	${FILE} copyright notice added"
   123  	elif [ "${EXT}" = "yaml" ]; then
   124  		cat shell_copyright.txt ${FILE} > ${TMPFILE}
   125  		cat ${TMPFILE} > ${FILE}
   126  		rm -f ${TMPFILE}
   127  		echo "	${FILE} copyright notice added"
   128  	elif [ "${EXT}" = "sh" ]; then
   129  		cat shell_copyright.txt ${FILE} > ${TMPFILE}
   130  		cat ${TMPFILE} > ${FILE}
   131  		rm -f ${TMPFILE}
   132  		echo "	${FILE} copyright notice added"
   133  	else
   134  		echo "invalid file extension"
   135  	fi
   136  done
   137  
   138  rm golang_copyright.txt shell_copyright.txt
   139  
   140  exit 0