Skip to content

Commit

Permalink
fix: apply with fuzzy lines
Browse files Browse the repository at this point in the history
Signed-off-by: jingfelix <jingfelix@outlook.com>
  • Loading branch information
jingfelix committed Oct 8, 2024
1 parent b7c33d6 commit 7b04074
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Modern patch, written in Python.

## Usage

```shell
patche apply <patch-file>
```

## Config

`patche` loads the configuration from a file named `.patche.env` in `$HOME`.
Expand Down
8 changes: 4 additions & 4 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies = [
"pydantic>=2.5.3",
"pydantic-settings>=2.2.1",
"cscopy>=0.0.1",
"whatthepatch-pydantic==1.0.6a1",
"whatthepatch-pydantic==1.0.6a2",
"structlog>=24.2.0",
]
requires-python = ">=3.10"
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ typer==0.12.3
typer[all]==0.12.3
typing-extensions==4.12.2
viztracer==0.16.3
whatthepatch-pydantic==1.0.6a1
whatthepatch-pydantic==1.0.6a2
17 changes: 12 additions & 5 deletions src/Patche/commands/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ def apply(

if not os.path.exists(patch_path):
logger.error(f"Warning: {patch_path} not found!")
return
raise typer.Exit(code=1)

if reverse:
logger.info("Reversing patch...")

has_failed = False

with open(patch_path, mode="r", encoding="utf-8") as (f):
diffes = parse_patch(f.read()).diff

Expand All @@ -47,12 +49,17 @@ def apply(

# 检查失败数
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)

# 写入文件
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")
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)
24 changes: 21 additions & 3 deletions src/Patche/utils/resolve.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from Patche.app import logger
from Patche.config import settings
from Patche.model import ApplyResult, Change, Hunk, Line
from Patche.model import ApplyResult, Hunk, Line
from Patche.utils.common import find_list_positions


Expand All @@ -25,14 +25,15 @@ def apply_change(

# 然后对每个hunk进行处理,添加偏移
failed_hunk_list: list[Hunk] = []
last_pos = None
for hunk in hunk_list:

current_hunk_fuzz = 0

while current_hunk_fuzz <= fuzz:

hunk.context = hunk.context[1:]
hunk.post = hunk.post[: fuzz - current_hunk_fuzz]
# hunk.context = hunk.context[1:]
# hunk.post = hunk.post[: fuzz - current_hunk_fuzz]

logger.debug(
f"current_fuzz: {current_hunk_fuzz} len(hunk.context): {len(hunk.context)} len(hunk.post): {len(hunk.post)}"
Expand All @@ -49,6 +50,10 @@ def apply_change(

current_hunk_fuzz += 1

if current_hunk_fuzz <= fuzz:
hunk.context = hunk.context[1:]
hunk.post = hunk.post[: 3 - current_hunk_fuzz]

# 初始位置是 context 的第一个
# 注意,前几个有可能是空
pos_origin = None
Expand Down Expand Up @@ -88,6 +93,19 @@ def apply_change(
# 选择 offset 最小的 pos
pos_new = pos_origin + min_offset - 1

# 处理 pos_new 小于 last_pos 的情况
logger.debug(f"pos_origin: {pos_origin}, last_pos: {last_pos}")
if last_pos is None:
last_pos = pos_new
elif pos_new < last_pos:
# 特别主要 pos_new 小于 last_pos 的情况
logger.warning(f"Apply failed with hunk {hunk.index}")
logger.error(f"pos: {pos_new} is greater than last_pos: {last_pos}")
failed_hunk_list.append(hunk)
continue
else:
last_pos = pos_new

old_lines = [
change.line
for change in hunk.context + hunk.middle + hunk.post
Expand Down

0 comments on commit 7b04074

Please sign in to comment.