diff --git a/deploy.prod.sh b/deploy.prod.sh index d721b2d..482f426 100755 --- a/deploy.prod.sh +++ b/deploy.prod.sh @@ -9,12 +9,12 @@ set -Eeuo pipefail PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$PROJECT_ROOT" -BRANCH="${1:-main}" +BRANCH_INPUT="${1:-}" REMOTE="${2:-origin}" echo "==> [prod] Project: $PROJECT_ROOT" echo "==> [prod] Remote: $REMOTE" -echo "==> [prod] Branch: $BRANCH" +echo "==> [prod] Branch: ${BRANCH_INPUT:-}" if [[ ! -d ".git" ]]; then echo "Error: current directory is not a git repository." @@ -29,6 +29,38 @@ fi echo "==> [prod] Fetching latest code" git fetch "$REMOTE" --prune +resolve_default_branch() { + local remote="$1" + local head_ref + head_ref="$(git symbolic-ref -q --short "refs/remotes/$remote/HEAD" 2>/dev/null || true)" + if [[ -n "$head_ref" ]]; then + echo "${head_ref#${remote}/}" + return 0 + fi + head_ref="$(git remote show "$remote" 2>/dev/null | awk '/HEAD branch/ {print $NF; exit}')" + if [[ -n "$head_ref" ]]; then + echo "$head_ref" + return 0 + fi + return 1 +} + +if [[ -n "$BRANCH_INPUT" ]]; then + BRANCH="$BRANCH_INPUT" +else + BRANCH="$(resolve_default_branch "$REMOTE" || true)" + if [[ -z "$BRANCH" ]]; then + BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)" + fi + if [[ -z "$BRANCH" || "$BRANCH" == "HEAD" ]]; then + echo "Error: could not determine deploy branch. Please pass one explicitly:" + echo " sh deploy.prod.sh [remote]" + exit 1 + fi +fi + +echo "==> [prod] Using branch: $BRANCH" + if ! git show-ref --verify --quiet "refs/remotes/$REMOTE/$BRANCH"; then echo "Error: remote branch '$REMOTE/$BRANCH' not found." exit 1