Skip to content

Commit

Permalink
CI: fix the copyright check script (for pre-commit hook)
Browse files Browse the repository at this point in the history
(cherry picked from commit 4f8c74a)
  • Loading branch information
fvennetier committed Apr 4, 2022
1 parent 288533f commit 5418d59
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions tools/oio-check-copyright.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# oio-check-copyright.sh
# Copyright (C) 2018-2019 OpenIO SAS, as part of OpenIO SDS
# Copyright (C) 2021 OVH SAS
# Copyright (C) 2021-2022 OVH SAS
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand Down Expand Up @@ -35,6 +35,23 @@ function find_last_modification {
done
}

is_staged () {
DIFF=$(git diff --staged "$1")
[ -z "$DIFF" ] && return 1 || return 0
}

check_staged_copyright () {
YEAR=$(date +%Y)
COMPANY="OVH"
COPYRIGHT_LINE=$(head -n 8 "$1" | grep -E "Copyright \(C\) ([[:digit:]]{4}-|)${YEAR} ${COMPANY} SAS")
if [ -z "${COPYRIGHT_LINE}" ]
then
echo "The Copyright section in $1 is not up to date (this file is staged for the current commit)"
[ -n "$VERBOSE" ] && git diff --staged "$1" | head -n 32
return 1
fi
}

check_copyright () {
RETCODE=0
while read FILE; do
Expand All @@ -54,11 +71,20 @@ check_copyright () {
continue
fi

if is_staged "${FILE}"
then
if ! check_staged_copyright "${FILE}"
then
RETCODE=1
fi
continue
fi

COMMIT=$(find_last_modification "${FILE}")
if [ -z "${COMMIT}" ]; then
echo "The last 8 commits of${FILE} only modify the Copyright section" 1>&2
RETCODE=1
continue
continue
fi
YEAR=$(git show -s --pretty="format:%cd" --date="format:%Y" "${COMMIT}")

Expand All @@ -76,7 +102,7 @@ check_copyright () {
RETCODE=1
fi
done
exit $RETCODE
exit $RETCODE
}

declare -x FILES
Expand Down

0 comments on commit 5418d59

Please sign in to comment.