Diff

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, orchanges between two blobs.

Keyword arguments:

  • a
  • None, a str (that refers to an Object, see revparse_single()) or aReference object.If None, b must be None, too. In this case the working directory iscompared with the index. Otherwise the referred object is compared to‘b’.
  • b
  • None, a str (that refers to an Object, see revparse_single()) or aReference 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’
  • cached
  • 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.
  • flag
  • a GITDIFF* constant
  • context_lines
  • the number of unchanged lines that define the boundaryof a hunk (and to display before and after)
  • interhunk_lines
  • the maximum number of unchanged lines between hunkboundaries before the hunks will be merged into a one
    Examples:
  1. # Changes in the working tree not yet staged for the next commit
  2. >>> diff()
  3.  
  4. # Changes between the index and your last commit
  5. >>> diff(cached=True)
  6.  
  7. # Changes in the working tree since your last commit
  8. >>> diff('HEAD')
  9.  
  10. # Changes between commits
  11. >>> t0 = revparse_single('HEAD')
  12. >>> t1 = revparse_single('HEAD^')
  13. >>> diff(t0, t1)
  14. >>> diff('HEAD', 'HEAD^') # equivalent

If you want to diff a tree against an empty tree, use the low levelAPI (Tree.diff_to_tree()) directly.

Examples

  1. # Changes between commits
  2. >>> t0 = revparse_single('HEAD')
  3. >>> t1 = revparse_single('HEAD^')
  4. >>> repo.diff(t0, t1)
  5. >>> t0.diff(t1) # equivalent
  6. >>> repo.diff('HEAD', 'HEAD^') # equivalent
  7.  
  8. # Get all patches for a diff
  9. >>> diff = repo.diff('HEAD^', 'HEAD~3')
  10. >>> patches = [p for p in diff]
  11.  
  12. # Get the stats for a diff
  13. >>> diff = repo.diff('HEAD^', 'HEAD~3')
  14. >>> diff.stats
  15.  
  16. # Diffing the empty tree
  17. >>> tree = revparse_single('HEAD').tree
  18. >>> tree.diff_to_tree()
  19.  
  20. # Diff empty tree to a tree
  21. >>> tree = revparse_single('HEAD').tree
  22. >>> tree.diff_to_tree(swap=True)

The Diff type

  • class pygit2.Diff
  • Diff objects.

    • iter()
    • Returns an iterator over the deltas/patches in this diff.

    • len()

    • Returns the number of deltas/patches in this diff.

    • deltas

    • Iterate over the diff deltas.

    • findsimilar([_flags, rename_threshold, copy_threshold, rename_from_rewrite_threshold, break_rewrite_threshold, rename_limit])

    • Find renamed files in diff and updates them in-place in the diff itself.

    • merge(diff)

    • Merge one diff into another.

    • static parsediff(_git_diff: str) → Diff

    • Parses a git unified diff into a diff object without a repository

    • patch

    • Patch diff string. Can be None in some cases, such as empty commits.

    • stats

    • Accumulate diff statistics for all patches.

The Patch type

Attributes:

  • class pygit2.Patch
  • Diff patch object.

    • static create_from()
    • Create a patch from blobs, buffers, or a blob and a buffer

    • data

    • The raw bytes of the patch’s contents.

    • delta

    • Get the delta associated with a patch.

    • hunks

    • line_stats
    • Get line counts of each type in a patch.

    • text

    • 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.

The DiffDelta type

  • class pygit2.DiffDelta
  • DiffDelta object.

    • flags
    • Combination of GITDIFF_FLAG* flags.

    • is_binary

    • True if binary data, False if not.

    • new_file

    • “to” side of the diff.

    • nfiles

    • Number of files in the delta.

    • old_file

    • “from” side of the diff.

    • similarity

    • For renamed and copied.

    • status

    • A GITDELTA* constant.

    • status_char()

    • Return the single character abbreviation for a delta status code.

The DiffFile type

  • class pygit2.DiffFile
  • DiffFile object.

    • flags
    • Combination of GITDIFF_FLAG* flags.

    • id

    • Oid of the item.

    • mode

    • Mode of the entry.

    • path

    • Path to the entry.

    • raw_path

    • Path to the entry (bytes).

    • size

    • Size of the entry.

The DiffHunk type

  • class pygit2.DiffHunk
  • DiffHunk object.

    • header
    • Header.

    • lines

    • Lines.

    • new_lines

    • New lines.

    • new_start

    • New start.

    • old_lines

    • Old lines.

    • old_start

    • Old start.

The DiffStats type

  • class pygit2.DiffStats
  • DiffStats object.

    • deletions
    • Total number of deletions

    • files_changed

    • Total number of files changed

    • format(format, width)

    • Format the stats as a string.

Returns: str.

Parameters:

  1. - format
  2. - The format to use. A pygit2.GIT_DIFF_STATS_* constant.
  3. - width
  4. - The width of the output. The output will be scaled to fit.
  • insertions
  • Total number of insertions

The DiffLine type

  • class pygit2.DiffLine
  • DiffLine object.

    • content
    • Content of the diff line

    • content_offset

    • Offset in the original file to the content

    • new_lineno

    • Line number in new file or -1 for deleted line

    • num_lines

    • Number of newline characters in content

    • old_lineno

    • Line number in old file or -1 for added line

    • origin

    • Type of the diff line

    • raw_content

    • Content of the diff line (byte string)