2020-12-14 17:12:57 +00:00
|
|
|
#!/bin/sh
|
2020-09-09 12:03:27 +01:00
|
|
|
|
2020-12-14 17:12:57 +00:00
|
|
|
set -e -f -u
|
2020-07-15 10:49:08 +01:00
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
# This comment is used to simplify checking local copies of the script. Bump
|
|
|
|
# this number every time a significant change is made to this script.
|
2023-02-21 14:07:12 +00:00
|
|
|
#
|
2024-11-08 14:18:16 +00:00
|
|
|
# AdGuard-Project-Version: 5
|
2024-02-08 17:39:18 +00:00
|
|
|
|
|
|
|
# TODO(a.garipov): Add pre-merge-commit.
|
2023-02-21 14:07:12 +00:00
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
# Only show interactive prompts if there a terminal is attached to stdout.
|
|
|
|
# While this technically doesn't guarantee that reading from /dev/tty works,
|
|
|
|
# this should work reasonably well on all of our supported development systems
|
|
|
|
# and in most terminal emulators.
|
2021-12-13 12:28:12 +00:00
|
|
|
is_tty='0'
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ -t '1' ]; then
|
2021-12-13 12:28:12 +00:00
|
|
|
is_tty='1'
|
|
|
|
fi
|
|
|
|
readonly is_tty
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
# prompt is a helper that prompts the user for interactive input if that can be
|
|
|
|
# done. If there is no terminal attached, it sleeps for two seconds, giving the
|
|
|
|
# programmer some time to react, and returns with a zero exit code.
|
2021-12-13 12:28:12 +00:00
|
|
|
prompt() {
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "$is_tty" -eq '0' ]; then
|
2021-12-13 12:28:12 +00:00
|
|
|
sleep 2
|
|
|
|
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
while true; do
|
2021-12-13 12:28:12 +00:00
|
|
|
printf 'commit anyway? y/[n]: '
|
2024-11-08 14:18:16 +00:00
|
|
|
read -r ans </dev/tty
|
2021-12-13 12:28:12 +00:00
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
case "$ans" in
|
|
|
|
'y' | 'Y')
|
2021-12-13 12:28:12 +00:00
|
|
|
break
|
|
|
|
;;
|
2024-11-08 14:18:16 +00:00
|
|
|
'' | 'n' | 'N')
|
2021-12-13 12:28:12 +00:00
|
|
|
exit 1
|
|
|
|
;;
|
2024-11-08 14:18:16 +00:00
|
|
|
*)
|
2021-12-13 12:28:12 +00:00
|
|
|
continue
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
# Warn the programmer about unstaged changes and untracked files, but do not
|
|
|
|
# fail the commit, because those changes might be temporary or for a different
|
|
|
|
# branch.
|
2024-08-20 16:38:04 +01:00
|
|
|
#
|
|
|
|
# shellcheck disable=SC2016
|
2021-12-13 12:28:12 +00:00
|
|
|
awk_prog='substr($2, 2, 1) != "." { print $9; } $1 == "?" { print $2; }'
|
|
|
|
readonly awk_prog
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
unstaged="$(git status --porcelain=2 | awk "$awk_prog")"
|
2021-12-13 12:28:12 +00:00
|
|
|
readonly unstaged
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "$unstaged" != '' ]; then
|
2021-12-13 12:28:12 +00:00
|
|
|
printf 'WARNING: you have unstaged changes:\n\n%s\n\n' "$unstaged"
|
|
|
|
prompt
|
|
|
|
fi
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
# Warn the programmer about temporary todos and skel FIXMEs, but do not fail the
|
|
|
|
# commit, because the commit could be in a temporary branch.
|
|
|
|
temp_todos="$(
|
|
|
|
git grep -e 'FIXME' -e 'TODO.*!!' -- \
|
|
|
|
':!./scripts/hooks/pre-commit' \
|
|
|
|
':!./client' \
|
|
|
|
|| :
|
|
|
|
)"
|
2021-12-13 12:28:12 +00:00
|
|
|
readonly temp_todos
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "$temp_todos" != '' ]; then
|
2021-12-13 12:28:12 +00:00
|
|
|
printf 'WARNING: you have temporary todos:\n\n%s\n\n' "$temp_todos"
|
|
|
|
prompt
|
|
|
|
fi
|
2021-07-29 15:40:31 +01:00
|
|
|
|
|
|
|
verbose="${VERBOSE:-0}"
|
|
|
|
readonly verbose
|
2021-06-28 16:24:33 +01:00
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "$(git diff --cached --name-only -- '*.md' || :)" != '' ]; then
|
2024-08-20 16:38:04 +01:00
|
|
|
make VERBOSE="$verbose" md-lint
|
|
|
|
fi
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "$(git diff --cached --name-only -- '*.sh' || :)" != '' ]; then
|
2024-08-20 16:38:04 +01:00
|
|
|
make VERBOSE="$verbose" sh-lint
|
|
|
|
fi
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "$(git diff --cached --name-only -- '*.md' '*.txt' '*.yaml' '*.yml' || :)" != '' ]; then
|
2021-07-29 15:40:31 +01:00
|
|
|
make VERBOSE="$verbose" txt-lint
|
2021-05-19 18:31:20 +01:00
|
|
|
fi
|
|
|
|
|
2024-11-08 14:18:16 +00:00
|
|
|
if [ "$(git diff --cached --name-only -- '*.go' '*.mod' 'Makefile' || :)" != '' ]; then
|
|
|
|
make VERBOSE="$verbose" go-os-check go-lint go-test
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$(git diff --cached --name-only -- './openapi/openapi.yaml' || :)" != '' ]; then
|
2021-07-29 15:40:31 +01:00
|
|
|
make VERBOSE="$verbose" openapi-lint
|
2021-01-14 10:48:52 +00:00
|
|
|
fi
|