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

Function checkoutRun

pkg/cmd/pr/checkout/checkout.go:114–201  ·  view source on GitHub ↗
(opts *CheckoutOptions)

Source from the content-addressed store, hash-verified

112}
113
114func checkoutRun(opts *CheckoutOptions) error {
115 pr, baseRepo, err := opts.PRResolver.Resolve()
116 if err != nil {
117 return err
118 }
119
120 var reuseWorktree bool
121 if opts.Worktree != "" {
122 if err := ensureWorktreePathSafe(opts.Worktree); err != nil {
123 return err
124 }
125 reuseWorktree, err = resolveWorktreeTarget(opts.GitClient, opts.Worktree)
126 if err != nil {
127 return err
128 }
129 }
130
131 cfg, err := opts.Config()
132 if err != nil {
133 return err
134 }
135 protocol := cfg.GitProtocol(baseRepo.RepoHost()).Value
136
137 remotes, err := opts.Remotes()
138 if err != nil {
139 return err
140 }
141 baseRemote, _ := remotes.FindByRepo(baseRepo.RepoOwner(), baseRepo.RepoName())
142 baseURLOrName := ghrepo.FormatRemoteURL(baseRepo, protocol)
143 if baseRemote != nil {
144 baseURLOrName = baseRemote.Name
145 }
146
147 headRemote := baseRemote
148 if pr.HeadRepository == nil {
149 headRemote = nil
150 } else if pr.IsCrossRepository {
151 headRemote, _ = remotes.FindByRepo(pr.HeadRepositoryOwner.Login, pr.HeadRepository.Name)
152 }
153
154 if strings.HasPrefix(pr.HeadRefName, "-") {
155 return fmt.Errorf("invalid branch name: %q", pr.HeadRefName)
156 }
157
158 var cmdQueue [][]string
159
160 if headRemote != nil {
161 cmdQueue = append(cmdQueue, cmdsForExistingRemote(headRemote, pr, opts, reuseWorktree)...)
162 } else {
163 httpClient, err := opts.HttpClient()
164 if err != nil {
165 return err
166 }
167 apiClient := api.NewClientFromHTTP(httpClient)
168
169 defaultBranch, err := api.RepoDefaultBranch(apiClient, baseRepo)
170 if err != nil {
171 return err

Callers 2

NewCmdCheckoutFunction · 0.85
Test_checkoutRunFunction · 0.85

Calls 15

FormatRemoteURLFunction · 0.92
NewClientFromHTTPFunction · 0.92
RepoDefaultBranchFunction · 0.92
ensureWorktreePathSafeFunction · 0.85
resolveWorktreeTargetFunction · 0.85
cmdsForExistingRemoteFunction · 0.85
cmdsForMissingRemoteFunction · 0.85
executeCmdsFunction · 0.85
FindByRepoMethod · 0.80
IsStdoutTTYMethod · 0.80
ColorSchemeMethod · 0.80

Tested by 1

Test_checkoutRunFunction · 0.68