|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | + |
| 8 | + "github.com/mritd/gitflow-toolkit/v3/consts" |
| 9 | + "github.com/mritd/gitflow-toolkit/v3/internal/git" |
| 10 | + "github.com/mritd/gitflow-toolkit/v3/internal/ui/common" |
| 11 | +) |
| 12 | + |
| 13 | +// luckyCmd represents the lucky commit command. |
| 14 | +var luckyCmd = &cobra.Command{ |
| 15 | + Use: consts.CmdLucky, |
| 16 | + Short: "Run lucky commit on the current HEAD", |
| 17 | + Long: `Re-run lucky commit on the current HEAD commit. |
| 18 | +
|
| 19 | +This modifies the latest commit's hash to start with the configured prefix. |
| 20 | +Useful when lucky commit was skipped or failed during the commit flow. |
| 21 | +
|
| 22 | +Configure the prefix with: |
| 23 | + git config gitflow.lucky-commit-prefix <hex-prefix>`, |
| 24 | + RunE: runLucky, |
| 25 | +} |
| 26 | + |
| 27 | +func init() { |
| 28 | + rootCmd.AddCommand(luckyCmd) |
| 29 | +} |
| 30 | + |
| 31 | +func runLucky(cmd *cobra.Command, _ []string) error { |
| 32 | + // Check lucky commit configuration |
| 33 | + rawPrefix := git.GetLuckyPrefix() |
| 34 | + if rawPrefix == "" { |
| 35 | + return renderError(cmd, "Lucky commit", fmt.Errorf("no prefix configured, set it with: git config gitflow.lucky-commit-prefix <hex>")) |
| 36 | + } |
| 37 | + |
| 38 | + prefix, err := git.ValidateLuckyPrefix(rawPrefix) |
| 39 | + if err != nil { |
| 40 | + return renderError(cmd, "Lucky commit", err) |
| 41 | + } |
| 42 | + |
| 43 | + if err := git.CheckLuckyCommit(); err != nil { |
| 44 | + return renderError(cmd, "Lucky commit", err) |
| 45 | + } |
| 46 | + |
| 47 | + // Run lucky commit on current HEAD |
| 48 | + luckyCmd := git.LuckyCommitCmd(prefix) |
| 49 | + result := common.RunLuckyCommit(prefix, luckyCmd, git.GetHeadHash) |
| 50 | + |
| 51 | + if result.Cancelled { |
| 52 | + r := common.Warning("Lucky commit skipped", "Operation was cancelled by user.") |
| 53 | + fmt.Print(common.RenderResult(r)) |
| 54 | + return nil |
| 55 | + } |
| 56 | + |
| 57 | + if result.Err != nil { |
| 58 | + return renderError(cmd, "Lucky commit failed", result.Err) |
| 59 | + } |
| 60 | + |
| 61 | + // Show success with hash |
| 62 | + content := common.StyleMuted.Render("Hash: " + result.Hash) |
| 63 | + r := common.Success("Lucky commit done", content) |
| 64 | + fmt.Print(common.RenderResult(r)) |
| 65 | + return nil |
| 66 | +} |
0 commit comments