Support Fork with Default Branch Only

This commit is contained in:
Josh Soref 2023-02-07 09:48:58 -05:00
parent d1e54ecb9b
commit e6d6427704
3 changed files with 18 additions and 13 deletions

View file

@ -504,7 +504,7 @@ type repositoryV3 struct {
}
// ForkRepo forks the repository on GitHub and returns the new repository
func ForkRepo(client *Client, repo ghrepo.Interface, org, newName string) (*Repository, error) {
func ForkRepo(client *Client, repo ghrepo.Interface, org, newName string, defaultBranchOnly bool) (*Repository, error) {
path := fmt.Sprintf("repos/%s/forks", ghrepo.FullName(repo))
params := map[string]interface{}{}
@ -514,6 +514,9 @@ func ForkRepo(client *Client, repo ghrepo.Interface, org, newName string) (*Repo
if newName != "" {
params["name"] = newName
}
if defaultBranchOnly {
params["default_branch_only"] = true
}
body := &bytes.Buffer{}
enc := json.NewEncoder(body)

View file

@ -698,7 +698,7 @@ func handlePush(opts CreateOptions, ctx CreateContext) error {
// one by forking the base repository
if headRepo == nil && ctx.IsPushEnabled {
opts.IO.StartProgressIndicator()
headRepo, err = api.ForkRepo(client, ctx.BaseRepo, "", "")
headRepo, err = api.ForkRepo(client, ctx.BaseRepo, "", "", false)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("error forking repo: %w", err)

View file

@ -33,16 +33,17 @@ type ForkOptions struct {
Remotes func() (ghContext.Remotes, error)
Since func(time.Time) time.Duration
GitArgs []string
Repository string
Clone bool
Remote bool
PromptClone bool
PromptRemote bool
RemoteName string
Organization string
ForkName string
Rename bool
GitArgs []string
Repository string
Clone bool
Remote bool
PromptClone bool
PromptRemote bool
RemoteName string
Organization string
ForkName string
Rename bool
DefaultBranchOnly bool
}
// TODO warn about useless flags (--remote, --remote-name) when running from outside a repository
@ -122,6 +123,7 @@ func NewCmdFork(f *cmdutil.Factory, runF func(*ForkOptions) error) *cobra.Comman
cmd.Flags().StringVar(&opts.RemoteName, "remote-name", defaultRemoteName, "Specify the name for the new remote")
cmd.Flags().StringVar(&opts.Organization, "org", "", "Create the fork in an organization")
cmd.Flags().StringVar(&opts.ForkName, "fork-name", "", "Rename the forked repository")
cmd.Flags().BoolVar(&opts.DefaultBranchOnly, "default-branch-only", false, "Only include the default branch in the fork")
return cmd
}
@ -181,7 +183,7 @@ func forkRun(opts *ForkOptions) error {
apiClient := api.NewClientFromHTTP(httpClient)
opts.IO.StartProgressIndicator()
forkedRepo, err := api.ForkRepo(apiClient, repoToFork, opts.Organization, opts.ForkName)
forkedRepo, err := api.ForkRepo(apiClient, repoToFork, opts.Organization, opts.ForkName, opts.DefaultBranchOnly)
opts.IO.StopProgressIndicator()
if err != nil {
return fmt.Errorf("failed to fork: %w", err)