Compare commits

..

2 Commits

Author SHA1 Message Date
jolheiser 190ea6c7f3
fix: add index to back/frontport
ci/woodpecker/push/goreleaser Pipeline was successful Details
ci/woodpecker/tag/goreleaser Pipeline was successful Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2023-01-10 21:57:47 -06:00
jolheiser fe0e2e5707
feat: tag command and force cleanup
ci/woodpecker/tag/goreleaser Pipeline was successful Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>
2022-12-27 22:40:45 -06:00
6 changed files with 130 additions and 8 deletions

View File

@ -85,7 +85,7 @@ func (h *Handler) Backport() *ffcli.Command {
index = m[1] index = m[1]
} }
branch := fmt.Sprintf("backport-%s", index) branch := fmt.Sprintf("backport-%s-%s", to, index)
base := fmt.Sprintf("upstream/release/v1.%s", to) base := fmt.Sprintf("upstream/release/v1.%s", to)
if err := h.Branch().ParseAndRun(ctx, []string{"--base", base, branch}); err != nil { if err := h.Branch().ParseAndRun(ctx, []string{"--base", base, branch}); err != nil {
return err return err

View File

@ -11,6 +11,8 @@ import (
func (h *Handler) Cleanup() *ffcli.Command { func (h *Handler) Cleanup() *ffcli.Command {
fs := flag.NewFlagSet("cleanup", flag.ContinueOnError) fs := flag.NewFlagSet("cleanup", flag.ContinueOnError)
forceFlag := fs.Bool("force", false, "Force cleanup")
fs.BoolVar(forceFlag, "f", *forceFlag, "--force")
return &ffcli.Command{ return &ffcli.Command{
Name: "cleanup", Name: "cleanup",
FlagSet: fs, FlagSet: fs,
@ -23,7 +25,7 @@ func (h *Handler) Cleanup() *ffcli.Command {
if len(args) > 0 { if len(args) > 0 {
for _, arg := range args { for _, arg := range args {
if err := removeWorktree(h, ctx, arg); err != nil { if err := removeWorktree(h, ctx, arg, *forceFlag); err != nil {
return err return err
} }
} }
@ -48,7 +50,7 @@ func (h *Handler) Cleanup() *ffcli.Command {
} }
for _, rm := range remove { for _, rm := range remove {
if err := removeWorktree(h, ctx, rm); err != nil { if err := removeWorktree(h, ctx, rm, *forceFlag); err != nil {
return err return err
} }
} }
@ -58,8 +60,13 @@ func (h *Handler) Cleanup() *ffcli.Command {
} }
} }
func removeWorktree(h *Handler, ctx context.Context, name string) error { func removeWorktree(h *Handler, ctx context.Context, name string, force bool) error {
if err := h.run(ctx, "git", "worktree", "remove", name); err != nil { args := []string{"worktree", "remove"}
if force {
args = append(args, "--force")
}
args = append(args, name)
if err := h.run(ctx, "git", args...); err != nil {
return nil return nil
} }
return h.run(ctx, "git", "branch", "-D", name) return h.run(ctx, "git", "branch", "-D", name)

View File

@ -53,6 +53,7 @@ func New() (*ffcli.Command, error) {
handler.Init(), handler.Init(),
handler.Post(), handler.Post(),
handler.PR(), handler.PR(),
handler.Tag(),
}, },
} }
c.Exec = func(_ context.Context, _ []string) error { c.Exec = func(_ context.Context, _ []string) error {

View File

@ -70,7 +70,7 @@ func (h *Handler) Frontport() *ffcli.Command {
index = m[1] index = m[1]
} }
branch := fmt.Sprintf("frontport-%s", index) branch := fmt.Sprintf("frontport-%s-%s", from, index)
base := *toFlag base := *toFlag
if base == "" { if base == "" {

View File

@ -3,10 +3,11 @@ package cmd
import ( import (
"context" "context"
"flag" "flag"
"github.com/AlecAivazis/survey/v2"
"github.com/peterbourgon/ff/v3/ffcli"
"os" "os"
"os/exec" "os/exec"
"github.com/AlecAivazis/survey/v2"
"github.com/peterbourgon/ff/v3/ffcli"
) )
func (h *Handler) IDE() *ffcli.Command { func (h *Handler) IDE() *ffcli.Command {

113
cmd/tag.go 100644
View File

@ -0,0 +1,113 @@
package cmd
import (
"context"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/go-git/go-git/v5"
"github.com/peterbourgon/ff/v3/ffcli"
)
var (
treeURL = "https://github.com/go-gitea/gitea/tree/%s"
versionRe = regexp.MustCompile(`v\d+\.\d+\.\d+`)
)
func (h *Handler) Tag() *ffcli.Command {
fs := flag.NewFlagSet("tag", flag.ContinueOnError)
return &ffcli.Command{
Name: "tag",
FlagSet: fs,
ShortUsage: "tag <branch>",
ShortHelp: "tag makes a signed tag for `branch`",
Exec: func(ctx context.Context, args []string) error {
if err := h.checkInit(); err != nil {
return err
}
h.fetch(ctx)
var branch string
if len(args) > 0 {
branch = args[0]
}
if branch == "" {
var branches []string
remote, err := h.repo().Remote("upstream")
if err != nil {
return err
}
refs, err := remote.List(&git.ListOptions{})
if err != nil {
return err
}
for _, ref := range refs {
if ref.Name().IsBranch() {
branches = append(branches, strings.TrimPrefix(ref.Name().String(), "refs/heads/"))
}
}
sort.Strings(branches)
if err := survey.AskOne(&survey.Select{
Message: "Branch",
Options: branches,
}, &branch, survey.WithValidator(survey.Required)); err != nil {
return err
}
}
if !strings.HasPrefix(branch, "release") {
if !strings.HasPrefix(branch, "v") {
branch = fmt.Sprintf("v%s", branch)
}
branch = fmt.Sprintf("release/%s", branch)
}
var version string
if err := survey.AskOne(&survey.Input{
Message: "Version",
}, &version, survey.WithValidator(func(ans any) error {
if !versionRe.MatchString(fmt.Sprint(ans)) {
return errors.New("version isn't valid v$maj.$min.$")
}
return nil
})); err != nil {
return err
}
workspaceName := strings.ReplaceAll(branch, "/", "-")
if err := h.Branch().ParseAndRun(ctx, []string{"--base", branch, workspaceName}); err != nil {
return err
}
var changelog string
if err := survey.AskOne(&survey.Editor{
Message: "Changelog",
FileName: "*.md",
}, &changelog, survey.WithValidator(survey.Required)); err != nil {
return err
}
fi, err := os.Create(filepath.Join(h.Config.WorkspaceBranch(workspaceName), "release.notes"))
if err != nil {
return err
}
if _, err := fi.WriteString(changelog); err != nil {
return err
}
if err := fi.Close(); err != nil {
return err
}
run(ctx, h.Config.WorkspaceBranch(workspaceName), "git", "tag", "-s", "-F", "release.notes", version)
fmt.Printf("cd %s && git push upstream %s \n", h.Config.WorkspaceBranch(workspaceName), version)
return nil
},
}
}