blob: 4a1d1a80a2df821928c582c82b8a11230c30fd9d [file] [log] [blame]
beierlm9da33122020-12-10 15:22:15 -05001#!/bin/sh
Mike Marchetti771f06a2017-07-28 16:08:31 -04002#
3# Copyright 2016 Telefónica Investigación y Desarrollo, S.A.U.
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# http://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#
Mike Marchetti771f06a2017-07-28 16:08:31 -040017apache=0
18nolicense=0
19other=0
garciadeblas15199a12025-05-21 11:23:13 +020020binaries=0
21deleted=0
beierlmbdca4722020-09-03 14:02:30 -040022exception_list="':(exclude)*.pdf' ':(exclude)*.png' ':(exclude)*.jpeg' ':(exclude)*.jpg' ':(exclude)*.gif' ':(exclude)*.json' ':(exclude)*.ico' ':(exclude)*.svg' ':(exclude)*.tiff'"
Mike Marchetti771f06a2017-07-28 16:08:31 -040023git fetch
24
garciadeblas15199a12025-05-21 11:23:13 +020025if [ -n "${GERRIT_BRANCH}" ]; then
26 COMMIT_TO_COMPARE="origin/${GERRIT_BRANCH}"
27else
28 COMMIT_TO_COMPARE="HEAD^"
29fi
Mark Beierl02467ed2022-08-11 21:59:11 -040030
garciadeblas15199a12025-05-21 11:23:13 +020031total_changes=$(git diff --name-only ${COMMIT_TO_COMPARE} -- . $(echo ${exception_list}) | wc -l)
32for file in $(git diff --name-only ${COMMIT_TO_COMPARE} -- . $(echo ${exception_list})); do
33 # Skip deleted files
34 if [ ! -f "$file" ]; then
35 deleted=$((deleted + 1))
36 continue
37 fi
38
39 file_type=$(file -b --mime-type "$file" | sed 's|/.*||')
40 echo "$file is $file_type"
Mark Beierl02467ed2022-08-11 21:59:11 -040041 case "$file_type" in
42 text)
43 binary=false
44 ;;
45 *)
46 binary=true
47 ;;
48 esac
49
50 if $binary ; then
51 license=Binary
Mike Marchetti771f06a2017-07-28 16:08:31 -040052 else
Mark Beierl02467ed2022-08-11 21:59:11 -040053 license="No Apache license found"
garciadeblas15199a12025-05-21 11:23:13 +020054 if [ -s "$file" ]; then
55 if grep -q "http://www.apache.org/licenses/LICENSE-2.0" "$file"; then
56 license="Apache-2.0"
Mark Beierl02467ed2022-08-11 21:59:11 -040057 fi
Mark Beierl02467ed2022-08-11 21:59:11 -040058 fi
Mike Marchetti771f06a2017-07-28 16:08:31 -040059 fi
beierlm9da33122020-12-10 15:22:15 -050060 echo "$file $license"
61 case "$license" in
Mike Marchetti771f06a2017-07-28 16:08:31 -040062 "Apache-2.0")
63 apache=$((apache + 1))
64 ;;
beierlm9da33122020-12-10 15:22:15 -050065 No*)
Mike Marchetti771f06a2017-07-28 16:08:31 -040066 nolicense=$((nolicense + 1))
67 ;;
Mark Beierl02467ed2022-08-11 21:59:11 -040068 "Binary")
garciadeblas15199a12025-05-21 11:23:13 +020069 binaries=$((binaries + 1))
Mark Beierl02467ed2022-08-11 21:59:11 -040070 ;;
Mike Marchetti771f06a2017-07-28 16:08:31 -040071 *)
72 echo "BAD LICENSE ON FILE $file"
73 other=$((other + 1))
74 ;;
75 esac
76done
77
garciadeblas15199a12025-05-21 11:23:13 +020078echo "Changes in license in this commit: ${total_changes}"
79echo " Deleted files: ${deleted}"
80echo " Binaries: ${binaries}"
81echo " Apache license: ${apache}"
82echo " No license: ${nolicense}"
83echo " Other license: ${other}"
Mike Marchetti771f06a2017-07-28 16:08:31 -040084
85if [ $nolicense -gt 0 ]; then
beierlm9da33122020-12-10 15:22:15 -050086 echo "FATAL: Files without apache license found"
madavi7273dfb2019-07-25 11:32:57 +053087 exit 2
Mike Marchetti771f06a2017-07-28 16:08:31 -040088fi
89
90exit 0