git-ea/cmd/pr.go

48 lines
1.0 KiB
Go

package cmd
import (
"context"
"errors"
"flag"
"fmt"
"github.com/peterbourgon/ff/v3/ffcli"
)
func (h *Handler) PR() *ffcli.Command {
fs := flag.NewFlagSet("pr", flag.ContinueOnError)
ideFlag := fs.Bool("ide", false, "Start an IDE for this PR")
fs.BoolVar(ideFlag, "i", *ideFlag, "--ide")
return &ffcli.Command{
Name: "pr",
FlagSet: fs,
ShortUsage: "pr <index>",
ShortHelp: "pr pulls down a pull request for testing",
Exec: func(ctx context.Context, args []string) error {
if err := h.checkInit(); err != nil {
return err
}
if len(args) == 0 {
return errors.New("pr requires an index")
}
idx := args[0]
branch := fmt.Sprintf("pr-%s", idx)
if err := h.run(ctx, "git", "fetch", "upstream", fmt.Sprintf("pull/%s/head:%s", idx, branch)); err != nil {
return err
}
if err := h.Branch().ParseAndRun(ctx, []string{"--base", branch, branch}); err != nil {
return err
}
if *ideFlag {
return h.IDE().ParseAndRun(ctx, []string{branch})
}
return nil
},
}
}