Skip to content
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

feat: support empty file (/dev/null) #1

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
hooks:
- id: black
- repo: https://github.com/pdm-project/pdm
rev: 2.17.3
rev: 2.22.0
hooks:
- id: pdm-export
args: ['-o', 'requirements.txt', '--without-hashes']
Expand Down
255 changes: 131 additions & 124 deletions pdm.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ dependencies = [
"typer[all]>=0.9.0",
"pydantic>=2.5.3",
"pydantic-settings>=2.2.1",
"cscopy>=0.0.1",
"whatthepatch-pydantic==1.0.6a2",
]
requires-python = ">=3.10"
Expand Down
16 changes: 7 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
annotated-types==0.7.0
click==8.1.7
colorama==0.4.6; platform_system == "Windows"
cscopy==0.0.1
markdown-it-py==3.0.0
mdurl==0.1.2
objprint==0.2.3
pydantic==2.8.2
pydantic-core==2.20.1
pydantic-settings==2.4.0
objprint==0.3.0
pydantic==2.10.3
pydantic-core==2.27.1
pydantic-settings==2.7.0
pygments==2.18.0
python-dotenv==1.0.1
rich==13.7.1
rich==13.9.4
shellingham==1.5.4
typer==0.12.3
typer[all]==0.12.3
typer[all]==0.15.1
typing-extensions==4.12.2
viztracer==0.16.3
viztracer==1.0.0
whatthepatch-pydantic==1.0.6a2
File renamed without changes.
69 changes: 49 additions & 20 deletions src/Patche/commands/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,58 @@ def apply(
old_filename = diff.header.old_path
new_filename = diff.header.new_path

if os.path.exists(old_filename):
logger.debug(f"old_filename: {old_filename} new_filename: {new_filename}")

logger.info(f"Applying patch to {old_filename}...")
if old_filename == "/dev/null":
# 根据 diffes 创建新文件
try:
assert len(diff.hunks) == 1

new_line_list = File(file_path=old_filename).line_list
apply_result = apply_change(
diff.hunks, new_line_list, reverse=reverse, fuzz=fuzz
)
new_line_list = apply_result.new_line_list
new_line_list = []
for line in diff.changes:

# 检查失败数
for failed_hunk in apply_result.failed_hunk_list:
has_failed = True
logger.error(f"Failed hunk: {failed_hunk.index}")
else:
logger.error(f"{old_filename} not found!")
raise typer.Exit(code=1)

# 写入文件
if not has_failed:
with open(new_filename, mode="w+", encoding="utf-8") as f:
for line in new_line_list:
if line.status:
assert line.old is None
new_line_list.append(line)

with open(new_filename, mode="w+", encoding="utf-8") as f:
for line in new_line_list:
f.write(line.content + "\n")

except AssertionError:
logger.error("Failed to create new file: invalid diff!")
raise typer.Exit(code=1)

elif new_filename == "/dev/null":
# 移除 old_filename
if os.path.exists(old_filename):
os.remove(old_filename)
else:
logger.error(f"{old_filename} not found!")
raise typer.Exit(code=1)
else:
if os.path.exists(old_filename):

logger.info(f"Applying patch to {old_filename}...")

new_line_list = File(file_path=old_filename).line_list
apply_result = apply_change(
diff.hunks, new_line_list, reverse=reverse, fuzz=fuzz
)
new_line_list = apply_result.new_line_list

# 检查失败数
for failed_hunk in apply_result.failed_hunk_list:
has_failed = True
logger.error(f"Failed hunk: {failed_hunk.index}")
else:
logger.error(f"{old_filename} not found!")
raise typer.Exit(code=1)

# 写入文件
if not has_failed:
with open(new_filename, mode="w+", encoding="utf-8") as f:
for line in new_line_list:
if line.status:
f.write(line.content + "\n")

raise typer.Exit(code=1 if has_failed else 0)
Loading