MCPcopy Create free account
hub / github.com/cli/cli / ForkRepo

Function ForkRepo

api/queries_repo.go:596–643  ·  view source on GitHub ↗

ForkRepo forks the repository on GitHub and returns the new repository

(client *Client, repo ghrepo.Interface, org, newName string, defaultBranchOnly bool)

Source from the content-addressed store, hash-verified

594
595// ForkRepo forks the repository on GitHub and returns the new repository
596func ForkRepo(client *Client, repo ghrepo.Interface, org, newName string, defaultBranchOnly bool) (*Repository, error) {
597 path, err := safeurl.JoinPath("repos", repo.RepoOwner(), repo.RepoName(), "forks")
598 if err != nil {
599 return nil, err
600 }
601
602 params := map[string]any{}
603 if org != "" {
604 params["organization"] = org
605 }
606 if newName != "" {
607 params["name"] = newName
608 }
609 if defaultBranchOnly {
610 params["default_branch_only"] = true
611 }
612
613 body := &bytes.Buffer{}
614 enc := json.NewEncoder(body)
615 if err := enc.Encode(params); err != nil {
616 return nil, err
617 }
618
619 result := repositoryV3{}
620 err = client.REST(repo.RepoHost(), "POST", path.String(), body, &result)
621 if err != nil {
622 return nil, err
623 }
624
625 newRepo := &Repository{
626 ID: result.NodeID,
627 Name: result.Name,
628 CreatedAt: result.CreatedAt,
629 Owner: RepositoryOwner{
630 Login: result.Owner.Login,
631 },
632 ViewerPermission: "WRITE",
633 hostname: repo.RepoHost(),
634 }
635
636 // The GitHub API will happily return a HTTP 200 when attempting to fork own repo even though no forking
637 // actually took place. Ensure that we raise an error instead.
638 if ghrepo.IsSame(repo, newRepo) {
639 return newRepo, fmt.Errorf("%s cannot be forked. A single user account cannot own both a parent and fork.", ghrepo.FullName(repo))
640 }
641
642 return newRepo, nil
643}
644
645// RenameRepo renames the repository on GitHub and returns the renamed repository
646func RenameRepo(client *Client, repo ghrepo.Interface, newRepoName string) (*Repository, error) {

Callers 3

forkRunFunction · 0.92
handlePushFunction · 0.92

Calls 9

JoinPathFunction · 0.92
IsSameFunction · 0.92
FullNameFunction · 0.92
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
RESTMethod · 0.65
RepoHostMethod · 0.65
StringMethod · 0.65
ErrorfMethod · 0.65