2022-08-23 17:04:13 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-03 21:58:02 +00:00
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-08-23 17:04:13 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-12-27 03:01:50 +00:00
|
|
|
"os/user"
|
2022-08-30 18:58:49 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2022-08-23 17:04:13 +00:00
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
"go.jolheiser.com/git-ea/config"
|
|
|
|
|
2022-08-23 19:38:41 +00:00
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
2022-09-03 21:58:02 +00:00
|
|
|
"github.com/peterbourgon/ff/v3/ffcli"
|
2022-08-23 19:38:41 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-08-23 17:04:13 +00:00
|
|
|
)
|
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
var Version = "x.y.z"
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
Config *config.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() (*ffcli.Command, error) {
|
|
|
|
cfg, err := config.Load()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := Handler{
|
|
|
|
Config: cfg,
|
|
|
|
}
|
|
|
|
|
|
|
|
fs := flag.NewFlagSet("git-ea", flag.ContinueOnError)
|
|
|
|
versionFlag := fs.Bool("version", false, "Print git-ea version")
|
|
|
|
fs.BoolVar(versionFlag, "v", *versionFlag, "--version")
|
|
|
|
|
2022-09-04 02:52:56 +00:00
|
|
|
c := &ffcli.Command{
|
2022-09-03 21:58:02 +00:00
|
|
|
Name: "git-ea",
|
|
|
|
FlagSet: fs,
|
|
|
|
ShortUsage: "git-ea <cmd>",
|
|
|
|
ShortHelp: "git-ea is the base command",
|
|
|
|
Subcommands: []*ffcli.Command{
|
|
|
|
handler.Cleanup(),
|
|
|
|
handler.Backport(),
|
|
|
|
handler.Branch(),
|
|
|
|
handler.Frontport(),
|
2022-09-04 02:06:06 +00:00
|
|
|
handler.IDE(),
|
2022-09-03 21:58:02 +00:00
|
|
|
handler.Init(),
|
2022-12-27 03:01:50 +00:00
|
|
|
handler.Post(),
|
2022-09-04 02:06:06 +00:00
|
|
|
handler.PR(),
|
2022-09-03 21:58:02 +00:00
|
|
|
},
|
2022-09-04 02:52:56 +00:00
|
|
|
}
|
|
|
|
c.Exec = func(_ context.Context, _ []string) error {
|
|
|
|
if *versionFlag {
|
|
|
|
log.Info().Msgf("git-ea v%s", Version)
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-03 21:58:02 +00:00
|
|
|
|
2022-09-04 02:52:56 +00:00
|
|
|
dir := cfg.Base
|
|
|
|
if fs.NArg() > 0 {
|
|
|
|
if strings.EqualFold(fs.Arg(0), "help") {
|
|
|
|
fmt.Println(ffcli.DefaultUsageFunc(c))
|
|
|
|
return nil
|
2022-09-03 21:58:02 +00:00
|
|
|
}
|
2022-09-04 02:52:56 +00:00
|
|
|
dir = cfg.WorkspaceBranch(fs.Arg(0))
|
|
|
|
}
|
2022-09-03 21:58:02 +00:00
|
|
|
|
2022-09-04 02:52:56 +00:00
|
|
|
fmt.Println(dir)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c, nil
|
2022-09-03 21:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) checkInit() error {
|
|
|
|
if !h.Config.IsInit() {
|
|
|
|
return errors.New("git-ea must be initialized first with `git ea init` in an appropriate directory")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) run(ctx context.Context, cmd string, args ...string) error {
|
|
|
|
return run(ctx, h.Config.Base, cmd, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(ctx context.Context, dir, cmd string, args ...string) error {
|
2022-08-23 17:04:13 +00:00
|
|
|
c := exec.CommandContext(ctx, cmd, args...)
|
2022-09-03 21:58:02 +00:00
|
|
|
c.Dir = dir
|
2022-08-23 17:04:13 +00:00
|
|
|
c.Stdout = os.Stdout
|
|
|
|
c.Stderr = os.Stderr
|
|
|
|
c.Stdin = os.Stdin
|
|
|
|
return c.Run()
|
|
|
|
}
|
2022-08-23 19:38:41 +00:00
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
func (h *Handler) fetch(ctx context.Context) {
|
|
|
|
if err := run(ctx, h.Config.Base, "git", "fetch", "upstream"); err != nil {
|
2022-08-23 19:38:41 +00:00
|
|
|
log.Err(err).Msg("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
func (h *Handler) repo() *git.Repository {
|
|
|
|
repo, err := git.PlainOpen(h.Config.Base)
|
2022-08-23 19:38:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("cannot open git repository")
|
|
|
|
}
|
|
|
|
return repo
|
|
|
|
}
|
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
func (h *Handler) worktree() *git.Worktree {
|
|
|
|
tree, err := h.repo().Worktree()
|
2022-08-23 19:38:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("cannot get git worktree")
|
|
|
|
}
|
|
|
|
return tree
|
|
|
|
}
|
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
func (h *Handler) head(name string) plumbing.Hash {
|
|
|
|
ref, err := h.repo().Reference(plumbing.NewRemoteReferenceName("upstream", name), false)
|
2022-08-23 19:38:41 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msgf("cannot get remote upstream %s HEAD", name)
|
|
|
|
}
|
|
|
|
return ref.Hash()
|
|
|
|
}
|
2022-08-30 18:58:49 +00:00
|
|
|
|
|
|
|
var releaseRe = regexp.MustCompile(`release/v1\.(\d+)`)
|
|
|
|
|
2022-09-03 21:58:02 +00:00
|
|
|
func (h *Handler) latestRelease() string {
|
2022-08-30 18:58:49 +00:00
|
|
|
cmd := exec.Command("git", "ls-remote", "upstream", "release/*")
|
2022-09-03 21:58:02 +00:00
|
|
|
cmd.Dir = h.Config.Base
|
2022-08-30 18:58:49 +00:00
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("could not get latest release")
|
|
|
|
}
|
|
|
|
matches := releaseRe.FindAllStringSubmatch(string(out), -1)
|
|
|
|
|
|
|
|
var latest string
|
|
|
|
for _, match := range matches {
|
|
|
|
m := match[1]
|
|
|
|
if len(m) < 3 {
|
|
|
|
m = strings.Repeat("0", 3-len(m)) + m
|
|
|
|
}
|
|
|
|
if m > latest {
|
|
|
|
latest = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimLeft(latest, "0")
|
|
|
|
}
|
2022-12-27 03:01:50 +00:00
|
|
|
|
|
|
|
func currentUser() string {
|
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return u.Username
|
|
|
|
}
|