<commit_before>#!/usr/bin/env bash

## IMPORTANT
# when an URL contains ( ) please replace them with the code %28 %29 otherwise
# the parser will deliver a false positive

ICEORYX_ROOT_PATH=$(git rev-parse --show-toplevel)
EXIT_CODE=0

ENABLE_URL_CHECK=1

# skips github issue urls since they tend to fail quiet often
# without reason
SKIP_GITHUB_URL_CHECK=1

FILE_TO_SCAN=$1

setupTerminalColors()
{
    if [[ -t 1 ]] ## output to console, we want colors!
    then
        COLOR_BLACK="\e[30m"
        COLOR_RED="\e[31m"
        COLOR_GREEN="\e[32m"
        COLOR_YELLOW="\e[33m"
        COLOR_BLUE="\e[34m"
        COLOR_MAGENTA="\e[35m"
        COLOR_CYAN="\e[36m"
        COLOR_LIGHT_GRAY="\e[37m"
        COLOR_GRAY="\e[90m"
        COLOR_LIGHT_RED="\e[91m"
        COLOR_LIGHT_GREEN="\e[92m"
        COLOR_LIGHT_YELLOW="\e[93m"
        COLOR_LIGHT_BLUE="\e[94m"
        COLOR_LIGHT_MAGENTA="\e[95m"
        COLOR_LIGHT_MAGENTA="\e[96m"
        COLOR_WHITE="\e[97m"
        COLOR_RESET="\e[0m"
    else ## output into file, no colors and escape codes!
        COLOR_BLACK=""
        COLOR_RED=""
        COLOR_GREEN=""
        COLOR_YELLOW=""
        COLOR_BLUE=""
        COLOR_MAGENTA=""
        COLOR_CYAN=""
        COLOR_LIGHT_GRAY=""
        COLOR_GRAY=""
        COLOR_LIGHT_RED=""
        COLOR_LIGHT_GREEN=""
        COLOR_LIGHT_YELLOW=""
        COLOR_LIGHT_BLUE=""
        COLOR_LIGHT_MAGENTA=""
        COLOR_LIGHT_MAGENTA=""
        COLOR_WHITE=""
        COLOR_RESET=""
    fi
}

doesWebURLExist()
{
    if curl --insecure --head --silent --fail $1 2> /dev/null 1>/dev/null ;
    then
        echo 1
    else
        echo 0
    fi
}

isWebLink()
{
    local RESULT
    RESULT=${RESULT}$(echo $1 | sed -n "s/^https:\/\/.*//p" | wc -l)
    RESULT=${RESULT}$(echo $1 | sed -n "s/^http:\/\/.*//p" | wc -l)
    echo $RESULT | grep 1 | wc -l
}

isMailLink()
{
    echo $1 | grep -E "^mailto:" | wc -l
}

isLinkToSection()
{
    echo $1 | grep -E "^#" | wc -l
}

isAbsolutePath()
{
    echo $1 | grep -E "^/" | wc -l
}

printLinkFailureSource()
{
    echo -e "  name: ${COLOR_LIGHT_YELLOW} $LINK_NAME ${COLOR_RESET}"
    echo -e "  line: ${COLOR_LIGHT_YELLOW} $LINE_NR ${COLOR_RESET}"
    echo -e "  link: ${COLOR_LIGHT_RED} $LINK${COLOR_RESET}"
    echo

    EXIT_CODE=1
}

verifyLinkToSection()
{
    local LOCAL_LINK_VALUE=$1
    local LOCAL_FILE=$2

    LINK=$(echo $LOCAL_LINK_VALUE | sed -n "s/^#\(.*\)/#\1/p" | sed "s/\./\\\./g" | tr - '.')
    local LOCAL_LINK=$(echo $LINK | cut -f 2 -d '#')
    if ! [[ $(cat $LOCAL_FILE | grep -iE "# $LOCAL_LINK\$" | wc -l ) == 1 ]]
    then
        printLinkFailureSource
    fi
}

verifyLinkToUrl()
{
    if [[ $ENABLE_URL_CHECK == "1" ]]
    then
        LINK=$1

        if [[ $SKIP_GITHUB_URL_CHECK == "1" ]]
        then
            if [[ $(echo $LINK | grep "https://github.com/eclipse-iceoryx/iceoryx/issue" | wc -l) == "1" ]]
            then
                return
            fi
        fi
        #### [[ $(echo $LINK | grep "https://github.com/eclipse-iceoryx/iceoryx/tree" | wc -l) == "1" ]] ||

        if [[ $(echo $LINK | grep "https://github.com/eclipse-iceoryx/iceoryx/blob" | wc -l) == "1" ]]<commit_msg>iox-#1294 Link error when linking to github tree<commit_after>#!/usr/bin/env bash

## usage hints
# * when an URL contains ( ) please replace them with the code %28 %29 otherwise
#   the parser will deliver a false positive
#
# * when a link should not be tested add the comment <!--NOLINT some comment-->
#   in the same line

ICEORYX_ROOT_PATH=$(git rev-parse --show-toplevel)
EXIT_CODE=0

ENABLE_URL_CHECK=1

# skips github issue urls since they tend to fail quiet often
# without reason
SKIP_GITHUB_URL_CHECK=1

FILE_TO_SCAN=$1

setupTerminalColors()
{
    if [[ -t 1 ]] ## output to console, we want colors!
    then
        COLOR_BLACK="\e[30m"
        COLOR_RED="\e[31m"
        COLOR_GREEN="\e[32m"
        COLOR_YELLOW="\e[33m"
        COLOR_BLUE="\e[34m"
        COLOR_MAGENTA="\e[35m"
        COLOR_CYAN="\e[36m"
        COLOR_LIGHT_GRAY="\e[37m"
        COLOR_GRAY="\e[90m"
        COLOR_LIGHT_RED="\e[91m"
        COLOR_LIGHT_GREEN="\e[92m"
        COLOR_LIGHT_YELLOW="\e[93m"
        COLOR_LIGHT_BLUE="\e[94m"
        COLOR_LIGHT_MAGENTA="\e[95m"
        COLOR_LIGHT_MAGENTA="\e[96m"
        COLOR_WHITE="\e[97m"
        COLOR_RESET="\e[0m"
    else ## output into file, no colors and escape codes!
        COLOR_BLACK=""
        COLOR_RED=""
        COLOR_GREEN=""
        COLOR_YELLOW=""
        COLOR_BLUE=""
        COLOR_MAGENTA=""
        COLOR_CYAN=""
        COLOR_LIGHT_GRAY=""
        COLOR_GRAY=""
        COLOR_LIGHT_RED=""
        COLOR_LIGHT_GREEN=""
        COLOR_LIGHT_YELLOW=""
        COLOR_LIGHT_BLUE=""
        COLOR_LIGHT_MAGENTA=""
        COLOR_LIGHT_MAGENTA=""
        COLOR_WHITE=""
        COLOR_RESET=""
    fi
}

doesWebURLExist()
{
    if curl --insecure --head --silent --fail $1 2> /dev/null 1>/dev/null ;
    then
        echo 1
    else
        echo 0
    fi
}

isWebLink()
{
    local RESULT
    RESULT=${RESULT}$(echo $1 | sed -n "s/^https:\/\/.*//p" | wc -l)
    RESULT=${RESULT}$(echo $1 | sed -n "s/^http:\/\/.*//p" | wc -l)
    echo $RESULT | grep 1 | wc -l
}

isMailLink()
{
    echo $1 | grep -E "^mailto:" | wc -l
}

isLinkToSection()
{
    echo $1 | grep -E "^#" | wc -l
}

isAbsolutePath()
{
    echo $1 | grep -E "^/" | wc -l
}

printLinkFailureSource()
{
    echo -e "  name: ${COLOR_LIGHT_YELLOW} $LINK_NAME ${COLOR_RESET}"
    echo -e "  line: ${COLOR_LIGHT_YELLOW} $LINE_NR ${COLOR_RESET}"
    echo -e "  link: ${COLOR_LIGHT_RED} $LINK${COLOR_RESET}"
    echo

    EXIT_CODE=1
}

verifyLinkToSection()
{
    local LOCAL_LINK_VALUE=$1
    local LOCAL_FILE=$2

    LINK=$(echo $LOCAL_LINK_VALUE | sed -n "s/^#\(.*\)/#\1/p" | sed "s/\./\\\./g" | tr - '.')
    local LOCAL_LINK=$(echo $LINK | cut -f 2 -d '#')
    if ! [[ $(cat $LOCAL_FILE | grep -iE "# $LOCAL_LINK\$" | wc -l ) == 1 ]]
    then
        printLinkFailureSource
    fi
}

verifyLinkToUrl()
{
    if [[ $ENABLE_URL_CHECK == "1" ]]
    then
        LINK=$1

        if [[ $SKIP_GITHUB_URL_CHECK == "1" ]]
        then
            if [[ $(echo $LINK | grep "https://github.com/eclipse-iceoryx/iceoryx/issue" | wc -l) == "1" ]]
            then
                return
            fi
        fi

        # verify that no link to a specific github tree is used in the documentation
        if [[ $(echo $LINK | grep "https://github.com/eclipse-iceoryx/iceoryx/tree/" | wc -l ) == "1" ]]
        then
            local PATH_AFTER_TREE=$(echo $LINK | sed -n "s/https:\/\/github.com\/eclipse-iceoryx\/iceoryx\/tree\/\(.*\)/\1/p")
            if [[ $(echo $PATH_AFTER_TREE | grep "/" | wc -l ) == "1" ]]
            then
                echo -e "${COLOR_LIGHT_RED}Please do not use a github url when also a relative path works!${COLOR_RESET}"
                echo -e "${COLOR_LIGHT_RED}Relative paths ensure that the tag/branch of link source and destination is equal.${COLOR_RESET}"
                printLinkFailureSource
                return
            fi
        fi

        # verify that no link to a specific github branch is used in the documentation
        if [[ $(echo $LINK | grep "https://github.com/eclipse-iceoryx/iceoryx/blob" | wc -l) == "1" ]]
{'fraction_non_alphanumeric': 0.15543613063249276, 'fraction_numerical': 0.01874052638831473, 'mean_word_length': 2.204415011037528, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, 'https://': 6, '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': '7573561', 'n_tokens_mistral': 2888, 'n_tokens_neox': 2662, 'n_words': 603}