Skip to content

Commit 0d25b8e

Browse files
authored
[ATfE] Ensure patch_repo script works when no patches are found (#419)
If path_repo.py cannot find any patches to apply, it will hang when using the `am` method, since the git command will instead expect input from stdin/tty. Since the `apply` method loops through the patches to individually apply them, it will not get similarly stuck. This patch solves this by only attempting to patch when patches are found.
1 parent f7cffbe commit 0d25b8e

File tree

1 file changed

+77
-74
lines changed

1 file changed

+77
-74
lines changed

arm-software/embedded/cmake/patch_repo.py

Lines changed: 77 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -67,87 +67,90 @@ def main():
6767
patch_list = list(pathlib.Path(abs_patch_dir).glob("*.patch"))
6868
patch_list.sort()
6969

70-
print(f"Found {len(patch_list)} patches to apply:")
71-
print("\n".join(p.name for p in patch_list))
72-
73-
if args.method == "am":
74-
merge_args = git_cmd + ["am", "-k", "--ignore-whitespace"]
75-
if args.three_way:
76-
merge_args.append("--3way")
77-
for patch in patch_list:
78-
merge_args.append(str(patch))
79-
p = subprocess.run(merge_args, capture_output=True, text=True)
80-
print(p.stdout)
81-
print(p.stderr)
82-
83-
if p.returncode == 0:
84-
print(f"All patches applied.")
85-
sys.exit(0)
86-
if args.restore_on_fail:
87-
# Check that the operation can be aborted.
88-
# git am doesn't give any specific return codes,
89-
# so check for unresolved working files.
90-
rebase_apply_path = os.path.join(".git", "rebase-apply")
91-
if args.repo_dir:
92-
rebase_apply_path = os.path.join(args.repo_dir, rebase_apply_path)
93-
if os.path.isdir(rebase_apply_path):
94-
print("Aborting git am...")
95-
subprocess.run(git_cmd + ["am", "--abort"], check=True)
96-
print(f"Abort successful.")
97-
sys.exit(2)
98-
else:
99-
print("Unable to abort.")
100-
sys.exit(1)
70+
if len(patch_list) == 0:
71+
print(f"Found no patches to apply.")
10172
else:
102-
applied_patches = []
103-
for current_patch in patch_list:
104-
print(f"Checking {current_patch.name}...")
105-
# Check that the patch applies before trying to apply it.
106-
apply_check_args = git_cmd + [
107-
"apply",
108-
"--ignore-whitespace",
109-
"--check",
110-
]
73+
print(f"Found {len(patch_list)} patches to apply:")
74+
print("\n".join(p.name for p in patch_list))
75+
76+
if args.method == "am":
77+
merge_args = git_cmd + ["am", "-k", "--ignore-whitespace"]
11178
if args.three_way:
112-
apply_check_args.append("--3way")
113-
apply_check_args.append(str(current_patch))
114-
p_check = subprocess.run(apply_check_args)
79+
merge_args.append("--3way")
80+
for patch in patch_list:
81+
merge_args.append(str(patch))
82+
p = subprocess.run(merge_args, capture_output=True, text=True)
83+
print(p.stdout)
84+
print(p.stderr)
11585

116-
if p_check.returncode == 0:
117-
# Patch will apply.
118-
print(f"Applying {current_patch.name}...")
119-
apply_args = git_cmd + [
86+
if p.returncode == 0:
87+
print(f"All patches applied.")
88+
sys.exit(0)
89+
if args.restore_on_fail:
90+
# Check that the operation can be aborted.
91+
# git am doesn't give any specific return codes,
92+
# so check for unresolved working files.
93+
rebase_apply_path = os.path.join(".git", "rebase-apply")
94+
if args.repo_dir:
95+
rebase_apply_path = os.path.join(args.repo_dir, rebase_apply_path)
96+
if os.path.isdir(rebase_apply_path):
97+
print("Aborting git am...")
98+
subprocess.run(git_cmd + ["am", "--abort"], check=True)
99+
print(f"Abort successful.")
100+
sys.exit(2)
101+
else:
102+
print("Unable to abort.")
103+
sys.exit(1)
104+
else:
105+
applied_patches = []
106+
for current_patch in patch_list:
107+
print(f"Checking {current_patch.name}...")
108+
# Check that the patch applies before trying to apply it.
109+
apply_check_args = git_cmd + [
120110
"apply",
121111
"--ignore-whitespace",
112+
"--check",
122113
]
123114
if args.three_way:
124-
apply_args.append("--3way")
125-
apply_args.append(str(current_patch))
126-
p = subprocess.run(apply_args, check=True)
127-
applied_patches.append(current_patch)
128-
else:
129-
# Patch won't apply.
130-
print(f"Unable to apply {current_patch.name}")
131-
if args.restore_on_fail:
132-
# Remove any patches that have already been applied.
133-
while len(applied_patches) > 0:
134-
previous_patch = applied_patches.pop()
135-
print(f"Reversing {previous_patch.name}...")
136-
reverse_args = git_cmd + [
137-
"apply",
138-
"--ignore-whitespace",
139-
"--reverse",
140-
]
141-
if args.three_way:
142-
reverse_args.append("--3way")
143-
reverse_args.append(str(previous_patch))
144-
p_check = subprocess.run(reverse_args, check=True)
145-
print(
146-
f"Rollback successful, failure occured on {current_patch.name}"
147-
)
148-
sys.exit(2)
149-
sys.exit(1)
150-
print(f"All patches applied.")
115+
apply_check_args.append("--3way")
116+
apply_check_args.append(str(current_patch))
117+
p_check = subprocess.run(apply_check_args)
118+
119+
if p_check.returncode == 0:
120+
# Patch will apply.
121+
print(f"Applying {current_patch.name}...")
122+
apply_args = git_cmd + [
123+
"apply",
124+
"--ignore-whitespace",
125+
]
126+
if args.three_way:
127+
apply_args.append("--3way")
128+
apply_args.append(str(current_patch))
129+
p = subprocess.run(apply_args, check=True)
130+
applied_patches.append(current_patch)
131+
else:
132+
# Patch won't apply.
133+
print(f"Unable to apply {current_patch.name}")
134+
if args.restore_on_fail:
135+
# Remove any patches that have already been applied.
136+
while len(applied_patches) > 0:
137+
previous_patch = applied_patches.pop()
138+
print(f"Reversing {previous_patch.name}...")
139+
reverse_args = git_cmd + [
140+
"apply",
141+
"--ignore-whitespace",
142+
"--reverse",
143+
]
144+
if args.three_way:
145+
reverse_args.append("--3way")
146+
reverse_args.append(str(previous_patch))
147+
p_check = subprocess.run(reverse_args, check=True)
148+
print(
149+
f"Rollback successful, failure occured on {current_patch.name}"
150+
)
151+
sys.exit(2)
152+
sys.exit(1)
153+
print(f"All patches applied.")
151154

152155

153156
main()

0 commit comments

Comments
 (0)