Skip to content

Commit c26e58e

Browse files
committed
chore 🧹 (cmd): Add commit message customization options
Signed-off-by: Zine Moualhi 🇵🇸 <zmoualhi@outlook.com>
1 parent f5d58d2 commit c26e58e

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

cmd/draft.go

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515

1616
var (
1717
commitDirectly bool
18+
overrideType string
19+
overrideScope string
1820
)
1921

2022
var draftCmd = &cobra.Command{
@@ -65,17 +67,23 @@ var draftCmd = &cobra.Command{
6567
finalCommitMessage := commitMessage
6668
if !cfg.NoEmoji { // Check if emojis are enabled
6769
// Regex to parse: <type>(<optional scope>): <message>
68-
// Group 1: type (e.g., "feat")
69-
// Group 2: full scope part (e.g., "(cmd)" or empty string)
70-
// Group 3: message content (e.g., "Add AI-powered commit message generation")
71-
re := regexp.MustCompile(`^([a-zA-Z]+)(\([^)]*\))?:\s*(.*)$`) // Regex now captures the full scope part including parentheses
70+
re := regexp.MustCompile(`^([a-zA-Z]+)(\([^)]*\))?:\s*(.*)$`)
7271
matches := re.FindStringSubmatch(commitMessage)
7372

7473
if len(matches) > 0 {
7574
commitType := matches[1]
76-
fullScopePart := matches[2] // This will be "(cmd)" or ""
75+
fullScopePart := matches[2]
7776
messagePart := matches[3]
7877

78+
// Apply overrides
79+
if overrideType != "" {
80+
commitType = overrideType
81+
}
82+
83+
if overrideScope != "" {
84+
fullScopePart = "(" + overrideScope + ")"
85+
}
86+
7987
var emoji string
8088
for _, t := range cfg.Types {
8189
if t.Name == commitType {
@@ -101,6 +109,34 @@ var draftCmd = &cobra.Command{
101109
finalCommitMessage = builder.String()
102110
}
103111
}
112+
} else if overrideType != "" || overrideScope != "" {
113+
// Handle overrides when emojis are disabled
114+
re := regexp.MustCompile(`^([a-zA-Z]+)(\([^)]*\))?:\s*(.*)$`)
115+
matches := re.FindStringSubmatch(commitMessage)
116+
117+
if len(matches) > 0 {
118+
commitType := matches[1]
119+
fullScopePart := matches[2]
120+
messagePart := matches[3]
121+
122+
if overrideType != "" {
123+
commitType = overrideType
124+
}
125+
126+
if overrideScope != "" {
127+
fullScopePart = "(" + overrideScope + ")"
128+
}
129+
130+
var builder strings.Builder
131+
builder.WriteString(commitType)
132+
if fullScopePart != "" {
133+
builder.WriteString(fullScopePart)
134+
}
135+
builder.WriteString(": ")
136+
builder.WriteString(strings.TrimSpace(messagePart))
137+
138+
finalCommitMessage = builder.String()
139+
}
104140
}
105141
// --- End of modified logic ---
106142

@@ -126,4 +162,6 @@ var draftCmd = &cobra.Command{
126162
func init() {
127163
rootCmd.AddCommand(draftCmd)
128164
draftCmd.Flags().BoolVarP(&commitDirectly, "commit", "c", false, "Commit the generated message directly")
165+
draftCmd.Flags().StringVarP(&overrideType, "type", "t", "", "Override the commit type (e.g., feat, fix, docs)")
166+
draftCmd.Flags().StringVarP(&overrideScope, "scope", "s", "", "Override the commit scope (e.g., api, ui, core)")
129167
}

0 commit comments

Comments
 (0)