Skip to content

Commit 69b6450

Browse files
committed
Add support for git flags in gh repo fork
1 parent c8b7dad commit 69b6450

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

pkg/cmd/repo/fork/fork.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/cli/cli/pkg/prompt"
1919
"github.com/cli/cli/utils"
2020
"github.com/spf13/cobra"
21+
"github.com/spf13/pflag"
2122
)
2223

2324
type ForkOptions struct {
@@ -27,6 +28,7 @@ type ForkOptions struct {
2728
BaseRepo func() (ghrepo.Interface, error)
2829
Remotes func() (context.Remotes, error)
2930

31+
GitArgs []string
3032
Repository string
3133
Clone bool
3234
Remote bool
@@ -48,16 +50,19 @@ func NewCmdFork(f *cmdutil.Factory, runF func(*ForkOptions) error) *cobra.Comman
4850
}
4951

5052
cmd := &cobra.Command{
51-
Use: "fork [<repository>]",
52-
Args: cobra.MaximumNArgs(1),
53+
Use: "fork [<repository>] [-- <gitflags>...]",
54+
Args: cobra.MaximumNArgs(2),
5355
Short: "Create a fork of a repository",
5456
Long: `Create a fork of a repository.
5557
56-
With no argument, creates a fork of the current repository. Otherwise, forks the specified repository.`,
58+
With no argument, creates a fork of the current repository. Otherwise, forks the specified repository.
59+
60+
If '--clone' is specified, you can pass aditional 'git clone' flags by listing them after '--'.`,
5761
RunE: func(cmd *cobra.Command, args []string) error {
5862
promptOk := opts.IO.CanPrompt()
5963
if len(args) > 0 {
6064
opts.Repository = args[0]
65+
opts.GitArgs = args[1:]
6166
}
6267

6368
if promptOk && !cmd.Flags().Changed("clone") {
@@ -74,6 +79,12 @@ With no argument, creates a fork of the current repository. Otherwise, forks the
7479
return forkRun(opts)
7580
},
7681
}
82+
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
83+
if err == pflag.ErrHelp {
84+
return err
85+
}
86+
return &cmdutil.FlagError{Err: fmt.Errorf("%w\nSeparate git clone flags with '--'.", err)}
87+
})
7788

7889
cmd.Flags().BoolVar(&opts.Clone, "clone", false, "Clone the fork {true|false}")
7990
cmd.Flags().BoolVar(&opts.Remote, "remote", false, "Add remote for fork {true|false}")
@@ -248,7 +259,7 @@ func forkRun(opts *ForkOptions) error {
248259
}
249260
if cloneDesired {
250261
forkedRepoURL := ghrepo.FormatRemoteURL(forkedRepo, protocol)
251-
cloneDir, err := git.RunClone(forkedRepoURL, []string{})
262+
cloneDir, err := git.RunClone(forkedRepoURL, opts.GitArgs)
252263
if err != nil {
253264
return fmt.Errorf("failed to clone fork: %w", err)
254265
}

pkg/cmd/repo/fork/fork_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,13 @@ func TestRepoFork_in_parent_survey_no(t *testing.T) {
465465
reg.Verify(t)
466466
}
467467

468+
func Test_RepoFork_flagError(t *testing.T) {
469+
_, err := runCommand(nil, nil, true, "--depth 1 OWNER/REPO")
470+
if err == nil || err.Error() != "unknown flag: --depth\nSeparate git clone flags with '--'." {
471+
t.Errorf("unexpected error %v", err)
472+
}
473+
}
474+
468475
func stubSpinner() {
469476
// not bothering with teardown since we never want spinners when doing tests
470477
utils.StartSpinner = func(_ *spinner.Spinner) {

0 commit comments

Comments
 (0)