Skip to content

Fix up the commit trailers functionality #1576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update Commit.trailer_list to return tuples.
  • Loading branch information
itsluketwist committed Apr 22, 2023
commit abde3eafd293e8fa2ef2dc22d58ba5d80f1702e9
27 changes: 16 additions & 11 deletions git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def stats(self) -> Stats:
return Stats._list_from_string(self.repo, text)

@property
def trailers_list(self) -> List[str]:
def trailers_list(self) -> List[Tuple[str, str]]:
"""Get the trailers of the message as a list

Git messages can contain trailer information that are similar to RFC 822
Expand All @@ -361,23 +361,29 @@ def trailers_list(self) -> List[str]:
Returned list will look like this::

[
"key1: value1.1",
"key1: value1.2",
"key2 : value 2 with inner spaces",
("key1", "value1.1"),
("key1", "value1.2"),
("key2", "value 2 with inner spaces"),
]


:return:
List containing whitespace stripped trailer information.
List containing key-value tuples of whitespace stripped trailer information.
"""
cmd = ["git", "interpret-trailers", "--parse"]
proc: Git.AutoInterrupt = self.repo.git.execute(cmd, as_process=True, istream=PIPE) # type: ignore
trailer: str = proc.communicate(str(self.message).encode())[0].decode()
trailer = trailer.strip()
if trailer:
return [t.strip() for t in trailer.split("\n")]

return []
if not trailer:
return []

trailer_list = []
for t in trailer.split("\n"):
key, val = t.split(":", 1)
trailer_list.append((key.strip(), val.strip()))

return trailer_list

@property
def trailers_dict(self) -> Dict[str, List[str]]:
Expand Down Expand Up @@ -416,9 +422,8 @@ def trailers_dict(self) -> Dict[str, List[str]]:
Mapping trailer keys to a list of their corresponding values.
"""
d = defaultdict(list)
for trailer in self.trailers_list:
key, value = trailer.split(":", 1)
d[key.strip()].append(value.strip())
for key, val in self.trailers_list:
d[key].append(val)
return dict(d)

@classmethod
Expand Down
8 changes: 4 additions & 4 deletions test/test_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,9 @@ def test_trailers(self):
commit = copy.copy(self.rorepo.commit("master"))
commit.message = msg
assert commit.trailers_list == [
f"{KEY_1}: {VALUE_1_1}",
f"{KEY_2}: {VALUE_2}",
f"{KEY_1}: {VALUE_1_2}",
(KEY_1, VALUE_1_1),
(KEY_2, VALUE_2),
(KEY_1, VALUE_1_2),
]
assert commit.trailers_dict == {
KEY_1: [VALUE_1_1, VALUE_1_2],
Expand All @@ -543,7 +543,7 @@ def test_trailers(self):
# check that only the last key value paragraph is evaluated
commit = copy.copy(self.rorepo.commit("master"))
commit.message = f"Subject\n\nMultiline\nBody\n\n{KEY_1}: {VALUE_1_1}\n\n{KEY_2}: {VALUE_2}\n"
assert commit.trailers_list == [f"{KEY_2}: {VALUE_2}"]
assert commit.trailers_list == [(KEY_2, VALUE_2)]
assert commit.trailers_dict == {KEY_2: [VALUE_2]}

def test_commit_co_authors(self):
Expand Down