|
2 | 2 | package mk
|
3 | 3 |
|
4 | 4 | import (
|
| 5 | + "fmt" |
| 6 | + |
5 | 7 | "github.com/jason-dour/git-wt/internal/cmn"
|
| 8 | + "github.com/jason-dour/git-wt/internal/git" |
6 | 9 | "github.com/spf13/cobra"
|
7 | 10 | )
|
8 | 11 |
|
9 | 12 | var (
|
10 |
| - command = "mk" // Command name. |
11 |
| - Cmd = &cobra.Command{ |
12 |
| - Use: command + " repo_url", |
| 13 | + command = "mk" // Command name. |
| 14 | + config *cmn.CfgMk = &cmn.CfgMk{} // Configuration for the command. |
| 15 | + Cmd = &cobra.Command{ |
| 16 | + Use: command + " worktree_name commit-ish", |
13 | 17 | Short: "Add a worktree to the project.",
|
14 | 18 | Long: cmn.Basename + " " + command + " - Add a worktree to the project.",
|
15 |
| - Args: cobra.ExactArgs(1), |
| 19 | + Args: cobra.ExactArgs(2), |
16 | 20 | Aliases: []string{"make"},
|
17 | 21 | RunE: run,
|
18 |
| - } // Cobra command definition for the 'clone' command. |
| 22 | + } // Cobra command definition for the 'mk' command. |
19 | 23 | )
|
20 | 24 |
|
| 25 | +// init performs initialization for the 'mk' command. |
| 26 | +func init() { |
| 27 | + Cmd.PersistentFlags().BoolVar(&config.Track, "track", false, "set up tracking mode") |
| 28 | + Cmd.PersistentFlags().StringVarP(&config.Branch, "b", "b", "", "create a new branch") |
| 29 | + Cmd.PersistentFlags().StringVarP(&config.BranchReset, "B", "B", "", "create or reset a branch") |
| 30 | + Cmd.PersistentFlags().BoolVar(&config.CheckoutNo, "no-checkout", false, "do not populate the new worktree") |
| 31 | + Cmd.PersistentFlags().BoolVarP(&config.Force, "force", "f", false, "checkout <branch> even if already checked out in other worktree") |
| 32 | + Cmd.PersistentFlags().BoolVarP(&config.Quiet, "quiet", "q", false, "suppress progress reporting") |
| 33 | +} |
| 34 | + |
| 35 | +// checkConfig scans config for proper use of flags. |
| 36 | +func checkConfig(args []string) error { |
| 37 | + funcName := "checkConfig" |
| 38 | + cmn.Debug("%s: %s: begin\n", command, funcName) |
| 39 | + |
| 40 | + cmn.Debug("%s: %s: check mutually exclusive branch flags\n", command, funcName) |
| 41 | + if len(config.Branch) > 0 && len(config.BranchReset) > 0 { |
| 42 | + return fmt.Errorf("config: set branch with either -b or -B; don't use both") |
| 43 | + } |
| 44 | + |
| 45 | + cmn.Debug("%s: %s: check track has a new branch in flags\n", command, funcName) |
| 46 | + if config.Track && !(len(config.Branch) > 0 || len(config.BranchReset) > 0) { |
| 47 | + return fmt.Errorf("config: track requires new branch via -b or -B") |
| 48 | + } |
| 49 | + |
| 50 | + cmn.Debug("%s: %s: end\n", command, funcName) |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +// run provides the core execution of the 'mk' command. |
21 | 55 | func run(cmd *cobra.Command, args []string) error {
|
22 | 56 | funcName := "run"
|
23 | 57 | cmn.Debug("%s: %s: begin\n", command, funcName)
|
| 58 | + cmn.Debug("%s: %s: config: %#v\n", command, funcName, config) |
24 | 59 | cmn.Debug("%s: %s: args: %v\n", command, funcName, args)
|
25 | 60 |
|
| 61 | + // Set the worktree name. |
| 62 | + wtName := args[0] |
| 63 | + cmn.Debug("%s: %s: worktree name: %s\n", command, funcName, wtName) |
| 64 | + |
| 65 | + // Set the commit-ish to be used. |
| 66 | + commitish := args[1] |
| 67 | + cmn.Debug("%s: %s: commit-ish: %s\n", command, funcName, commitish) |
| 68 | + |
| 69 | + // Check configuration. |
| 70 | + err := checkConfig(args) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + // Add the worktree. |
| 76 | + output, err := git.WorktreeAdd(config, wtName, commitish) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + fmt.Print(string(output)) |
| 81 | + |
26 | 82 | cmn.Debug("%s: %s: end\n", command, funcName)
|
27 | 83 | return nil
|
28 | 84 | }
|
0 commit comments