Skip to main content

Module format_patch

Module format_patch 

Source
Expand description

git format-patch mail-rendering primitives.

The full format-patch command in the grit binary parses argv, walks the revision range, reads config, runs rename detection, talks to the notes / range-diff machinery, decides on output files vs. stdout, prints the written filenames, and maps exit codes. Those responsibilities — argv parsing, revision selection, config/env resolution, file output, terminal printing, and the crate-internal log/range-diff dispatch — stay in the CLI.

What lives here is the self-contained, presentation-free core of mbox patch assembly: the byte-exact RFC 2047 / RFC 822 header encoders and folders, the [PATCH n/m] subject builder, the filename sanitizer/truncator, the committer-date formatter, the threading-header writer, and the small string transforms (mboxrd escaping, ident formatting, reroll/version labels) that git’s pretty.c, utf8.c, and builtin/log.c use to turn a commit plus its diff into an email. Each function computes a result from plain strings, bytes, or a CommitData alone — no argv, no terminal output, and no process/filesystem state — so the CLI can call them while still owning every I/O and config decision.

Enums§

Rfc2047Type

Functions§

add_rfc822_quoted
add_rfc2047
Append line RFC2047-Q-encoded to out, folding at 76 columns with continuation lines.
add_wrapped_text
Port of git’s strbuf_add_wrapped_text for ASCII text (used for subject/From folding). indent1 negative means -indent1 columns are already consumed on the current line.
apply_rfc_prefix
Apply the --rfc[=<str>] modifier to a subject prefix. Default RFC prepends “RFC “; a value starting with - appends (...); else replaces leader.
build_patch_filename
Build the full patch basename <file_prefix><NNNN>-<sanitized-subject>.patch, truncating the whole basename to filename_max_length - 1 chars (Git’s FORMAT_PATCH_NAME_MAX, default 64).
build_patch_subject
Build a patch Subject value: [<prefix> n/m] <subject> with proper handling of empty prefix.
commit_author_timestamp
encode_display_name
Encode a display name portion of an email address.
encode_email_address
Encode an email address for use in email headers.
encode_email_address_for_charset
Like encode_email_address but uses charset_label for RFC 2047 when non-ASCII.
ensure_trailing_slash
Ensure a directory prefix ends with /.
first_subject_line
The first physical line of the subject (used for the patch filename, matching git which stops format_sanitized_subject at the first newline). Returns the whole trimmed message if single-line.
flatten_subject
Flatten a multi-line commit message into a single-line subject (paragraph join with spaces).
format_date_rfc2822
Extract date from identity string and format as RFC 2822-like.
format_ident
Format an identity string as “Name ”.
is_valid_from_ident
Validate a --from=<ident> value: must look like an email ident (contain @).
last_line_length
Length of the last line of s (bytes after the final \n).
mboxrd_escape
Apply mboxrd >From escaping to body lines if mboxrd is set.
needs_rfc822_quoting
needs_rfc2047_encoding
True if line needs RFC2047 encoding (non-ASCII, newline, or =?).
notes_value_to_ref
Map a format.notes / --notes= value to a full notes ref (refs/notes/<x> unless already a full refs/... ref). An empty value or true means the default refs/notes/commits.
path_matches_spec
prev_version_label
Interdiff against v<N-1>: label, or None if reroll is not an integer >= 2.
push_indented
Append body to out, indenting every line by two spaces (matching sed -e "s/^/ /").
rfc2047_charset_label
Charset token for RFC 2047 =?charset?q?...?= (matches Git test expectations).
rfc2047_encode
RFC 2047 UTF-8 quoted-printable encoding for an email display name.
sanitize_reroll
Sanitize a reroll-count string for use in a filename prefix (v<x>-), like git’s sanitizer.
sanitize_subject
Sanitize a subject line for use as a filename, matching Git’s format_sanitized_subject byte-for-byte: runs of non-title bytes collapse into a single -, consecutive . collapse into one, and trailing ./- are trimmed. Operates on raw bytes (non-ASCII → separators).
strip_angles
write_addr_header
Write a From:/recipient address header <Name> <mail>, encoding/folding the display name.
write_folded_header
Write a folded email header with multiple values.
write_signature
Write the trailing signature block -- \n<sig>\n\n, or nothing when suppressed.
write_subject_header
Write the Subject: header (already-built subject string), encoding/folding like git.
write_thread_headers
Write In-Reply-To / References / Message-ID threading headers.