From 93f723b94b790351477e26c85b026bee147989a7 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 19 Jul 2025 22:17:50 +0300 Subject: fix: switch branches before deletion in abandoned branch script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add branch switching logic to prevent "refusing to delete current branch" error - Check if we're on the branch to be deleted and switch to main/master first - Skip deletion if no main/master branch exists to switch to - Bump version to 0.8.2 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- internal/sync/branch_analyzer.go | 36 ++++++++++++++++++++++++++++++++++++ internal/version/version.go | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/internal/sync/branch_analyzer.go b/internal/sync/branch_analyzer.go index 95827db..3038fcc 100644 --- a/internal/sync/branch_analyzer.go +++ b/internal/sync/branch_analyzer.go @@ -541,6 +541,24 @@ func (s *Syncer) GenerateDeleteScript() (string, error) { fmt.Fprintf(file, "else\n") fmt.Fprintf(file, " echo \" 🔸 Deleting branch: %s (last commit: %s)\"\n", branch.Name, branch.LastCommit.Format("2006-01-02")) + // Check if we're on the branch to be deleted, and switch to main/master if so + fmt.Fprintf(file, " # Check if we're on the branch to be deleted\n") + fmt.Fprintf(file, " current_branch=$(git branch --show-current)\n") + fmt.Fprintf(file, " if [[ \"$current_branch\" == \"%s\" ]]; then\n", branch.Name) + fmt.Fprintf(file, " echo \" Switching from %s to main/master branch before deletion...\"\n", branch.Name) + fmt.Fprintf(file, " main_branch=$(find_main_branch)\n") + fmt.Fprintf(file, " if [[ -n \"$main_branch\" ]]; then\n") + fmt.Fprintf(file, " execute_cmd git checkout \"$main_branch\"\n") + fmt.Fprintf(file, " else\n") + fmt.Fprintf(file, " echo \" ⚠️ No main/master branch found to switch to!\"\n") + fmt.Fprintf(file, " echo \" Skipping deletion of %s\"\n", branch.Name) + fmt.Fprintf(file, " fi\n") + fmt.Fprintf(file, " fi\n") + fmt.Fprintf(file, " # Skip to next branch if we couldn't switch\n") + fmt.Fprintf(file, " if [[ \"$current_branch\" == \"%s\" ]] && [[ -z \"$main_branch\" ]]; then\n", branch.Name) + fmt.Fprintf(file, " continue\n") + fmt.Fprintf(file, " fi\n") + // Delete from remotes for _, remote := range branch.RemotesWithBranch { fmt.Fprintf(file, " execute_cmd git push %s --delete \"%s\"\n", remote, branch.Name) @@ -563,6 +581,24 @@ func (s *Syncer) GenerateDeleteScript() (string, error) { fmt.Fprintf(file, "else\n") fmt.Fprintf(file, " echo \" 🔹 Deleting ignored branch: %s (last commit: %s)\"\n", branch.Name, branch.LastCommit.Format("2006-01-02")) + // Check if we're on the branch to be deleted, and switch to main/master if so + fmt.Fprintf(file, " # Check if we're on the branch to be deleted\n") + fmt.Fprintf(file, " current_branch=$(git branch --show-current)\n") + fmt.Fprintf(file, " if [[ \"$current_branch\" == \"%s\" ]]; then\n", branch.Name) + fmt.Fprintf(file, " echo \" Switching from %s to main/master branch before deletion...\"\n", branch.Name) + fmt.Fprintf(file, " main_branch=$(find_main_branch)\n") + fmt.Fprintf(file, " if [[ -n \"$main_branch\" ]]; then\n") + fmt.Fprintf(file, " execute_cmd git checkout \"$main_branch\"\n") + fmt.Fprintf(file, " else\n") + fmt.Fprintf(file, " echo \" ⚠️ No main/master branch found to switch to!\"\n") + fmt.Fprintf(file, " echo \" Skipping deletion of %s\"\n", branch.Name) + fmt.Fprintf(file, " fi\n") + fmt.Fprintf(file, " fi\n") + fmt.Fprintf(file, " # Skip to next branch if we couldn't switch\n") + fmt.Fprintf(file, " if [[ \"$current_branch\" == \"%s\" ]] && [[ -z \"$main_branch\" ]]; then\n", branch.Name) + fmt.Fprintf(file, " continue\n") + fmt.Fprintf(file, " fi\n") + // Delete from remotes for _, remote := range branch.RemotesWithBranch { fmt.Fprintf(file, " execute_cmd git push %s --delete \"%s\"\n", remote, branch.Name) diff --git a/internal/version/version.go b/internal/version/version.go index b31f9ec..a136261 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -7,7 +7,7 @@ import ( var ( // Version is the current version of gitsyncer - Version = "0.8.1" + Version = "0.8.2" // GitCommit is the git commit hash at build time GitCommit = "unknown" -- cgit v1.2.3