diff --git a/git-open-pull.go b/git-open-pull.go index cd75e08..688d57f 100644 --- a/git-open-pull.go +++ b/git-open-pull.go @@ -78,6 +78,28 @@ func GetIssueNumber(ctx context.Context, client *github.Client, settings *Settin return detected, nil } +// ForkBranches returns the names of all of the branches in the user's fork +func ForkBranches(ctx context.Context, client *github.Client, settings *Settings) ([]string, error) { + opts := &github.BranchListOptions{ListOptions: github.ListOptions{PerPage: 100}} + var o []string + for { + branches, resp, err := client.Repositories.ListBranches(ctx, settings.User, settings.BaseRepo, opts) + if err != nil { + return nil, err + } + for _, b := range branches { + if b.Name != nil { + o = append(o, *b.Name) + } + } + if resp.NextPage == 0 { + break + } + opts.Page = resp.NextPage + } + return o, nil +} + func printUsage(settings *Settings) { out := flag.CommandLine.Output() fmt.Fprintln(out, "git-open-pull creates an issue, renames the local branch to include that issue number, pushes the renamed branch and finally converts the issue into a pull request against the renamed branch.") @@ -263,26 +285,20 @@ func main() { time.Sleep(2 * time.Second) // check branch exists on remote - branches, _, err := client.Repositories.ListBranches(ctx, settings.User, settings.BaseRepo, &github.BranchListOptions{ListOptions: github.ListOptions{PerPage: 100}}) + branches, err := ForkBranches(ctx, client, settings) if err != nil { log.Fatal(err) } var foundBranch bool for _, b := range branches { - if *b.Name == branch { + if b == branch { foundBranch = true } } if !foundBranch { fmt.Printf("Error: branch %s does not exist in %s/%s\n", branch, settings.User, settings.BaseRepo) if len(branches) > 1 { - fmt.Printf("valid branches are:") - for i, b := range branches { - if i > 0 { - fmt.Print(", ") - } - fmt.Printf("%s", *b.Name) - } + fmt.Printf("valid branches are:%s", strings.Join(branches, ", ")) } os.Exit(1) }