From 15199a106631c43e4a7bd05321db9790d5aa565a Mon Sep 17 00:00:00 2001 From: garciadeblas Date: Wed, 21 May 2025 11:23:13 +0200 Subject: [PATCH] Update license_scan to skip deleted files Change-Id: Iba537d72285af539835d1cff244cc651b89d8902 Signed-off-by: garciadeblas --- tools/license_scan.sh | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/tools/license_scan.sh b/tools/license_scan.sh index a7946360..4a1d1a80 100755 --- a/tools/license_scan.sh +++ b/tools/license_scan.sh @@ -14,17 +14,30 @@ # See the License for the specific language governing permissions and # limitations under the License. # - apache=0 nolicense=0 other=0 +binaries=0 +deleted=0 exception_list="':(exclude)*.pdf' ':(exclude)*.png' ':(exclude)*.jpeg' ':(exclude)*.jpg' ':(exclude)*.gif' ':(exclude)*.json' ':(exclude)*.ico' ':(exclude)*.svg' ':(exclude)*.tiff'" git fetch -for file in $(echo ${exception_list} | xargs git diff --name-only origin/$GERRIT_BRANCH -- . ); do +if [ -n "${GERRIT_BRANCH}" ]; then + COMMIT_TO_COMPARE="origin/${GERRIT_BRANCH}" +else + COMMIT_TO_COMPARE="HEAD^" +fi - file_type=$(file -b --mime-type $file | sed 's|/.*||') - echo $file is $file_type +total_changes=$(git diff --name-only ${COMMIT_TO_COMPARE} -- . $(echo ${exception_list}) | wc -l) +for file in $(git diff --name-only ${COMMIT_TO_COMPARE} -- . $(echo ${exception_list})); do + # Skip deleted files + if [ ! -f "$file" ]; then + deleted=$((deleted + 1)) + continue + fi + + file_type=$(file -b --mime-type "$file" | sed 's|/.*||') + echo "$file is $file_type" case "$file_type" in text) binary=false @@ -38,14 +51,10 @@ for file in $(echo ${exception_list} | xargs git diff --name-only origin/$GERRIT license=Binary else license="No Apache license found" - if [ -f $file ]; then - if [ -s $file ]; then - if [ $(grep -c "http://www.apache.org/licenses/LICENSE-2.0" $file) -ge 1 ] ; then - license="Apache-2.0" - fi + if [ -s "$file" ]; then + if grep -q "http://www.apache.org/licenses/LICENSE-2.0" "$file"; then + license="Apache-2.0" fi - else - license="DELETED" fi fi echo "$file $license" @@ -56,9 +65,8 @@ for file in $(echo ${exception_list} | xargs git diff --name-only origin/$GERRIT No*) nolicense=$((nolicense + 1)) ;; - "DELETED") - ;; "Binary") + binaries=$((binaries + 1)) ;; *) echo "BAD LICENSE ON FILE $file" @@ -67,6 +75,12 @@ for file in $(echo ${exception_list} | xargs git diff --name-only origin/$GERRIT esac done +echo "Changes in license in this commit: ${total_changes}" +echo " Deleted files: ${deleted}" +echo " Binaries: ${binaries}" +echo " Apache license: ${apache}" +echo " No license: ${nolicense}" +echo " Other license: ${other}" if [ $nolicense -gt 0 ]; then echo "FATAL: Files without apache license found" -- 2.25.1