diff --git a/docs/get-clickhouse-docs.sh b/docs/get-clickhouse-docs.sh old mode 100644 new mode 100755 index 1ba0dae9844..7868f281bd0 --- a/docs/get-clickhouse-docs.sh +++ b/docs/get-clickhouse-docs.sh @@ -27,5 +27,27 @@ else exit 1 ;; esac + + if [ -n "$2" ]; then + set_git_hook="$2" + elif [ ! -n "$1" ]; then + read -rp "Would you like to setup git hook for automatic update? (y|n): " set_git_hook + fi + + if [ "$set_git_hook" = "y" ]; then + hook_command="$(pwd)/pull-clickhouse-docs-hook.sh 24" + hook_file=$(realpath "$(pwd)/../.git/hooks/post-checkout") + already_have=$(grep -Faq "pull-clickhouse-docs-hook.sh" "$hook_file" 2>/dev/null && echo 1 || echo 0) + if [ $already_have -eq 0 ]; then + echo "Appending '$hook_command' to $hook_file" + echo "$hook_command" >> "$hook_file" + chmod u+x "$hook_file" # Just in case it did not exist before append + else + echo "Looks like the update hook already exists, will not add another one" + fi + elif [ ! "$set_git_hook" = "n" ]; then + echo "Expected 'y' or 'n', got '$set_git_hook', will not setup git hook" + fi + git clone "$git_url" "clickhouse-docs" fi diff --git a/docs/pull-clickhouse-docs-hook.sh b/docs/pull-clickhouse-docs-hook.sh new file mode 100755 index 00000000000..bd93a1d3997 --- /dev/null +++ b/docs/pull-clickhouse-docs-hook.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -e +# The script to update user-guides documentation repo +# https://github.com/ClickHouse/clickhouse-docs + +WORKDIR=$(dirname "$0") +WORKDIR=$(readlink -f "${WORKDIR}") +cd "$WORKDIR" + +UPDATE_PERIOD_HOURS="${1:-24}" # By default update once per 24 hours; 0 means "always update" + +if [ ! -d "clickhouse-docs" ]; then + echo "There's no clickhouse-docs/ dir, run get-clickhouse-docs.sh first to clone the repo" + exit 1 +fi + +# Do not update it too often +LAST_FETCH_TS=$(stat -c %Y clickhouse-docs/.git/FETCH_HEAD 2>/dev/null || echo 0) +CURRENT_TS=$(date +%s) +HOURS_SINCE_LAST_FETCH=$(( (CURRENT_TS - LAST_FETCH_TS) / 60 / 60 )) + +if [ "$HOURS_SINCE_LAST_FETCH" -lt "$UPDATE_PERIOD_HOURS" ]; then + exit 0; +fi + +echo "Updating clickhouse-docs..." +git -C clickhouse-docs pull