view hg-web/run.sh @ 257:609d3c6aff4e

[seobeo] Add persistent SSE streams Co-authored-by: Copilot <[email protected]>
author MrJuneJune <me@mrjunejune.com>
date Tue, 04 Aug 2026 16:49:11 -0700
parents c5129452493e
children
line wrap: on
line source

#!/usr/bin/env bash
set -Eeuo pipefail

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
bundle_root="$(dirname "$script_dir")"
repository="${HG_REPOSITORY_PATH:-$bundle_root/repository}"
hg_binary="$bundle_root/third_party/mercurial/runtime/bin/hg"
server="$bundle_root/hg_web_server"
hg_pid=""
server_pid=""

if [[ ! -x "$hg_binary" ]]; then
  echo "Bundled Mercurial is unavailable: $hg_binary" >&2
  exit 1
fi
if [[ ! -x "$server" ]]; then
  echo "hg-web server is unavailable: $server" >&2
  exit 1
fi
if [[ ! -d "$repository/.hg" ]]; then
  echo "Mercurial repository snapshot is unavailable: $repository" >&2
  exit 1
fi

cleanup() {
  trap - EXIT INT TERM
  for pid in "$server_pid" "$hg_pid"; do
    if [[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null; then
      kill "$pid" 2>/dev/null || true
    fi
  done
  for pid in "$server_pid" "$hg_pid"; do
    if [[ -n "$pid" ]]; then
      wait "$pid" 2>/dev/null || true
    fi
  done
}
trap cleanup EXIT
trap 'exit 130' INT TERM

hg_arguments=(
  --repository "$repository"
  serve
  --address 127.0.0.1
  --port 4444
  --accesslog -
  --errorlog -
)
if [[ "${HG_ALLOW_PUSH:-false}" == "true" ]]; then
  echo "WARNING: Mercurial pushes are enabled; protect /repo in Nginx."
  hg_arguments=(
    --config "web.allow-push=*"
    --config "web.deny-push="
    --config "web.push_ssl=false"
    "${hg_arguments[@]}"
  )
else
  hg_arguments=(
    --config "web.allow-push="
    --config "web.deny-push=*"
    "${hg_arguments[@]}"
  )
fi

export HGRCPATH=
"$hg_binary" "${hg_arguments[@]}" &
hg_pid=$!

(
  cd "$bundle_root"
  exec "$server"
) &
server_pid=$!

echo "hg-web: http://127.0.0.1:6970"
echo "hg serve repository: $repository"

set +e
wait -n "$hg_pid" "$server_pid"
status=$?
set -e
exit "$status"