← Back to team overview

kicad-developers team mailing list archive

Here, have my copyright date git hook

 

Here, just thought I'd share this with anybody who wants it (and is using git).
It's a git pre-commit hook that checks all the files you've staged to make sure
the copyright header has been updated accordingly. Just make sure it's set
executable and stuff it in .git/hooks in the root of your repository.

-- 
Chris
#!/bin/bash

CORRECT_FILE="AUTHORS.txt"

# Regex allows most variants of the copyright line, including with a range of years. Groups are:
# 1: (C) - this is incidental, no need to use it
# 2: first year of range and its hyphen. incidental, no need to use it
# 3: final year of range
# 4: file name for contributors list
COPYRIGHT_LINE_RE='^\s*\*\s*Copyright\s*(\(C\))?\s*([0-9]+-)?([0-9]+)\s+Ki[Cc]ad Developers, see ([A-Za-z0-9_.-]+) for contributors\.\s*$'

CURRENT_YEAR="$(date +%Y)"

# Takes file name as #1, but actual data at stdin. This allows grabbing the actual
# staged data from git.
function check_file()
{
    local matches="$(sed -nr "s/${COPYRIGHT_LINE_RE}/\3\t\4/p")"

    local year="$(cut -f1 <<<"$matches")"
    local filename="$(cut -f2 <<<"$matches")"

    local errors=0

    if [[ "x$year" == "x" ]]; then
        echo "$1 - no general copyright line"
        errors=$((errors + 1))
    fi

    if [[ "$year" -lt "$CURRENT_YEAR" ]]; then
        echo "$1 - year too old ($year)"
        errors=$((errors + 1))
    fi

    if [[ "$filename" != "$CORRECT_FILE" ]]; then
        echo "$1 - contributors list filename wrong ($filename)"
        errors=$((errors + 1))
    fi

    if [[ $errors -gt 0 ]]; then
        return 1
    fi
}


if git rev-parse --verify HEAD >/dev/null 2>&1
then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
    against=$(git hash-object -t tree /dev/null)
fi

# Redirect output to stderr.
exec 1>&2

git diff --cached --name-only -z $against | (
    ERRORS=0
    while IFS= read -d $'\0' -r filename; do
        extension="${filename##*.}"
        if [[ "x$extension" != "xcpp" ]] && [[ "x$extention" != "xh" ]]; then
            continue
        fi

        git show ":$filename" | check_file "$filename"
        if [[ $? -gt 0 ]]; then
            ERRORS=1
        fi
    done
    exit $ERRORS
    )
ERRORS=$?

if [[ $ERRORS -gt 0 ]]; then
    echo "Some files had copyright header errors."
    exit 1
fi