2022-08-23 19:38:41 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
|
|
|
"github.com/peterbourgon/ff/v3/ffcli"
|
2022-08-30 18:58:49 +00:00
|
|
|
"github.com/skratchdot/open-golang/open"
|
2022-08-23 19:38:41 +00:00
|
|
|
)
|
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
func (h *Handler) Backport() *ffcli.Command {
|
|
|
|
fs := flag.NewFlagSet("backport", flag.ContinueOnError)
|
|
|
|
fromFlag := fs.String("from", "", "Release to backport from (ex: `main`, default: main)")
|
|
|
|
fs.StringVar(fromFlag, "f", *fromFlag, "--from")
|
|
|
|
toFlag := fs.String("to", "", "Release to backport to (ex: `1.17`, default: `latest`)")
|
|
|
|
fs.StringVar(toFlag, "t", *toFlag, "--to")
|
|
|
|
listFlag := fs.Bool("list", false, "Open repository to see needed backports")
|
|
|
|
fs.BoolVar(listFlag, "l", *listFlag, "--list")
|
|
|
|
return &ffcli.Command{
|
2022-08-23 19:38:41 +00:00
|
|
|
Name: "backport",
|
2022-09-03 21:58:02 +00:00
|
|
|
FlagSet: fs,
|
2022-08-30 18:58:49 +00:00
|
|
|
ShortUsage: "backport --from [release=main] --to [release=latest]",
|
2022-09-03 21:58:02 +00:00
|
|
|
ShortHelp: "backport cherry-picks a commit and applies it to a clean branch based on `release`",
|
2022-08-30 18:58:49 +00:00
|
|
|
Exec: func(ctx context.Context, _ []string) error {
|
2022-09-03 21:58:02 +00:00
|
|
|
if err := h.checkInit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
to := *toFlag
|
2022-08-30 18:58:49 +00:00
|
|
|
if to == "" {
|
2022-09-03 21:58:02 +00:00
|
|
|
to = h.latestRelease()
|
2022-08-30 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
if *listFlag {
|
2022-08-30 18:58:49 +00:00
|
|
|
if err := open.Run(fmt.Sprintf("https://github.com/go-gitea/gitea/pulls?q=is%%3Apr+-label%%3Abackport%%2Fdone+label%%3Abackport%%2Fv1.%s+is%%3Amerged", to)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-08-23 19:38:41 +00:00
|
|
|
}
|
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
h.fetch(ctx)
|
2022-08-23 19:38:41 +00:00
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
from := *fromFlag
|
2022-08-30 18:58:49 +00:00
|
|
|
if from == "" {
|
|
|
|
from = "main"
|
|
|
|
}
|
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
commits, err := h.repo().Log(&git.LogOptions{
|
|
|
|
From: h.head(from),
|
2022-08-23 19:38:41 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
optMap := make(map[string]plumbing.Hash)
|
|
|
|
var opts []string
|
|
|
|
if err := commits.ForEach(func(c *object.Commit) error {
|
|
|
|
title := strings.Split(c.Message, "\n")[0]
|
|
|
|
opts = append(opts, title)
|
|
|
|
optMap[title] = c.Hash
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp string
|
|
|
|
if err := survey.AskOne(&survey.Select{
|
|
|
|
Message: "Commit to cherry-pick",
|
|
|
|
Options: opts,
|
|
|
|
}, &resp, survey.WithValidator(survey.Required)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := optMap[resp]
|
|
|
|
branch := fmt.Sprintf("backport-%s", hash)
|
2022-08-30 18:58:49 +00:00
|
|
|
base := fmt.Sprintf("upstream/release/v1.%s", to)
|
2022-09-03 21:58:02 +00:00
|
|
|
if err := h.Branch().ParseAndRun(ctx, []string{"--base", base, branch}); err != nil {
|
2022-08-23 19:38:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
return run(ctx, h.Config.WorkspaceBranch(branch), "git", "cherry-pick", hash.String())
|
2022-08-23 19:38:41 +00:00
|
|
|
},
|
|
|
|
}
|
2022-09-03 21:58:02 +00:00
|
|
|
}
|