MCPcopy
hub / github.com/cli/cli / checkoutBranch

Function checkoutBranch

pkg/cmd/issue/develop/develop.go:321–370  ·  view source on GitHub ↗
(opts *DevelopOptions, branchRepo ghrepo.Interface, checkoutBranch string)

Source from the content-addressed store, hash-verified

319}
320
321func checkoutBranch(opts *DevelopOptions, branchRepo ghrepo.Interface, checkoutBranch string) (err error) {
322 remotes, err := opts.Remotes()
323 if err != nil {
324 // If the user specified the branch to be checked out and no remotes are found
325 // display an error. Otherwise bail out silently, likely the command was not
326 // run from inside a git directory.
327 if opts.Checkout {
328 return err
329 } else {
330 return nil
331 }
332 }
333
334 baseRemote, err := remotes.FindByRepo(branchRepo.RepoOwner(), branchRepo.RepoName())
335 if err != nil {
336 // If the user specified the branch to be checked out and no remote matches the
337 // base repo, then display an error. Otherwise bail out silently.
338 if opts.Checkout {
339 return err
340 } else {
341 return nil
342 }
343 }
344
345 gc := opts.GitClient
346
347 if err := gc.Fetch(ctx.Background(), baseRemote.Name, fmt.Sprintf("+refs/heads/%[1]s:refs/remotes/%[2]s/%[1]s", checkoutBranch, baseRemote.Name)); err != nil {
348 return err
349 }
350
351 if !opts.Checkout {
352 return nil
353 }
354
355 if gc.HasLocalBranch(ctx.Background(), checkoutBranch) {
356 if err := gc.CheckoutBranch(ctx.Background(), checkoutBranch); err != nil {
357 return err
358 }
359
360 if err := gc.Pull(ctx.Background(), baseRemote.Name, checkoutBranch); err != nil {
361 _, _ = fmt.Fprintf(opts.IO.ErrOut, "%s warning: not possible to fast-forward to: %q\n", opts.IO.ColorScheme().WarningIcon(), checkoutBranch)
362 }
363 } else {
364 if err := gc.CheckoutNewBranch(ctx.Background(), baseRemote.Name, checkoutBranch); err != nil {
365 return err
366 }
367 }
368
369 return nil
370}

Callers 1

developRunCreateFunction · 0.85

Calls 11

FindByRepoMethod · 0.80
WarningIconMethod · 0.80
ColorSchemeMethod · 0.80
CheckoutNewBranchMethod · 0.80
RemotesMethod · 0.65
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
FetchMethod · 0.65
HasLocalBranchMethod · 0.65
CheckoutBranchMethod · 0.65
PullMethod · 0.65

Tested by

no test coverage detected