#!/usr/bin/env bash set -euo pipefail echo "[push-ext-commit] Ensure all submodules on ext, commit if needed, push ext" PUSH_FORCE=${PUSH_FORCE:-0} # Work from repo root ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$ROOT_DIR" # Update submodules metadata (non-recursive update of refs; content is already cloned) git submodule init >/dev/null 2>&1 || true git submodule foreach --recursive ' set -e echo "[submodule] Enter: $name ($path)" # Ensure remote exists git remote show origin >/dev/null 2>&1 || { echo "[submodule] missing origin remote"; exit 0; } git config pull.rebase true || true git fetch --prune origin +refs/heads/*:refs/remotes/origin/* || true # Ensure local ext tracking origin/ext when available if git show-ref --verify --quiet refs/remotes/origin/ext; then if git rev-parse --verify ext >/dev/null 2>&1; then git switch ext git branch --set-upstream-to=origin/ext ext || true else git switch -C ext origin/ext fi else # Fallback: create ext if missing on remote if git rev-parse --verify ext >/dev/null 2>&1; then git switch ext else git switch -C ext fi fi # Rebase on remote if present to avoid non-fast-forward git fetch origin ext || true git rev-parse --verify origin/ext >/dev/null 2>&1 && git pull --rebase --autostash origin ext || true # Stage and commit if needed git add -A if ! git diff --cached --quiet; then git commit -m "auto_clea" fi # Push with upstream set; handle non-FF by one-time rebase+retry (then optional force-with-lease) if ! git push -u origin ext; then echo "[submodule] push failed, retrying after rebase" git fetch origin ext || true git pull --rebase --autostash origin ext || true if ! git push -u origin ext; then if [ "$PUSH_FORCE" = "1" ]; then echo "[submodule] rebase push failed, forcing with lease" git push --force-with-lease -u origin ext || echo "[submodule] push still failing for $name" else echo "[submodule] push still failing for $name (set PUSH_FORCE=1 to force)" fi fi fi ' echo "[root] Process root repository" git config pull.rebase true || true git fetch --prune origin +refs/heads/*:refs/remotes/origin/* || true if git show-ref --verify --quiet refs/remotes/origin/ext; then if git rev-parse --verify ext >/dev/null 2>&1; then git switch ext git branch --set-upstream-to=origin/ext ext || true else git switch -C ext origin/ext fi else if git rev-parse --verify ext >/dev/null 2>&1; then git switch ext else git switch -C ext fi fi git fetch origin ext || true git rev-parse --verify origin/ext >/dev/null 2>&1 && git pull --rebase --autostash origin ext || true git add -A if ! git diff --cached --quiet; then git commit -m "auto_clea" fi if ! git push -u origin ext; then echo "[root] push failed, retrying after rebase" git fetch origin ext || true git pull --rebase --autostash origin ext || true if ! git push -u origin ext; then if [ "$PUSH_FORCE" = "1" ]; then echo "[root] rebase push failed, forcing with lease" git push --force-with-lease -u origin ext || echo "[root] push still failing" else echo "[root] push still failing (set PUSH_FORCE=1 to force)" fi fi fi echo "[push-ext-commit] Done."