| 102 | } |
| 103 | |
| 104 | func checkoutRun(opts *CheckoutOptions) error { |
| 105 | pr, baseRepo, err := opts.PRResolver.Resolve() |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | |
| 110 | cfg, err := opts.Config() |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | protocol := cfg.GitProtocol(baseRepo.RepoHost()).Value |
| 115 | |
| 116 | remotes, err := opts.Remotes() |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | baseRemote, _ := remotes.FindByRepo(baseRepo.RepoOwner(), baseRepo.RepoName()) |
| 121 | baseURLOrName := ghrepo.FormatRemoteURL(baseRepo, protocol) |
| 122 | if baseRemote != nil { |
| 123 | baseURLOrName = baseRemote.Name |
| 124 | } |
| 125 | |
| 126 | headRemote := baseRemote |
| 127 | if pr.HeadRepository == nil { |
| 128 | headRemote = nil |
| 129 | } else if pr.IsCrossRepository { |
| 130 | headRemote, _ = remotes.FindByRepo(pr.HeadRepositoryOwner.Login, pr.HeadRepository.Name) |
| 131 | } |
| 132 | |
| 133 | if strings.HasPrefix(pr.HeadRefName, "-") { |
| 134 | return fmt.Errorf("invalid branch name: %q", pr.HeadRefName) |
| 135 | } |
| 136 | |
| 137 | var cmdQueue [][]string |
| 138 | |
| 139 | if headRemote != nil { |
| 140 | cmdQueue = append(cmdQueue, cmdsForExistingRemote(headRemote, pr, opts)...) |
| 141 | } else { |
| 142 | httpClient, err := opts.HttpClient() |
| 143 | if err != nil { |
| 144 | return err |
| 145 | } |
| 146 | apiClient := api.NewClientFromHTTP(httpClient) |
| 147 | |
| 148 | defaultBranch, err := api.RepoDefaultBranch(apiClient, baseRepo) |
| 149 | if err != nil { |
| 150 | return err |
| 151 | } |
| 152 | cmdQueue = append(cmdQueue, cmdsForMissingRemote(pr, baseURLOrName, baseRepo.RepoHost(), defaultBranch, protocol, opts)...) |
| 153 | } |
| 154 | |
| 155 | if opts.RecurseSubmodules { |
| 156 | cmdQueue = append(cmdQueue, []string{"submodule", "sync", "--recursive"}) |
| 157 | cmdQueue = append(cmdQueue, []string{"submodule", "update", "--init", "--recursive"}) |
| 158 | } |
| 159 | |
| 160 | // Note that although we will probably be fetching from the head, in practice, PR checkout can only |
| 161 | // ever point to one host, and we know baseRepo must be populated. |