(ctx *snap.Context)
| 4352 | } |
| 4353 | |
| 4354 | func runGitCheckout(ctx *snap.Context) error { |
| 4355 | if ctx.NArgs() > 1 { |
| 4356 | fmt.Fprintf(ctx.Stderr(), "Usage: %s gitCheckout [branch-or-url]\n", commandName) |
| 4357 | return fmt.Errorf("expected at most 1 argument, got %d", ctx.NArgs()) |
| 4358 | } |
| 4359 | |
| 4360 | var ( |
| 4361 | branchInput string |
| 4362 | err error |
| 4363 | ) |
| 4364 | |
| 4365 | if ctx.NArgs() == 1 { |
| 4366 | branchInput = strings.TrimSpace(ctx.Arg(0)) |
| 4367 | } else { |
| 4368 | branchInput, err = promptLine(ctx, "Branch name or GitHub tree URL: ") |
| 4369 | if err != nil { |
| 4370 | return fmt.Errorf("read branch input: %w", err) |
| 4371 | } |
| 4372 | } |
| 4373 | |
| 4374 | if branchInput = strings.TrimSpace(branchInput); branchInput == "" { |
| 4375 | fmt.Fprintf(ctx.Stderr(), "Usage: %s gitCheckout [branch-or-url]\n", commandName) |
| 4376 | return fmt.Errorf("branch reference cannot be empty") |
| 4377 | } |
| 4378 | |
| 4379 | if err := ensureGitRepository(); err != nil { |
| 4380 | return err |
| 4381 | } |
| 4382 | |
| 4383 | remotes, err := listGitRemotes() |
| 4384 | if err != nil { |
| 4385 | return err |
| 4386 | } |
| 4387 | |
| 4388 | var ( |
| 4389 | branchName string |
| 4390 | preferredRemote string |
| 4391 | branchCandidates []string |
| 4392 | branchDerivedFromURL bool |
| 4393 | ) |
| 4394 | |
| 4395 | if strings.HasPrefix(branchInput, "http://") || strings.HasPrefix(branchInput, "https://") { |
| 4396 | candidates, err := parseGitHubTreeURL(branchInput) |
| 4397 | if err != nil { |
| 4398 | return fmt.Errorf("parse GitHub tree URL: %w", err) |
| 4399 | } |
| 4400 | branchCandidates = candidates |
| 4401 | branchName = branchCandidates[0] |
| 4402 | branchDerivedFromURL = true |
| 4403 | } else { |
| 4404 | branchName = branchInput |
| 4405 | branchCandidates = []string{branchName} |
| 4406 | |
| 4407 | if idx := strings.Index(branchInput, "/"); idx > 0 { |
| 4408 | candidateRemote := branchInput[:idx] |
| 4409 | remaining := branchInput[idx+1:] |
| 4410 | if remaining != "" { |
| 4411 | for _, r := range remotes { |
no test coverage detected