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)
| 62 | // TODO remote-name has no effect on its own; error that or change behavior |
| 63 | |
| 64 | func 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 | RunE: func(cmd *cobra.Command, args []string) error { |
| 100 | promptOk := opts.IO.CanPrompt() |
| 101 | if len(args) > 0 { |
| 102 | opts.Repository = args[0] |
| 103 | opts.GitArgs = args[1:] |
| 104 | } |
| 105 | |
| 106 | if cmd.Flags().Changed("org") && opts.Organization == "" { |
| 107 | return cmdutil.FlagErrorf("--org cannot be blank") |
| 108 | } |
| 109 | |
| 110 | if opts.RemoteName == "" { |
| 111 | return cmdutil.FlagErrorf("--remote-name cannot be blank") |
| 112 | } else if !cmd.Flags().Changed("remote-name") { |
| 113 | opts.Rename = true // Any existing 'origin' will be renamed to upstream |
| 114 | } |
| 115 | |
| 116 | if opts.Repository != "" && cmd.Flags().Changed("remote") { |
| 117 | return cmdutil.FlagErrorf("the `--remote` flag is unsupported when a repository argument is provided") |
| 118 | } |
| 119 | |
| 120 | if promptOk { |
| 121 | // We can prompt for these if they were not specified. |