Skip to content

Add trailer property #1350

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 4 commits into from
Jan 7, 2022
Merged

Add trailer property #1350

merged 4 commits into from
Jan 7, 2022

Conversation

Ket3r
Copy link
Contributor

@Ket3r Ket3r commented Sep 29, 2021

Git provides ways to interact trailer lines in the commit messages that look similar to RFC 822 e-mail headers (git interpret-trailers or git log --format="%(trailers)"). The new trailers property of the commit object parses its message and returns this trailer information in a dictionary. This is e.g. usefull when writing a Changelog generator https://docs.gitlab.com/ee/development/changelog.html

Example commit message:

Adds feature xyz

Better explanation of this feature

Changelog: Added
Co-Author: Jane Doe

To access the trailer information you can now use

commit.trailers["Changelog"] # Returns: Added
commit.trailers["Co-Author"] # Returns: Jane Doe

Ket3r and others added 3 commits September 29, 2021 21:41
The ddt package changed the function signature in version 1.4.3 from
idata(iterable) to idata(iterable, index_len). Hopefully this was just a
mistake and the new argument will be optional in future versions (see
issue datadriventests/ddt#97)
With the command `git interpret-trailers` git provides a way to interact
with trailer lines in the commit messages that look similar to RFC 822
e-mail headers (see: https://git-scm.com/docs/git-interpret-trailers).
The new property returns those parsed trailer lines from the message as
dictionary.
@Byron Byron added this to the v3.1.25 - Bugfixes milestone Sep 30, 2021
Copy link
Member

@Byron Byron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your contribution!

There is only a tiny question about what to do with whitespace in keys. If it's allows, it's worth mentioning it in the docs. Otherwise there could be a test that shows those won't be considered keys, in order to reduce ambiguity.

What do you think about this?

The whitespace handling and trailer selection isn't very trivial or good
documented. It therefore seemed easier and less error prone to just call
git to parse the message for the trailers section and remove superfluos
whitespaces.
"""
d = {}
cmd = ['git', 'interpret-trailers', '--parse']
proc: Git.AutoInterrupt = self.repo.git.execute(cmd, as_process=True, istream=PIPE) # type: ignore
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can someone help with this?
I don't know how to get mypy to accept subprocess.PIPE as argument

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe @Yobmod could help here.

Copy link
Contributor

@Yobmod Yobmod Oct 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the istream type annotation is too narrow in cmd.Git.execute().
It should be int | IO | None if we accept any args that Popen does.

But execute() needs a lot more annotations anyway to clear up the remaining str | bytes problems from py2 and all the overloapping arg type @overloads.
So you can update the type annotation, or leave the type: ignore for now.
Once annotations updated, mypy should then warn that the type: ignore is no longer needed.

@Byron Byron requested a review from Yobmod October 3, 2021 11:31
@Byron
Copy link
Member

Byron commented Jan 7, 2022

As I have disabled mypi to not fail CI anymore, I think it's safe to merge this feature and deal with mypi related typing improvements another time.

Thanks everyone for their involvement and help, and sorry for the wait.

@Byron Byron merged commit cd8b9b2 into gitpython-developers:main Jan 7, 2022
@nickbroon
Copy link

Should trailers be a list instead of a dictionary?

Consider for example this linux kernel commit: https://gitlab.com/linux-kernel/stable/-/commit/1382999aa0548a171a272ca817f6c38e797c458c which has following trailers that include multiple cc and signed-off-by trailers:

Link: https://lore.kernel.org/lkml/[email protected]/


Cc: [email protected] # 6.1+
Reported-by: default avatar[Vlastimil Babka](mailto:[email protected]) <[[email protected]](mailto:[email protected])>
Suggested-by: default avatar[Linus Torvalds](mailto:[email protected]) <[[email protected]](mailto:[email protected])>
Acked-by: default avatar[Luigi Semenzato](mailto:[email protected]) <[[email protected]](mailto:[email protected])>
Cc: Peter Huewe <[email protected]>
Cc: Jarkko Sakkinen <[email protected]>
Cc: James Bottomley <[email protected]>
Cc: Johannes Altmanninger <[email protected]>
Signed-off-by: Jason Donenfeld's avatar[Jason A. Donenfeld](https://gitlab.com/zx2c4) <[[email protected]](mailto:[email protected])>
Signed-off-by: default avatar[Linus Torvalds](mailto:[email protected]) <[[email protected]](mailto:[email protected])>

And git interpret-trailers --parse ... will return all of these.

@Byron
Copy link
Member

Byron commented Jan 10, 2023

I agree - sorry for not catching this. It's better to return it as flat list and leave any kind of indexing to the user.
Maybe a follow-up PR could be created that adds another method, like trailer_list() (or better, if you have ideas) which returns such list. The trailers() could then use that to build a dict along with a big warning that it only shows the last seen value with a certain prefix.

@nickbroon
Copy link

nickbroon commented Jan 10, 2023

I agree - sorry for not catching this. It's better to return it as flat list and leave any kind of indexing to the user. Maybe a follow-up PR could be created that adds another method, like trailer_list() (or better, if you have ideas) which returns such list. The trailers() could then use that to build a dict along with a big warning that it only shows the last seen value with a certain prefix.

Assuming you don't want to make non-backwards compatible change to the API with the fix to this bug then adding a new trailer_list() API seems appropriate. Adding as documentation/comment warning on trailer() should definitely be done, and perhaps marking it as deprecated for removal in future revision should be considered as I don't with its current behaviour it's particularly useful.

I've opened #1533 to track this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants