@@ -154,6 +154,46 @@ def _checked_regex(pattern: str, setting: str) -> str:
154154 return pattern
155155
156156
157+ def _string_setting (value : Any , setting : str ) -> str :
158+ """*value*, once it is known to be a string, stripped.
159+
160+ A number or a boolean used to reach ``.strip()`` and come back from
161+ main's catch-all as "'int' object has no attribute 'strip'" under exit
162+ code 1, the code a rejected commit gets.
163+ """
164+ if not isinstance (value , str ):
165+ raise ConfigError (f"{ setting } must be a string, got { value !r} " , setting = setting )
166+ return value .strip ()
167+
168+
169+ def _string_list (
170+ section : dict [str , Any ], key : str , default : list [str ], setting : str
171+ ) -> list [str ]:
172+ """``section[key]`` as a list of strings, a comma-separated string split.
173+
174+ ``--allow-commit-types`` and ``CCHK_ALLOW_COMMIT_TYPES`` already split
175+ on commas; the same string in the TOML file was iterated letter by
176+ letter, so ``"feat,fix"`` allowed a type called ``f``. Anything else
177+ that is not a list of strings is refused rather than guessed at.
178+ """
179+ value = section .get (key , default )
180+ # The built-in default needs no checking, and this runs on every rule
181+ # set built, so it is returned as is.
182+ if value is default :
183+ return value
184+ if isinstance (value , list ):
185+ for item in value :
186+ if not isinstance (item , str ):
187+ break
188+ else :
189+ return value
190+ elif isinstance (value , str ):
191+ return [item .strip () for item in value .split ("," ) if item .strip ()]
192+ raise ConfigError (
193+ f"{ setting } must be a list of strings, got { value !r} " , setting = setting
194+ )
195+
196+
157197class RuleBuilder :
158198 """Builds validation rules from config and catalog entries."""
159199
@@ -446,7 +486,9 @@ def _build_conventional_commit_rule(
446486 enforce custom formats such as JIRA smart commits
447487 (``PROJ-123: description``).
448488 """
449- custom_pattern = self .commit_config .get ("message_pattern" , "" ).strip ()
489+ custom_pattern = _string_setting (
490+ self .commit_config .get ("message_pattern" , "" ), "[commit] message_pattern"
491+ )
450492 if custom_pattern :
451493 custom_pattern = _checked_regex (custom_pattern , "[commit] message_pattern" )
452494 # A team that opted out of Conventional Commits must not be told
@@ -507,8 +549,14 @@ def _build_length_rule(
507549 ) -> ValidationRule | None :
508550 """Build subject length validation rule."""
509551 length = self .commit_config .get (config_key )
510- if not isinstance ( length , int ) :
552+ if length is None :
511553 return None
554+ # bool is an int: ``true`` used to become a limit of 1 character.
555+ if isinstance (length , bool ) or not isinstance (length , int ):
556+ setting = f"[commit] { config_key } "
557+ raise ConfigError (
558+ f"{ setting } must be a whole number, got { length !r} " , setting = setting
559+ )
512560
513561 # The suggestion is templated on the same values as the error: naming
514562 # the configured length is the whole point of the advice, and "the
@@ -548,10 +596,10 @@ def _build_author_pattern_rule(
548596 ) -> ValidationRule | None :
549597 """Build author name or email validation rule."""
550598 regex = catalog_entry .regex
551- if self . commit_config . get ( config_key , "" ):
552- regex = _checked_regex (
553- self . commit_config . get ( config_key , "" ). strip (), f"[commit] { config_key } "
554- )
599+ setting = f"[commit] { config_key } "
600+ configured = _string_setting ( self . commit_config . get ( config_key , "" ), setting )
601+ if configured :
602+ regex = _checked_regex ( configured , setting )
555603
556604 return ValidationRule (
557605 check = catalog_entry .check ,
@@ -720,17 +768,32 @@ def _build_boolean_rule(
720768
721769 def _get_allowed_commit_types (self ) -> list [str ]:
722770 """Get deduplicated list of allowed commit types."""
723- types = self .commit_config .get ("allow_commit_types" , DEFAULT_COMMIT_TYPES )
771+ types = _string_list (
772+ self .commit_config ,
773+ "allow_commit_types" ,
774+ DEFAULT_COMMIT_TYPES ,
775+ "[commit] allow_commit_types" ,
776+ )
724777 return list (dict .fromkeys (types )) # Preserve order, remove duplicates
725778
726779 def _get_allowed_branch_types (self ) -> list [str ]:
727780 """Get deduplicated list of allowed branch types."""
728- types = self .branch_config .get ("allow_branch_types" , DEFAULT_BRANCH_TYPES )
781+ types = _string_list (
782+ self .branch_config ,
783+ "allow_branch_types" ,
784+ DEFAULT_BRANCH_TYPES ,
785+ "[branch] allow_branch_types" ,
786+ )
729787 return list (dict .fromkeys (types )) # Preserve order, remove duplicates
730788
731789 def _get_allowed_branch_names (self ) -> list [str ]:
732790 """Get deduplicated list of allowed branch names."""
733- names = self .branch_config .get ("allow_branch_names" , DEFAULT_BRANCH_NAMES )
791+ names = _string_list (
792+ self .branch_config ,
793+ "allow_branch_names" ,
794+ DEFAULT_BRANCH_NAMES ,
795+ "[branch] allow_branch_names" ,
796+ )
734797 return list (dict .fromkeys (names )) # Preserve order, remove duplicates
735798
736799 def _build_conventional_commit_regex (self , allowed_types : list [str ]) -> str :
0 commit comments