summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-07-19 22:17:50 +0300
committerPaul Buetow <paul@buetow.org>2025-07-19 22:17:50 +0300
commit93f723b94b790351477e26c85b026bee147989a7 (patch)
tree27a77e57324c3991e32d5bb5890970c4554dd26f
parent5ca3c39da7854a753d8535465ec42bebfa3fcf8e (diff)
fix: switch branches before deletion in abandoned branch scriptv0.8.2
- 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 <noreply@anthropic.com>
-rw-r--r--internal/sync/branch_analyzer.go36
-rw-r--r--internal/version/version.go2
2 files changed, 37 insertions, 1 deletions
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"