MCPcopy
hub / github.com/cli/cli / cmdsForMissingRemote

Function cmdsForMissingRemote

pkg/cmd/pr/checkout/checkout.go:204–259  ·  view source on GitHub ↗
(pr *api.PullRequest, baseURLOrName, repoHost, defaultBranch, protocol string, opts *CheckoutOptions)

Source from the content-addressed store, hash-verified

202}
203
204func cmdsForMissingRemote(pr *api.PullRequest, baseURLOrName, repoHost, defaultBranch, protocol string, opts *CheckoutOptions) [][]string {
205 var cmds [][]string
206 ref := fmt.Sprintf("refs/pull/%d/head", pr.Number)
207
208 if opts.Detach {
209 cmds = append(cmds, []string{"fetch", baseURLOrName, ref, "--no-tags"})
210 cmds = append(cmds, []string{"checkout", "--detach", "FETCH_HEAD"})
211 return cmds
212 }
213
214 localBranch := pr.HeadRefName
215 if opts.BranchName != "" {
216 localBranch = opts.BranchName
217 } else if pr.HeadRefName == defaultBranch {
218 // avoid naming the new branch the same as the default branch
219 localBranch = fmt.Sprintf("%s/%s", pr.HeadRepositoryOwner.Login, localBranch)
220 }
221
222 currentBranch, _ := opts.Branch()
223 if localBranch == currentBranch {
224 // PR head matches currently checked out branch
225 cmds = append(cmds, []string{"fetch", baseURLOrName, ref, "--no-tags"})
226 if opts.Force {
227 cmds = append(cmds, []string{"reset", "--hard", "FETCH_HEAD"})
228 } else {
229 // TODO: check if non-fast-forward and suggest to use `--force`
230 cmds = append(cmds, []string{"merge", "--ff-only", "FETCH_HEAD"})
231 }
232 } else {
233 // TODO: check if non-fast-forward and suggest to use `--force`
234 fetchCmd := []string{"fetch", baseURLOrName, fmt.Sprintf("%s:%s", ref, localBranch), "--no-tags"}
235 if opts.Force {
236 fetchCmd = append(fetchCmd, "--force")
237 }
238 cmds = append(cmds, fetchCmd)
239 cmds = append(cmds, []string{"checkout", localBranch})
240 }
241
242 remote := baseURLOrName
243 mergeRef := ref
244 if pr.MaintainerCanModify && pr.HeadRepository != nil {
245 headRepo := ghrepo.NewWithHost(pr.HeadRepositoryOwner.Login, pr.HeadRepository.Name, repoHost)
246 remote = ghrepo.FormatRemoteURL(headRepo, protocol)
247 mergeRef = fmt.Sprintf("refs/heads/%s", pr.HeadRefName)
248 }
249 if missingMergeConfigForBranch(opts.GitClient, localBranch) {
250 // .remote is needed for `git pull` to work
251 // .pushRemote is needed for `git push` to work, if user has set `remote.pushDefault`.
252 // see https://git-scm.com/docs/git-config#Documentation/git-config.txt-branchltnamegtremote
253 cmds = append(cmds, []string{"config", fmt.Sprintf("branch.%s.remote", localBranch), remote})
254 cmds = append(cmds, []string{"config", fmt.Sprintf("branch.%s.pushRemote", localBranch), remote})
255 cmds = append(cmds, []string{"config", fmt.Sprintf("branch.%s.merge", localBranch), mergeRef})
256 }
257
258 return cmds
259}
260
261func missingMergeConfigForBranch(client *git.Client, b string) bool {

Callers 1

checkoutRunFunction · 0.85

Calls 3

NewWithHostFunction · 0.92
FormatRemoteURLFunction · 0.92

Tested by

no test coverage detected