代码拉取完成,页面将自动刷新
import os
import re
from pathlib import Path
def apply_patch(patch_file, target_folder):
with open(patch_file, 'r') as f:
patch_content = f.read()
# Split the patch into individual file diffs
file_diffs = re.split(r'diff --git ', patch_content)[1:]
for diff in file_diffs:
# Extract the file path
file_path = diff.split('\n')[0].split()[-1][2:]
target_file = Path(target_folder) / file_path
if not target_file.exists():
print(f"Warning: Target file {target_file} does not exist. Skipping.")
continue
# Read the original file content
with open(target_file, 'r') as f:
original_content = f.readlines()
# Parse the diff hunks
hunks = re.findall(r'@@ -(\d+),(\d+) \+(\d+),(\d+) @@([\s\S]+?)(?=@@|\Z)', diff)
# Apply the changes
new_content = original_content.copy()
offset = 0
for hunk in hunks:
start = int(hunk[2]) - 1 + offset
removed = int(hunk[1])
added = int(hunk[3])
changes = hunk[4].strip().split('\n')
# Remove lines
del new_content[start:start + removed]
# Add new lines
for change in changes:
if change.startswith('+'):
new_content.insert(start, change[1:] + '\n')
start += 1
offset += added - removed
# Write the modified content back to the file
with open(target_file, 'w') as f:
f.writelines(new_content)
print(f"Applied changes to {target_file}")
# Usage
patch_file = '/media/tom/1T-Disk/kernel/patch-5.10.209-rt101.patch'
target_folder = '/media/tom/1T-Disk/linux-5.10.209(test)'
apply_patch(patch_file, target_folder)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。