<commit_before>#!/bin/sh

# Copyright 2017 <NAME> <<EMAIL>>
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.

: "${RIOTBASE:=$(cd $(dirname $0)/../../../; pwd)}"
cd $RIOTBASE

: "${RIOTTOOLS:=${RIOTBASE}/dist/tools}"
. "${RIOTTOOLS}"/ci/changed_files.sh

EXIT_CODE=0

filter() {
    if [ $QUIET -eq 0 ]; then
        cat
    else
        grep '^---' | cut -f 2 -d ' '
    fi
}

_headercheck() {
    OUT=$(${RIOTTOOLS}/headerguards/headerguards.py ${FILES} 2>&1 | filter)
    if [ -n "$OUT" ]; then
        EXIT_CODE=1
        echo "$OUT"
    fi
}

: ${FILES:=$(FILEREGEX='\.h$' changed_files)}

if [ -z "${FILES}" ]; then
    exit
fi

: ${QUIET:=0}

if [ -z "$*" ]; then
    _headercheck
fi

exit $EXIT_CODE<commit_msg>dist/tools/headerguards: annotate errors in Github Action<commit_after>#!/bin/sh

# Copyright 2017 <NAME> <<EMAIL>>
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.

: "${RIOTBASE:=$(cd $(dirname $0)/../../../; pwd)}"
cd $RIOTBASE

: "${RIOTTOOLS:=${RIOTBASE}/dist/tools}"
. "${RIOTTOOLS}"/ci/changed_files.sh
. "${RIOTTOOLS}"/ci/github_annotate.sh

EXIT_CODE=0

filter() {
    if [ $QUIET -eq 0 ]; then
        cat
    else
        grep '^---' | cut -f 2 -d ' '
    fi
}

_annotate_diff() {
    if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
        github_annotate_error "$1" "$2" "Wrong header guard format:\n\n$3"
    fi
}

_headercheck() {
    OUT=$(${RIOTTOOLS}/headerguards/headerguards.py ${FILES} 2>&1 | filter)
    if [ -n "$OUT" ]; then
        EXIT_CODE=1
        if github_annotate_is_on; then
            DIFF=""
            DIFFFILE=""
            DIFFLINE=""
            echo "$OUT" | {
                # see https://stackoverflow.com/a/30064493/11921757 for why we
                # use a sub shell here
                while read line; do
                    # file has no or broken header guard
                    if echo "$line" | grep -q '^.*: no / broken header guard$'; then
                        # this output comes outside of a diff, so reset diff parser
                        _annotate_diff "$DIFFFILE" "$DIFFLINE" "$DIFF"
                        DIFF=""
                        DIFFFILE=""
                        DIFFLINE=""
                        # annotate broken header guard
                        FILE=$(echo "$line" | cut -d: -f1 | xargs echo)
                        MESSAGE=$(echo "$line" | cut -d: -f2 | xargs echo)
                        github_annotate_error "$FILE" 0 "$MESSAGE"
                    # parse beginning of new diff
                    elif echo "$line" | grep -q '^--- .\+$'; then
                        _annotate_diff "$DIFFFILE" "$DIFFLINE" "$DIFF"
                        DIFF="$line"
                        DIFFFILE=$(echo "$line" | sed 's/^--- \(.\+\)$/\1/g')
                        DIFFLINE=""
                    # we are in a diff currently
                    elif [ -n "$DIFF" ]; then
                        # grep first line number of diff including offset to
                        # comment _under_ diff
                        if echo "$line" | \
                           grep -q "@@ -[0-9]\+\(,[0-9]\+\)\? +[0-9]\+\(,[0-9]\+\)\? @@"
                           then
                           DIFFLINE="$(echo "$line" | sed 's/@@ -\([0-9]\+\).*$/\1/')"
                        fi
                        DIFF="$DIFF\n$line"
                    fi
                done
                _annotate_diff "$DIFFFILE" "$DIFFLINE" "$DIFF"
            }
        else
            echo "$OUT"
        fi
    fi
}

: ${FILES:=$(FILEREGEX='\.h$' changed_files)}

if [ -z "${FILES}" ]; then
    exit
fi

github_annotate_setup

: ${QUIET:=0}

if [ -z "$*" ]; then
    _headercheck
fi

github_annotate_teardown

exit $EXIT_CODE
{'fraction_non_alphanumeric': 0.15742849911414833, 'fraction_numerical': 0.01645153125790939, 'mean_word_length': 1.5220165922144224, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '905704', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1253, 'n_words': 344}