Class: GitAuto::Validators::CommitMessageValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/git_auto/validators/commit_message_validator.rb

Constant Summary collapse

HEADER_MAX_LENGTH =
72
TYPES =

Conventional commit types and their descriptions

{
  "feat" => "A new feature",
  "fix" => "A bug fix",
  "docs" => "Documentation only changes",
  "style" => "Changes that do not affect the meaning of the code",
  "refactor" => "A code change that neither fixes a bug nor adds a feature",
  "test" => "Adding missing tests or correcting existing tests",
  "chore" => "Changes to the build process or auxiliary tools",
  "perf" => "A code change that improves performance",
  "ci" => "Changes to CI configuration files and scripts",
  "build" => "Changes that affect the build system or external dependencies",
  "revert" => "Reverts a previous commit"
}.freeze
MINIMAL_COMMIT_PATTERN =
/
  ^(?<type>#{TYPES.keys.join("|")})           # Commit type
  :\s                                         # Colon and space separator
  (?<description>.+)                          # Commit description
/x
CONVENTIONAL_COMMIT_PATTERN =
%r{
  ^(?<type>#{TYPES.keys.join("|")})           # Commit type
  (\((?<scope>[a-z0-9/_-]+)\))?               # Optional scope in parentheses
  :\s                                         # Colon and space separator
  (?<description>.+)                          # Commit description
}x

Instance Method Summary collapse

Instance Method Details

#format_error(error) ⇒ Object



55
56
57
# File 'lib/git_auto/validators/commit_message_validator.rb', line 55

def format_error(error)
  "#{error}".red
end

#format_warning(warning) ⇒ Object



59
60
61
# File 'lib/git_auto/validators/commit_message_validator.rb', line 59

def format_warning(warning)
  "⚠️  #{warning}".yellow
end

#valid?(message) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/git_auto/validators/commit_message_validator.rb', line 50

def valid?(message)
  result = validate(message)
  result[:errors].empty?
end

#validate(message) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/git_auto/validators/commit_message_validator.rb', line 36

def validate(message)
  errors = []
  warnings = []

  # Skip validation if message is empty
  return { errors: ["Commit message cannot be empty"], warnings: [] } if message.nil? || message.strip.empty?

  header_result = validate_header(message)
  errors.concat(header_result[:errors])
  warnings.concat(header_result[:warnings])

  { errors: errors, warnings: warnings }
end