| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent edbf76f commit cd8b9b2
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,8 +4,7 @@ | |||
| 4 | 4 | # This module is part of GitPython and is released under | |
| 5 | 5 | # the BSD License: http://www.opensource.org/licenses/bsd-license.php | |
| 6 | 6 | import datetime | |
| 7 | - import re | ||
| 8 | - from subprocess import Popen | ||
| 7 | + from subprocess import Popen, PIPE | ||
| 9 | 8 | from gitdb import IStream | |
| 10 | 9 | from git.util import ( | |
| 11 | 10 | hex_to_bin, | |
@@ -14,6 +13,7 @@ | |||
| 14 | 13 | finalize_process | |
| 15 | 14 | ) | |
| 16 | 15 | from git.diff import Diffable | |
| 16 | + from git.cmd import Git | ||
| 17 | 17 | ||
| 18 | 18 | from .tree import Tree | |
| 19 | 19 | from . import base | |
@@ -322,10 +322,10 @@ def trailers(self) -> Dict: | |||
| 322 | 322 | ||
| 323 | 323 | Git messages can contain trailer information that are similar to RFC 822 | |
| 324 | 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. | ||
| 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. | ||
| 329 | 329 | ||
| 330 | 330 | Valid message with trailer: | |
| 331 | 331 | ||
@@ -338,20 +338,29 @@ def trailers(self) -> Dict: | |||
| 338 | 338 | another information | |
| 339 | 339 | ||
| 340 | 340 | 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 | + } | ||
| 342 | 350 | ||
| 343 | 351 | :return: Dictionary containing whitespace stripped trailer information | |
| 352 | + | ||
| 344 | 353 | """ | |
| 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() | ||
| 355 | 364 | return d | |
| 356 | 365 | ||
| 357 | 366 | @ classmethod | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -435,14 +435,14 @@ def test_trailers(self): | |||
| 435 | 435 | KEY_1 = "Hello" | |
| 436 | 436 | VALUE_1 = "World" | |
| 437 | 437 | KEY_2 = "Key" | |
| 438 | - VALUE_2 = "Value" | ||
| 438 | + VALUE_2 = "Value with inner spaces" | ||
| 439 | 439 | ||
| 440 | 440 | # Check if KEY 1 & 2 with Value 1 & 2 is extracted from multiple msg variations | |
| 441 | 441 | msgs = [] | |
| 442 | 442 | msgs.append(f"Subject\n\n{KEY_1}: {VALUE_1}\n{KEY_2}: {VALUE_2}\n") | |
| 443 | 443 | msgs.append(f"Subject\n \nSome body of a function\n \n{KEY_1}: {VALUE_1}\n{KEY_2}: {VALUE_2}\n") | |
| 444 | 444 | 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") | ||
| 446 | 446 | ||
| 447 | 447 | for msg in msgs: | |
| 448 | 448 | commit = self.rorepo.commit('master') | |
| Back | FazBrowse Home | New Git URL |
0 commit comments