Classes for dealing with git am-style patches.
These patches are basically unified diffs with some extra metadata tacked on.
| Class | |
Represents a patch for a single file. |
| Class | |
Result of mailinfo parsing. |
| Class | |
Represents a single hunk in a unified diff. |
| Exception | |
Raised when a requested diff algorithm is not available. |
| Exception | |
Raised when a patch does not apply cleanly. |
| Function | apply |
Apply patch hunks to file content. |
| Function | apply |
Apply a list of file patches to a repository. |
| Function | commit |
Compute patch ID for a commit. |
| Function | gen |
Write a blob diff header. |
| Function | get |
Determine the summary line for use in a filename. |
| Function | git |
Parse a git-am-style patch and split it up into bits. |
| Function | git |
Decode Git's base85-encoded binary data. |
| Function | is |
See if the first few bytes contain any null characters. |
| Function | mailinfo |
Extract patch information from an email message. |
| Function | parse |
Extract a Commit object and patch from an e-mail message. |
| Function | parse |
Parse a unified diff into FilePatch objects. |
| Function | patch |
Generate patch filename. |
| Function | patch |
Compute patch ID for a diff. |
| Function | shortid |
Get short object ID. |
| Function | unified |
difflib.unified_diff that can detect "No newline at end of file" as original "git diff" does. |
| Function | unified |
Generate unified diff with specified algorithm. |
| Function | write |
Write blob diff. |
| Function | write |
Write a individual file patch. |
| Function | write |
Write the diff for an object. |
| Function | write |
Write tree diff. |
| Constant | DEFAULT |
Undocumented |
| Constant | FIRST |
Undocumented |
| Function | _apply |
Apply a rename or copy operation. |
| Function | _find |
Find the scissors line in message body. |
| Function | _format |
Convert range to the "ed" format. |
| Function | _get |
Get appropriate sequence matcher for the given algorithm. |
| Function | _munge |
Munge email subject line for commit message. |
Repo, patches: list[ FilePatch], cached: bool = False, reverse: bool = False, check: bool = False, strip: int = 1, three_way: bool = False):
¶
Apply a list of file patches to a repository.
| Parameters | |
r:Repo | Repository object |
patches:list[ | List of FilePatch objects to apply |
cached:bool | Apply patch to index only, not working tree |
reverse:bool | Apply patch in reverse |
check:bool | Only check if patch can be applied, don't apply |
strip:int | Number of leading path components to strip (default: 1) |
threebool | Fall back to 3-way merge if patch does not apply cleanly |
| Raises | |
ValueError | If patch cannot be applied |
Compute patch ID for a commit.
| Parameters | |
store:BaseObjectStore | Object store to read objects from |
commitObjectID | RawObjectID | Commit ID (40-byte hex string) |
| Returns | |
bytes | Patch ID (40-byte hex string) |
Decode Git's base85-encoded binary data.
Git uses a custom base85 encoding with its own alphabet and line format. Each line starts with a length byte followed by base85-encoded data.
| Parameters | |
data:bytes | Base85-encoded data as bytes (may contain multiple lines) |
| Returns | |
bytes | Decoded binary data |
| Raises | |
ValueError | If the data is invalid |
email.message.Message | BinaryIO | TextIO, keep_subject: bool = False, keep_non_patch: bool = False, encoding: str | None = None, scissors: bool = False, message_id: bool = False) -> MailinfoResult:
¶
Extract patch information from an email message.
This function parses an email message and extracts commit metadata (author, email, subject) and separates the commit message from the patch content, similar to git mailinfo.
| Parameters | |
msg:email.message.Message | BinaryIO | TextIO | Email message (email.message.Message object) or file handle to read from |
keepbool | If True, keep subject intact without munging (-k) |
keepbool | If True, only strip [PATCH] from brackets (-b) |
encoding:str | None | Character encoding to use (default: detect from message) |
scissors:bool | If True, remove everything before scissors line |
messagebool | If True, include Message-ID in commit message (-m) |
| Returns | |
MailinfoResult | MailinfoResult with parsed information |
| Raises | |
ValueError | If message is malformed or missing required fields |
email.message.Message, encoding: str | None = None) -> tuple[ Commit, bytes, bytes | None]:
¶
Extract a Commit object and patch from an e-mail message.
Returns: Tuple with commit object, diff contents and git version
| Parameters | |
msg:email.message.Message | An email message (email.message.Message) |
encoding:str | None | Encoding to use to encode Git commits |
| Returns | |
tuple[ | Undocumented |
Compute patch ID for a diff.
The patch ID is computed by normalizing the diff and computing a SHA1 hash. This follows git's patch-id algorithm which: 1. Removes whitespace from lines starting with + or - 2. Replaces line numbers in @@ headers with a canonical form 3. Computes SHA1 of the result
TODO: This implementation uses a simple line-by-line approach. For better compatibility with git's patch-id, consider using proper patch parsing that: - Handles edge cases in diff format (binary diffs, mode changes, etc.) - Properly parses unified diff format according to the spec - Matches git's exact normalization algorithm byte-for-byte See git's patch-id.c for reference implementation.
| Parameters | |
diffbytes | Raw diff data as bytes |
| Returns | |
bytes | SHA1 hash of normalized diff (40-byte hex string) |
Sequence[ bytes], b: Sequence[ bytes], fromfile: bytes = b'', tofile: bytes = b'', fromfiledate: str = '', tofiledate: str = '', n: int = 3, lineterm: str = '\n', tree_encoding: str = 'utf-8', output_encoding: str = 'utf-8') -> Generator[ bytes, None, None]:
¶
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
Sequence[ bytes], b: Sequence[ bytes], fromfile: bytes = b'', tofile: bytes = b'', fromfiledate: str = '', tofiledate: str = '', n: int = 3, lineterm: str = '\n', tree_encoding: str = 'utf-8', output_encoding: str = 'utf-8', algorithm: str | None = None) -> Generator[ bytes, None, None]:
¶
Generate unified diff with specified algorithm.
| Parameters | |
a:Sequence[ | First sequence of lines |
b:Sequence[ | Second sequence of lines |
fromfile:bytes | Name of first file |
tofile:bytes | Name of second file |
fromfiledate:str | Date of first file |
tofiledate:str | Date of second file |
n:int | Number of context lines |
lineterm:str | Line terminator |
treestr | Encoding for tree paths |
outputstr | Encoding for output |
algorithm:str | None | Diff algorithm to use ("myers" or "patience") |
| Returns | |
Generator[ | Generator yielding diff lines |
| Raises | |
DiffAlgorithmNotAvailable | If patience algorithm requested but patiencediff not available |
IO[ bytes], old_file: tuple[ bytes | None, int | None, Blob | None], new_file: tuple[ bytes | None, int | None, Blob | None], diff_algorithm: str | None = None):
¶
Write blob diff.
Note: The use of write_object_diff is recommended over this function.
| Parameters | |
f:IO[ | File-like object to write to |
oldtuple[ | (path, mode, hexsha) tuple (None if nonexisting) |
newtuple[ | (path, mode, hexsha) tuple (None if nonexisting) |
diffstr | None | Algorithm to use for diffing ("myers" or "patience") |
IO[ bytes], commit: Commit, contents: str | bytes, progress: tuple[ int, int], version: str | None = None, encoding: str | None = None):
¶
Write a individual file patch.
| Parameters | |
f:IO[ | File-like object to write to |
commit:Commit | Commit object |
contents:str | bytes | Contents of the patch |
progress:tuple[ | tuple with current patch number and total. |
version:str | None | Version string to include in patch header |
encoding:str | None | Encoding to use for the patch |
| Returns | |
| tuple with filename and contents |
IO[ bytes], store: BaseObjectStore, old_file: tuple[ bytes | None, int | None, ObjectID | None], new_file: tuple[ bytes | None, int | None, ObjectID | None], diff_binary: bool = False, diff_algorithm: str | None = None):
¶
Write the diff for an object.
Note: the tuple elements should be None for nonexistent files
| Parameters | |
f:IO[ | File-like object to write to |
store:BaseObjectStore | Store to retrieve objects from, if necessary |
oldtuple[ | (path, mode, hexsha) tuple |
newtuple[ | (path, mode, hexsha) tuple |
diffbool | Whether to diff files even if they are considered binary files by is_binary(). |
diffstr | None | Algorithm to use for diffing ("myers" or "patience") |
IO[ bytes], store: BaseObjectStore, old_tree: ObjectID | None, new_tree: ObjectID | None, diff_binary: bool = False, diff_algorithm: str | None = None):
¶
Write tree diff.
| Parameters | |
f:IO[ | File-like object to write to. |
store:BaseObjectStore | Object store to read from |
oldObjectID | None | Old tree id |
newObjectID | None | New tree id |
diffbool | Whether to diff files even if they are considered binary files by is_binary(). |
diffstr | None | Algorithm to use for diffing ("myers" or "patience") |
Repo, src_path: bytes, dst_path: bytes, strip: int, patch: FilePatch, is_rename: bool, cached: bool, check: bool) -> tuple[ list[ bytes] | None, bool]:
¶
Apply a rename or copy operation.
| Parameters | |
r:Repo | Repository object |
srcbytes | Source path |
dstbytes | Destination path |
strip:int | Number of path components to strip |
patch:FilePatch | FilePatch object |
isbool | True for rename, False for copy |
cached:bool | Apply to index only, not working tree |
check:bool | Check only, don't apply |
| Returns | |
A tuple of (original_lines, should_continue) where |
|
str, a: Sequence[ bytes], b: Sequence[ bytes]) -> SequenceMatcher[ bytes]:
¶
Get appropriate sequence matcher for the given algorithm.
| Parameters | |
algorithm:str | Diff algorithm ("myers" or "patience") |
a:Sequence[ | First sequence |
b:Sequence[ | Second sequence |
| Returns | |
SequenceMatcher[ | Configured sequence matcher instance |
| Raises | |
DiffAlgorithmNotAvailable | If patience requested but not available |