git-ea/cmd/branch.go

42 lines
878 B
Go

package cmd
import (
"context"
"errors"
"flag"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/rs/zerolog/log"
)
var (
branchFS = flag.NewFlagSet("branch", flag.ContinueOnError)
branchNoFetch = branchFS.Bool("no-fetch", false, "Skip fetching")
Branch = &ffcli.Command{
Name: "branch",
FlagSet: branchFS,
ShortUsage: "branch <name> [base=main]",
ShortHelp: "branch creates a new branch called <name> based on [base]",
Exec: func(ctx context.Context, args []string) error {
if len(args) < 1 {
return errors.New("branch requires at least one argument")
}
name, base := args[0], "upstream/main"
if len(args) > 1 {
base = args[1]
}
if !isClean() {
log.Fatal().Msg("working tree is dirty")
}
if !*branchNoFetch {
fetch(ctx)
}
return run(ctx, "git", "checkout", "-b", name, base)
},
}
)