From 29041f25703416efab1552b7d8fc2f1e58d9bd36 Mon Sep 17 00:00:00 2001 From: Ben Totten Date: Thu, 21 Mar 2024 22:59:12 +0000 Subject: [PATCH] diff: Add option to specify lines of context to diff function --- git/diff.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/git/diff.py b/git/diff.py index 0e39fe7a8..b53b9e26e 100644 --- a/git/diff.py +++ b/git/diff.py @@ -185,6 +185,7 @@ def diff( other: Union[DiffConstants, "Tree", "Commit", str, None] = INDEX, paths: Union[PathLike, List[PathLike], Tuple[PathLike, ...], None] = None, create_patch: bool = False, + lines_of_context: int = 3, **kwargs: Any, ) -> "DiffIndex": """Create diffs between two items being trees, trees and index or an index and @@ -214,6 +215,10 @@ def diff( applied makes the self to other. Patches are somewhat costly as blobs have to be read and diffed. + :param line_of_context: + Default 3. Specify the lines of context before and after each change + introduced in the patch. This cooresponds to the -U flag for git diff. + :param kwargs: Additional arguments passed to :manpage:`git-diff(1)`, such as ``R=True`` to swap both sides of the diff. @@ -241,6 +246,9 @@ def diff( args.append("--raw") args.append("-z") + if lines_of_context: + args.append(f"-U{lines_of_context}") + # Ensure we never see colored output. # Fixes: https://github.com/gitpython-developers/GitPython/issues/172 args.append("--no-color")