|
1 | 1 | package command |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | 4 | "fmt" |
6 | 5 | "io" |
7 | | - "os" |
8 | | - "os/exec" |
9 | 6 | "regexp" |
10 | 7 | "strconv" |
11 | 8 | "strings" |
@@ -46,17 +43,6 @@ A pull request can be supplied as argument in any of the following formats: |
46 | 43 | - by URL, e.g. "https://github.com/OWNER/REPO/pull/123"; or |
47 | 44 | - by the name of its head branch, e.g. "patch-1" or "OWNER:patch-1".`, |
48 | 45 | } |
49 | | -var prCheckoutCmd = &cobra.Command{ |
50 | | - Use: "checkout {<number> | <url> | <branch>}", |
51 | | - Short: "Check out a pull request in Git", |
52 | | - Args: func(cmd *cobra.Command, args []string) error { |
53 | | - if len(args) < 1 { |
54 | | - return errors.New("requires a PR number as an argument") |
55 | | - } |
56 | | - return nil |
57 | | - }, |
58 | | - RunE: prCheckout, |
59 | | -} |
60 | 46 | var prListCmd = &cobra.Command{ |
61 | 47 | Use: "list", |
62 | 48 | Short: "List and filter pull requests in this repository", |
@@ -386,95 +372,6 @@ func prSelectorForCurrentBranch(ctx context.Context) (prNumber int, prHeadRef st |
386 | 372 | return |
387 | 373 | } |
388 | 374 |
|
389 | | -func prCheckout(cmd *cobra.Command, args []string) error { |
390 | | - ctx := contextForCommand(cmd) |
391 | | - currentBranch, _ := ctx.Branch() |
392 | | - remotes, err := ctx.Remotes() |
393 | | - if err != nil { |
394 | | - return err |
395 | | - } |
396 | | - // FIXME: duplicates logic from fsContext.BaseRepo |
397 | | - baseRemote, err := remotes.FindByName("upstream", "github", "origin", "*") |
398 | | - if err != nil { |
399 | | - return err |
400 | | - } |
401 | | - apiClient, err := apiClientForContext(ctx) |
402 | | - if err != nil { |
403 | | - return err |
404 | | - } |
405 | | - |
406 | | - pr, err := prFromArg(apiClient, baseRemote, args[0]) |
407 | | - if err != nil { |
408 | | - return err |
409 | | - } |
410 | | - |
411 | | - headRemote := baseRemote |
412 | | - if pr.IsCrossRepository { |
413 | | - headRemote, _ = remotes.FindByRepo(pr.HeadRepositoryOwner.Login, pr.HeadRepository.Name) |
414 | | - } |
415 | | - |
416 | | - cmdQueue := [][]string{} |
417 | | - |
418 | | - newBranchName := pr.HeadRefName |
419 | | - if headRemote != nil { |
420 | | - // there is an existing git remote for PR head |
421 | | - remoteBranch := fmt.Sprintf("%s/%s", headRemote.Name, pr.HeadRefName) |
422 | | - refSpec := fmt.Sprintf("+refs/heads/%s:refs/remotes/%s", pr.HeadRefName, remoteBranch) |
423 | | - |
424 | | - cmdQueue = append(cmdQueue, []string{"git", "fetch", headRemote.Name, refSpec}) |
425 | | - |
426 | | - // local branch already exists |
427 | | - if git.VerifyRef("refs/heads/" + newBranchName) { |
428 | | - cmdQueue = append(cmdQueue, []string{"git", "checkout", newBranchName}) |
429 | | - cmdQueue = append(cmdQueue, []string{"git", "merge", "--ff-only", fmt.Sprintf("refs/remotes/%s", remoteBranch)}) |
430 | | - } else { |
431 | | - cmdQueue = append(cmdQueue, []string{"git", "checkout", "-b", newBranchName, "--no-track", remoteBranch}) |
432 | | - cmdQueue = append(cmdQueue, []string{"git", "config", fmt.Sprintf("branch.%s.remote", newBranchName), headRemote.Name}) |
433 | | - cmdQueue = append(cmdQueue, []string{"git", "config", fmt.Sprintf("branch.%s.merge", newBranchName), "refs/heads/" + pr.HeadRefName}) |
434 | | - } |
435 | | - } else { |
436 | | - // no git remote for PR head |
437 | | - |
438 | | - // avoid naming the new branch the same as the default branch |
439 | | - if newBranchName == pr.HeadRepository.DefaultBranchRef.Name { |
440 | | - newBranchName = fmt.Sprintf("%s/%s", pr.HeadRepositoryOwner.Login, newBranchName) |
441 | | - } |
442 | | - |
443 | | - ref := fmt.Sprintf("refs/pull/%d/head", pr.Number) |
444 | | - if newBranchName == currentBranch { |
445 | | - // PR head matches currently checked out branch |
446 | | - cmdQueue = append(cmdQueue, []string{"git", "fetch", baseRemote.Name, ref}) |
447 | | - cmdQueue = append(cmdQueue, []string{"git", "merge", "--ff-only", "FETCH_HEAD"}) |
448 | | - } else { |
449 | | - // create a new branch |
450 | | - cmdQueue = append(cmdQueue, []string{"git", "fetch", baseRemote.Name, fmt.Sprintf("%s:%s", ref, newBranchName)}) |
451 | | - cmdQueue = append(cmdQueue, []string{"git", "checkout", newBranchName}) |
452 | | - } |
453 | | - |
454 | | - remote := baseRemote.Name |
455 | | - mergeRef := ref |
456 | | - if pr.MaintainerCanModify { |
457 | | - remote = fmt.Sprintf("https://github.com/%s/%s.git", pr.HeadRepositoryOwner.Login, pr.HeadRepository.Name) |
458 | | - mergeRef = fmt.Sprintf("refs/heads/%s", pr.HeadRefName) |
459 | | - } |
460 | | - if mc, err := git.Config(fmt.Sprintf("branch.%s.merge", newBranchName)); err != nil || mc == "" { |
461 | | - cmdQueue = append(cmdQueue, []string{"git", "config", fmt.Sprintf("branch.%s.remote", newBranchName), remote}) |
462 | | - cmdQueue = append(cmdQueue, []string{"git", "config", fmt.Sprintf("branch.%s.merge", newBranchName), mergeRef}) |
463 | | - } |
464 | | - } |
465 | | - |
466 | | - for _, args := range cmdQueue { |
467 | | - cmd := exec.Command(args[0], args[1:]...) |
468 | | - cmd.Stdout = os.Stdout |
469 | | - cmd.Stderr = os.Stderr |
470 | | - if err := utils.PrepareCmd(cmd).Run(); err != nil { |
471 | | - return err |
472 | | - } |
473 | | - } |
474 | | - |
475 | | - return nil |
476 | | -} |
477 | | - |
478 | 375 | func printPrs(w io.Writer, totalCount int, prs ...api.PullRequest) { |
479 | 376 | for _, pr := range prs { |
480 | 377 | prNumber := fmt.Sprintf("#%d", pr.Number) |
|
0 commit comments