-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
Respect existing trailers (including co-author lines) when backporting #46
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -468,6 +468,96 @@ def test_normalize_short_commit_message(): | |
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"commit_message,expected_commit_message", | ||
( | ||
# ensure existing co-author is retained | ||
( | ||
"""Fix broken `Show Source` links on documentation pages (GH-3113) | ||
|
||
Co-authored-by: PR Co-Author <[email protected]>""", | ||
"""[3.6] Fix broken `Show Source` links on documentation pages (GH-3113) | ||
(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69) | ||
|
||
Co-authored-by: PR Author <[email protected]> | ||
Co-authored-by: PR Co-Author <[email protected]>""", | ||
), | ||
# ensure co-author trailer is not duplicated | ||
( | ||
"""Fix broken `Show Source` links on documentation pages (GH-3113) | ||
|
||
Co-authored-by: PR Author <[email protected]>""", | ||
"""[3.6] Fix broken `Show Source` links on documentation pages (GH-3113) | ||
(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69) | ||
|
||
Co-authored-by: PR Author <[email protected]>""", | ||
), | ||
# ensure message is formatted properly when original commit is short | ||
( | ||
"Fix broken `Show Source` links on documentation pages (GH-3113)", | ||
"""[3.6] Fix broken `Show Source` links on documentation pages (GH-3113) | ||
(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69) | ||
|
||
Co-authored-by: PR Author <[email protected]>""", | ||
), | ||
# ensure message is formatted properly when original commit is long | ||
( | ||
"""Fix broken `Show Source` links on documentation pages (GH-3113) | ||
|
||
The `Show Source` was broken because of a change made in sphinx 1.5.1 | ||
In Sphinx 1.4.9, the sourcename was "index.txt". | ||
In Sphinx 1.5.1+, it is now "index.rst.txt".""", | ||
"""[3.6] Fix broken `Show Source` links on documentation pages (GH-3113) | ||
|
||
The `Show Source` was broken because of a change made in sphinx 1.5.1 | ||
In Sphinx 1.4.9, the sourcename was "index.txt". | ||
In Sphinx 1.5.1+, it is now "index.rst.txt". | ||
(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69) | ||
|
||
Co-authored-by: PR Author <[email protected]>""", | ||
), | ||
# ensure message is formatted properly when original commit is long | ||
# and it has a co-author | ||
( | ||
"""Fix broken `Show Source` links on documentation pages (GH-3113) | ||
|
||
The `Show Source` was broken because of a change made in sphinx 1.5.1 | ||
In Sphinx 1.4.9, the sourcename was "index.txt". | ||
In Sphinx 1.5.1+, it is now "index.rst.txt". | ||
|
||
Co-authored-by: PR Co-Author <[email protected]>""", | ||
"""[3.6] Fix broken `Show Source` links on documentation pages (GH-3113) | ||
|
||
The `Show Source` was broken because of a change made in sphinx 1.5.1 | ||
In Sphinx 1.4.9, the sourcename was "index.txt". | ||
In Sphinx 1.5.1+, it is now "index.rst.txt". | ||
(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69) | ||
|
||
Co-authored-by: PR Author <[email protected]> | ||
Co-authored-by: PR Co-Author <[email protected]>""", | ||
), | ||
), | ||
) | ||
def test_get_updated_commit_message_with_trailers(commit_message, expected_commit_message): | ||
cherry_pick_branch = "backport-22a594a-3.6" | ||
commit = "b9ff498793611d1c6a9b99df464812931a1e2d69" | ||
|
||
with mock.patch("cherry_picker.cherry_picker.validate_sha", return_value=True): | ||
cherry_picker = CherryPicker("origin", commit, []) | ||
|
||
with mock.patch( | ||
"cherry_picker.cherry_picker.validate_sha", return_value=True | ||
), mock.patch.object( | ||
cherry_picker, "get_commit_message", return_value=commit_message | ||
), mock.patch( | ||
"cherry_picker.cherry_picker.get_author_info_from_short_sha", | ||
return_value="PR Author <[email protected]>", | ||
): | ||
updated_commit_message = cherry_picker.get_updated_commit_message(cherry_pick_branch) | ||
|
||
assert updated_commit_message == expected_commit_message | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input_path", ("/some/path/without/revision", "HEAD:some/non-existent/path") | ||
) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(One of the comments from my previous review got lost.)
It would be helpful to add a docstring and a couple more comments to this function to explain what it does, since it's not immediately obvious.
AFAIK and by looking at the tests, it seems it retrieves the original commit message and:
I also wonder if there's a more straightforward way to do achieve this.
Can you use
git interpret-trailers --only-trailers
to extract the metadata and then do something like this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Welp, some value sure was lost by me not adding more comments in there in the first place... I added some now to explain the way this is supposed to work.
I can look into an alternative way of doing this. The important things are:
(cherry picked from commit ...)
One of the problems is that
git interpret-trailers
does not have a flag that does the opposite of--only-trailers
so getting the "original commit message" may not be as easy as it seems. A naive way of doing it would be to cut the end of the full message by the length of the output ofgit interpret-trailers
but I wouldn't be too surprised if this wouldn't work that well in practice (as I said at the beginning, some value sure was lost by me not writing some more comments when I wrote this PR so I'll have to look into that).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can isolate all the trailers (and
--only-trailers
seems to do that) you could pass them through aset()
to remove duplicates. Even if the order is lost I'm not sure it matters. If you want to preserve it, it's pretty easy to write a function that removes duplicates without losing the order.Recently I was looking into manually adding co-authors, and apparently you had to leave two empty lines between the message and the trailers, so an
msg = full_commit_msg.rsplit('\n\n', 1)[0]
might work (assuming this is enforced). Otherwise we can cut the end like you said or remove individual trailer lines fromfull_commit_msg.splitlines()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cherry-picker is Python 3.7+ so this could just rely on the insertion order of dictionaries, possibly with the help of
dict.fromkeys()
.The problem with this is that we would need to write a proper detection of trailers or we risk the trailers no longer being trailers. A single split is not enough as we plan on adding a trailer and that means we need to make sure that we add a trailer with other trailers. For example, if this were the original commit message:
Then we can't just simply add it to the last part:
On the other hand, we also have to keep all trailers together so if this were the original commit message:
Then this would not be acceptable:
For reference, here's the logic that git uses: https://github.com/git/git/blob/afa70145a25e81faa685dc0b465e52b45d2444bd/trailer.c#L818-L915
Based on these two:
https://github.com/git/git/blob/afa70145a25e81faa685dc0b465e52b45d2444bd/trailer.c#L1108-L1123
https://github.com/git/git/blob/afa70145a25e81faa685dc0b465e52b45d2444bd/trailer.c#L982-L990
I think there's no alteration done to the printed trailers which would make this doable but I'll have to read into it just a bit more to be sure. I didn't want to delay my response further since I might not be able to get to it at all today so I have not yet carefully examined these two linked fragments of code.
I think this falls into same category as
rsplit
but I'm not sure I fully understand what you mean by this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given this:
You could do something like this:
That is, if you take the full commit and remove the trailer lines you obtained with
git interpret-trailers --only-trailers
you'll be left with the message only. Once you have the message and the trailers you can update the trailers and recombine everything in a new msg. Do you think this will work?