Repository navigation
feat(adopt): non-interactive --include ancestors for remote adoption #756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
simsinght
wants to merge
2
commits into
master
Choose a base branch
from
adopt-remote-include-ancestors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,8 +25,12 @@ var adoptFlags struct { | |
| Parent string | ||
| DryRun bool | ||
| RemoteBranchName string | ||
| Include string | ||
| } | ||
|
|
||
| // adoptIncludeValues are the valid values for the --include flag. | ||
| var adoptIncludeValues = []string{"ancestors"} | ||
|
|
||
| var adoptCmd = &cobra.Command{ | ||
| Use: "adopt", | ||
| Short: "Adopt branches that are not managed by av", | ||
|
|
@@ -42,10 +46,22 @@ the parent. | |
|
|
||
| If you want to adopt branches on the remote repository, use --remote $BRANCH_NAME to adopt branches. | ||
| The command will adopt the stack of pull requests starting from the specified branch name. | ||
|
|
||
| By default --remote opens an interactive picker. Pass --include ancestors to skip the picker and | ||
| non-interactively adopt the named branch together with its ancestor branches up to the trunk. | ||
| `), | ||
| Args: cobra.NoArgs, | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
| ctx := cmd.Context() | ||
| if adoptFlags.Include != "" { | ||
| if adoptFlags.RemoteBranchName == "" { | ||
| return errors.New("--include can only be used together with --remote") | ||
| } | ||
| if !slices.Contains(adoptIncludeValues, adoptFlags.Include) { | ||
| return errors.New("invalid value for --include; must be one of " + | ||
| strings.Join(adoptIncludeValues, ", ")) | ||
| } | ||
| } | ||
| repo, err := getRepo(ctx) | ||
| if err != nil { | ||
| return err | ||
|
|
@@ -71,10 +87,11 @@ The command will adopt the stack of pull requests starting from the specified br | |
| return err | ||
| } | ||
| return uiutils.RunBubbleTea(&remoteAdoptViewModel{ | ||
| repo: repo, | ||
| db: db, | ||
| ghClient: client, | ||
| branchName: adoptFlags.RemoteBranchName, | ||
| repo: repo, | ||
| db: db, | ||
| ghClient: client, | ||
| branchName: adoptFlags.RemoteBranchName, | ||
| nonInteractive: adoptFlags.Include != "", | ||
| }) | ||
| } | ||
| return uiutils.RunBubbleTea(&adoptViewModel{ | ||
|
|
@@ -262,10 +279,11 @@ func (vm *adoptViewModel) ExitError() error { | |
| } | ||
|
|
||
| type remoteAdoptViewModel struct { | ||
| repo *git.Repo | ||
| db meta.DB | ||
| ghClient *gh.Client | ||
| branchName string | ||
| repo *git.Repo | ||
| db meta.DB | ||
| ghClient *gh.Client | ||
| branchName string | ||
| nonInteractive bool | ||
|
|
||
| uiutils.BaseStackedView | ||
| } | ||
|
|
@@ -311,6 +329,26 @@ func (vm *remoteAdoptViewModel) initTreeSelector(prs []actions.RemotePRInfo) tea | |
| tea.Quit, | ||
| ) | ||
| } | ||
| if vm.nonInteractive { | ||
| // --include ancestors: adopt the named branch and its ancestors without | ||
| // prompting. adoptionTargets already holds that chain (the open, not-yet | ||
| // adopted PRs walked up from the named branch to the trunk). | ||
| if adoptFlags.DryRun { | ||
| parents := make(map[string]string, len(prs)) | ||
| for _, prInfo := range prs { | ||
| parents[prInfo.Name] = prInfo.Parent.Name | ||
| } | ||
| lines := []string{"Would adopt:"} | ||
| for _, target := range adoptionTargets { | ||
| lines = append(lines, fmt.Sprintf(" %s (parent: %s)", target.Short(), parents[target.Short()])) | ||
| } | ||
| return tea.Batch( | ||
| vm.AddView(uiutils.SimpleMessageView{Message: colors.SuccessStyle.Render(strings.Join(lines, "\n"))}), | ||
| tea.Quit, | ||
| ) | ||
| } | ||
| return vm.initGitFetch(prs, adoptionTargets) | ||
| } | ||
| var lastNode *stackutils.StackTreeNode | ||
| for _, prInfo := range prs { | ||
| node := &stackutils.StackTreeNode{ | ||
|
|
@@ -360,10 +398,13 @@ func (vm *remoteAdoptViewModel) initGitFetch(prs []actions.RemotePRInfo, chosenT | |
| tea.Quit, | ||
| ) | ||
| } | ||
| remote := vm.repo.GetRemoteName() | ||
| refspecs := []string{} | ||
| for _, target := range chosenTargets { | ||
| // Directly clone as a local branch. | ||
| // Clone as a local branch, and create the remote-tracking ref so we can | ||
| // set upstream tracking after adoption. | ||
| refspecs = append(refspecs, fmt.Sprintf("refs/heads/%s:refs/heads/%s", target.Short(), target.Short())) | ||
| refspecs = append(refspecs, fmt.Sprintf("refs/heads/%s:refs/remotes/%s/%s", target.Short(), remote, target.Short())) | ||
| } | ||
| return vm.AddView( | ||
| actions.NewGitFetchModel(vm.repo, refspecs, func() tea.Cmd { | ||
|
|
@@ -405,9 +446,11 @@ func (vm *remoteAdoptViewModel) initAdoption(prs []actions.RemotePRInfo, chosenT | |
| vm.db, | ||
| branches, | ||
| func() tea.Cmd { | ||
| remote := vm.repo.GetRemoteName() | ||
| hasChild := make(map[string]bool) | ||
| for _, ab := range branches { | ||
| hasChild[ab.Parent.Name] = true | ||
| _ = vm.repo.BranchSetUpstream(context.Background(), ab.Name, remote) | ||
| } | ||
| for _, ab := range branches { | ||
| if !hasChild[ab.Name] { | ||
|
|
@@ -442,6 +485,10 @@ func init() { | |
| &adoptFlags.RemoteBranchName, "remote", "", | ||
| "adopt branches from remote pull requests, starting from the specified branch", | ||
| ) | ||
| adoptCmd.Flags().StringVar( | ||
| &adoptFlags.Include, "include", "", | ||
| "with --remote, non-interactively adopt the named branch and related branches\n(ancestors)", | ||
| ) | ||
|
Comment on lines
+488
to
+491
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To improve the CLI user experience, we can register a shell completion function for the new adoptCmd.Flags().StringVar(
&adoptFlags.Include, "include", "",
"with --remote, non-interactively adopt the named branch and related branches\n(ancestors)",
)
_ = adoptCmd.RegisterFlagCompletionFunc(
"include",
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return adoptIncludeValues, cobra.ShellCompDirectiveNoFileComp
},
) |
||
|
|
||
| _ = adoptCmd.RegisterFlagCompletionFunc( | ||
| "parent", | ||
|
|
@@ -450,4 +497,10 @@ func init() { | |
| return branches, cobra.ShellCompDirectiveNoFileComp | ||
| }, | ||
| ) | ||
| _ = adoptCmd.RegisterFlagCompletionFunc( | ||
| "include", | ||
| func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return adoptIncludeValues, cobra.ShellCompDirectiveNoFileComp | ||
| }, | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
e2e_tests/testdata/script/adopt_remote_include_ancestors.txtar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Test non-interactive remote adoption with --include ancestors. | ||
| # | ||
| # Remote stack: main -> feat-a (PR1) -> feat-b (PR2) | ||
| # | ||
| # `av adopt --remote feat-b --include ancestors` should adopt feat-b and its | ||
| # ancestor feat-a without opening the interactive picker. Here we use --dry-run | ||
| # to assert the adoption plan. | ||
|
|
||
| # --include requires --remote. | ||
| ! exec av adopt --include ancestors | ||
| stderr '--include can only be used together with --remote' | ||
|
|
||
| # --include only accepts known values. | ||
| ! exec av adopt --remote feat-b --include cousins | ||
| stderr 'invalid value for --include' | ||
|
|
||
| # Model the remote PR stack: each PR body carries av stack metadata so the | ||
| # walk can climb from feat-b up to the trunk. | ||
| mock-pull feat-a 1 OPEN base=main body=$WORK/feat-a-body.txt | ||
| mock-pull feat-b 2 OPEN base=feat-a body=$WORK/feat-b-body.txt | ||
|
|
||
| exec av adopt --remote feat-b --include ancestors --dry-run | ||
| stdout 'Would adopt:' | ||
| stdout 'feat-b \(parent: feat-a\)' | ||
| stdout 'feat-a \(parent: main\)' | ||
|
|
||
| -- feat-a-body.txt -- | ||
| <!-- av pr metadata | ||
| ``` | ||
| {"parent":"main","trunk":"main"} | ||
| ``` | ||
| --> | ||
| -- feat-b-body.txt -- | ||
| <!-- av pr metadata | ||
| ``` | ||
| {"parent":"feat-a","parentPull":1,"trunk":"main"} | ||
| ``` | ||
| --> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: aviator-co/av
Length of output: 665
🏁 Script executed:
Repository: aviator-co/av
Length of output: 3934
🏁 Script executed:
Repository: aviator-co/av
Length of output: 2620
🏁 Script executed:
Repository: aviator-co/av
Length of output: 982
🏁 Script executed:
Repository: aviator-co/av
Length of output: 9163
Dry-run should use the same reparent walk as adoption.
prInfo.Parent.Namecan still point to a merged/closed PR, soWould adoptmay show a parent that differs from the branch actually adopted afterinitAdoptionwalks up to an open ancestor or trunk.🤖 Prompt for AI Agents