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

Function checkoutBranch

pkg/cmd/issue/develop/develop.go:353–420  ·  view source on GitHub ↗
(opts *DevelopOptions, branchRepo ghrepo.Interface, checkoutBranch string, worktreeTarget prShared.WorktreeTarget)

Source from the content-addressed store, hash-verified

351}
352
353func checkoutBranch(opts *DevelopOptions, branchRepo ghrepo.Interface, checkoutBranch string, worktreeTarget prShared.WorktreeTarget) (err error) {
354 remotes, err := opts.Remotes()
355 if err != nil {
356 // If the user specified the branch to be checked out and no remotes are found
357 // display an error. Otherwise bail out silently, likely the command was not
358 // run from inside a git directory.
359 if opts.Checkout {
360 return err
361 } else {
362 return nil
363 }
364 }
365
366 baseRemote, err := remotes.FindByRepo(branchRepo.RepoOwner(), branchRepo.RepoName())
367 if err != nil {
368 // If the user specified the branch to be checked out and no remote matches the
369 // base repo, then display an error. Otherwise bail out silently.
370 if opts.Checkout {
371 return err
372 } else {
373 return nil
374 }
375 }
376
377 gc := opts.GitClient
378
379 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 {
380 return err
381 }
382
383 if !opts.Checkout {
384 return nil
385 }
386
387 if opts.Worktree != "" {
388 commands, branchExists := prShared.WorktreeCheckoutCommands(
389 gc,
390 worktreeTarget,
391 checkoutBranch,
392 fmt.Sprintf("%s/%s", baseRemote.Name, checkoutBranch),
393 )
394 if err := runGitCommands(gc, commands); err != nil {
395 return err
396 }
397 if branchExists {
398 if err := gc.Pull(ctx.Background(), baseRemote.Name, checkoutBranch, git.WithRepoDir(opts.Worktree)); err != nil {
399 _, _ = fmt.Fprintf(opts.IO.ErrOut, "%s warning: not possible to fast-forward to: %q\n", opts.IO.ColorScheme().WarningIcon(), checkoutBranch)
400 }
401 }
402 return nil
403 }
404
405 if gc.HasLocalBranch(ctx.Background(), checkoutBranch) {
406 if err := gc.CheckoutBranch(ctx.Background(), checkoutBranch); err != nil {
407 return err
408 }
409
410 if err := gc.Pull(ctx.Background(), baseRemote.Name, checkoutBranch); err != nil {

Callers 1

developRunCreateFunction · 0.85

Calls 13

WithRepoDirFunction · 0.92
runGitCommandsFunction · 0.85
FindByRepoMethod · 0.80
WarningIconMethod · 0.80
ColorSchemeMethod · 0.80
CheckoutNewBranchMethod · 0.80
RemotesMethod · 0.65
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
FetchMethod · 0.65
PullMethod · 0.65
HasLocalBranchMethod · 0.65

Tested by

no test coverage detected