diff --git a/ciq-cherry-pick.py b/ciq-cherry-pick.py index 3a7194b..fa92364 100644 --- a/ciq-cherry-pick.py +++ b/ciq-cherry-pick.py @@ -179,6 +179,14 @@ def manage_commit_message(full_sha, ciq_tags, jira_ticket, commit_successful): except IOError as e: raise RuntimeError(f"Failed to read commit message from {MERGE_MSG}: {e}") from e + # git appends a "# Conflicts:" comment block to MERGE_MSG on a conflicted + # cherry-pick, and committing with -F doesn't strip comment lines. Drop the + # block to keep it out of the final commit message. + for i, line in enumerate(original_msg): + if line.rstrip("\n") == "# Conflicts:": + original_msg = original_msg[:i] + break + optional_msg = "" if commit_successful else "upstream-diff |" new_msg = CIQ_cherry_pick_commit_standardization( original_msg, full_sha, jira=jira_ticket, tags=new_tags, optional_msg=optional_msg diff --git a/kt/ktlib/ciq_helpers.py b/kt/ktlib/ciq_helpers.py index de9d4a3..05b0c4e 100644 --- a/kt/ktlib/ciq_helpers.py +++ b/kt/ktlib/ciq_helpers.py @@ -134,8 +134,8 @@ def CIQ_cherry_pick_commit_standardization(lines, commit, tags=None, jira="", op # will atttempt to read these lines and email everyone on the list. We do not want # to annoy the community when doing our own work. for i in range(5, len(lines)): - # The (cherry Picked from commit: ) line is the indicator we cherry-picked - if lines[i].startswith("cherry picked from commit"): + # The (cherry picked from commit ) line is the indicator we cherry-picked + if lines[i].lstrip().startswith("(cherry picked from commit"): break if ( lines[i].startswith("Signed-off-by") diff --git a/tests/kt/ktlib/test_ciq_helpers.py b/tests/kt/ktlib/test_ciq_helpers.py new file mode 100644 index 0000000..d88668e --- /dev/null +++ b/tests/kt/ktlib/test_ciq_helpers.py @@ -0,0 +1,31 @@ +from kt.ktlib.ciq_helpers import CIQ_cherry_pick_commit_standardization + +UPSTREAM_SHA = "1234567890abcdef1234567890abcdef12345678" +AUTHOR_TAG = "commit-author Upstream Author " + + +def standardized_cherry_pick_msg(): + """Run standardization on commit message lines mimicking MERGE_MSG after `git cherry-pick -nsx`.""" + lines = [ + "gve: fix a bug in the driver\n", + "\n", + "Some body text describing the fix.\n", + "\n", + "Signed-off-by: Upstream Author \n", + "Reviewed-by: Upstream Reviewer \n", + f"(cherry picked from commit {UPSTREAM_SHA})\n", + "Signed-off-by: Backporter \n", + ] + return CIQ_cherry_pick_commit_standardization(lines, UPSTREAM_SHA, tags=[AUTHOR_TAG], jira="VULN-123") + + +def test_cherry_pick_standardization_indents_upstream_trailers(): + lines = standardized_cherry_pick_msg() + assert "\tSigned-off-by: Upstream Author \n" in lines + assert "\tReviewed-by: Upstream Reviewer \n" in lines + + +def test_cherry_pick_standardization_stops_indenting_at_marker(): + lines = standardized_cherry_pick_msg() + assert lines[-2] == f"(cherry picked from commit {UPSTREAM_SHA})\n" + assert lines[-1] == "Signed-off-by: Backporter \n"