From 69b64507ea6fddf09ddd4d3ec76a90b3a221d31a Mon Sep 17 00:00:00 2001 From: Cristian Dominguez Date: Fri, 23 Oct 2020 18:45:20 -0300 Subject: [PATCH 1/5] 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) { From c6938941d53847a2baa2fb95da8cca24f3720dc5 Mon Sep 17 00:00:00 2001 From: Cristian Dominguez Date: Fri, 23 Oct 2020 22:32:26 -0300 Subject: [PATCH 2/5] Fix typo --- pkg/cmd/repo/fork/fork.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/repo/fork/fork.go b/pkg/cmd/repo/fork/fork.go index d24bd486a..0eb5569c8 100644 --- a/pkg/cmd/repo/fork/fork.go +++ b/pkg/cmd/repo/fork/fork.go @@ -57,7 +57,7 @@ func NewCmdFork(f *cmdutil.Factory, runF func(*ForkOptions) error) *cobra.Comman 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 '--'.`, +If '--clone' is specified, you can pass additional 'git clone' flags by listing them after '--'.`, RunE: func(cmd *cobra.Command, args []string) error { promptOk := opts.IO.CanPrompt() if len(args) > 0 { From 97aa0b2f7daf7abaf445970aa16e89b1532a42c7 Mon Sep 17 00:00:00 2001 From: Cristian Dominguez Date: Wed, 4 Nov 2020 12:10:35 -0300 Subject: [PATCH 3/5] Warn when passing git flags without repository arg --- pkg/cmd/repo/fork/fork.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/repo/fork/fork.go b/pkg/cmd/repo/fork/fork.go index 0eb5569c8..ce99f4a6b 100644 --- a/pkg/cmd/repo/fork/fork.go +++ b/pkg/cmd/repo/fork/fork.go @@ -50,8 +50,13 @@ func NewCmdFork(f *cmdutil.Factory, runF func(*ForkOptions) error) *cobra.Comman } cmd := &cobra.Command{ - Use: "fork [] [-- ...]", - Args: cobra.MaximumNArgs(2), + Use: "fork [] [-- ...]", + Args: func(cmd *cobra.Command, args []string) error { + if cmd.ArgsLenAtDash() == 0 && len(args[1:]) > 0 { + return cmdutil.FlagError{Err: fmt.Errorf("repository argument required when passing 'git clone' flags")} + } + return nil + }, Short: "Create a fork of a repository", Long: `Create a fork of a repository. From 07bd6472112f965352a9242c4260bb8618187614 Mon Sep 17 00:00:00 2001 From: Cristian Dominguez Date: Wed, 4 Nov 2020 12:14:02 -0300 Subject: [PATCH 4/5] Update repo fork help msg --- pkg/cmd/repo/fork/fork.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/repo/fork/fork.go b/pkg/cmd/repo/fork/fork.go index ce99f4a6b..d5f9c359b 100644 --- a/pkg/cmd/repo/fork/fork.go +++ b/pkg/cmd/repo/fork/fork.go @@ -62,7 +62,7 @@ func NewCmdFork(f *cmdutil.Factory, runF func(*ForkOptions) error) *cobra.Comman With no argument, creates a fork of the current repository. Otherwise, forks the specified repository. -If '--clone' is specified, you can pass additional 'git clone' flags by listing them after '--'.`, +Additional 'git clone' flags can be passed in by listing them after '--'.`, RunE: func(cmd *cobra.Command, args []string) error { promptOk := opts.IO.CanPrompt() if len(args) > 0 { From 48c89076f667b943c4e233cbb0746b17eaf97d6a Mon Sep 17 00:00:00 2001 From: vilmibm Date: Thu, 21 Jan 2021 12:04:19 -0800 Subject: [PATCH 5/5] add positive case test --- pkg/cmd/repo/fork/fork_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pkg/cmd/repo/fork/fork_test.go b/pkg/cmd/repo/fork/fork_test.go index bec0b782d..9772bcb0d 100644 --- a/pkg/cmd/repo/fork/fork_test.go +++ b/pkg/cmd/repo/fork/fork_test.go @@ -495,6 +495,28 @@ func TestRepoFork_in_parent_survey_no(t *testing.T) { reg.Verify(t) } +func Test_RepoFork_gitFlags(t *testing.T) { + defer stubSince(2 * time.Second)() + reg := &httpmock.Registry{} + defer reg.StubWithFixturePath(200, "./forkResult.json")() + httpClient := &http.Client{Transport: reg} + + cs, cmdTeardown := run.Stub() + defer cmdTeardown(t) + + cs.Register(`git clone --depth 1 https://github.com/someone/REPO.git`, 0, "") + cs.Register(`git -C REPO remote add -f upstream https://github.com/OWNER/REPO.git`, 0, "") + + output, err := runCommand(httpClient, nil, false, "--clone OWNER/REPO -- --depth 1") + if err != nil { + t.Errorf("error running command `repo fork`: %v", err) + } + + assert.Equal(t, "", output.String()) + assert.Equal(t, output.Stderr(), "") + 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 '--'." {