MCPcopy
hub / github.com/cli/cli / NewCmdFork

Function NewCmdFork

pkg/cmd/repo/fork/fork.go:64–157  ·  view source on GitHub ↗

TODO warn about useless flags (--remote, --remote-name) when running from outside a repository TODO output over STDOUT not STDERR TODO remote-name has no effect on its own; error that or change behavior

(f *cmdutil.Factory, runF func(*ForkOptions) error)

Source from the content-addressed store, hash-verified

62// TODO remote-name has no effect on its own; error that or change behavior
63
64func NewCmdFork(f *cmdutil.Factory, runF func(*ForkOptions) error) *cobra.Command {
65 opts := &ForkOptions{
66 IO: f.IOStreams,
67 HttpClient: f.HttpClient,
68 GitClient: f.GitClient,
69 Config: f.Config,
70 BaseRepo: f.BaseRepo,
71 Remotes: f.Remotes,
72 Prompter: f.Prompter,
73 Since: time.Since,
74 }
75
76 cmd := &cobra.Command{
77 Use: "fork [<repository>] [-- <gitflags>...]",
78 Args: func(cmd *cobra.Command, args []string) error {
79 if cmd.ArgsLenAtDash() == 0 && len(args[1:]) > 0 {
80 return cmdutil.FlagErrorf("repository argument required when passing git clone flags")
81 }
82 return nil
83 },
84 Short: "Create a fork of a repository",
85 Long: heredoc.Docf(`
86 Create a fork of a repository.
87
88 With no argument, creates a fork of the current repository. Otherwise, forks
89 the specified repository.
90
91 By default, the new fork is set to be your %[1]sorigin%[1]s remote and any existing
92 origin remote is renamed to %[1]supstream%[1]s. To alter this behavior, you can set
93 a name for the new fork's remote with %[1]s--remote-name%[1]s.
94
95 The %[1]supstream%[1]s remote will be set as the default remote repository.
96
97 Additional %[1]sgit clone%[1]s flags can be passed after %[1]s--%[1]s.
98 `, "`"),
99 Example: heredoc.Doc(`
100 # Fork a repository
101 $ gh repo fork owner/repo
102
103 # Fork a repository and clone it locally
104 $ gh repo fork owner/repo --clone
105
106 # Fork a repository without cloning it, skip the prompt
107 $ gh repo fork owner/repo --clone=false
108 `),
109 RunE: func(cmd *cobra.Command, args []string) error {
110 promptOk := opts.IO.CanPrompt()
111 if len(args) > 0 {
112 opts.Repository = args[0]
113 opts.GitArgs = args[1:]
114 }
115
116 if cmd.Flags().Changed("org") && opts.Organization == "" {
117 return cmdutil.FlagErrorf("--org cannot be blank")
118 }
119
120 if opts.RemoteName == "" {
121 return cmdutil.FlagErrorf("--remote-name cannot be blank")

Callers 1

TestNewCmdForkFunction · 0.85

Calls 3

FlagErrorfFunction · 0.92
forkRunFunction · 0.85
CanPromptMethod · 0.80

Tested by 1

TestNewCmdForkFunction · 0.68