From b992749ad8f538540d6f89c50ee79ee478e2c770 Mon Sep 17 00:00:00 2001 From: Masaya Suzuki Date: Wed, 5 Nov 2025 12:55:35 +0900 Subject: [PATCH] Allow adopting branches on top of local branches (#625) There's a case where a branch already has been adopted locally, and they want to adopt a new branch on top of that local branch. This change allows that action. While we are here, after a successful adoption, we check out the leaf branch so that the user is in a good state to continue working. --- cmd/av/adopt.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/cmd/av/adopt.go b/cmd/av/adopt.go index 47c7bae..bc50304 100644 --- a/cmd/av/adopt.go +++ b/cmd/av/adopt.go @@ -292,7 +292,10 @@ func (vm *remoteAdoptViewModel) initTreeSelector(prs []actions.RemotePRInfo) tea for _, prInfo := range prs { branch := plumbing.NewBranchReferenceName(prInfo.Name) if prInfo.PullRequest.State == githubv4.PullRequestStateOpen { - adoptionTargets = append(adoptionTargets, branch) + // Check if the branch is already adopted. + if _, ok := vm.db.ReadTx().Branch(prInfo.Name); !ok { + adoptionTargets = append(adoptionTargets, branch) + } } infos[branch] = actions.BranchTreeInfo{ TitleLine: prInfo.Title, @@ -399,7 +402,20 @@ func (vm *remoteAdoptViewModel) initAdoption(prs []actions.RemotePRInfo, chosenT actions.NewAdoptBranchesModel( vm.db, branches, - func() tea.Cmd { return tea.Quit }, + func() tea.Cmd { + hasChild := make(map[string]bool) + for _, ab := range branches { + hasChild[ab.Parent.Name] = true + } + for _, ab := range branches { + if !hasChild[ab.Name] { + // Check out the leaf branch. This can fail if the workspace is dirty. In that case, just quietly exit. + _, _ = vm.repo.Git(context.Background(), "switch", ab.Name, "--quiet") + break + } + } + return tea.Quit + }, ), ) }