-
Notifications
You must be signed in to change notification settings - Fork 101
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
Use the same patching script and command for all repos #584
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
# Function to generate a PATCH_COMMAND, calling the | ||
# patch_repo.py script using a target set of patches. | ||
|
||
function(get_patch_command patch_dir patch_command_out) | ||
set(patch_script ${CMAKE_CURRENT_LIST_DIR}/patch_repo.py) | ||
list(APPEND patch_script_args ${Python3_EXECUTABLE} ${patch_script}) | ||
if(GIT_PATCH_METHOD STREQUAL "am") | ||
list(APPEND patch_script_args "--method" "am") | ||
elseif(GIT_PATCH_METHOD STREQUAL "apply") | ||
list(APPEND patch_script_args "--method" "apply") | ||
endif() | ||
list(APPEND patch_script_args ${CMAKE_CURRENT_LIST_DIR}/../patches/${patch_dir}) | ||
|
||
set(${patch_command_out} ${patch_script_args} PARENT_SCOPE) | ||
endfunction() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
Script to apply a set of patches to llvm-project sources. | ||
Script to apply a set of patches to a git repository. | ||
""" | ||
|
||
import argparse | ||
|
@@ -18,8 +18,8 @@ def main(): | |
help="Set of patches to apply. This should be a directory containing one or more ordered *.patch files.", | ||
) | ||
parser.add_argument( | ||
"--llvm_dir", | ||
help="Directory of the llvm-project git checkout, if not the current directory.", | ||
"--repo_dir", | ||
help="Directory of the git checkout, if not the current directory.", | ||
) | ||
parser.add_argument( | ||
"--method", | ||
|
@@ -36,10 +36,20 @@ def main(): | |
action="store_true", | ||
help="If a patch in a series cannot be applied, restore the original state instead of leaving patches missing. Return code will be 2 instead of 1.", | ||
) | ||
parser.add_argument( | ||
"--3way", | ||
action="store_true", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks as if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The I'll update the commit message when squashing to mention all this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine – that makes sense. If it's disabled on purpose and you say why, I'm happy. |
||
dest="three_way", | ||
help="If the patch does not apply cleanly, fall back on 3-way merge.", | ||
) | ||
args = parser.parse_args() | ||
|
||
if args.llvm_dir: | ||
git_cmd = ["git", "-C", args.llvm_dir] | ||
if args.method == "apply" and args.restore_on_fail and args.three_way: | ||
print("--restore_on_fail is incompatible with --3way using apply") | ||
exit(1) | ||
|
||
if args.repo_dir: | ||
git_cmd = ["git", "-C", args.repo_dir] | ||
else: | ||
git_cmd = ["git"] | ||
|
||
|
@@ -57,7 +67,9 @@ def main(): | |
print("\n".join(p.name for p in patch_list)) | ||
|
||
if args.method == "am": | ||
merge_args = git_cmd + ["am", "-k", "--ignore-whitespace", "--3way"] | ||
merge_args = git_cmd + ["am", "-k", "--ignore-whitespace"] | ||
if args.three_way: | ||
merge_args.append("--3way") | ||
for patch in patch_list: | ||
merge_args.append(str(patch)) | ||
p = subprocess.run(merge_args, capture_output=True, text=True) | ||
|
@@ -72,8 +84,8 @@ def main(): | |
# git am doesn't give any specific return codes, | ||
# so check for unresolved working files. | ||
rebase_apply_path = os.path.join(".git", "rebase-apply") | ||
if args.llvm_dir: | ||
rebase_apply_path = os.path.join(args.llvm_dir, rebase_apply_path) | ||
if args.repo_dir: | ||
rebase_apply_path = os.path.join(args.repo_dir, rebase_apply_path) | ||
if os.path.isdir(rebase_apply_path): | ||
print("Aborting git am...") | ||
subprocess.run(git_cmd + ["am", "--abort"], check=True) | ||
|
@@ -90,10 +102,11 @@ def main(): | |
apply_check_args = git_cmd + [ | ||
"apply", | ||
"--ignore-whitespace", | ||
"--3way", | ||
"--check", | ||
str(current_patch), | ||
] | ||
if args.three_way: | ||
apply_check_args.append("--3way") | ||
apply_check_args.append(str(current_patch)) | ||
p_check = subprocess.run(apply_check_args) | ||
|
||
if p_check.returncode == 0: | ||
|
@@ -102,10 +115,11 @@ def main(): | |
apply_args = git_cmd + [ | ||
"apply", | ||
"--ignore-whitespace", | ||
"--3way", | ||
str(current_patch), | ||
] | ||
apply_args = subprocess.run(apply_args, check=True) | ||
if args.three_way: | ||
apply_args.append("--3way") | ||
apply_args.append(str(current_patch)) | ||
p = subprocess.run(apply_args, check=True) | ||
applied_patches.append(current_patch) | ||
else: | ||
# Patch won't apply. | ||
|
@@ -118,10 +132,11 @@ def main(): | |
reverse_args = git_cmd + [ | ||
"apply", | ||
"--ignore-whitespace", | ||
"--3way", | ||
"--reverse", | ||
str(previous_patch), | ||
] | ||
if args.three_way: | ||
reverse_args.append("--3way") | ||
reverse_args.append(str(previous_patch)) | ||
p_check = subprocess.run(reverse_args, check=True) | ||
print( | ||
f"Rollback successful, failure occured on {current_patch.name}" | ||
|
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.
I think that using
${CMAKE_CURRENT_LIST_DIR}
in this context means it won't be evaluated until the function is called, and then it'll use its caller's value of that variable. So if a function not in thecmake
subdir were to callget_patch_command
, it would get the wrong directory here.At the moment this is fine, because all the callers live in that subdir too, but I could imagine it maybe not staying that way. So for one extra safety precaution, how about copying
${CMAKE_CURRENT_LIST_DIR}
into a new variable in this include file:and similarly refer to
${patch_list_dir}/../patches
?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.
True - although setting
patch_list_dir
insidepatch_repo.cmake
does not guarantee that the value will still be correct by the time the function is called, since it can be freely modified within the scope. The safest option I can see is pass the toolchain root to the function as an argument, which can then be used within to derive the patch/script locations.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.
I'm not sure what you mean by that. Yes, something else could modify
patch_list_dir
, but it would have to do it on purpose – I can't see any plausible way it would happen by accident, in the way that "oops I calledget_patch_command
from a script somewhere other than thecmake
directory" seemed to me like an easy mistake.But this version works too!