From a061d7781cef93aa1f13a54fde89ccd8e719c979 Mon Sep 17 00:00:00 2001 From: him0 Date: Thu, 17 Jul 2025 02:00:46 +0900 Subject: [PATCH] Fix current branch tracking after deletion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a feature branch is deleted during sync, the currentBranch variable was not being updated after checkout. This caused the new current branch (default branch) to be processed incorrectly in subsequent iterations, leading to staging of differences between the deleted feature branch and the default branch. This fix updates currentBranch to track the actual current branch after checkout, matching hub sync behavior exactly. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- main.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 41c2115..a5f7fb4 100644 --- a/main.go +++ b/main.go @@ -88,7 +88,7 @@ func main() { fullDefaultBranch := fmt.Sprintf("refs/remotes/%s/%s", remote.Name, defaultBranch) for _, branch := range branches { - processBranch(branch, remote, branchToRemote, currentBranch, defaultBranch, fullDefaultBranch) + processBranch(branch, remote, branchToRemote, ¤tBranch, defaultBranch, fullDefaultBranch) } } @@ -227,7 +227,7 @@ func getLocalBranches() ([]string, error) { return branches, nil } -func processBranch(branch string, remote *Remote, branchToRemote map[string]string, currentBranch, defaultBranch, fullDefaultBranch string) { +func processBranch(branch string, remote *Remote, branchToRemote map[string]string, currentBranch *string, defaultBranch, fullDefaultBranch string) { fullBranch := fmt.Sprintf("refs/heads/%s", branch) remoteBranch := fmt.Sprintf("refs/remotes/%s/%s", remote.Name, branch) gone := false @@ -255,7 +255,7 @@ func processBranch(branch string, remote *Remote, branchToRemote map[string]stri } else if ahead == 0 && behind > 0 { // Local branch is behind, can fast-forward oldCommit := getCommitSHA(fullBranch) - if branch == currentBranch { + if branch == *currentBranch { runGitSilent("merge", "--ff-only", "--quiet", remoteBranch) } else { runGitSilent("update-ref", fullBranch, remoteBranch) @@ -272,8 +272,9 @@ func processBranch(branch string, remote *Remote, branchToRemote map[string]stri if ahead == 0 && behind >= 0 { // Branch is ancestor of default branch, safe to delete oldCommit := getCommitSHA(fullBranch) - if branch == currentBranch { + if branch == *currentBranch { runGitSilent("checkout", "--quiet", defaultBranch) + *currentBranch = defaultBranch } runGitSilent("branch", "-D", branch) fmt.Printf("%sDeleted branch %s%s%s (was %s).\n", red, lightRed, branch, resetColor, oldCommit[:7])