Something went wrong. Try again.
CLI that turns LLM input into well-formatted Conventional Commits
Something went wrong. Try again.
git-format trailers.go
1.5 kB · 68 lines
Go
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>//// SPDX-License-Identifier: AGPL-3.0-or-later
package main
import ( "fmt" "strings" "unicode")
func validateTrailer(trailer string) error { if trailer == "" { return fmt.Errorf("trailer cannot be empty") }
lines := strings.Split(trailer, "\n") firstLine := lines[0]
if len(firstLine) > 0 && unicode.IsSpace(rune(firstLine[0])) { return fmt.Errorf("trailer key cannot start with whitespace: %q", trailer) }
colonIdx := strings.Index(firstLine, ":") if colonIdx == -1 { return fmt.Errorf("trailer must contain ':' separator: %q", trailer) }
key := firstLine[:colonIdx]
if strings.TrimSpace(key) != key { return fmt.Errorf("trailer key cannot have leading or trailing whitespace: %q", key) }
if strings.ContainsAny(key, " \t") { return fmt.Errorf("trailer key cannot contain whitespace: %q", key) }
if key == "" { return fmt.Errorf("trailer key cannot be empty: %q", trailer) }
for i, line := range lines[1:] { if line == "" { continue } if len(line) > 0 && !unicode.IsSpace(rune(line[0])) { return fmt.Errorf("trailer continuation line %d must start with whitespace: %q", i+2, line) } }
return nil}
func buildTrailersBlock(trailers []string) (string, error) { if len(trailers) == 0 { return "", nil }
for _, trailer := range trailers { if err := validateTrailer(trailer); err != nil { return "", err } }
return strings.Join(trailers, "\n"), nil}