Skip to content

Commit 21d911b

Browse files
committed
ciq-cherry-pick.py: Automatically cherry pick cve-bf commits
It now automatically cherry picks the Fixes: dependencies. To accomodate this, CIQ_find_mainline_fixes was moved to ciq_helpers. Signed-off-by: Roxana Nicolescu <rnicolescu@ciq.com>
1 parent d011463 commit 21d911b

File tree

2 files changed

+60
-33
lines changed

2 files changed

+60
-33
lines changed

check_kernel_commits.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from ciq_helpers import (
1212
CIQ_commit_exists_in_branch,
1313
CIQ_extract_fixes_references_from_commit_body_lines,
14+
CIQ_find_fixes_in_mainline,
1415
CIQ_get_commit_body,
1516
CIQ_hash_exists_in_ref,
1617
CIQ_run_git,
@@ -238,7 +239,7 @@ def main():
238239
)
239240
out_lines.append("") # blank line
240241
continue
241-
fixes = find_fixes_in_mainline(args.repo, args.pr_branch, upstream_ref, uhash)
242+
fixes = CIQ_find_fixes_in_mainline(args.repo, args.pr_branch, upstream_ref, uhash)
242243
if fixes:
243244
any_findings = True
244245

ciq-cherry-pick.py

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import argparse
22
import logging
33
import os
4+
import re
45
import subprocess
56

67
import git
78

89
from ciq_helpers import (
910
CIQ_cherry_pick_commit_standardization,
1011
CIQ_commit_exists_in_current_branch,
12+
CIQ_find_fixes_in_mainline_current_branch,
1113
CIQ_fixes_references,
14+
CIQ_get_full_hash,
1215
CIQ_original_commit_author_to_tag_string,
1316
)
1417

@@ -31,42 +34,17 @@ def check_fixes(sha):
3134
raise RuntimeError(f"The commit you want to cherry pick references a Fixes {fix}: but this is not here")
3235

3336

34-
if __name__ == "__main__":
35-
print("CIQ custom cherry picker")
36-
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
37-
parser.add_argument("--sha", help="Target SHA1 to cherry-pick")
38-
parser.add_argument("--ticket", help="Ticket associated to cherry-pick work, comma separated list is supported.")
39-
parser.add_argument(
40-
"--ciq-tag",
41-
help="Tags for commit message <feature><-optional modifier> <identifier>.\n"
42-
"example: cve CVE-2022-45884 - A patch for a CVE Fix.\n"
43-
" cve-bf CVE-1974-0001 - A bug fix for a CVE currently being patched\n"
44-
" cve-pre CVE-1974-0001 - A pre-condition or dependency needed for the CVE\n"
45-
"Multiple tags are separated with a comma. ex: cve CVE-1974-0001, cve CVE-1974-0002\n",
46-
)
47-
args = parser.parse_args()
48-
37+
def cherry_pick(sha, ciq_tags, jira_ticket):
4938
# Expand the provided SHA1 to the full SHA1 in case it's either abbreviated or an expression
50-
git_sha_res = subprocess.run(["git", "show", "--pretty=%H", "-s", args.sha], stdout=subprocess.PIPE)
51-
if git_sha_res.returncode != 0:
52-
print(f"[FAILED] git show --pretty=%H -s {args.sha}")
53-
print("Subprocess Call:")
54-
print(git_sha_res)
55-
print("")
56-
else:
57-
args.sha = git_sha_res.stdout.decode("utf-8").strip()
39+
full_sha = CIQ_get_full_hash(".", sha)
5840

59-
tags = []
60-
if args.ciq_tag is not None:
61-
tags = args.ciq_tag.split(",")
41+
check_fixes(full_sha)
6242

63-
check_fixes(args.sha)
64-
65-
author = CIQ_original_commit_author_to_tag_string(repo_path=os.getcwd(), sha=args.sha)
66-
if author is None:
43+
author = CIQ_original_commit_author_to_tag_string(repo_path=os.getcwd(), sha=full_sha)
44+
if author is None: # TODO raise Exception maybe
6745
exit(1)
6846

69-
git_res = subprocess.run(["git", "cherry-pick", "-nsx", args.sha])
47+
git_res = subprocess.run(["git", "cherry-pick", "-nsx", full_sha]) # Move it into a separate method
7048
if git_res.returncode != 0:
7149
print(f"[FAILED] git cherry-pick -nsx {args.sha}")
7250
print(" Manually resolve conflict and include `upstream-diff` tag in commit message")
@@ -82,7 +60,7 @@ def check_fixes(sha):
8260
with open(MERGE_MSG, "r") as file:
8361
original_msg = file.readlines()
8462

85-
new_msg = CIQ_cherry_pick_commit_standardization(original_msg, args.sha, jira=args.ticket, tags=tags)
63+
new_msg = CIQ_cherry_pick_commit_standardization(original_msg, full_sha, jira=jira_ticket, tags=ciq_tags)
8664

8765
print(f"Cherry Pick New Message for {args.sha}")
8866
for line in new_msg:
@@ -94,3 +72,51 @@ def check_fixes(sha):
9472

9573
if git_res.returncode == 0:
9674
subprocess.run(["git", "commit", "-F", MERGE_MSG])
75+
76+
77+
def cherry_pick_fixes(sha, ciq_tags, jira_ticket, upstream_ref):
78+
fixes_in_mainline = CIQ_find_fixes_in_mainline_current_branch(".", upstream_ref, sha)
79+
80+
# Replace cve with cve-bf
81+
# Leave cve-pre and cve-bf as they are
82+
ciq_tags = [re.sub(r"^cve ", "cve-bf ", s) for s in ciq_tags]
83+
for full_hash, display_str in fixes_in_mainline:
84+
print(f"Extra cherry picking {display_str}")
85+
full_cherry_pick(sha=full_hash, ciq_tags=ciq_tags, jira_ticket=jira_ticket, upstream_ref=upstream_ref)
86+
87+
88+
def full_cherry_pick(sha, ciq_tags, jira_ticket, upstream_ref):
89+
# Cherry pick the commit
90+
cherry_pick(sha=sha, ciq_tags=ciq_tags, jira_ticket=jira_ticket)
91+
92+
# Cherry pick the fixed-by dependencies
93+
cherry_pick_fixes(sha=sha, ciq_tags=ciq_tags, jira_ticket=jira_ticket, upstream_ref=upstream_ref)
94+
95+
96+
if __name__ == "__main__":
97+
print("CIQ custom cherry picker")
98+
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
99+
parser.add_argument("--sha", help="Target SHA1 to cherry-pick")
100+
parser.add_argument("--ticket", help="Ticket associated to cherry-pick work, comma separated list is supported.")
101+
parser.add_argument(
102+
"--ciq-tag",
103+
help="Tags for commit message <feature><-optional modifier> <identifier>.\n"
104+
"example: cve CVE-2022-45884 - A patch for a CVE Fix.\n"
105+
" cve-bf CVE-1974-0001 - A bug fix for a CVE currently being patched\n"
106+
" cve-pre CVE-1974-0001 - A pre-condition or dependency needed for the CVE\n"
107+
"Multiple tags are separated with a comma. ex: cve CVE-1974-0001, cve CVE-1974-0002\n",
108+
)
109+
110+
parser.add_argument(
111+
"--upstream-ref",
112+
default="origin/kernel-mainline",
113+
help="Reference to upstream mainline branch (default: origin/kernel-mainline)",
114+
)
115+
116+
args = parser.parse_args()
117+
118+
tags = []
119+
if args.ciq_tag is not None:
120+
tags = args.ciq_tag.split(",")
121+
122+
full_cherry_pick(sha=args.sha, ciq_tags=tags, jira_ticket=args.ticket, upstream_ref=args.upstream_ref)

0 commit comments

Comments
 (0)