Skip to content

Commit 6666666

Browse files
committed
feat(cmd): add lucky command for commit analysis
- Add new `lucky` command to run lucky commit on HEAD - Add "lucky" alias to command aliases and symlink list Signed-off-by: kovacs <mritd@linux.com>
1 parent 6666666 commit 6666666

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

cmd/lucky.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}

consts/consts.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
const (
2222
CmdCommit = "ci"
2323
CmdPush = "ps"
24+
CmdLucky = "lucky"
2425
)
2526

2627
// CommitType represents a commit type with its name and description.
@@ -216,5 +217,6 @@ func SymlinkCommands() []string {
216217
Perf,
217218
Hotfix,
218219
Build,
220+
CmdLucky,
219221
}
220222
}

0 commit comments

Comments
 (0)