From 417942f674ca2cbfedcfd40d65dc8022c594b8ba Mon Sep 17 00:00:00 2001 From: Muhammad Ibrahim Date: Sat, 20 Sep 2025 13:26:51 +0100 Subject: [PATCH] Add automatic branch switching for updates - Added branch detection and switching logic to both scripts - manage-patchmon-dev.sh: automatically switches instances to dev branch - manage-patchmon.sh: automatically switches instances to main branch - Handles local changes by stashing before branch switch - Creates local branch from origin if it doesn't exist locally - Fixes issue where main branch instances couldn't be updated with dev script --- manage-patchmon-dev.sh | 25 +++++++++++++++++++++++++ manage-patchmon.sh | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/manage-patchmon-dev.sh b/manage-patchmon-dev.sh index 6a9a0e2..53e616d 100755 --- a/manage-patchmon-dev.sh +++ b/manage-patchmon-dev.sh @@ -2335,6 +2335,31 @@ update_single_instance() { cd "$app_dir" + # Check current branch and switch to dev if needed + local current_branch=$(git branch --show-current 2>/dev/null || echo "unknown") + if [ "$current_branch" != "dev" ]; then + print_info "Current branch: $current_branch, switching to dev branch..." + + # Stash any local changes before switching + if git status --porcelain | grep -q .; then + print_info "Stashing local changes before branch switch..." + git stash push -m "Auto-stash before switching to dev branch $(date)" + fi + + # Switch to dev branch + if git checkout dev 2>/dev/null; then + print_status "Successfully switched to dev branch" + else + # If dev branch doesn't exist locally, create it from origin/dev + print_info "Creating local dev branch from origin/dev..." + git fetch origin dev + git checkout -b dev origin/dev + print_status "Created and switched to dev branch" + fi + else + print_info "Already on dev branch" + fi + # Backup database first print_info "Creating database backup..." diff --git a/manage-patchmon.sh b/manage-patchmon.sh index d9a24c0..0b44f3b 100755 --- a/manage-patchmon.sh +++ b/manage-patchmon.sh @@ -2330,6 +2330,31 @@ update_single_instance() { cd "$app_dir" + # Check current branch and switch to main if needed (for main script) + local current_branch=$(git branch --show-current 2>/dev/null || echo "unknown") + if [ "$current_branch" != "main" ]; then + print_info "Current branch: $current_branch, switching to main branch..." + + # Stash any local changes before switching + if git status --porcelain | grep -q .; then + print_info "Stashing local changes before branch switch..." + git stash push -m "Auto-stash before switching to main branch $(date)" + fi + + # Switch to main branch + if git checkout main 2>/dev/null; then + print_status "Successfully switched to main branch" + else + # If main branch doesn't exist locally, create it from origin/main + print_info "Creating local main branch from origin/main..." + git fetch origin main + git checkout -b main origin/main + print_status "Created and switched to main branch" + fi + else + print_info "Already on main branch" + fi + # Backup database first print_info "Creating database backup..."