dulwich.patch module

Classes for dealing with git am-style patches.

These patches are basically unified diffs with some extra metadata tacked on.

dulwich.patch.gen_diff_header(paths, modes, shas)

Write a blob diff header.

Parameters
  • paths – Tuple with old and new path

  • modes – Tuple with old and new modes

  • shas – Tuple with old and new shas

dulwich.patch.get_summary(commit)

Determine the summary line for use in a filename.

Parameters

commit – Commit

Returns: Summary string

dulwich.patch.git_am_patch_split(f, encoding=None)

Parse a git-am-style patch and split it up into bits.

Parameters
  • f – File-like object to parse

  • encoding – Encoding to use when creating Git objects

Returns: Tuple with commit object, diff contents and git version

dulwich.patch.is_binary(content)

See if the first few bytes contain any null characters.

Parameters

content – Bytestring to check for binary content

dulwich.patch.parse_patch_message(msg, encoding=None)

Extract a Commit object and patch from an e-mail message.

Parameters
  • msg – An email message (email.message.Message)

  • encoding – Encoding to use to encode Git commits

Returns: Tuple with commit object, diff contents and git version

dulwich.patch.patch_filename(p, root)
dulwich.patch.shortid(hexsha)
dulwich.patch.unified_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n', tree_encoding='utf-8', output_encoding='utf-8')

difflib.unified_diff that can detect “No newline at end of file” as original “git diff” does.

Based on the same function in Python2.7 difflib.py

dulwich.patch.write_blob_diff(f, old_file, new_file)

Write blob diff.

Parameters
  • f – File-like object to write to

  • old_file – (path, mode, hexsha) tuple (None if nonexisting)

  • new_file – (path, mode, hexsha) tuple (None if nonexisting)

Note: The use of write_object_diff is recommended over this function.

dulwich.patch.write_commit_patch(f, commit, contents, progress, version=None, encoding=None)

Write a individual file patch.

Parameters
  • commit – Commit object

  • progress – Tuple with current patch number and total.

Returns

tuple with filename and contents

dulwich.patch.write_object_diff(f, store, old_file, new_file, diff_binary=False)

Write the diff for an object.

Parameters
  • f – File-like object to write to

  • store – Store to retrieve objects from, if necessary

  • old_file – (path, mode, hexsha) tuple

  • new_file – (path, mode, hexsha) tuple

  • diff_binary – Whether to diff files even if they are considered binary files by is_binary().

Note: the tuple elements should be None for nonexistant files

dulwich.patch.write_tree_diff(f, store, old_tree, new_tree, diff_binary=False)

Write tree diff.

Parameters
  • f – File-like object to write to.

  • old_tree – Old tree id

  • new_tree – New tree id

  • diff_binary – Whether to diff files even if they are considered binary files by is_binary().