Documentation
¶
Overview ¶
Package receivepack parses the client side of git-receive-pack and writes its reply, as specified by gitprotocol-pack(5) ("Reference Update Request and Packfile Transfer", "Report Status") and gitprotocol-capabilities(5).
Parse reads everything that precedes the packfile and returns a plain Request value, so that a forge can run its push policy (who may update which ref, ref names, number of commands, whether a pack is expected at all) BEFORE a single byte of pack is read. It never reads the pack: after a successful Parse the pktline.Reader's source is positioned on the first byte of the pack (pktline.Reader does no read-ahead).
Index ¶
- Constants
- Variables
- func Advertise(w io.Writer, refs []AdvRef, agent string) error
- func CheckRefName(name string) error
- func FoldKey(ref string) string
- func WriteReport(w io.Writer, caps Capabilities, rep *Report) error
- func WriteSidebandMessage(w io.Writer, caps Capabilities, msg string) error
- type AdvRef
- type Capabilities
- type Command
- type Limits
- type ProtocolError
- type RefResult
- type Report
- type Request
Constants ¶
const AdvertisedCaps = "report-status report-status-v2 delete-refs side-band-64k quiet atomic ofs-delta push-options object-format=sha1"
AdvertisedCaps is the capability list a server built on this package advertises: exactly what Parse accepts and what WriteReport implements.
const ZeroID = "0000000000000000000000000000000000000000"
ZeroID is the all-zero object id: "no object" in a command.
Variables ¶
var DefaultLimits = Limits{ MaxMemoryBytes: 64 << 20, MaxCommands: 10_000, MaxRefNameBytes: 1024, MaxPushOptions: 256, MaxPushOptionBytes: 4096, MaxRequestBytes: 8 << 20, Timeout: 30 * time.Second, }
DefaultLimits are used for every zero field of Limits.
Functions ¶
func Advertise ¶
Advertise writes the version-0 ref advertisement of receive-pack (gitprotocol-pack(5), "Reference Discovery"): the first line carries the capabilities after a NUL, an empty repository advertises "capabilities^{}" with the zero id, and a flush-pkt ends the list. agent is appended to the capabilities when non-empty. The "# service=" preamble of smart HTTP is the caller's.
func CheckRefName ¶
CheckRefName applies git-check-ref-format(1) to a full ref name that must live under refs/: components are non-empty, do not start with "." and do not end with ".lock"; the name has no "..", no "@{", no ASCII control byte, no space, "~", "^", ":", "?", "*", "[" or "\", does not end with "/" or ".", and is not "@". To keep FoldKey sound and to hold on a file system that folds names (APFS, NTFS) it also refuses: a code point unassigned in the build's Unicode tables; a default-ignorable code point; a name that x/text would cut with a U+034F (a long run of combining marks; some starters count too); a component whose FOLDED form is empty, starts with ".", ends with ".lock" or is a reserved Windows device name (see badComponent), so "main.LOCK" is refused; the characters '"', '<', '>' and '|'; and a name longer than 65520 bytes. It allocates up to 24 times the name for a name that is not ASCII, uncharged and not retained.
Like git receive-pack, which checks the part after "refs/" as a ref name that is not one-level, it refuses "refs/stash" and "refs/x": a ref lives in at least a category and a name ("refs/heads/x").
func FoldKey ¶
FoldKey is the name a case-insensitive or normalising file system (macOS HFS+/APFS, Windows NTFS) would give to ref, component by component (see folder.component). Two refs with the same FoldKey are one file there, and a ref whose FoldKey is a directory of another's is a file and a directory there. The key is at most three times as long as ref.
FoldKey REFUSES NOTHING: it folds any string, and it folds with the Unicode tables of the toolchain that built the binary (unicode.Version), so it is blind to a code point added by a later Unicode, and x/text puts a U+034F where its Stream-Safe counter says (UAX #15 section 13: after 30 non-starters, and for some starters too) where a file system may reorder marks across it. The refusals that make its keys sound are CheckRefName's (so Parse's): a code point unassigned in the build's tables, a default-ignorable code point, and a name whose normalisation x/text would cut with a U+034F. Call FoldKey on names that passed it.
A key is NEVER to be persisted: it depends on the toolchain's Unicode tables (U+A7DC folds to U+019B under Unicode 16 and not before). Compute it again from the ref name, and check the refs already stored with CheckRefName.
func WriteReport ¶
func WriteReport(w io.Writer, caps Capabilities, rep *Report) error
WriteReport writes the report-status (or report-status-v2) reply of gitprotocol-pack(5), in the form the client asked for in caps: nothing when the client did not ask for report-status; the body wrapped in side-band channel 1 (each inner pkt-line then a flush, itself wrapped) when side-band-64k was negotiated; a final flush-pkt in every case that writes.
func WriteSidebandMessage ¶
func WriteSidebandMessage(w io.Writer, caps Capabilities, msg string) error
WriteSidebandMessage writes a human-readable progress/error message on side-band channel 2 ("remote: ..." on the client), if side-band-64k was negotiated and the client did not ask for quiet; it is a no-op otherwise.
The client prints the message on its terminal, so msg is made safe first: the escape character, carriage return, every other control byte (C1 controls included) and invalid UTF-8 are replaced by '?'; only a newline and a tab pass. A message that names a ref or a user must not be able to move the cursor, clear the screen or rewrite the "remote:" prefix.
Types ¶
type Capabilities ¶
type Capabilities struct {
ReportStatus bool
ReportStatusV2 bool
Atomic bool
OfsDelta bool
// SideBand64k is set when the client asked for side-band-64k.
SideBand64k bool
Quiet bool
PushOptions bool
// Agent is the client's agent string, or "".
Agent string
}
Capabilities is the capability list the client sent with its first command.
type Command ¶
type Command struct {
// Old is the id the client believes the ref has; ZeroID creates the ref.
Old string
// New is the id to set; ZeroID deletes the ref.
New string
// Ref is the full ref name, validated like git check-ref-format.
Ref string
}
Command is one reference update command.
type Limits ¶
type Limits struct {
// MaxCommands bounds the reference update commands.
MaxCommands int
// MaxRefNameBytes bounds one ref name.
MaxRefNameBytes int
// MaxPushOptions bounds the push-option lines.
MaxPushOptions int
// MaxPushOptionBytes bounds one push-option line.
MaxPushOptionBytes int
// MaxRequestBytes bounds the bytes read before the pack.
MaxRequestBytes int64
// MaxMemoryBytes bounds what Parse holds in memory for the request: the
// command lines, the Command values (counted twice, for the growth of the
// slice), the index the conflict check sorts and the push options, then
// what Request.FoldCollision builds. Every allocation is charged in
// bytes to this one budget BEFORE it is made, except the transient copies
// CheckRefName makes of one ref name that is not ASCII (up to 24 times
// the name, 24 KiB at the default MaxRefNameBytes, freed before the next
// name is read). A zero value is the
// default, 64 MiB (finite whatever MaxRequestBytes is: a caller who
// raises MaxRequestBytes raises this too).
MaxMemoryBytes int64
// Timeout bounds the whole of Parse, from the first byte to the last
// push option (see ParseContext for what it can interrupt).
Timeout time.Duration
}
Limits bound what one command list may cost. A zero field means the default; a negative one is refused by Validate.
type ProtocolError ¶
type ProtocolError struct{ Msg string }
ProtocolError is a refusal caused by the client's request.
func (*ProtocolError) Error ¶
func (e *ProtocolError) Error() string
type RefResult ¶
type RefResult struct {
// Ref is the ref name of the command.
Ref string
// Err is "" when the update succeeded, else the reason shown to the
// client ("ng <ref> <Err>"). It must be one line.
Err string
// Option lines of report-status-v2 for a successful command.
// NewRef is the ref name when the update was rewritten to another ref.
NewRef string
// OldID, NewID are reported as "option old-oid"/"option new-oid" when
// non-empty; ForcedUpdate as "option forced-update".
OldID, NewID string
ForcedUpdate bool
}
RefResult is the outcome of one command.
type Report ¶
type Report struct {
// UnpackErr is "" for "unpack ok", else the reason after "unpack ".
UnpackErr string
Results []RefResult
}
Report is the whole outcome of a push.
type Request ¶
type Request struct {
Commands []Command
Caps Capabilities
// PushOptions are the push-option lines, in order (LF stripped).
PushOptions []string
// WantsPack is true when the client sends a packfile: at least one
// command is not a delete (gitprotocol-pack: "the packfile MUST NOT be
// sent if the only commands used are delete").
WantsPack bool
// contains filtered or unexported fields
}
Request is a parsed receive-pack command list.
func Parse ¶
Parse reads one receive-pack command list from r.
It returns io.EOF when the stream ends before any packet or on a lone flush-pkt (a client that found nothing to push; git sends exactly that). A "shallow" line, a push certificate, an unknown capability, an unsupported object format, duplicate or malformed commands, or two commands whose refs conflict as a file and a directory ("refs/heads/a" and "refs/heads/a/b"), are *ProtocolError. Invalid limits (Limits.Validate) are refused first, with an ordinary error. Refs that differ only by case or Unicode normalisation are distinct here (as are refs that differ by ignorable code points or a trailing dot): Request.FoldCollision finds them, for a forge whose storage folds names. Transport failures are returned as read.
Parse is ParseContext with a background context.
func ParseContext ¶
ParseContext is Parse bounded by ctx and by Limits.Timeout, whichever ends first, and by Limits.MaxMemoryBytes. A source with SetReadDeadline (a net.Conn) has its read deadline set to the deadline and moved to the past on cancellation, and is left with no read deadline when ParseContext returns; a source without one is only checked between packets, so a read that never returns never lets ParseContext return. No goroutine outlives the call. An expired deadline is a *ProtocolError; a cancellation is ctx.Err().
func (*Request) FoldCollision ¶
FoldCollision reports the first two commands whose refs differ but collide on a folding file system: the same FoldKey ("refs/heads/Main" and "refs/heads/main", the NFC and NFD spellings of an accented name, "ß" and "ss", a name with an ignorable code point or a trailing dot), or a FoldKey that is a directory of the other's ("refs/heads/A" and "refs/heads/a/b"). Parse accepts such commands, as git on a case-sensitive file system does; a forge whose ref storage folds names refuses the request when this returns ok.
It draws on the budget and the deadline of the Parse that made the request (Limits.MaxMemoryBytes, Limits.Timeout): it charges the keys (at most three times the size of the refs), the index and its working copies BEFORE allocating them, gives them back when it returns, and returns a *ProtocolError when the budget or the time is out (a deadline passed by the time it has finished is an error too, never a late answer). A Request that Parse did not make carries neither: it is refused with a *ProtocolError. Its time is that of a sort. It is not safe to call from two goroutines at once.