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