MCPcopy
hub / github.com/nikivdev/go / runGitCheckoutRemote

Function runGitCheckoutRemote

cli/flow/main.go:4465–4528  ·  view source on GitHub ↗
(ctx *snap.Context)

Source from the content-addressed store, hash-verified

4463}
4464
4465func runGitCheckoutRemote(ctx *snap.Context) error {
4466 if ctx.NArgs() != 0 {
4467 fmt.Fprintf(ctx.Stderr(), "Usage: %s gitCheckoutRemote\n", commandName)
4468 return fmt.Errorf("expected 0 arguments, got %d", ctx.NArgs())
4469 }
4470
4471 if err := ensureGitRepository(); err != nil {
4472 return err
4473 }
4474
4475 if err := runGitCommandStreaming(ctx, "fetch", "--all", "--prune"); err != nil {
4476 return fmt.Errorf("git fetch --all --prune: %w", err)
4477 }
4478
4479 branches, err := listRemoteBranches()
4480 if err != nil {
4481 return err
4482 }
4483
4484 idx, err := fuzzyfinder.Find(
4485 branches,
4486 func(i int) string {
4487 return branches[i].fullRef()
4488 },
4489 fuzzyfinder.WithPromptString("gitCheckoutRemote> "),
4490 )
4491 if err != nil {
4492 if errors.Is(err, fuzzyfinder.ErrAbort) {
4493 return nil
4494 }
4495 return fmt.Errorf("select remote branch: %w", err)
4496 }
4497
4498 selected := branches[idx]
4499 remoteRef := selected.fullRef()
4500
4501 remoteExists, err := gitRefExists(remoteRef)
4502 if err != nil {
4503 return fmt.Errorf("check remote branch %s: %w", remoteRef, err)
4504 }
4505 if !remoteExists {
4506 return fmt.Errorf("remote branch %s not found", remoteRef)
4507 }
4508
4509 localExists, err := gitRefExists(selected.Name)
4510 if err != nil {
4511 return fmt.Errorf("check local branch %s: %w", selected.Name, err)
4512 }
4513
4514 if localExists {
4515 if err := runGitCommandStreaming(ctx, "checkout", selected.Name); err != nil {
4516 return fmt.Errorf("git checkout %s: %w", selected.Name, err)
4517 }
4518 fmt.Fprintf(ctx.Stdout(), "✔️ Switched to %s\n", selected.Name)
4519 return nil
4520 }
4521
4522 if err := runGitCommandStreaming(ctx, "checkout", "-b", selected.Name, remoteRef); err != nil {

Callers 1

mainFunction · 0.85

Calls 5

ensureGitRepositoryFunction · 0.85
runGitCommandStreamingFunction · 0.85
listRemoteBranchesFunction · 0.85
gitRefExistsFunction · 0.85
fullRefMethod · 0.80

Tested by

no test coverage detected