Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitedbf76f

Browse files
Peter KempterByron
Peter Kempter
authored andcommitted
Add trailer as commit property
With the command `git interpret-trailers` git provides a way to interactwith trailer lines in the commit messages that look similar to RFC 822e-mail headers (see:https://git-scm.com/docs/git-interpret-trailers).The new property returns those parsed trailer lines from the message asdictionary.
1 parentcdf7ffc commitedbf76f

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

‎git/objects/commit.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# This module is part of GitPython and is released under
55
# the BSD License: http://www.opensource.org/licenses/bsd-license.php
66
importdatetime
7+
importre
78
fromsubprocessimportPopen
89
fromgitdbimportIStream
910
fromgit.utilimport (
@@ -39,7 +40,7 @@
3940

4041
# typing ------------------------------------------------------------------
4142

42-
fromtypingimportAny,IO,Iterator,List,Sequence,Tuple,Union,TYPE_CHECKING,cast
43+
fromtypingimportAny,IO,Iterator,List,Sequence,Tuple,Union,TYPE_CHECKING,cast,Dict
4344

4445
fromgit.typesimportPathLike,Literal
4546

@@ -315,6 +316,44 @@ def stats(self) -> Stats:
315316
text=self.repo.git.diff(self.parents[0].hexsha,self.hexsha,'--',numstat=True)
316317
returnStats._list_from_string(self.repo,text)
317318

319+
@property
320+
deftrailers(self)->Dict:
321+
"""Get the trailers of the message as dictionary
322+
323+
Git messages can contain trailer information that are similar to RFC 822
324+
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.
329+
330+
Valid message with trailer:
331+
332+
.. code-block::
333+
334+
Subject line
335+
336+
some body information
337+
338+
another information
339+
340+
key1: value1
341+
key2: value2
342+
343+
:return: Dictionary containing whitespace stripped trailer information
344+
"""
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+
ifmatchisNone:
348+
returnd
349+
last_paragraph=match.group(1)
350+
ifnotall(':'inlineforlineinlast_paragraph.split('\n')):
351+
returnd
352+
forlineinlast_paragraph.split('\n'):
353+
key,value=line.split(':',1)
354+
d[key.strip()]=value.strip()
355+
returnd
356+
318357
@classmethod
319358
def_iter_from_process_or_stream(cls,repo:'Repo',proc_or_stream:Union[Popen,IO])->Iterator['Commit']:
320359
"""Parse out commit information into a list of Commit objects

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp