Skip to content

Commit 4860ccc

Browse files
author
Nate Smith
authored
Merge pull request cli#2282 from cristiand391/repo-fork-gitflags
Add support for git flags in gh repo fork
2 parents 938f6f4 + 48c8907 commit 4860ccc

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

pkg/cmd/repo/fork/fork.go

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

2223
type ForkOptions struct {
@@ -26,6 +27,7 @@ type ForkOptions struct {
2627
BaseRepo func() (ghrepo.Interface, error)
2728
Remotes func() (context.Remotes, error)
2829

30+
GitArgs []string
2931
Repository string
3032
Clone bool
3133
Remote bool
@@ -48,16 +50,24 @@ 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: func(cmd *cobra.Command, args []string) error {
55+
if cmd.ArgsLenAtDash() == 0 && len(args[1:]) > 0 {
56+
return cmdutil.FlagError{Err: fmt.Errorf("repository argument required when passing 'git clone' flags")}
57+
}
58+
return nil
59+
},
5360
Short: "Create a fork of a repository",
5461
Long: `Create a fork of a repository.
5562
56-
With no argument, creates a fork of the current repository. Otherwise, forks the specified repository.`,
63+
With no argument, creates a fork of the current repository. Otherwise, forks the specified repository.
64+
65+
Additional 'git clone' flags can be passed in by listing them after '--'.`,
5766
RunE: func(cmd *cobra.Command, args []string) error {
5867
promptOk := opts.IO.CanPrompt()
5968
if len(args) > 0 {
6069
opts.Repository = args[0]
70+
opts.GitArgs = args[1:]
6171
}
6272

6373
if promptOk && !cmd.Flags().Changed("clone") {
@@ -74,6 +84,12 @@ With no argument, creates a fork of the current repository. Otherwise, forks the
7484
return forkRun(opts)
7585
},
7686
}
87+
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
88+
if err == pflag.ErrHelp {
89+
return err
90+
}
91+
return &cmdutil.FlagError{Err: fmt.Errorf("%w\nSeparate git clone flags with '--'.", err)}
92+
})
7793

7894
cmd.Flags().BoolVar(&opts.Clone, "clone", false, "Clone the fork {true|false}")
7995
cmd.Flags().BoolVar(&opts.Remote, "remote", false, "Add remote for fork {true|false}")
@@ -243,7 +259,7 @@ func forkRun(opts *ForkOptions) error {
243259
}
244260
if cloneDesired {
245261
forkedRepoURL := ghrepo.FormatRemoteURL(forkedRepo, protocol)
246-
cloneDir, err := git.RunClone(forkedRepoURL, []string{})
262+
cloneDir, err := git.RunClone(forkedRepoURL, opts.GitArgs)
247263
if err != nil {
248264
return fmt.Errorf("failed to clone fork: %w", err)
249265
}

pkg/cmd/repo/fork/fork_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,35 @@ func TestRepoFork_in_parent_survey_no(t *testing.T) {
495495
reg.Verify(t)
496496
}
497497

498+
func Test_RepoFork_gitFlags(t *testing.T) {
499+
defer stubSince(2 * time.Second)()
500+
reg := &httpmock.Registry{}
501+
defer reg.StubWithFixturePath(200, "./forkResult.json")()
502+
httpClient := &http.Client{Transport: reg}
503+
504+
cs, cmdTeardown := run.Stub()
505+
defer cmdTeardown(t)
506+
507+
cs.Register(`git clone --depth 1 https://github.com/someone/REPO.git`, 0, "")
508+
cs.Register(`git -C REPO remote add -f upstream https://github.com/OWNER/REPO.git`, 0, "")
509+
510+
output, err := runCommand(httpClient, nil, false, "--clone OWNER/REPO -- --depth 1")
511+
if err != nil {
512+
t.Errorf("error running command `repo fork`: %v", err)
513+
}
514+
515+
assert.Equal(t, "", output.String())
516+
assert.Equal(t, output.Stderr(), "")
517+
reg.Verify(t)
518+
}
519+
520+
func Test_RepoFork_flagError(t *testing.T) {
521+
_, err := runCommand(nil, nil, true, "--depth 1 OWNER/REPO")
522+
if err == nil || err.Error() != "unknown flag: --depth\nSeparate git clone flags with '--'." {
523+
t.Errorf("unexpected error %v", err)
524+
}
525+
}
526+
498527
func TestRepoFork_in_parent_match_protocol(t *testing.T) {
499528
defer stubSince(2 * time.Second)()
500529
reg := &httpmock.Registry{}

0 commit comments

Comments
 (0)