From 29fbff3e7da8fc2d2029ac7593096fede1670e0c Mon Sep 17 00:00:00 2001 From: Amolith Date: Tue, 21 Oct 2025 17:45:45 -0600 Subject: [PATCH] feat: add subject validation and length checking Add buildAndValidateSubject function to construct commit subjects in conventional commit format and validate they don't exceed 50 characters. Truncated subjects show exceeding portion with ellipsis in error output. Implements: bug-5b35298 Co-authored-by: Crush --- main.go | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index d9ee8873a467b99fbafdbe2e56db8035e0b5ec79..30e4975fdd84e0e6f097b5b081710edfa49a3162 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,10 @@ package main import ( "context" + "fmt" "os" "runtime/debug" + "strings" "github.com/charmbracelet/fang" "github.com/spf13/cobra" @@ -47,11 +49,12 @@ formatted-commit -t refactor -s "web/git-bug" -m "fancy shmancy" \ -b "Had to do a weird thing because..." `, RunE: func(cmd *cobra.Command, args []string) error { - // TODO: Implement commit formatting logic here - // 1. Validate subject length (type(scope): message <= 50 chars) - // 2. Format body as Markdown wrapped to 72 columns - // 3. Validate and format trailers - // 4. Pipe result to `git commit -F -` + subject, err := buildAndValidateSubject(commitType, scope, message, breakingChange) + if err != nil { + return err + } + + _ = subject return nil }, @@ -73,6 +76,36 @@ func init() { } } +func buildAndValidateSubject(commitType, scope, message string, breaking bool) (string, error) { + var subject strings.Builder + + subject.WriteString(commitType) + + if scope != "" { + subject.WriteString("(") + subject.WriteString(scope) + subject.WriteString(")") + } + + if breaking { + subject.WriteString("!") + } + + subject.WriteString(": ") + subject.WriteString(message) + + result := subject.String() + length := len(result) + + if length > 50 { + exceededBy := length - 50 + truncated := result[:50] + "…" + return "", fmt.Errorf("subject exceeds 50 character limit by %d:\n%s", exceededBy, truncated) + } + + return result, nil +} + func main() { ctx := context.Background()