44# This module is part of GitPython and is released under
55# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66import datetime
7- import re
8- from subprocess import Popen
7+ from subprocess import Popen , PIPE
98from gitdb import IStream
109from git .util import (
1110 hex_to_bin ,
1413 finalize_process
1514)
1615from git .diff import Diffable
16+ from git .cmd import Git
1717
1818from .tree import Tree
1919from . import base
@@ -322,10 +322,10 @@ def trailers(self) -> Dict:
322322
323323 Git messages can contain trailer information that are similar to RFC 822
324324 e-mail headers (see: https://git-scm.com/docs/git-interpret-trailers).
325-
326- The trailer is thereby the last paragraph (seperated by a empty line
327- from the subject/body). This trailer paragraph must contain a ``:`` as
328- seperator for key and value in every line .
325+
326+ This funcions calls ``git interpret-trailers --parse`` onto the message
327+ to extract the trailer information. The key value pairs are stripped of
328+ leading and trailing whitespaces before they get saved into a dictionary .
329329
330330 Valid message with trailer:
331331
@@ -338,20 +338,29 @@ def trailers(self) -> Dict:
338338 another information
339339
340340 key1: value1
341- key2: value2
341+ key2 : value 2 with inner spaces
342+
343+ dictionary will look like this:
344+ .. code-block::
345+
346+ {
347+ "key1": "value1",
348+ "key2": "value 2 with inner spaces"
349+ }
342350
343351 :return: Dictionary containing whitespace stripped trailer information
352+
344353 """
345- d : Dict [ str , str ] = {}
346- match = re . search ( r".+^\s*$\n([\w\n\s:]+?)\s*\Z" , str ( self . message ), re . MULTILINE | re . DOTALL )
347- if match is None :
348- return d
349- last_paragraph = match . group ( 1 )
350- if not all ( ':' in line for line in last_paragraph . split ( ' \n ' )):
351- return d
352- for line in last_paragraph .split ('\n ' ):
353- key , value = line .split (':' , 1 )
354- d [key .strip ()] = value .strip ()
354+ d = {}
355+ cmd = [ 'git' , 'interpret-trailers' , '--parse' ]
356+ proc : Git . AutoInterrupt = self . repo . git . execute ( cmd , as_process = True , istream = PIPE ) # type: ignore
357+ trailer : str = proc . communicate ( str ( self . message ). encode ())[ 0 ]. decode ()
358+ if trailer . endswith ( ' \n ' ):
359+ trailer = trailer [ 0 : - 1 ]
360+ if trailer != '' :
361+ for line in trailer .split ('\n ' ):
362+ key , value = line .split (':' , 1 )
363+ d [key .strip ()] = value .strip ()
355364 return d
356365
357366 @ classmethod
0 commit comments