A diff shows the changes between trees, an index or the working dir.
- Repository.diff(a=None, b=None, cached=False, flags=0, context_lines=3, interhunk_lines=0)
Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, or changes between two blobs.
None, a str (that refers to an Object, see revparse_single()) or a Reference object. If None, b must be None, too. In this case the working directory is compared with the index. Otherwise the referred object is compared to ‘b’.
None, a str (that refers to an Object, see revparse_single()) or a Reference object. If None, the working directory is compared to ‘a’. (except ‘cached’ is True, in which case the index is compared to ‘a’). Otherwise the referred object is compared to ‘a’
If ‘b’ is None, by default the working directory is compared to ‘a’. If ‘cached’ is set to True, the index/staging area is used for comparing.
A combination of GIT_DIFF_* constants. For a list of the constants, with a description, see git_diff_option_t in https://github.com/libgit2/libgit2/blob/master/include/git2/diff.h
The number of unchanged lines that define the boundary of a hunk (and to display before and after)
The maximum number of unchanged lines between hunk boundaries before the hunks will be merged into a one
# Changes in the working tree not yet staged for the next commit >>> diff() # Changes between the index and your last commit >>> diff(cached=True) # Changes in the working tree since your last commit >>> diff('HEAD') # Changes between commits >>> t0 = revparse_single('HEAD') >>> t1 = revparse_single('HEAD^') >>> diff(t0, t1) >>> diff('HEAD', 'HEAD^') # equivalent
If you want to diff a tree against an empty tree, use the low level API (Tree.diff_to_tree()) directly.
# Changes between commits >>> t0 = revparse_single('HEAD') >>> t1 = revparse_single('HEAD^') >>> repo.diff(t0, t1) >>> t0.diff(t1) # equivalent >>> repo.diff('HEAD', 'HEAD^') # equivalent # Get all patches for a diff >>> diff = repo.diff('HEAD^', 'HEAD~3') >>> patches = [p for p in diff] # Get the stats for a diff >>> diff = repo.diff('HEAD^', 'HEAD~3') >>> diff.stats # Diffing the empty tree >>> tree = revparse_single('HEAD').tree >>> tree.diff_to_tree() # Diff empty tree to a tree >>> tree = revparse_single('HEAD').tree >>> tree.diff_to_tree(swap=True)
- class pygit2.Diff
Returns an iterator over the deltas/patches in this diff.
Returns the number of deltas/patches in this diff.
Iterate over the diff deltas.
- find_similar(flags: int = GIT_DIFF_FIND_BY_CONFIG, rename_threshold: int = 50, copy_threshold: int = 50, rename_from_rewrite_threshold: int = 50, break_rewrite_threshold: int = 60, rename_limit: int = 1000)
Transform a diff marking file renames, copies, etc.
This modifies a diff in place, replacing old entries that look like renames or copies with new entries reflecting those changes. This also will, if requested, break modified files into add/remove pairs if the amount of change is above a threshold.
flags - Combination of GIT_DIFF_FIND_* and GIT_DIFF_BREAK_* constants.
- static parse_diff(git_diff: str | bytes) Diff
Parses a git unified diff into a diff object without a repository
Patch diff string. Can be None in some cases, such as empty commits.
Accumulate diff statistics for all patches.
- class pygit2.Patch
Diff patch object.
- static create_from()
Create a patch from blobs, buffers, or a blob and a buffer
The raw bytes of the patch’s contents.
Get the delta associated with a patch.
Get line counts of each type in a patch (context, additions, deletions).
Patch diff string. Can be None in some cases, such as empty commits. Note that this decodes the content to Unicode assuming UTF-8 encoding. For non-UTF-8 content that can lead be a lossy, non-reversible process. To access the raw, un-decoded patch, use patch.data.
- class pygit2.DiffDelta
Combination of GIT_DIFF_FLAG_* flags.
True if binary data, False if text, None if not (yet) known.
“to” side of the diff.
Number of files in the delta.
“from” side of the diff.
For renamed and copied.
A GIT_DELTA_* constant.
- status_char() str
Return the single character abbreviation for a delta status code.
- class pygit2.DiffStats
Total number of deletions
Total number of files changed
- format(format: int, width: int) str
Format the stats as a string.
The format to use. A pygit2.GIT_DIFF_STATS_* constant.
The width of the output. The output will be scaled to fit.
Total number of insertions
- class pygit2.DiffLine
Content of the diff line
Offset in the original file to the content
Line number in new file or -1 for deleted line
Number of newline characters in content
Line number in old file or -1 for added line
Type of the diff line
Content of the diff line (byte string)