|
|
@@ -4,6 +4,15 @@ set -euo pipefail
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
DEFAULT_APP_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
|
|
APP_DIR="$(git -C "$DEFAULT_APP_DIR" rev-parse --show-toplevel 2>/dev/null || printf '%s' "$DEFAULT_APP_DIR")"
|
|
|
+force_commit="${GIT_FORCE_COMMIT:-0}"
|
|
|
+if [[ "${1:-}" == "--force-commit" ]]; then
|
|
|
+ force_commit="1"
|
|
|
+ shift
|
|
|
+fi
|
|
|
+if [[ ! "$force_commit" =~ ^[01]$ ]]; then
|
|
|
+ echo "[vmess-domain-rotator] invalid GIT_FORCE_COMMIT=${force_commit}, expected 0 or 1"
|
|
|
+ exit 1
|
|
|
+fi
|
|
|
CONFIG_PATH="${1:-${APP_DIR}/config.json}"
|
|
|
DOMAIN_FILE="${APP_DIR}/runtime/current_domain.txt"
|
|
|
|
|
|
@@ -116,6 +125,16 @@ if [[ "$work_branch" != "$runtime_branch" ]]; then
|
|
|
exit 1
|
|
|
fi
|
|
|
|
|
|
+before=""
|
|
|
+if before_raw="$(git -C "$work_dir" show "HEAD:runtime/current_domain.txt" 2>/dev/null)"; then
|
|
|
+ before="$(printf '%s' "$before_raw" | tr -d '\r\n')"
|
|
|
+fi
|
|
|
+
|
|
|
+if [[ "$force_commit" != "1" ]] && [[ -n "$before" ]] && [[ "$after" == "$before" ]]; then
|
|
|
+ echo "[vmess-domain-rotator] selected domain unchanged (${after}), skip git commit and push"
|
|
|
+ exit 0
|
|
|
+fi
|
|
|
+
|
|
|
mkdir -p "$work_dir/runtime"
|
|
|
for file in current_domain.txt current_domain.json state.json substore_vars.json; do
|
|
|
src="$APP_DIR/runtime/$file"
|
|
|
@@ -129,15 +148,31 @@ done
|
|
|
|
|
|
git -C "$work_dir" add -A runtime/current_domain.txt runtime/current_domain.json runtime/state.json runtime/substore_vars.json || true
|
|
|
|
|
|
+staged_changed="1"
|
|
|
if git -C "$work_dir" diff --cached --quiet; then
|
|
|
+ staged_changed="0"
|
|
|
+fi
|
|
|
+
|
|
|
+if [[ "$staged_changed" == "0" ]] && [[ "$force_commit" != "1" ]]; then
|
|
|
echo "[vmess-domain-rotator] no staged changes for ${runtime_branch}, skip git commit"
|
|
|
exit 0
|
|
|
fi
|
|
|
|
|
|
+commit_extra_args=()
|
|
|
+if [[ "$staged_changed" == "0" ]] && [[ "$force_commit" == "1" ]]; then
|
|
|
+ commit_extra_args+=(--allow-empty)
|
|
|
+ echo "[vmess-domain-rotator] force commit enabled with unchanged content, creating empty commit"
|
|
|
+fi
|
|
|
+
|
|
|
+commit_message="chore: rotate preferred domain to ${after} (${ts})"
|
|
|
+if [[ "$force_commit" == "1" ]]; then
|
|
|
+ commit_message="manual: domain ${after}, updated at ${ts}"
|
|
|
+fi
|
|
|
+
|
|
|
git -C "$work_dir" \
|
|
|
-c user.name="$commit_name" \
|
|
|
-c user.email="$commit_email" \
|
|
|
- commit -m "chore: rotate preferred domain to ${after} (${ts})"
|
|
|
+ commit "${commit_extra_args[@]}" -m "$commit_message"
|
|
|
|
|
|
if [[ "$push_enabled" != "1" ]]; then
|
|
|
echo "[vmess-domain-rotator] git push disabled by GIT_PUSH_ENABLED=${push_enabled}"
|