Add copyright checker for newly added files (#4214)
authorДилшоджон Умронхонович Пошшоев/AI Tools Lab /SRR/Engineer/삼성전자 <d.poshshoev@samsung.com>
Tue, 9 Apr 2019 04:11:19 +0000 (07:11 +0300)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Tue, 9 Apr 2019 04:11:19 +0000 (13:11 +0900)
* Add copyright checker for newly added files

Add copyright checker for newly added files

Signed-off-by: Poshshoev Dilshodzhon <d.poshshoev@samsung.com>
* Update command that lists newly added files

Update command that lists newly added files

Signed-off-by: Poshshoev Dilshodzhon <d.poshshoev@samsung.com>
* Check just listed directories

Check just listed directories by skipping exluded ones

Signed-off-by: Poshshoev Dilshodzhon <d.poshshoev@samsung.com>
* Check all files, that were added this year

This prevents from updating copyright year of a file, that were added
with correct copyright, but later on somehow was updated incorrectly.
Also, this addresses feedbacks of @hseok82-oh

Signed-off-by: Poshshoev Dilshodzhon <d.poshshoev@samsung.com>
* Fix Copyright grep command

Fix Copyright grep command

Signed-off-by: Poshshoev Dilshodzhon <d.poshshoev@samsung.com>
* Move checker to seperate file and change checking method

Also fixed files, that have wrong copyright
Checks just `.h, .hpp, .cc, .cpp, .cl` files
Check each files of subdir, that were added to git, or moved from other
directory in current year. If a file is added (not merged into master)
in this year and its copyright has incorrect format or copyright year
isn't current year, then print it.

Signed-off-by: Poshshoev Dilshodzhon <d.poshshoev@samsung.com>
* Rebase

Signed-off-by: Poshshoev Dilshodzhon <d.poshshoev@samsung.com>
* Remove unused pushd and popd

Signed-off-by: Poshshoev Dilshodzhon <d.poshshoev@samsung.com>
* Revert fixed copyrights

Signed-off-by: Poshshoev Dilshodzhon <d.poshshoev@samsung.com>
scripts/command/copyright-checker.sh [new file with mode: 0755]

diff --git a/scripts/command/copyright-checker.sh b/scripts/command/copyright-checker.sh
new file mode 100755 (executable)
index 0000000..9f2201f
--- /dev/null
@@ -0,0 +1,46 @@
+#!/bin/bash
+
+check_copyright_year() {
+    DIRECTORIES_NOT_TO_BE_TESTED=$2
+    YEAR=`date +"%Y"`
+    CORRECT_COPYRIGHT="Copyright (c) $YEAR Samsung Electronics Co"
+    FILE_EXT_TO_SEARCH="\.h$\|\.hpp$\|\.cc$\|\.cpp$\|\.cl$"
+
+    # Check newly added files
+    #this also includes files, that were moved here from another dir
+    NEW_FILES_OF_SUBDIR_TO_CHECK=$(git whatchanged --diff-filter=A --since "01/01/2019"\
+                                    --oneline --name-only --pretty=format: . | sort | uniq\
+                                    | grep $FILE_EXT_TO_SEARCH)
+    ARR=($NEW_FILES_OF_SUBDIR_TO_CHECK)
+    for s in ${DIRECTORIES_NOT_TO_BE_TESTED[@]}; do
+        if [[ $s = $TEST_DIR* ]]; then
+            skip=${s#$TEST_DIR/}/
+            ARR=(${ARR[*]//$skip*})
+        fi
+    done
+    NEW_FILES_OF_SUBDIR_TO_CHECK=${ARR[*]}
+    if [[ ${#NEW_FILES_OF_SUBDIR_TO_CHECK} -ne 0 ]]; then
+        for f in $NEW_FILES_OF_SUBDIR_TO_CHECK; do
+            [[ -f "$f" ]] || continue
+
+            CREATED_YEAR=$(git log --follow --format=%aD $f | tail -1)
+            [[ $CREATED_YEAR != *"$YEAR"* ]] && continue
+
+            COPYRIGHT_YEAR=$(sed -rn  '0,/.*Copyright \(c\) ([^ ]+).*/ s//\1/p' $f)
+            if [[ $COPYRIGHT_YEAR != $YEAR ]]; then
+                [[ -z "$COPYRIGHT_YEAR" ]] && COPYRIGHT_YEAR="None"
+                echo "Copyright year of $f is incorrect: expected $YEAR, found $COPYRIGHT_YEAR"
+            elif ! grep -q "$CORRECT_COPYRIGHT" $f; then
+                echo "Copyright format of $f is incorrect: expected $CORRECT_COPYRIGHT"
+            fi
+        done
+    fi
+}
+
+DIRECTORIES_NOT_TO_BE_TESTED=()
+
+for DIR_NOT_TO_BE_TESTED in $(find -name '.FORMATDENY' -exec dirname {} \;); do
+    DIRECTORIES_NOT_TO_BE_TESTED+=("$DIR_NOT_TO_BE_TESTED")
+done
+
+check_copyright_year $DIRECTORIES_NOT_TO_BE_TESTED