#!/bin/bash # Merge a feature branch with rebase, close PR, and cleanup # Usage: gitea-merge.sh [branch-name] # If no branch specified, uses current branch set -euo pipefail GITEA_URL="https://gitea-http.taildb3494.ts.net" REPO="will/claude-code" TOKEN_FILE="$HOME/.config/gitea-token" if [[ ! -f "$TOKEN_FILE" ]]; then echo "Error: Gitea token not found at $TOKEN_FILE" >&2 exit 1 fi TOKEN=$(cat "$TOKEN_FILE") # Get branch to merge BRANCH="${1:-$(git rev-parse --abbrev-ref HEAD)}" if [[ "$BRANCH" == "main" ]]; then echo "Error: Already on main, specify a feature branch" >&2 exit 1 fi echo "Merging branch: $BRANCH" # Find PR number for this branch PR_NUMBER=$(curl -s -H "Authorization: token $TOKEN" \ "$GITEA_URL/api/v1/repos/$REPO/pulls?state=open" | \ python3 -c " import sys, json prs = json.load(sys.stdin) for pr in prs: if pr.get('head', {}).get('ref') == '$BRANCH': print(pr['number']) break " 2>/dev/null || echo "") # Stash any uncommitted changes STASHED=false if ! git diff --quiet || ! git diff --cached --quiet; then echo "Stashing uncommitted changes..." git stash STASHED=true fi # Checkout main and rebase echo "Checking out main..." git checkout main git pull origin main echo "Rebasing $BRANCH onto main..." git rebase "$BRANCH" echo "Pushing to origin..." git push origin main # Delete local branch echo "Deleting local branch..." git branch -d "$BRANCH" 2>/dev/null || true # Delete remote branch echo "Deleting remote branch..." git push origin --delete "$BRANCH" 2>/dev/null || true # Close PR if found if [[ -n "$PR_NUMBER" ]]; then echo "Closing PR #$PR_NUMBER..." curl -s -X PATCH \ -H "Authorization: token $TOKEN" \ -H "Content-Type: application/json" \ -d '{"state":"closed"}' \ "$GITEA_URL/api/v1/repos/$REPO/pulls/$PR_NUMBER" >/dev/null echo "PR #$PR_NUMBER closed" else echo "No open PR found for branch $BRANCH" fi # Restore stashed changes if [[ "$STASHED" == "true" ]]; then echo "Restoring stashed changes..." git stash pop fi echo "" echo "Done! Branch $BRANCH merged to main"