Skip to content

Commit

Permalink
Remove multiple commit prefixes (#118)
Browse files Browse the repository at this point in the history
Multiple commit prefixes "[X.Y] " were added in multilevel backports.
Only the leftmost one matters. Others just increase the length of the title.
  • Loading branch information
serhiy-storchaka committed Feb 13, 2024
1 parent 19634f2 commit 9aaad23
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
42 changes: 21 additions & 21 deletions cherry_picker/cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,11 @@ def get_updated_commit_message(self, cherry_pick_branch):
"""
# Get the original commit message and prefix it with the branch name
# if that's enabled.
commit_prefix = ""
updated_commit_message = self.get_commit_message(self.commit_sha1)
if self.prefix_commit:
commit_prefix = f"[{get_base_branch(cherry_pick_branch)}] "
updated_commit_message = (
f"{commit_prefix}{self.get_commit_message(self.commit_sha1)}"
)
updated_commit_message = remove_commit_prefix(updated_commit_message)
base_branch = get_base_branch(cherry_pick_branch)
updated_commit_message = f"[{base_branch}] {updated_commit_message}"

# Add '(cherry picked from commit ...)' to the message
# and add new Co-authored-by trailer if necessary.
Expand Down Expand Up @@ -443,6 +442,7 @@ def create_gh_pr(self, base_branch, head_branch, *, commit_message, gh_auth):
request_headers = sansio.create_headers(self.username, oauth_token=gh_auth)
title, body = normalize_commit_message(commit_message)
if not self.prefix_commit:
title = remove_commit_prefix(title)
title = f"[{base_branch}] {title}"
data = {
"title": title,
Expand Down Expand Up @@ -880,19 +880,10 @@ def version_from_branch(branch):
"""
return version information from a git branch name
"""
try:
return tuple(
map(
int,
re.match(r"^.*(?P<version>\d+(\.\d+)+).*$", branch)
.groupdict()["version"]
.split("."),
)
)
except AttributeError as attr_err:
raise ValueError(
f"Branch {branch} seems to not have a version in its name."
) from attr_err
m = re.search(r"\d+(?:\.\d+)+", branch)
if not m:
raise ValueError(f"Branch {branch} seems to not have a version in its name.")
return tuple(map(int, m[0].split(".")))


def get_current_branch():
Expand Down Expand Up @@ -929,12 +920,21 @@ def normalize_commit_message(commit_message):
"""
Return a tuple of title and body from the commit message
"""
split_commit_message = commit_message.split("\n")
title = split_commit_message[0]
body = "\n".join(split_commit_message[1:])
title, _, body = commit_message.partition("\n")
return title, body.lstrip("\n")


def remove_commit_prefix(commit_message):
"""
Remove prefix "[X.Y] " from the commit message
"""
while True:
m = re.match(r"\[\d+(?:\.\d+)+\] *", commit_message)
if not m:
return commit_message
commit_message = commit_message[m.end() :]


def is_git_repo():
"""Check whether the current folder is a Git repo."""
cmd = "git", "rev-parse", "--git-dir"
Expand Down
24 changes: 24 additions & 0 deletions cherry_picker/test_cherry_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
load_config,
load_val_from_git_cfg,
normalize_commit_message,
remove_commit_prefix,
reset_state,
reset_stored_config_ref,
set_state,
Expand Down Expand Up @@ -558,6 +559,19 @@ def test_normalize_short_commit_message():
)


@pytest.mark.parametrize(
"commit_message, expected",
[
("[3.12] Fix something (GH-3113)", "Fix something (GH-3113)"),
("[3.11] [3.12] Fix something (GH-3113)", "Fix something (GH-3113)"),
("Fix something (GH-3113)", "Fix something (GH-3113)"),
("[WIP] Fix something (GH-3113)", "[WIP] Fix something (GH-3113)"),
],
)
def test_remove_commit_prefix(commit_message, expected):
assert remove_commit_prefix(commit_message) == expected


@pytest.mark.parametrize(
"commit_message,expected_commit_message",
(
Expand Down Expand Up @@ -626,6 +640,16 @@ def test_normalize_short_commit_message():
Co-authored-by: PR Author <author@name.email>
Co-authored-by: PR Co-Author <another@author.com>""",
),
# ensure the existing commit prefix is replaced
(
"[3.7] [3.8] Fix broken `Show Source` links on documentation "
"pages (GH-3113) (GH-3114) (GH-3115)",
"""[3.6] Fix broken `Show Source` links on documentation """
"""pages (GH-3113) (GH-3114) (GH-3115)
(cherry picked from commit b9ff498793611d1c6a9b99df464812931a1e2d69)
Co-authored-by: PR Author <author@name.email>""",
),
),
)
def test_get_updated_commit_message_with_trailers(
Expand Down

0 comments on commit 9aaad23

Please sign in to comment.