git-ea/cmd/branch.go

48 lines
1.1 KiB
Go

package cmd
import (
"context"
"errors"
"flag"
"fmt"
"path/filepath"
"strings"
"github.com/peterbourgon/ff/v3/ffcli"
)
func (h *Handler) Branch() *ffcli.Command {
fs := flag.NewFlagSet("branch", flag.ContinueOnError)
noFetchFlag := fs.Bool("no-fetch", false, "Skip fetching")
fs.BoolVar(noFetchFlag, "nf", *noFetchFlag, "--no-fetch")
baseFlag := fs.String("base", "main", "Ref to base from")
fs.StringVar(baseFlag, "b", *baseFlag, "--base")
return &ffcli.Command{
Name: "branch",
FlagSet: fs,
ShortUsage: "branch --base [ref=main] <name>",
ShortHelp: "branch creates a new branch called `name` based on `base`",
Exec: func(ctx context.Context, args []string) error {
if err := h.checkInit(); err != nil {
return err
}
if len(args) < 1 {
return errors.New("branch requires a name")
}
name := args[0]
base := *baseFlag
if !strings.HasPrefix(base, "upstream") {
base = fmt.Sprintf("upstream/%s", base)
}
if !*noFetchFlag {
h.fetch(ctx)
}
return h.run(ctx, "git", "worktree", "add", "-B", name, filepath.Join(h.Config.Workspace(), name), base)
},
}
}