github.com/abhinav/git-pr@v0.6.1-0.20171029234004-54218d68c11b/cmd/git-pr/rebase.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 8 "github.com/abhinav/git-pr/cli" 9 "github.com/abhinav/git-pr/service" 10 11 "github.com/jessevdk/go-flags" 12 ) 13 14 type rebaseCmd struct { 15 OnlyMine bool `long:"only-mine" description:"IF set, only PRs owned by the current user will be rebased."` 16 Base string `long:"onto" value-name:"BASE" description:"Name of the base branch. If unspecified, only the dependents of the current branch will be rebased onto it."` 17 Args struct { 18 Branch string `positional-arg-name:"BRANCH" description:"Name of the branch to rebase. Defaults to the branch in the current directory."` 19 } `positional-args:"yes"` 20 21 getConfig configBuilder 22 } 23 24 func newRebaseCommand(cbuild cli.ConfigBuilder) flags.Commander { 25 return &rebaseCmd{getConfig: newConfigBuilder(cbuild)} 26 } 27 28 func (r *rebaseCmd) Execute([]string) error { 29 ctx := context.Background() 30 31 cfg, err := r.getConfig() 32 if err != nil { 33 return err 34 } 35 36 // TODO: accept other inputs for the PR to land 37 branch := r.Args.Branch 38 if branch == "" { 39 out, err := cfg.Git().CurrentBranch() 40 if err != nil { 41 return err 42 } 43 branch = out 44 } 45 46 prs, err := cfg.GitHub().ListPullRequestsByHead(ctx, "", branch) 47 if err != nil { 48 return err 49 } 50 51 if len(prs) == 0 { 52 return fmt.Errorf("Could not find PRs with head %q", branch) 53 } 54 55 var req service.RebaseRequest 56 if r.Base == "" { 57 if len(prs) > 1 { 58 return errTooManyPRsWithHead{Head: branch, Pulls: prs} 59 } 60 61 head := *prs[0].Head.Ref 62 dependents, err := cfg.GitHub().ListPullRequestsByBase(ctx, head) 63 if err != nil { 64 return err 65 } 66 req = service.RebaseRequest{PullRequests: dependents, Base: head} 67 } else { 68 req = service.RebaseRequest{PullRequests: prs, Base: r.Base} 69 } 70 71 if r.OnlyMine { 72 req.Author = cfg.CurrentGitHubUser() 73 } 74 75 log.Println("Rebasing:") 76 for _, pr := range req.PullRequests { 77 log.Printf(" - %v", *pr.HTMLURL) 78 } 79 80 res, err := cfg.Service.Rebase(ctx, &req) 81 if err != nil { 82 return err 83 } 84 85 if len(res.BranchesNotUpdated) > 0 { 86 log.Println("The following local branches were not updated because " + 87 "they did not match the corresponding remotes") 88 for _, br := range res.BranchesNotUpdated { 89 log.Println(" -", br) 90 } 91 } 92 return nil 93 }