-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcommit_message.go
More file actions
189 lines (161 loc) · 4.9 KB
/
commit_message.go
File metadata and controls
189 lines (161 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package cmd
import (
"fmt"
"os"
"strings"
"github.com/marcus/td/internal/config"
"github.com/marcus/td/internal/db"
"github.com/marcus/td/internal/git"
"github.com/marcus/td/internal/models"
"github.com/marcus/td/internal/output"
"github.com/spf13/cobra"
)
var commitMessageCmd = &cobra.Command{
Use: "commit-message [summary]",
Aliases: []string{"commit-msg"},
Short: "Normalize a commit subject for the current td issue",
Long: `Normalize a commit subject to td's conventional format:
<type>[optional !]: <summary> (td-<id>)
The issue ID comes from --issue, a trailing (td-<id>) suffix already present in
the subject, or the focused issue. When no issue is available, only typed
docs/test/chore/ci subjects can stay no-issue commits. When
--file is set, td rewrites only the first line of the commit message file in
place and preserves the body/trailers. Git-generated merge, revert, and
autosquash subjects are left unchanged, and existing breaking-change markers
such as feat!: or fix(scope)!: are preserved.`,
Example: ` td commit-message "normalize commit hook docs"
td commit-message --issue td-a1b2 "normalize commit hook docs"
td commit-message --type docs "Update changelog for v0.43.0"
td commit-message --file .git/COMMIT_EDITMSG`,
GroupID: "system",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
baseDir := getBaseDir()
filePath, _ := cmd.Flags().GetString("file")
if filePath != "" && len(args) > 0 {
return fmt.Errorf("summary argument cannot be used with --file")
}
if filePath == "" && len(args) == 0 {
return fmt.Errorf(`summary required. Use: td commit-message [--issue <id>] "summary"`)
}
subject, err := commitMessageSubject(args, filePath)
if err != nil {
output.Error("%v", err)
return err
}
if git.ShouldSkipCommitMessageNormalization(subject) {
if filePath == "" {
_, _ = fmt.Fprintln(cmd.OutOrStdout(), strings.TrimSpace(subject))
}
return nil
}
issueID, issueType, err := resolveCommitMessageContext(baseDir, cmd, subject)
if err != nil {
output.Error("%v", err)
return err
}
explicitType, _ := cmd.Flags().GetString("type")
opts := git.CommitMessageOptions{
IssueID: issueID,
IssueType: issueType,
Type: git.CommitType(explicitType),
}
if filePath != "" {
if err := git.RewriteCommitMessageFile(filePath, opts); err != nil {
output.Error("%v", err)
return err
}
return nil
}
normalized, err := git.NormalizeCommitSubject(subject, opts)
if err != nil {
output.Error("%v", err)
return err
}
_, _ = fmt.Fprintln(cmd.OutOrStdout(), normalized)
return nil
},
}
func commitMessageSubject(args []string, filePath string) (string, error) {
if filePath == "" {
return args[0], nil
}
data, err := os.ReadFile(filePath)
if err != nil {
return "", err
}
message := string(data)
if idx := strings.Index(message, "\n"); idx >= 0 {
return strings.TrimSuffix(message[:idx], "\r"), nil
}
return message, nil
}
func resolveCommitMessageContext(baseDir string, cmd *cobra.Command, subject string) (string, models.Type, error) {
issueFlag, _ := cmd.Flags().GetString("issue")
issueID, err := normalizeCommitMessageIssueRef(baseDir, issueFlag)
if err != nil {
return "", "", err
}
if issueID == "" {
issueID, err = git.ExtractCommitIssueID(subject)
if err != nil {
return "", "", err
}
}
explicitType, _ := cmd.Flags().GetString("type")
if issueID == "" && strings.TrimSpace(explicitType) != "" {
commitType, err := git.NormalizeCommitType(explicitType)
if err != nil {
return "", "", err
}
if git.CommitTypeAllowsNoIssue(commitType) {
return "", "", nil
}
}
if issueID == "" {
focusedID, err := config.GetFocus(baseDir)
if err != nil {
return "", "", err
}
issueID, err = git.NormalizeCommitIssueID(focusedID)
if err != nil {
return "", "", err
}
}
if issueID == "" {
return "", "", nil
}
database, err := db.Open(baseDir)
if err != nil {
return "", "", err
}
defer database.Close()
issue, err := database.GetIssue(issueID)
if err != nil {
return "", "", err
}
return issueID, issue.Type, nil
}
func normalizeCommitMessageIssueRef(baseDir, raw string) (string, error) {
trimmed := strings.TrimSpace(raw)
if trimmed == "" {
return "", nil
}
if trimmed == "." {
focusedID, err := config.GetFocus(baseDir)
if err != nil {
return "", err
}
if strings.TrimSpace(focusedID) == "" {
return "", fmt.Errorf("no focused issue available for --issue .")
}
trimmed = focusedID
}
return git.NormalizeCommitIssueID(trimmed)
}
func init() {
rootCmd.AddCommand(commitMessageCmd)
commitMessageCmd.Flags().StringP("issue", "i", "", "Issue ID (default: subject suffix or focused issue)")
commitMessageCmd.Flags().StringP("type", "t", "", "Commit type override (feat, fix, docs, test, chore, ci)")
commitMessageCmd.Flags().StringP("file", "f", "", "Rewrite a commit message file in place")
}