pre-commit-src/pre-commit
author KrzGalcz <k.galczynski@eyeo.com>
Wed, 13 Nov 2024 15:09:55 +0100
changeset 25711 2bb339143f01
parent 25710 b3dddcc5f208
child 25712 990d3ed1b36b
permissions -rwxr-xr-x
Added OS specific code and performance improvements
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
     1
#!/bin/bash
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
     2
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
     3
# Improve error handling, option e is not picked because it's expected for functions
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
     4
# to return non-0 statuses.
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
     5
set -Eeuo pipefail
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
     6
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
     7
# Allow user input during commit
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
     8
exec < /dev/tty
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
     9
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    10
# Allow modifying aliases
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    11
shopt -s expand_aliases
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    12
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    13
templates_content='{}'
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    14
templates_names=()
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    15
unique_json_files=()
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    16
unique_filterlists_to_include=()
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
    17
all_domains_variables_in_json_files='[]'
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    18
all_domains_variables_names='{}'
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    19
all_json_files_contents='{}'
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    20
variables_in_json_files='{}'
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
    21
variables_in_included_filterlists='{}'
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    22
all_domain_variables_matches_in_filterlists='[]'
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    23
last_error=''
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    24
unstaged_files=''
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    25
testing=false
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    26
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    27
# When using Windows there is a problem with return character, therefore there is a need for extra --binary option. Look https://jqlang.github.io/jq/manual/
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    28
# In general running this script directly within windows is very slow, so use of WSL2 is recommended but in case of some tests performed in windows, those lines are still needed.
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    29
if [ $OSTYPE = 'msys' ] || [ $OSTYPE = 'cygwin' ]; then
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    30
    alias jq='jq -b'
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    31
else
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    32
    alias jq='command jq'
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    33
fi
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    34
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    35
# Mac grep doesn't have Perl regex, therefor it needs to be checked with another method
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    36
if [ $OSTYPE = 'darwin'* ]; then
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    37
    alias grep='ggrep'
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    38
else
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    39
    alias grep='command grep'
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    40
fi
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    41
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    42
# In case on WSL (Windows) it's faster to invoke git.exe directly from Windows (since the files are in the windows FS).
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    43
if [ -n $(echo $WSL_DISTRO_NAME || echo '') ]; then
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    44
    alias git='git.exe'
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    45
else
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    46
    alias git='command git'
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    47
fi
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    48
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    49
error_handler() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    50
    local exit_code=$?
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    51
    local line_number=$1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    52
    echo "Error: Script failed with exit code $exit_code at line $line_number"
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
    53
    if [ "${BASH_COMMAND:-}" = "return 1" ]; then
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    54
        echo -e "Last error message:\n$last_error"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    55
    else
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    56
        echo -e "\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    57
        echo "THIS ERROR SHOULDN'T HAPPEN, PLEASE REPORT IT TO AFB TEAM OR KRIS"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    58
        echo -e "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n"
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
    59
        echo "Last executed command: ${BASH_COMMAND:-}"
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    60
    fi
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
    61
    if [ "$testing" = true ] && [ "${BASH_COMMAND:-}" = "return 1" ]; then
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    62
        exit 0
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    63
    else
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    64
        exit $exit_code
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    65
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    66
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    67
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    68
# Set up trap to catch errors and invoke the error handler
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    69
trap 'error_handler $LINENO' ERR
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    70
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    71
get_unstaged_files() {
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    72
    unstaged_files=$(git ls-files . --exclude-standard --others -m)
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    73
    local git_exit_code=$?
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    74
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    75
    if [ $git_exit_code -ne 0 ]; then
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    76
        last_error="Wrong setting of git, be sure that you run git as the same user as you cloned repo"
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    77
        return 1
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    78
    fi
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    79
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    80
    if [ -n "$unstaged_files" ]; then
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    81
        read -p "There are changes not staged for commit. The script will check only the staged version. Do you want to continue? (y/n): " choice
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    82
        case "$choice" in 
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    83
            y|Y ) echo "";;
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    84
            n|N ) echo "Aborting."; exit 1;;
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    85
            * ) echo "Invalid choice. Aborting."; exit 1;;
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    86
        esac
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    87
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    88
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    89
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    90
get_staged_files() {
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    91
  local -n staged_files_nameref=$1
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    92
  staged_files_nameref=$(git diff --name-only --cached)
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    93
}
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
    94
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    95
check_if_jq_is_installed() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    96
    if ! command -v jq &>/dev/null; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    97
        last_error="ERROR: jq is not installed. Please install jq to continue."
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    98
        return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
    99
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   100
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   101
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   102
check_if_file_exists() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   103
    local file_path="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   104
    if ! [ -f "$file_path" ]; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   105
        last_error="ERROR: File $file_path does not exist"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   106
        return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   107
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   108
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   109
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   110
check_if_unstaged_changes() {
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   111
    local file_path=$1
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   112
    local -n answer_nameref=$2
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   113
    check_if_file_exists "$file_path"
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   114
    local git_status=$(git status $file_path)
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   115
    local lines_indicating_changes=$(grep -s "Changes not staged for commit" <<< $git_status || grep -s "Untracked files:" <<< $git_status || echo '')
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   116
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   117
    if [ -n "$lines_indicating_changes" ]; then
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   118
        answer_nameref=true
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   119
    else
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   120
        answer_nameref=false
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   121
    fi
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   122
}
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   123
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   124
get_staged_version_of_a_file() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   125
    local file_name="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   126
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   127
    # Using name reference in order to not create subshells for each assignment and allow to use global variables
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   128
    # and global error handling
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   129
    local -n staged_file_content_nameref=$2
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   130
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   131
    check_if_file_exists "$file_name"
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   132
    local matched_unstaged_file=$(grep -x "$file_name" <<< "$unstaged_files" || echo '')
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   133
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   134
    if [ -n "$matched_unstaged_file" ]; then
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   135
        staged_file_content_nameref=$(git show ":$file_name" || echo '')
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   136
    else
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   137
        staged_file_content_nameref=$(cat "$file_name")
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   138
    fi
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   139
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   140
    if [ -z "$staged_file_content_nameref" ]; then
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   141
        last_error="ERROR: File $file_name was requested by a template but it's not tracked neither staged."
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   142
        return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   143
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   144
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   145
parse_template_data() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   146
    local template="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   147
    local -n file_data_nameref=$2
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   148
    local staged_template
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   149
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   150
    get_staged_version_of_a_file "$template" staged_template
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   151
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   152
    # Extract lines starting with %domainsVariables exceptionrules: and process them with jq
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   153
    local json_files_in_template=$(grep "^%domainsVariables exceptionrules:" <<< $staged_template | sed 's/^%domainsVariables exceptionrules://; s/%$//' | jq -Rs 'split("\n") | map(select(length > 0))' || echo '[]')
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   154
    # Extract lines starting with %include exceptionrules: and process them with jq
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   155
    local included_filterlists_files_in_template=$(grep "^%include exceptionrules:" <<< $staged_template | sed 's/^%include exceptionrules://; s/%$//' | jq -Rs 'split("\n") | map(select(length > 0))' || echo '[]')
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   156
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   157
    if [ "$included_filterlists_files_in_template" = "[]" ]; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   158
        last_error="ERROR: There is no list included in template $template_name"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   159
        return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   160
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   161
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   162
    # Create a JSON object with the template name as the key and the extracted domainsVariables and include data as values
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   163
    file_data_nameref=$(jq -n --arg template "$template" --argjson json_files_in_template "$json_files_in_template" --argjson included_filterlists_files_in_template "$included_filterlists_files_in_template" '
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   164
        {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   165
            $template: {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   166
                "domainsVariables": $json_files_in_template,
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   167
                "include": $included_filterlists_files_in_template
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   168
            }
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   169
        }')
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   170
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   171
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   172
update_templates_content() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   173
    local file_template="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   174
    # Merge the new template data into the existing templates_content JSON
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   175
    templates_content=$(jq -n --argjson templates_content "$templates_content" --argjson file_template "$file_template" '$templates_content + $file_template')
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   176
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   177
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   178
update_unique_json_files() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   179
    local template_name="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   180
    local file_data="$2"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   181
    # Extract the list of domainsVariables files from the template data
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   182
    local json_files_list=$(jq -r --arg template_name "$template_name" '.[$template_name].domainsVariables[]' <<< $file_data)
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   183
    for json_file in $json_files_list; do
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   184
        local matches_in_unique_json_files=$(grep -swF "$json_file" <<< ${unique_json_files[@]} || echo '')
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   185
        if [ -z "$matches_in_unique_json_files" ]; then
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   186
            unique_json_files+=("$json_file")
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   187
        fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   188
    done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   189
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   190
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   191
update_unique_includes() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   192
    local template_name="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   193
    local file_data="$2"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   194
    # Extract the list of included files from the template data
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   195
    local included_files_list=$(jq -r --arg file "$template_name" '.[$file].include[]' <<< $file_data)
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   196
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   197
    for included_filterlist in $included_files_list; do
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   198
        local matches_in_unique_filterlists=$(grep -swF "$included_filterlist" <<< ${unique_filterlists_to_include[@]} || echo '')
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   199
        if [ -z "$matches_in_unique_filterlists" ]; then
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   200
            unique_filterlists_to_include+=("$included_filterlist")
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   201
        fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   202
    done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   203
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   204
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   205
check_if_valid_json() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   206
    local json_file_path="$1"
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   207
    if ! jq -e . >/dev/null 2>&1 <<< $(cat "$json_file_path"); then
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   208
        last_error="ERROR: Invalid JSON content in $json_file_path"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   209
        return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   210
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   211
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   212
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   213
get_domains_variables_names_incl_duplicates() {
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   214
    local json_file_content="$1"
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   215
    local -n domains_variables_names_incl_duplicated_nameref=$2
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   216
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   217
    # If jq would be used the duplicates would be automatically removed, therefore I used perl
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   218
    domains_variables_names_incl_duplicated_nameref=$(perl -0777 -ne 'print "$1\n" while /"([^"]+?)"(?=[\s\r\n]*:)/g' <<< $json_file_content)
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   219
}
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   220
check_if_correct_domains_variables_json_structure() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   221
    local json_file_content="$1"
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   222
    local json_file_path="$2"
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   223
    # Check if the JSON structure is valid and matches the expected format:
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   224
    # { "variable1": ["domain1", "domain2" (...)], "variable2": ["domain1", "domain3" (...)], (...)}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   225
    if ! echo $json_file_content | jq -e '
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   226
        type == "object" and
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   227
        ([keys[] as $k | .[$k] | type == "array" and all(.[]; type == "string")] | all)
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   228
        '   >/dev/null 2>&1;
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   229
    then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   230
        last_error="ERROR: JSON structure is invalid in $json_file_path"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   231
        return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   232
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   233
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   234
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   235
check_if_duplicated_domains_variable_name_in_single_file() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   236
    local json_file_path="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   237
    local domains_variables_names="$2"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   238
    if [ "$(echo "$domains_variables_names" | sort | uniq -d)" ]; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   239
        last_error="ERROR: Duplicate domains_variables_names found in $json_file_path"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   240
        return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   241
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   242
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   243
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   244
check_if_valid_domains_variable_name() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   245
    local domains_variable_name="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   246
    if ! [[ "$domains_variable_name" =~ ^[[:alnum:]_]+$ ]]; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   247
        last_error="ERROR: Invalid domains variable name: $domains_variable_name, only alphanumeric characters and underscores are allowed"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   248
        return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   249
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   250
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   251
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   252
check_if_duplicated_domains() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   253
    local json_file_path="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   254
    local key="$2"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   255
    local value="$3"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   256
    # Value of domainsVariable is an array of strings representing domains,
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   257
    # therefore the error message mentions domains.
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   258
    if [ -n "$(echo "$value" | sort | uniq -di)" ]; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   259
        last_error="ERROR: There are duplicated domains in $key in file $json_file_path:\n"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   260
        last_error+="$(echo "$value" | sort | uniq -di)"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   261
        return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   262
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   263
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   264
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   265
check_if_correct_domain() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   266
    local json_file_path="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   267
    local domains_variable_name="$2"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   268
    local domains="$3"
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   269
    # Preselect candidates for invalid domains, then check them 
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   270
    local candidates=$(grep -vP "^(([A-Za-z0-9][A-Za-z0-9-]{0,61})?[A-Za-z0-9]\.)+([A-Za-z]{2,63}|\*|xn--[a-zA-Z0-9-]{1,59})$" <<< $domains || echo '')
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   271
    if [ -n "$candidates" ]; then
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   272
      last_error="ERROR: Invalid domain in $domains_variable_name: $candidates in file $json_file_path"
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   273
      return 1
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   274
    fi
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   275
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   276
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   277
check_if_correct_domains_variables() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   278
    local json_file_path="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   279
    local domains_variables_names="$2"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   280
    local json_file_content="$3"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   281
    check_if_duplicated_domains_variable_name_in_single_file "$json_file_path" "$domains_variables_names"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   282
    for domains_variable_name in $domains_variables_names; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   283
        check_if_valid_domains_variable_name "$domains_variable_name"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   284
        # Extract the value associated with the domains variable name from the JSON file
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   285
        local domains=$(jq -r --arg key "$domains_variable_name" '.[$key][]' <<< $json_file_content)
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   286
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   287
        check_if_duplicated_domains "$json_file_path" "$domains_variable_name" "$domains"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   288
        check_if_correct_domain "$json_file_path" "$domains_variable_name" "$domains"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   289
    done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   290
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   291
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   292
update_domains_variables_data() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   293
    local domains_variables_names="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   294
    local json_file_path="$2"
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   295
    local json_file_content="$3"
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   296
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   297
    # Merge the new domains variables into the existing all_domains_variables_in_json_files array
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   298
    all_domains_variables_in_json_files=$(jq -n --argjson all_domains_variables_in_json_files "$all_domains_variables_in_json_files" --argjson domains_variables_names "$(echo "$domains_variables_names" | jq -Rs 'split("\n") | map(select(length > 0))')" '$all_domains_variables_in_json_files + $domains_variables_names | unique')
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   299
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   300
    # Adding all the json files content to the variable was causing "Too big variable" error
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   301
    # Therefore, the content is saved to a temporary file and then loaded back to the variable
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   302
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   303
    all_json_files_temp_file=$(mktemp)
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   304
    echo "$all_json_files_contents" > "$all_json_files_temp_file"
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   305
    json_file_content_temp=$(mktemp)
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   306
    echo "$json_file_content" > "$json_file_content_temp"
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   307
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   308
    all_json_files_contents=$(jq --arg json_file_path "$json_file_path" --slurpfile json_file_content "$json_file_content_temp" '.[$json_file_path] = $json_file_content[0]' "$all_json_files_temp_file")
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   309
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   310
    rm "$json_file_content_temp"
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   311
    rm "$all_json_files_temp_file"
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   312
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   313
    # Update the variables_in_json_files object with the keys from the JSON file
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   314
    variables_in_json_files=$(echo "$variables_in_json_files" | jq --arg key "$json_file_path" --argjson value "$(echo "$all_json_files_contents" | jq 'to_entries | map({key: .key, value: (.value | keys)}) | from_entries' | jq --arg key "$json_file_path" '.[$key]')" '. * {($key): $value}')
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   315
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   316
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   317
check_if_duplicated_domains_variable_name_between_files() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   318
    local json_file_path="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   319
    local domains_variables_names="$2"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   320
    for domains_variable_name in $domains_variables_names; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   321
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   322
        # Check if the domains variable name exists in the all_domains_variables_in_json_files array
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   323
        if jq -e --arg name "$domains_variable_name" 'index($name) != null' <<< $all_domains_variables_in_json_files >/dev/null 2>&1; then
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   324
            # If a duplicate is found, identify the file containing the duplicate and report an error
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   325
            local file_with_duplicate=$(jq -r --arg name "$domains_variable_name" '
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   326
            to_entries | map(select(.value | has($name))) | .[0].key
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   327
            ' <<< $all_json_files_contents)
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   328
            last_error="ERROR: Duplicate domains variable found in $json_file_path and $file_with_duplicate file: $domains_variable_name"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   329
            return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   330
        fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   331
    done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   332
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   333
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   334
find_domain_variables_syntax_in_filterlist() {
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   335
    local filterlist_content="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   336
    local filterlist_path="$2"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   337
    local -n all_lines_with_domain_variables_in_filterlist_nameref=$3
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   338
    # Find lines containing domain variables in the filterlist
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   339
    # The regex to find them is simpler than in filterlist delivery to also catch domains variables in the wrong place
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   340
    # without starting with a complex regex. The full regex is in one of the next steps
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   341
    all_lines_with_domain_variables_in_filterlist_nameref=$(grep -P '%<\{.*\}>%' <<< $filterlist_content || echo '')
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   342
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   343
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   344
check_for_similar_to_domain_variable() {
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   345
    local filterlist_content="$1"
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   346
    local filterlist_path="$2"
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   347
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   348
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   349
    # Precheck to quickly gather only the potentiall candidates for the full check
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   350
    local weak_candidates=$(grep -P "[%{}<>].\w+.[%{}<>]" <<< $filterlist_content || echo '')
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   351
    local candidates=$(grep -P "[%{}<>].?[%{}<>].*[%{}<>].?[%{}<>]" <<< $weak_candidates || echo '')
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   352
    local strong_candidates=$(grep -oP "([^%{}<>]|^)[%{}<>]+.?[%{}<>]+.*[%{}<>]+.?[%{}<>]+([^%{}<>]|$)" <<< $candidates || echo '')
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   353
    if [ -n "$strong_candidates" ]; then
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   354
        local lines_with_similar_to_domain_variable=$(grep -vP "([|,=]|^)%<\{\w+\}>%([,|#]|$)" <<< $candidates || echo '')
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   355
        if [ -n "$lines_with_similar_to_domain_variable" ]; then
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   356
            last_error="ERROR: Found a line in $filterlist_path that is similar to a domain variable, but it's not a domain variable:\n\n"
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   357
            last_error+="$lines_with_similar_to_domain_variable"
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   358
            return 1
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   359
        fi
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   360
    fi
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   361
    }
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   362
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   363
process_filters() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   364
    local all_lines_with_domain_variables_in_filterlist="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   365
    local -n domains_variables_collected_from_filterlist_nameref="$2"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   366
    domains_variables_collected_from_filterlist_nameref='[]'
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   367
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   368
    for filter in $all_lines_with_domain_variables_in_filterlist; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   369
        # Extract the domain variable from the filter
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   370
        local domains_variable_match=$(grep -oP '(?<=%<\{).*?(?=\}>%)' <<< $filter)
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   371
        if [ "$(echo "$domains_variable_match" | wc -l)" -gt 1 ]; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   372
            last_error="ERROR: More than 2 domain variables found in filter: $filter"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   373
            return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   374
        fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   375
        # Ensure the domain variable is correctly formatted in the filter
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   376
        local true_matches
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   377
        true_matches=$(grep -P '(%<{(\w+)}>%(?:,~?[a-zA-Z0-9*.~-]+)*#[?@$]?#)|([,$]domain=(?:[a-zA-Z0-9*.~-]+\|)*%<{(\w+)}>%)' <<< $filter)
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   378
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   379
        if [ -z "$true_matches" ]; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   380
            last_error="ERROR: Domain variable added in a wrong way in filter: $filter"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   381
            return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   382
        fi
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   383
        domains_variables_collected_from_filterlist_nameref=$(jq --arg domains_variable_match "$domains_variable_match" '. + [$domains_variable_match]' <<< $domains_variables_collected_from_filterlist_nameref)
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   384
    done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   385
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   386
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   387
update_matches_and_variables() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   388
    local domains_variables_collected_from_filterlist="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   389
    local file_path="$2"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   390
    
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   391
    # Update the list of all domain variable matches in filterlists
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   392
    all_domain_variables_matches_in_filterlists=$(jq -n --argjson all_domain_variables_matches_in_filterlists "$all_domain_variables_matches_in_filterlists" --argjson matches "$domains_variables_collected_from_filterlist" '$all_domain_variables_matches_in_filterlists + $matches | unique')
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   393
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   394
    # Update the variables_in_included_filterlists object with the matches from the current filterlist
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   395
    variables_in_included_filterlists=$(jq --arg key "$file_path" --argjson value "$domains_variables_collected_from_filterlist" '.[$key] = $value' <<< $variables_in_included_filterlists)
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   396
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   397
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   398
extract_domains_variables_in_included_filterlists() {
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   399
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   400
    local template_name="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   401
    local -n domains_variables_in_included_filterlists_nameref=$2
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   402
    # Extract the list of included filterlists from the template
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   403
    local included_filterlists=$(jq -r --arg template_name "$template_name" '.[$template_name].include[]' <<< "$templates_content")
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   404
    domains_variables_in_included_filterlists_nameref=()
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   405
    for included_filterlist in $included_filterlists; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   406
        # Extract the domain variables from each included filterlist
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   407
        local domains_variables=$(jq -r --arg included_filterlist "$included_filterlist" '.[$included_filterlist][]' <<< "$variables_in_included_filterlists")
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   408
        for domain_variable in $domains_variables; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   409
            domains_variables_in_included_filterlists_nameref+=("$domain_variable")
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   410
        done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   411
    done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   412
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   413
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   414
extract_domains_variables_in_included_json_files() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   415
    local template_name="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   416
    local -n domains_variables_in_included_json_files_nameref=$2
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   417
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   418
    # Extract the list of included JSON files from the template
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   419
    local included_json_files=$(jq -r --arg template_name "$template_name" '.[$template_name].domainsVariables[]' <<< $templates_content)
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   420
    domains_variables_in_included_json_files_nameref=()
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   421
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   422
    for included_json_file in $included_json_files; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   423
        # Extract the domain variables from each included JSON file
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   424
        local domains_variables=$(jq -r --arg key "$included_json_file" '.[$key][]' <<< $variables_in_json_files)
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   425
        for domain_variable in $domains_variables; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   426
            domains_variables_in_included_json_files_nameref+=("$domain_variable")
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   427
        done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   428
    done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   429
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   430
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   431
check_domain_variables_in_filterlists() {
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   432
    local template_name="$1"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   433
    local domains_variables_in_included_filterlists=()
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   434
    local domains_variables_in_included_json_files=()
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   435
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   436
    # When for example $2 was empty, then the array had one element with empty string
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   437
    if [ -n "$2" ]; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   438
        domains_variables_in_included_filterlists=($2)
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   439
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   440
    if [ -n "$3" ]; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   441
        domains_variables_in_included_json_files=($3)
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   442
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   443
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   444
    # Extract the list of included filterlists and JSON files from the template
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   445
    local included_filterlists=$(jq -r --arg template_name "$template_name" '.[$template_name].include[]' <<< $templates_content)
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   446
    local included_json_files=$(jq -r --arg template_name "$template_name" '.[$template_name].domainsVariables[]' <<< $templates_content)
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   447
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   448
    for domain_variable_in_filterlist in ${domains_variables_in_included_filterlists[@]}; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   449
        local found=false
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   450
        for domain_variable_in_json_file in ${domains_variables_in_included_json_files[@]}; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   451
            if [ "$domain_variable_in_filterlist" = "$domain_variable_in_json_file" ]; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   452
                found=true
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   453
                break
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   454
            fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   455
        done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   456
        if ! $found; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   457
            last_error="Error: One of the filterlists:\n\n"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   458
            last_error+="$included_filterlists\n\n"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   459
            last_error+="included in the template $template_name contain a domain variable $domain_variable_in_filterlist "
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   460
            last_error+="which wasn't found in any of the domains variables files included in that template:\n\n"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   461
            last_error+="$included_json_files"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   462
            return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   463
        fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   464
    done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   465
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   466
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   467
check_if_domains_variables_are_identical_in_lists_and_jsons() {
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   468
    if [ "$all_domains_variables_in_json_files" != "$all_domain_variables_matches_in_filterlists" ]; then
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   469
        last_error="Error: the domain variables in domain-variables file and the filter list are not the same\n"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   470
        last_error+="Extra variables in domain-variables files:\n"
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   471
        last_error+="$(jq -n --argjson all_domains_variables_in_json_files "$all_domains_variables_in_json_files" --argjson all_domain_variables_matches_in_filterlists "$all_domain_variables_matches_in_filterlists" '$all_domains_variables_in_json_files - $all_domain_variables_matches_in_filterlists')\n"
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   472
        last_error+="Extra variables in filter lists:\n"
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   473
        last_error+=$(jq -n --argjson all_domains_variables_in_json_files "$all_domains_variables_in_json_files" --argjson all_domain_variables_matches_in_filterlists "$all_domain_variables_matches_in_filterlists" '$all_domain_variables_matches_in_filterlists - $all_domains_variables_in_json_files')
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   474
        return 1
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   475
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   476
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   477
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   478
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   479
main() {
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   480
    get_unstaged_files
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   481
    check_if_jq_is_installed
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   482
    for template_name in *.txt; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   483
        templates_names+=("$template_name")
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   484
        # To avoid creating a subshell, the variable is passed as a reference to parse_template_data function
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   485
        # That helps with the error handling and allows to use global variables
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   486
        local file_data
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   487
        # Parse data from the template
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   488
        parse_template_data "$template_name" file_data
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   489
        # Update the templates_content JSON with the data from the file
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   490
        update_templates_content "$file_data"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   491
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   492
        update_unique_json_files "$template_name" "$file_data"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   493
        update_unique_includes "$template_name" "$file_data"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   494
    done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   495
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   496
    for domains_variables_path in ${unique_json_files[@]}; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   497
        local staged_domains_variables_file
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   498
        get_staged_version_of_a_file "$domains_variables_path" staged_domains_variables_file
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   499
        check_if_correct_domains_variables_json_structure "$staged_domains_variables_file" "$domains_variables_path"
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   500
        local domains_variables_names_incl_duplicates
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   501
        get_domains_variables_names_incl_duplicates "$staged_domains_variables_file" domains_variables_names_incl_duplicates
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   502
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   503
        check_if_correct_domains_variables "$domains_variables_path" "$domains_variables_names_incl_duplicates" "$staged_domains_variables_file"
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   504
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   505
        check_if_duplicated_domains_variable_name_between_files "$domains_variables_path" "$domains_variables_names_incl_duplicates"
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   506
        update_domains_variables_data "$domains_variables_names_incl_duplicates" "$domains_variables_path" "$staged_domains_variables_file"
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   507
    done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   508
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   509
    for filterlist_path in ${unique_filterlists_to_include[@]}; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   510
        local filterlist_content
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   511
        get_staged_version_of_a_file "$filterlist_path" filterlist_content
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   512
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   513
        check_for_similar_to_domain_variable  "$filterlist_content" "$filterlist_path"
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   514
        local all_lines_with_domain_variables_in_filterlist
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   515
        find_domain_variables_syntax_in_filterlist "$filterlist_content" "$filterlist_path" all_lines_with_domain_variables_in_filterlist
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   516
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   517
        if [ -z "$all_lines_with_domain_variables_in_filterlist" ]; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   518
            # In case of lack of matches, the value of all_lines_with_domain_variables_in_filterlist should have just
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   519
            # a message to show.
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   520
            local domains_variables_collected_from_filterlist='[]'
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   521
        else
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   522
            local domains_variables_collected_from_filterlist
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   523
            process_filters "$all_lines_with_domain_variables_in_filterlist" domains_variables_collected_from_filterlist
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   524
        fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   525
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   526
        update_matches_and_variables "$domains_variables_collected_from_filterlist" "$filterlist_path"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   527
    done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   528
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   529
    local domains_variables_in_included_filterlists=()
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   530
    for template_name in ${templates_names[@]}; do
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   531
        extract_domains_variables_in_included_filterlists "$template_name" domains_variables_in_included_filterlists
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   532
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   533
        local domains_variables_in_included_json_files
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   534
        extract_domains_variables_in_included_json_files "$template_name" domains_variables_in_included_json_files
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   535
        check_domain_variables_in_filterlists "$template_name" "$(echo ${domains_variables_in_included_filterlists[@]})" "$(echo ${domains_variables_in_included_json_files[@]})"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   536
    done
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   537
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   538
    check_if_domains_variables_are_identical_in_lists_and_jsons
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   539
}
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   540
25708
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   541
check_unit_tests() {
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   542
    local exit_status=0
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   543
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   544
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   545
    ./pre-commit-src/tests/pre-commit-tests.sh '--no-verbose' || exit_status=1
25708
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   546
    function_exit_code=$?
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   547
    if [ $exit_status -ne 1 ]; then
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   548
        exit_status=$function_exit_code
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   549
    fi
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   550
25708
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   551
    if [ $exit_status -ne 0 ]; then
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   552
        last_error="Unit tests failed with exit code $exit_status"
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   553
        last_error+="\nPlease fix the issues before continuing"
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   554
        last_error+="To check the details run 'pre-commit-src/tests/pre-commit-tests.sh'"
25708
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   555
        return $exit_status
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   556
    else
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   557
        last_error="Unit tests passed successfully"
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   558
    fi
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   559
}
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   560
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   561
check_pre_commit_files() {
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   562
    local staged_files
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   563
    get_staged_files staged_files
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   564
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   565
    local pre_commit_path='pre-commit-src/pre-commit'
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   566
    check_if_file_exists "$pre_commit_path"
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   567
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   568
    local matched_pre_commit_in_unstaged=$(grep -x "$pre_commit_path" <<< "$unstaged_files" || echo '')
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   569
    if [ -n "$matched_pre_commit_in_unstaged" ]; then
25708
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   570
        last_error="Unstaged changes detected in pre-commit file. Stage pre-commit changes before continuing."
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   571
        return 1
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   572
    fi
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   573
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   574
    local pre_commit_script_path='pre-commit-src/pre-commit-script.sh'
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   575
    check_if_file_exists "$pre_commit_script_path"
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   576
    local matched_pre_commit_script_in_unstaged='' # This file will go with the next commit. To better show what changed since the last push I decided to first push it without name change $(grep -x "$pre_commit_script_path" <<< "$unstaged_files" || echo '')
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   577
    if [ -n "$matched_pre_commit_script_in_unstaged" ]; then
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   578
        last_error="Unstaged changes detected in pre-commit-script.sh file. Stage pre-commit-script changes before continuing."
25708
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   579
        return 1
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   580
    fi
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   581
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   582
    pre_commit_tests_path='pre-commit-src/tests/pre-commit-tests.sh'
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   583
    check_if_file_exists "$pre_commit_tests_path"
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   584
    local matched_pre_commit_tests_in_unstaged=$(grep -x "$pre_commit_tests_path" <<< "$unstaged_files" || echo '')
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   585
    if [ -n "$matched_pre_commit_tests_in_unstaged" ]; then
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   586
        last_error="Unstaged changes detected in pre-commit-tests file. Stage pre-commit tests changes before continuing."
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   587
        return 1
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   588
    fi
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   589
    # Only if something changed in pre commit or pre commit tests the unit tests should be run
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   590
    if [ -z "$matched_pre_commit_in_unstaged" + "$matched_pre_commit_script_in_unstaged" "$matched_pre_commit_tests_in_unstaged" ]; then
25708
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   591
        check_unit_tests
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   592
    fi
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   593
}
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   594
25708
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   595
# For testing purposes only if the script has no arguments or the argument is main the process should run
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   596
# thanks to that the script can be tested without running the main function
25710
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   597
if [ -z "${1:-}" ]; then
b3dddcc5f208 Improved performance, messaging, fix bugs and tests
eyeokg <k.galczynski@eyeo.com>
parents: 25709
diff changeset
   598
    echo 'Running pre-commit checks...'
25708
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   599
    main
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   600
    check_pre_commit_files
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   601
25709
ca5e4f738e57 Improved pre_commit and tests, removed old pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25708
diff changeset
   602
    echo "Pre-commit checks passed successfully. Double check if there were no error messages above this message before pushing"
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   603
    unalias jq
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   604
    unalias grep
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   605
    unalias git
25708
d3198dc01a79 Reorganized pre-commit
eyeokg <k.galczynski@eyeo.com>
parents: 25707
diff changeset
   606
    exit 0
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   607
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   608
elif [ "$1" = "--load-only" ]; then
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   609
    testing=true
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   610
    echo "Script loaded successfully"
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   611
else
25711
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   612
    echo Wrong option provided "$@"
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   613
    unalias jq
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   614
    unalias grep
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   615
    unalias git
2bb339143f01 Added OS specific code and performance improvements
KrzGalcz <k.galczynski@eyeo.com>
parents: 25710
diff changeset
   616
    exit 1
25707
2a19e3a985b6 Moved pre-commit files and added first test
eyeokg <k.galczynski@eyeo.com>
parents:
diff changeset
   617
fi