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

Function ForkRepo

api/queries_repo.go:595–642  ·  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

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