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