git-ea/cmd/backport.go

89 lines
2.3 KiB
Go
Raw Normal View History

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"
"github.com/rs/zerolog/log"
"github.com/skratchdot/open-golang/open"
)
var (
backportFS = flag.NewFlagSet("backport", flag.ContinueOnError)
backportFromFlag = backportFS.String("from", "", "Release to backport from (ex: `main`, default: main)")
backportToFlag = backportFS.String("to", "", "Release to backport to (ex: `1.17`, default: <latest>)")
backportListFlag = backportFS.Bool("list", false, "Open repository to see needed backports")
Backport = &ffcli.Command{
Name: "backport",
FlagSet: backportFS,
ShortUsage: "backport --from [release=main] --to [release=latest]",
ShortHelp: "backport cherry-picks a <commit> and applies it to a clean branch based on <release>",
Exec: func(ctx context.Context, _ []string) error {
to := *backportToFlag
if to == "" {
to = latestRelease()
}
if *backportListFlag {
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
}
fetch(ctx)
if !isClean() {
log.Fatal().Msg("working tree is dirty")
}
from := *backportFromFlag
if from == "" {
from = "main"
}
commits, err := repo().Log(&git.LogOptions{
From: head(from),
})
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)
base := fmt.Sprintf("upstream/release/v1.%s", to)
if err := Branch.ParseAndRun(ctx, []string{"--base", base, branch}); err != nil {
return err
}
return run(ctx, "git", "cherry-pick", hash.String())
},
}
)