The parser in TrailerCodecService.decode() walks backwards from the bottom of the commit message to efficiently locate the trailer block. This is faster than scanning each line from the top because trailers are guaranteed to live at the end of the message.
- Normalize line endings to
\nand split the message. - Consume the title (the first line) and drop the optional blank line that separates it from the body.
- Walk backward with
_findTrailerStartIndexuntil either a non-matching line or an empty line appears; contiguousKey: Valuepatterns form the trailer block. - Validate the separator:
_validateTrailerSeparationensures there is a blank line before the trailers. Messages that omit the blank line now throwTrailerNoSeparatorError. - Trim the body without double allocations:
_trimBodytrims leading/trailing blank lines via index arithmetic and onejoin. - Parse trailers using the schema bundle’s
keyPattern, instantiating trailers via the injectedtrailerFactory.
The backward walk plus the blank-line guard ensure attackers cannot append arbitrary trailers to the body without the required separator.