diff --git a/repo_watcher.sh b/repo_watcher.sh index 64bd5cf..5c7fd52 100644 --- a/repo_watcher.sh +++ b/repo_watcher.sh @@ -18,17 +18,30 @@ process_repo() { fi cd "$REPO_DIR" || exit - # Detect the default branch dynamically - DEFAULT_BRANCH=$(git remote show origin | awk '/HEAD branch/ {print $NF}') - + # Fetch the latest branch information + git fetch origin > /dev/null 2>&1 + + # List the available branches at the remote + AVAILABLE_BRANCHES=$(git ls-remote --heads origin | awk '{print $2}' | sed 's/refs\/heads\///') + + # Detect the default branch dynamically (check for both 'main' and 'master') + if echo "$AVAILABLE_BRANCHES" | grep -q "^main$"; then + DEFAULT_BRANCH="main" + elif echo "$AVAILABLE_BRANCHES" | grep -q "^master$"; then + DEFAULT_BRANCH="master" + else + echo "Error: Neither 'main' nor 'master' branch exists in $FULL_REPO_URL" + exit 1 + fi + # Check if the branch exists locally, and if not, create and set upstream if ! git show-ref --verify --quiet refs/heads/$DEFAULT_BRANCH; then - git checkout -b $DEFAULT_BRANCH origin/$DEFAULT_BRANCH + git checkout -b $DEFAULT_BRANCH origin/$DEFAULT_BRANCH || exit 1 fi # Check if an upstream is configured; if not, set it if ! git rev-parse --abbrev-ref --symbolic-full-name @{u} > /dev/null 2>&1; then - git branch --set-upstream-to=origin/$DEFAULT_BRANCH $DEFAULT_BRANCH + git branch --set-upstream-to=origin/$DEFAULT_BRANCH $DEFAULT_BRANCH || exit 1 fi # Fetch the latest changes from the correct branch