From 8cf8a16e6a48dc8378f7b30cdcb12dfca7950f5f Mon Sep 17 00:00:00 2001 From: Chris Westra Date: Mon, 12 Sep 2022 17:20:24 -0400 Subject: [PATCH] WIP add --checkout option --- pkg/cmd/issue/develop/develop.go | 62 +++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/issue/develop/develop.go b/pkg/cmd/issue/develop/develop.go index a9073ffa0..49b5c1874 100644 --- a/pkg/cmd/issue/develop/develop.go +++ b/pkg/cmd/issue/develop/develop.go @@ -6,8 +6,11 @@ import ( "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" + "github.com/cli/cli/v2/context" + "github.com/cli/cli/v2/git" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/internal/run" "github.com/cli/cli/v2/pkg/cmd/issue/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" @@ -19,6 +22,7 @@ type DevelopOptions struct { Config func() (config.Config, error) IO *iostreams.IOStreams BaseRepo func() (ghrepo.Interface, error) + Remotes func() (context.Remotes, error) IssueRepo string IssueSelector string @@ -33,15 +37,17 @@ func NewCmdDevelop(f *cmdutil.Factory, runF func(*DevelopOptions) error) *cobra. HttpClient: f.HttpClient, Config: f.Config, BaseRepo: f.BaseRepo, + Remotes: f.Remotes, } cmd := &cobra.Command{ - Use: "develop", - Short: "Manage branches for an issue", + Use: "develop [flags] { | }", + Short: "Manage linked branches for an issue", Example: heredoc.Doc(` $ gh issue develop --list 123 # list branches for issue 123 $ gh issue develop --issue-repo "github/cli" 123 list branches for issue 123 in repo "github/cli" $ gh issue develop 123 --name "my-branch" --head main + $ gh issue develop 123 --checkout # checkout the branch for issue 123 after creating it `), Args: cmdutil.ExactArgs(1, "issue number is required"), RunE: func(cmd *cobra.Command, args []string) error { @@ -53,9 +59,10 @@ func NewCmdDevelop(f *cmdutil.Factory, runF func(*DevelopOptions) error) *cobra. }, } fl := cmd.Flags() - fl.StringVarP(&opts.Name, "name", "n", "", "Name of the branch to create") fl.StringVarP(&opts.BaseBranch, "base-branch", "b", "", "Name of the base branch") - fl.StringVarP(&opts.IssueRepo, "issue-repo", "i", "", "Name of the issue's repository") + fl.BoolVarP(&opts.Checkout, "checkout", "c", false, "Checkout the branch after creating it") + fl.StringVarP(&opts.IssueRepo, "issue-repo", "i", "", "Name or URL of the issue's repository") + fl.StringVarP(&opts.Name, "name", "n", "", "Name of the branch to create") return cmd } @@ -69,6 +76,7 @@ func developRun(opts *DevelopOptions) (err error) { if err != nil { return err } + opts.IO.StartProgressIndicator() repo, err := api.GitHubRepo(apiClient, baseRepo) if err != nil { return err @@ -98,11 +106,57 @@ func developRun(opts *DevelopOptions) (err error) { } ref, err := api.CreateBranchIssueReference(apiClient, repo, params) + opts.IO.StopProgressIndicator() if ref != nil { fmt.Fprintf(opts.IO.Out, "Created %s\n", ref.BranchName) + + if opts.Checkout { + return checkoutBranch(opts, baseRepo, ref.BranchName) + } } if err != nil { return err } return } + +func checkoutBranch(opts *DevelopOptions, baseRepo ghrepo.Interface, checkoutBranch string) (err error) { + + remotes, err := opts.Remotes() + if err != nil { + return err + } + + baseRemote, err := remotes.FindByRepo(baseRepo.RepoOwner(), baseRepo.RepoName()) + if err != nil { + return err + } + + if git.HasLocalBranch(checkoutBranch) { + if err := git.CheckoutBranch(checkoutBranch); err != nil { + return err + } + } else { + gitFetch, err := git.GitCommand("fetch", "origin", fmt.Sprintf("+refs/heads/%[1]s:refs/remotes/origin/%[1]s", checkoutBranch)) + + if err != nil { + return err + } + + gitFetch.Stdout = opts.IO.Out + gitFetch.Stderr = opts.IO.ErrOut + err = run.PrepareCmd(gitFetch).Run() + if err != nil { + return err + } + if err := git.CheckoutNewBranch(baseRemote.Name, checkoutBranch); err != nil { + return err + } + } + + if err := git.Pull(baseRemote.Name, checkoutBranch); err != nil { + _, _ = fmt.Fprintf(opts.IO.ErrOut, "%s warning: not possible to fast-forward to: %q\n", opts.IO.ColorScheme().WarningIcon(), checkoutBranch) + } + + return nil +}