From 69b64507ea6fddf09ddd4d3ec76a90b3a221d31a Mon Sep 17 00:00:00 2001 From: Cristian Dominguez Date: Fri, 23 Oct 2020 18:45:20 -0300 Subject: [PATCH] Add support for git flags in gh repo fork --- pkg/cmd/repo/fork/fork.go | 19 +++++++++++++++---- pkg/cmd/repo/fork/fork_test.go | 7 +++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/repo/fork/fork.go b/pkg/cmd/repo/fork/fork.go index b22e2501c..d24bd486a 100644 --- a/pkg/cmd/repo/fork/fork.go +++ b/pkg/cmd/repo/fork/fork.go @@ -18,6 +18,7 @@ import ( "github.com/cli/cli/pkg/prompt" "github.com/cli/cli/utils" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) type ForkOptions struct { @@ -27,6 +28,7 @@ type ForkOptions struct { BaseRepo func() (ghrepo.Interface, error) Remotes func() (context.Remotes, error) + GitArgs []string Repository string Clone bool Remote bool @@ -48,16 +50,19 @@ func NewCmdFork(f *cmdutil.Factory, runF func(*ForkOptions) error) *cobra.Comman } cmd := &cobra.Command{ - Use: "fork []", - Args: cobra.MaximumNArgs(1), + Use: "fork [] [-- ...]", + Args: cobra.MaximumNArgs(2), Short: "Create a fork of a repository", Long: `Create a fork of a repository. -With no argument, creates a fork of the current repository. Otherwise, forks the specified repository.`, +With no argument, creates a fork of the current repository. Otherwise, forks the specified repository. + +If '--clone' is specified, you can pass aditional 'git clone' flags by listing them after '--'.`, RunE: func(cmd *cobra.Command, args []string) error { promptOk := opts.IO.CanPrompt() if len(args) > 0 { opts.Repository = args[0] + opts.GitArgs = args[1:] } if promptOk && !cmd.Flags().Changed("clone") { @@ -74,6 +79,12 @@ With no argument, creates a fork of the current repository. Otherwise, forks the return forkRun(opts) }, } + cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { + if err == pflag.ErrHelp { + return err + } + return &cmdutil.FlagError{Err: fmt.Errorf("%w\nSeparate git clone flags with '--'.", err)} + }) cmd.Flags().BoolVar(&opts.Clone, "clone", false, "Clone the fork {true|false}") cmd.Flags().BoolVar(&opts.Remote, "remote", false, "Add remote for fork {true|false}") @@ -248,7 +259,7 @@ func forkRun(opts *ForkOptions) error { } if cloneDesired { forkedRepoURL := ghrepo.FormatRemoteURL(forkedRepo, protocol) - cloneDir, err := git.RunClone(forkedRepoURL, []string{}) + cloneDir, err := git.RunClone(forkedRepoURL, opts.GitArgs) if err != nil { return fmt.Errorf("failed to clone fork: %w", err) } diff --git a/pkg/cmd/repo/fork/fork_test.go b/pkg/cmd/repo/fork/fork_test.go index 0a92b77e5..baf6cb995 100644 --- a/pkg/cmd/repo/fork/fork_test.go +++ b/pkg/cmd/repo/fork/fork_test.go @@ -465,6 +465,13 @@ func TestRepoFork_in_parent_survey_no(t *testing.T) { reg.Verify(t) } +func Test_RepoFork_flagError(t *testing.T) { + _, err := runCommand(nil, nil, true, "--depth 1 OWNER/REPO") + if err == nil || err.Error() != "unknown flag: --depth\nSeparate git clone flags with '--'." { + t.Errorf("unexpected error %v", err) + } +} + func stubSpinner() { // not bothering with teardown since we never want spinners when doing tests utils.StartSpinner = func(_ *spinner.Spinner) {