diff options
| author | Loek Le Blansch <loek@pipeframe.xyz> | 2025-10-26 10:00:28 +0100 |
|---|---|---|
| committer | Loek Le Blansch <loek@pipeframe.xyz> | 2025-10-26 10:00:28 +0100 |
| commit | 0b7a11d7c9ad9288fcef9a0b26e81f129cc902d2 (patch) | |
| tree | d9dc1ec950e3450017fc76f0d13030fd9559d422 | |
| parent | a90ca9caf3b3a5f20f405512cd2d0875b1d723ae (diff) | |
add -c context option
| -rw-r--r-- | .editorconfig | 2 | ||||
| -rw-r--r-- | patchtree/cli.py | 9 | ||||
| -rw-r--r-- | patchtree/config.py | 1 | ||||
| -rw-r--r-- | patchtree/context.py | 43 | ||||
| -rw-r--r-- | patchtree/diff.py | 14 | ||||
| -rw-r--r-- | patchtree/patch.py | 2 |
6 files changed, 49 insertions, 22 deletions
diff --git a/.editorconfig b/.editorconfig index a5ddde0..22da3f7 100644 --- a/.editorconfig +++ b/.editorconfig @@ -2,7 +2,7 @@ root = true [*] end_of_line = lf -insert_final_newline = false +insert_final_newline = true max_line_length = 80 [*.py] diff --git a/patchtree/cli.py b/patchtree/cli.py index 7b1a108..2535256 100644 --- a/patchtree/cli.py +++ b/patchtree/cli.py @@ -33,11 +33,20 @@ def parse_arguments(config: Config) -> Context: help="patch target in-place", action="store_true", ) + parser.add_argument( + "-c", + "--context", + help="lines of context in output diff", + type=int, + ) parser.add_argument("target", help="target directory or archive") parser.add_argument("patch", help="patch input glob(s)", nargs="+") options = parser.parse_args() + if options.context is not None: + config.diff_context = options.context + try: return config.context(options) except Exception as e: diff --git a/patchtree/config.py b/patchtree/config.py index 555494e..743307b 100644 --- a/patchtree/config.py +++ b/patchtree/config.py @@ -58,6 +58,7 @@ class Config: default_factory=lambda: DEFAULT_DIFFS ) header: type[Header] = Header + diff_context: int = 3 def __post_init__(self): self.processors = {**DEFAULT_PROCESSORS, **self.processors} diff --git a/patchtree/context.py b/patchtree/context.py index c864307..554d09e 100644 --- a/patchtree/context.py +++ b/patchtree/context.py @@ -109,21 +109,13 @@ class Context: output: IO options: Namespace - cache: Path - def __init__(self, options: Namespace): self.options = options self.fs = self.get_fs() self.output = self.get_output() if self.options.in_place: - location = cast(DiskFS, self.fs).path - self.cache = location.joinpath(".patchtree.diff") - if self.cache.exists(): - run( - ("git", "apply", "--reverse", str(self.cache.absolute())), - cwd=str(location.absolute()), - ) + self.apply(True) def __del__(self): # patch must have a trailing newline @@ -131,15 +123,7 @@ class Context: self.output.flush() if self.options.in_place: - self.output.seek(0) - patch = self.output.read() - location = cast(DiskFS, self.fs).path - if len(patch) > 0: - self.cache.write_text(patch) - run( - ("git", "apply", str(self.cache.absolute())), - cwd=str(location.absolute()), - ) + self.apply(False) self.output.close() @@ -181,3 +165,26 @@ class Context: return open(self.options.out, "w+") return stdout + + def apply(self, reverse: bool) -> None: + location = cast(DiskFS, self.fs).path + cache = location.joinpath(".patchtree.diff") + + cmd = [ + "git", + "apply", + "--unidiff-zero", + "--allow-empty", + ] + + if reverse: + if not cache.exists(): + return + cmd.append("--reverse") + else: + self.output.seek(0) + patch = self.output.read() + cache.write_text(patch) + + cmd.append(str(cache.absolute())) + run(cmd, cwd=str(location.absolute())) diff --git a/patchtree/diff.py b/patchtree/diff.py index 9712f77..3982296 100644 --- a/patchtree/diff.py +++ b/patchtree/diff.py @@ -1,6 +1,12 @@ +from __future__ import annotations +from typing import TYPE_CHECKING + from dataclasses import dataclass from difflib import unified_diff +if TYPE_CHECKING: + from .config import Config + @dataclass class DiffFile: @@ -18,12 +24,14 @@ class Diff: with the file in the patch directory. """ + config: Config file: str a: DiffFile b: DiffFile - def __init__(self, file: str): + def __init__(self, config: Config, file: str): + self.config = config self.file = file def compare(self) -> str: @@ -58,7 +66,9 @@ class Diff: lines_a = a.lines() lines_b = b.lines() - diff = unified_diff(lines_a, lines_b, fromfile, tofile, lineterm="") + diff = unified_diff( + lines_a, lines_b, fromfile, tofile, lineterm="", n=self.config.diff_context + ) delta += "".join(f"{line}\n" for line in diff) return delta diff --git a/patchtree/patch.py b/patchtree/patch.py index 0b1f69c..d2d0ae8 100644 --- a/patchtree/patch.py +++ b/patchtree/patch.py @@ -61,7 +61,7 @@ class Patch: diff_class = self.get_diff() processor_classes = self.get_processors() - diff = diff_class(self.file) + diff = diff_class(self.config, self.file) diff.a = DiffFile( content=context.get_content(self.file), |