# Diff

Contents

- Diff
  - The Diff type
  - The Patch type
  - The DiffDelta type
  - The DiffFile type
  - The DiffHunk type
  - The DiffStats type
  - The DiffLine type

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.

  Keyword arguments:

  cached
    use staged changes instead of workdir
  flag
    a GIT_DIFF_* constant
  context_lines
    the number of unchanged lines that define the boundary of a hunk (and to display before and after)
  interhunk_lines
    the maximum number of unchanged lines between hunk boundaries before the hunks will be merged into a one

  Examples:

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

Examples

    # 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)

## The Diff type

`Diff.``patch`

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

`Diff.``__iter__`()

  Returns an iterator over the deltas/patches in this diff.

`Diff.``__len__`()

  Returns the number of deltas/patches in this diff.

`Diff.``merge`(diff)

  Merge one diff into another.

`Diff.``find_similar`([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.

## The Patch type

Attributes:

`Patch.``delta`

  Get the delta associated with a patch.

`Patch.``hunks`

`Patch.``line_stats`

  Get line counts of each type in a patch.

## The DiffDelta type

Attributes:

`DiffDelta.``old_file`

  “from” side of the diff.

`DiffDelta.``new_file`

  “to” side of the diff.

`DiffDelta.``status`

  A GIT_DELTA_* constant.

`DiffDelta.``similarity`

  For renamed and copied.

Getters:

`DiffDelta.``is_binary`

  True if binary data, False if not.

## The DiffFile type

Attributes:

`DiffFile.``path`

  Path to the entry.

`DiffFile.``id`

  Oid of the item.

`DiffFile.``size`

  Size of the entry.

`DiffFile.``flags`

  Combination of GIT_DIFF_FLAG_* flags.

`DiffFile.``mode`

  Mode of the entry.

## The DiffHunk type

`DiffHunk.``old_start`

  Old start.

`DiffHunk.``old_lines`

  Old lines.

`DiffHunk.``new_start`

  New start.

`DiffHunk.``new_lines`

  New lines.

`DiffHunk.``lines`

  Lines.

## The DiffStats type

`DiffStats.``insertions`

  Total number of insertions

`DiffStats.``deletions`

  Total number of deletions

`DiffStats.``files_changed`

  Total number of files changed

`DiffStats.``format`(format, width) → str

  Format the stats as a string

  Arguments:

  format
    The format to use. A pygit2.GIT_DIFF_STATS_* constant
  width
    The width of the output. The output will be scaled to fit.

## The DiffLine type

`DiffLine.``origin`

  Type of the diff line

`DiffLine.``content`

  Content of the diff line

`DiffLine.``old_lineno`

  Line number in old file or -1 for added line

`DiffLine.``old_lineno`

  Line number in old file or -1 for added line

`DiffLine.``num_lines`

  Number of newline characters in content