diff --git a/api/queries_repo.go b/api/queries_repo.go index 85d8e0386..702cf7075 100644 --- a/api/queries_repo.go +++ b/api/queries_repo.go @@ -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) diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index 2bdf47208..ca10bafea 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -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) diff --git a/pkg/cmd/repo/fork/fork.go b/pkg/cmd/repo/fork/fork.go index e17e4b34d..9ac196617 100644 --- a/pkg/cmd/repo/fork/fork.go +++ b/pkg/cmd/repo/fork/fork.go @@ -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)