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

Function cmdsForExistingRemote

pkg/cmd/pr/checkout/checkout.go:199–242  ·  view source on GitHub ↗
(remote *cliContext.Remote, pr *api.PullRequest, opts *CheckoutOptions, reuseWorktree bool)

Source from the content-addressed store, hash-verified

197}
198
199func cmdsForExistingRemote(remote *cliContext.Remote, pr *api.PullRequest, opts *CheckoutOptions, reuseWorktree bool) [][]string {
200 var cmds [][]string
201 remoteBranch := fmt.Sprintf("%s/%s", remote.Name, pr.HeadRefName)
202
203 refSpec := fmt.Sprintf("+refs/heads/%s", pr.HeadRefName)
204 if !opts.Detach {
205 refSpec += fmt.Sprintf(":refs/remotes/%s", remoteBranch)
206 }
207
208 localBranch := pr.HeadRefName
209 if opts.BranchName != "" {
210 localBranch = opts.BranchName
211 }
212
213 remoteBranchRef := fmt.Sprintf("refs/remotes/%s", remoteBranch)
214 fetchCmd := []string{"fetch", remote.Name, refSpec, "--no-tags"}
215
216 if opts.Detach {
217 return append(cmds, detachCmds(fetchCmd, opts.Worktree, reuseWorktree)...)
218 }
219
220 cmds = append(cmds, fetchCmd)
221
222 switch {
223 case opts.Worktree != "":
224 worktreeCmds, branchExists := shared.WorktreeCheckoutCommands(
225 opts.GitClient,
226 shared.WorktreeTarget{Path: opts.Worktree, Reuse: reuseWorktree},
227 localBranch,
228 remoteBranch,
229 )
230 cmds = append(cmds, worktreeCmds...)
231 if branchExists {
232 cmds = append(cmds, syncBranchCmds(opts.Worktree, remoteBranchRef, opts.Force)...)
233 }
234 case opts.GitClient.HasLocalBranch(context.Background(), localBranch):
235 cmds = append(cmds, []string{"checkout", localBranch})
236 cmds = append(cmds, syncBranchCmds("", remoteBranchRef, opts.Force)...)
237 default:
238 cmds = append(cmds, []string{"checkout", "-b", localBranch, "--track", remoteBranch})
239 }
240
241 return cmds
242}
243
244func cmdsForMissingRemote(pr *api.PullRequest, baseURLOrName, repoHost, defaultBranch, protocol string, opts *CheckoutOptions, reuseWorktree bool) [][]string {
245 var cmds [][]string

Callers 1

checkoutRunFunction · 0.85

Calls 4

WorktreeCheckoutCommandsFunction · 0.92
detachCmdsFunction · 0.85
syncBranchCmdsFunction · 0.85
HasLocalBranchMethod · 0.65

Tested by

no test coverage detected