Add support for git flags in gh repo fork

This commit is contained in:
Cristian Dominguez 2020-10-23 18:45:20 -03:00
parent c8b7dade30
commit 69b64507ea
2 changed files with 22 additions and 4 deletions

View file

@ -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 [<repository>]",
Args: cobra.MaximumNArgs(1),
Use: "fork [<repository>] [-- <gitflags>...]",
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)
}

View file

@ -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) {