|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright (C) 2016-2025 Intel Corporation |
| 3 | +# Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | + |
| 6 | +# check-headers.sh - check copyright and license in source files |
| 7 | + |
| 8 | +SELF=$0 |
| 9 | + |
| 10 | +function usage() { |
| 11 | + echo "Usage: $SELF <source_root_path> <license_tag> [-h|-v|-a|-d]" |
| 12 | + echo " -h, --help this help message" |
| 13 | + echo " -v, --verbose verbose mode" |
| 14 | + echo " -a, --all check all files (only modified files are checked by default)" |
| 15 | + echo " -d, --update_dates change Copyright dates in all analyzed files (rather not use with -a)" |
| 16 | +} |
| 17 | + |
| 18 | +if [ "$#" -lt 2 ]; then |
| 19 | + usage >&2 |
| 20 | + exit 2 |
| 21 | +fi |
| 22 | + |
| 23 | +SOURCE_ROOT=$1 |
| 24 | +shift |
| 25 | +LICENSE=$1 |
| 26 | +shift |
| 27 | + |
| 28 | +PATTERN=`mktemp` |
| 29 | +TMP=`mktemp` |
| 30 | +TMP2=`mktemp` |
| 31 | +TEMPFILE=`mktemp` |
| 32 | +rm -f $PATTERN $TMP $TMP2 |
| 33 | + |
| 34 | +if [ "$1" == "-h" -o "$1" == "--help" ]; then |
| 35 | + usage |
| 36 | + exit 0 |
| 37 | +fi |
| 38 | + |
| 39 | +export GIT="git -C ${SOURCE_ROOT}" |
| 40 | +$GIT rev-parse || exit 1 |
| 41 | + |
| 42 | +if [ -f $SOURCE_ROOT/.git/shallow ]; then |
| 43 | + SHALLOW_CLONE=1 |
| 44 | + echo |
| 45 | + echo "Warning: This is a shallow clone. Checking dates in copyright headers" |
| 46 | + echo " will be skipped in case of files that have no history." |
| 47 | + echo |
| 48 | +else |
| 49 | + SHALLOW_CLONE=0 |
| 50 | +fi |
| 51 | + |
| 52 | +VERBOSE=0 |
| 53 | +CHECK_ALL=0 |
| 54 | +UPDATE_DATES=0 |
| 55 | +while [ "$1" != "" ]; do |
| 56 | + case $1 in |
| 57 | + -v|--verbose) |
| 58 | + VERBOSE=1 |
| 59 | + ;; |
| 60 | + -a|--all) |
| 61 | + CHECK_ALL=1 |
| 62 | + ;; |
| 63 | + -d|--update_dates) |
| 64 | + UPDATE_DATES=1 |
| 65 | + ;; |
| 66 | + esac |
| 67 | + shift |
| 68 | +done |
| 69 | + |
| 70 | +if [ $CHECK_ALL -eq 0 ]; then |
| 71 | + CURRENT_COMMIT=$($GIT --no-pager log --pretty=%H -1) |
| 72 | + MERGE_BASE=$($GIT merge-base HEAD origin/main 2>/dev/null) |
| 73 | + [ -z $MERGE_BASE ] && \ |
| 74 | + MERGE_BASE=$($GIT --no-pager log --pretty="%cN:%H" | grep GitHub 2>/dev/null | head -n1 | cut -d: -f2) |
| 75 | + [ -z $MERGE_BASE -o "$CURRENT_COMMIT" = "$MERGE_BASE" ] && \ |
| 76 | + CHECK_ALL=1 |
| 77 | +fi |
| 78 | + |
| 79 | +if [ $CHECK_ALL -eq 1 ]; then |
| 80 | + echo "INFO: Checking copyright headers of all files..." |
| 81 | + GIT_COMMAND="ls-tree -r --name-only HEAD" |
| 82 | +else |
| 83 | + echo "INFO: Checking copyright headers of modified files only..." |
| 84 | + GIT_COMMAND="diff --name-only $MERGE_BASE $CURRENT_COMMIT" |
| 85 | +fi |
| 86 | + |
| 87 | +FILES=$($GIT $GIT_COMMAND | ${SOURCE_ROOT}/scripts/check_license/file-exceptions.sh) |
| 88 | + |
| 89 | +RV=0 |
| 90 | +for file in $FILES ; do |
| 91 | + if [ $VERBOSE -eq 1 ]; then |
| 92 | + echo "Checking file: $file" |
| 93 | + fi |
| 94 | + # The src_path is a path which should be used in every command except git. |
| 95 | + # git is called with -C flag so filepaths should be relative to SOURCE_ROOT |
| 96 | + src_path="${SOURCE_ROOT}/$file" |
| 97 | + [ ! -f $src_path ] && continue |
| 98 | + # ensure that file is UTF-8 encoded |
| 99 | + ENCODING=`file -b --mime-encoding $src_path` |
| 100 | + iconv -f $ENCODING -t "UTF-8" $src_path > $TEMPFILE |
| 101 | + |
| 102 | + if ! grep -q "SPDX-License-Identifier: $LICENSE" $src_path; then |
| 103 | + echo >&2 "error: no $LICENSE SPDX tag in file: $src_path" |
| 104 | + RV=1 |
| 105 | + fi |
| 106 | + |
| 107 | + if [ $SHALLOW_CLONE -eq 0 ]; then |
| 108 | + $GIT log --no-merges --format="%ai %aE" -- $file | sort > $TMP |
| 109 | + else |
| 110 | + # mark the grafted commits (commits with no parents) |
| 111 | + $GIT log --no-merges --format="%ai %aE grafted-%p-commit" -- $file | sort > $TMP |
| 112 | + fi |
| 113 | + |
| 114 | + # skip checking dates for non-Intel commits |
| 115 | + [[ ! $(tail -n1 $TMP) =~ "@intel.com" ]] && continue |
| 116 | + |
| 117 | + # skip checking dates for new files |
| 118 | + [ $(cat $TMP | wc -l) -le 1 ] && continue |
| 119 | + |
| 120 | + # grep out the grafted commits (commits with no parents) |
| 121 | + # and skip checking dates for non-Intel commits |
| 122 | + grep -v -e "grafted--commit" $TMP | grep -e "@intel.com" > $TMP2 |
| 123 | + |
| 124 | + [ $(cat $TMP2 | wc -l) -eq 0 ] && continue |
| 125 | + |
| 126 | + FIRST=`head -n1 $TMP2` |
| 127 | + LAST=` tail -n1 $TMP2` |
| 128 | + |
| 129 | + YEARS=$(sed ' |
| 130 | +/.*Copyright (C) [0-9-]\+ Intel Corporation/!d |
| 131 | +s/.*Copyright (C) \([0-9]\+\)-\([0-9]\+\).*/\1-\2/ |
| 132 | +s/.*Copyright (C) \([0-9]\+\).*/\1/' "$src_path") |
| 133 | + if [ -z "$YEARS" ]; then |
| 134 | + echo >&2 "No copyright years in $src_path" |
| 135 | + RV=1 |
| 136 | + continue |
| 137 | + fi |
| 138 | + |
| 139 | + HEADER_FIRST=`echo $YEARS | cut -d"-" -f1` |
| 140 | + HEADER_LAST=` echo $YEARS | cut -d"-" -f2` |
| 141 | + |
| 142 | + COMMIT_FIRST=`echo $FIRST | cut -d"-" -f1` |
| 143 | + COMMIT_LAST=` echo $LAST | cut -d"-" -f1` |
| 144 | + |
| 145 | + if [ "$COMMIT_FIRST" != "" -a "$COMMIT_LAST" != "" ]; then |
| 146 | + if [ "$COMMIT_FIRST" -lt "$HEADER_FIRST" ]; then |
| 147 | + RV=1 |
| 148 | + fi |
| 149 | + |
| 150 | + if [[ -n "$COMMIT_FIRST" && -n "$COMMIT_LAST" ]]; then |
| 151 | + if [[ $HEADER_FIRST -le $COMMIT_FIRST ]]; then |
| 152 | + if [[ $HEADER_LAST -eq $COMMIT_LAST ]]; then |
| 153 | + continue |
| 154 | + else |
| 155 | + NEW="$HEADER_FIRST-$COMMIT_LAST" |
| 156 | + if [[ ${UPDATE_DATES} -eq 1 ]]; then |
| 157 | + echo "Updating copyright date in $src_path: $YEARS -> $NEW" |
| 158 | + sed -i "s/Copyright (C) ${YEARS}/Copyright (C) ${NEW}/g" "${src_path}" |
| 159 | + else |
| 160 | + echo "$file:1: error: wrong copyright date: (is: $YEARS, should be: $NEW)" >&2 |
| 161 | + RV=1 |
| 162 | + fi |
| 163 | + fi |
| 164 | + else |
| 165 | + if [[ $COMMIT_FIRST -eq $COMMIT_LAST ]]; then |
| 166 | + NEW=$COMMIT_LAST |
| 167 | + else |
| 168 | + NEW=$COMMIT_FIRST-$COMMIT_LAST |
| 169 | + fi |
| 170 | + |
| 171 | + if [[ "$YEARS" == "$NEW" ]]; then |
| 172 | + continue |
| 173 | + else |
| 174 | + if [[ ${UPDATE_DATES} -eq 1 ]]; then |
| 175 | + echo "Updating copyright date in $src_path: $YEARS -> $NEW" |
| 176 | + sed -i "s/Copyright (C) ${YEARS}/Copyright (C) ${NEW}/g" "${src_path}" |
| 177 | + else |
| 178 | + echo "$file:1: error: wrong copyright date: (is: $YEARS, should be: $NEW)" >&2 |
| 179 | + RV=1 |
| 180 | + fi |
| 181 | + fi |
| 182 | + fi |
| 183 | + fi |
| 184 | + else |
| 185 | + echo "error: unknown commit dates in file: $file" >&2 |
| 186 | + RV=1 |
| 187 | + fi |
| 188 | +done |
| 189 | +rm -f $TMP $TMP2 $TEMPFILE |
| 190 | + |
| 191 | +# check if error found |
| 192 | +if [ $RV -eq 0 ]; then |
| 193 | + echo "Copyright headers are OK." |
| 194 | +else |
| 195 | + echo "Error(s) in copyright headers found!" >&2 |
| 196 | +fi |
| 197 | +exit $RV |
0 commit comments