| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
This package is not in the latest version of its module.
Go to latest Published: Jul 3, 2026 License: MITThe Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Modules with tagged versions give importers more predictable builds.
When a project reaches major version v1 it is considered stable.
Package mailpatch parses git "format-patch" emails into structured data.
`git format-patch` turns commits into RFC 5322 email messages: the commit subject becomes the mail Subject (prefixed with "[PATCH n/m]"), the author and date become headers, the commit message becomes the body, and the diff follows after a "---" separator and a diffstat. `git send-email` mails those out; reviewers reply, and maintainers feed them back to `git am`.
mailpatch reads one of those messages — or a whole mbox of them — and gives you the pieces without shelling out to git:
It depends only on the standard library and never executes git.
This section is empty.
var ( // ErrEmpty is returned when the input has no message at all. ErrEmpty = errors.New("mailpatch: empty input") // ErrMalformed is returned when the input is not a parseable RFC 5322 // message (bad headers, truncated mid-header, and similar). ErrMalformed = errors.New("mailpatch: malformed message") )
Sentinel errors. Compare with errors.Is.
func Format(opts FormatOptions) ([]byte, error)
Format constructs a raw RFC 5322 format-patch email from structured data. The returned bytes are suitable for sending via SMTP or saving as an mbox entry.
SplitBodyDiff is the exported version of splitBodyDiff, allowing callers to separate a commit message from its diff without fully parsing the email.
type ChangeType int
ChangeType classifies what happened to a file in a diff.
const ( // Modified is an in-place edit (the default). Modified ChangeType = iota // Added is a new file (old side is /dev/null). Added // Deleted is a removed file (new side is /dev/null). Deleted // Renamed is a move, possibly with edits. Renamed // Copied is a copy, possibly with edits. Copied )
func (c ChangeType) String() string
type FileChange struct {
OldPath string
NewPath string
Type ChangeType
IsBinary bool
// OldMode and NewMode are the unix mode strings when git reports them
// (e.g. "100644"), otherwise empty.
OldMode string
NewMode string
// Additions and Deletions count added and removed lines across all hunks.
Additions int
Deletions int
Hunks []Hunk
}
FileChange is the diff for a single file.
func ParseDiff(diff string) ([]FileChange, error)
ParseDiff parses a unified diff (git or plain) into per-file changes. It accepts the output of `git diff`/`git format-patch` as well as a bare "--- / +++ / @@" diff with no "diff --git" headers. Unrecognized lines are ignored, so a diff embedded in surrounding text still parses.
func (f FileChange) Path() string
Path returns the file's current path: NewPath, or OldPath for a deletion.
type FormatOptions struct {
// From is the sender address in "Name <email>" form (required).
From string
// To is the primary recipient address list, comma-separated (required).
To string
// Cc is the carbon-copy recipient list, comma-separated.
Cc string
// Subject is the patch subject without any [PATCH...] prefix (required).
Subject string
// Body is the commit message text (the prose before the diff).
Body string
// Diff is the raw unified diff ("diff --git ..." lines).
Diff string
// Version is the series revision (1 default, 2 for v2, etc.).
Version int
// Index is the patch position within a series (1-based). 0 means single patch.
Index int
// Total is the total number of patches in the series. 0 means single patch.
Total int
// Prefix overrides the bracket prefix token; "PATCH" by default, "RFC PATCH" for RFCs.
Prefix string
// InReplyTo is the Message-ID this patch replies to (for threading).
InReplyTo string
// References is the full References header value (space-separated Message-IDs).
References string
// Date overrides the Date header; if zero, time.Now() is used.
Date time.Time
}
FormatOptions describes a patch email to be constructed.
type Hunk struct {
OldStart int
OldLines int
NewStart int
NewLines int
// Section is the text after the closing "@@" (often the enclosing
// function), trimmed.
Section string
Lines []Line
}
Hunk is one "@@ ... @@" section of a file diff.
type Patch struct {
// From is the raw From header (decoded from any RFC 2047 encoding).
From string
// AuthorName and AuthorEmail are From split into its parts, best effort.
AuthorName string
AuthorEmail string
// Date is the parsed Date header; the zero time if it was absent or
// unparseable.
Date time.Time
// Subject is the subject with any "[PATCH ...]" prefix stripped.
Subject string
// RawSubject is the original, undecoded-prefix subject line.
RawSubject string
// MessageID, InReplyTo and References come from the corresponding headers
// (angle brackets stripped). They thread a series together.
MessageID string
InReplyTo string
References []string
// Series is the position parsed from the subject prefix.
Series SeriesInfo
// Body is the commit message: everything between the headers and the
// diffstat/diff separator.
Body string
// Diff is the raw unified diff text, signature stripped. Empty for a
// cover letter.
Diff string
// Files is Diff parsed into per-file changes.
Files []FileChange
// Stat is the diffstat computed from Files.
Stat DiffStat
// Header is the full set of decoded message headers, for callers that
// need a field this struct does not surface.
Header mail.Header
}
Patch is a single parsed format-patch email.
A message that carries no diff — most often a "0/n" cover letter — still parses into a Patch; its Diff is empty and HasDiff reports false.
ParseBytes is Parse over an in-memory message.
ParseMbox parses every message in an mbox stream into a Patch, in file order. Messages without a diff (cover letters) are included.
IsCoverLetter reports whether this is a series cover letter: a "0/n" subject prefix, or simply a patch mail with no diff.
type Series struct {
// Cover is the "0/n" cover letter, or nil if the series had none.
Cover *Patch
// Patches are the numbered patches, sorted by SeriesInfo.Index.
Patches []*Patch
// Version is the series revision (1, 2, ... from "[PATCH vN ...]").
Version int
// Total is the expected patch count (m in "[PATCH n/m]"), 0 if unknown.
Total int
}
Series is a patch series: an optional cover letter plus the numbered patches, ordered by their position in the series.
ParseSeries parses an mbox and groups it into a single Series: the cover letter (if any) and the numbered patches sorted by index.
type SeriesInfo struct {
// Index is n in "[PATCH n/m]"; 0 for a cover letter or a lone patch with
// no "n/m".
Index int
// Total is m in "[PATCH n/m]"; 0 when the subject had no count.
Total int
// Version is the revision: 2 for "[PATCH v2 ...]", 1 when unspecified.
Version int
// Prefix is the prefix words other than the version and count, e.g.
// "PATCH" or "RFC PATCH".
Prefix string
// IsCover is true for the "0/m" message.
IsCover bool
}
SeriesInfo is the position of a patch within a series, parsed from the "[PATCH n/m]" (or "[RFC PATCH v2 n/m]") subject prefix.
| Back | FazBrowse Home | New Git URL |