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.
This commit is contained in:
Masaya Suzuki
2025-11-05 12:55:35 +09:00
committed by GitHub
parent 89c8eccebb
commit b992749ad8

View File

@@ -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
},
),
)
}