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

Commitcd8b9b2

Browse files
Ket3rByron
authored andcommitted
Use git interpret-trailers for trailers property
The whitespace handling and trailer selection isn't very trivial or gooddocumented. It therefore seemed easier and less error prone to just callgit to parse the message for the trailers section and remove superfluoswhitespaces.
1 parentedbf76f commitcd8b9b2

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

‎git/objects/commit.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +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
8-
fromsubprocessimportPopen
7+
fromsubprocessimportPopen,PIPE
98
fromgitdbimportIStream
109
fromgit.utilimport (
1110
hex_to_bin,
@@ -14,6 +13,7 @@
1413
finalize_process
1514
)
1615
fromgit.diffimportDiffable
16+
fromgit.cmdimportGit
1717

1818
from .treeimportTree
1919
from .importbase
@@ -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-
fromthesubject/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 extractthetrailer 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-
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()
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+
iftrailer.endswith('\n'):
359+
trailer=trailer[0:-1]
360+
iftrailer!='':
361+
forlineintrailer.split('\n'):
362+
key,value=line.split(':',1)
363+
d[key.strip()]=value.strip()
355364
returnd
356365

357366
@classmethod

‎test/test_commit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,14 @@ def test_trailers(self):
435435
KEY_1="Hello"
436436
VALUE_1="World"
437437
KEY_2="Key"
438-
VALUE_2="Value"
438+
VALUE_2="Value with inner spaces"
439439

440440
# Check if KEY 1 & 2 with Value 1 & 2 is extracted from multiple msg variations
441441
msgs= []
442442
msgs.append(f"Subject\n\n{KEY_1}:{VALUE_1}\n{KEY_2}:{VALUE_2}\n")
443443
msgs.append(f"Subject\n\nSome body of a function\n\n{KEY_1}:{VALUE_1}\n{KEY_2}:{VALUE_2}\n")
444444
msgs.append(f"Subject\n\nSome body of a function\n\nnon-key: non-value\n\n{KEY_1}:{VALUE_1}\n{KEY_2}:{VALUE_2}\n")
445-
msgs.append(f"Subject\n\nSome multiline\n body of a function\n\nnon-key: non-value\n\n{KEY_1}:{VALUE_1}\n{KEY_2}:{VALUE_2}\n")
445+
msgs.append(f"Subject\n\nSome multiline\n body of a function\n\nnon-key: non-value\n\n{KEY_1}:{VALUE_1}\n{KEY_2} :{VALUE_2}\n")
446446

447447
formsginmsgs:
448448
commit=self.rorepo.commit('master')

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp