Documentation
¶
Overview ¶
Package gitproto implements higher-level Git smart-HTTP / stateless-RPC wire helpers built on top of the pkt-line layer in github.com/go-git/go-git/v6/plumbing/format/pktline:
- v2 capability-advertisement detection (IsV2Advertisement)
- v2-request command extraction (V2Command)
- reading a flush-terminated v2 message verbatim (ReadFlushTerminatedMessage)
- stripping the smart-HTTP service announcement (ReadPostServiceAdvertisement, backed by packp.SmartReply)
- reading the receive-pack request body the helper relays
- reading the send-pack stateless-rpc outer framing
Everything here is pure Git protocol — no Entire-specific knowledge.
Index ¶
- func AppendAgentToReceivePackBody(body *RequestBody, helperAgent string) error
- func AppendAgentToReceivePackRequest(message []byte, helperAgent string) ([]byte, error)
- func AppendAgentToUploadPackRequest(message []byte, helperAgent string) ([]byte, error)
- func AppendAgentToV2Request(message []byte, helperAgent string) ([]byte, error)
- func IsV2Advertisement(p []byte) bool
- func ReadFlushTerminatedMessage(r io.Reader) ([]byte, bool, error)
- func ReadPostServiceAdvertisement(r io.Reader) ([]byte, error)
- func ReadSendPackRequest(r *bufio.Reader, dst *RequestBody) error
- func RemoveV2FetchFeature(advertisement []byte, feature string) ([]byte, error)
- func V2Command(message []byte) string
- type RequestBody
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendAgentToReceivePackBody ¶ added in v0.13.0
func AppendAgentToReceivePackBody(body *RequestBody, helperAgent string) error
AppendAgentToReceivePackBody appends helperAgent to the agent capability on the request's first command line: the first pkt-line carrying a NUL before the flush. Shallow lines can precede it, and send-pack's stateless-rpc framing can split it across chunks, so the lines are read through the body. Only that line is copied. It leaves requests without agent= unchanged.
func AppendAgentToReceivePackRequest ¶
AppendAgentToReceivePackRequest appends helperAgent to the agent capability on the first receive-pack command line. It leaves requests without agent= unchanged.
func AppendAgentToUploadPackRequest ¶
AppendAgentToUploadPackRequest appends helperAgent to the agent capability in a v0/v1 upload-pack request. It leaves requests without agent= unchanged.
func AppendAgentToV2Request ¶
AppendAgentToV2Request appends helperAgent to an existing v2 client-side agent line. It leaves requests without agent= unchanged.
func IsV2Advertisement ¶
IsV2Advertisement reports whether p begins with the v2 service capability line ("version 2\n"). The peek is conservative: if the length prefix is malformed or claims more bytes than p contains, we return false rather than panic.
func ReadFlushTerminatedMessage ¶
ReadFlushTerminatedMessage reads pkt-lines from r until a flush (0000), returning the concatenated raw bytes including every length prefix and the trailing flush. The boolean is false (with nil error) when r is at EOF before any byte has been read — the natural signal that the stream closed cleanly between messages.
Special lengths 0001 (delim) and 0002 (response-end) are passed through verbatim — they're part of v2 framing.
We reach for the raw bytes instead of pktline.Scanner because the helper proxies messages over HTTP unchanged; reconstructing the length prefixes from Scanner's payload-only output would re-encode and risk drift.
func ReadPostServiceAdvertisement ¶
ReadPostServiceAdvertisement reads a v0/v1 smart-HTTP info/refs body and returns the bytes that follow the service announcement plus its terminating flush — i.e. just the ref advertisement.
`git send-pack --stateless-rpc` runs the input through discover_version + get_remote_heads, which die with "protocol error: unexpected '# service=...'" if the announcement preamble is left in. Strip it once, here, so callers can feed the result straight into send-pack.
func ReadSendPackRequest ¶
func ReadSendPackRequest(r *bufio.Reader, dst *RequestBody) error
ReadSendPackRequest reads one request emitted by `git send-pack --stateless-rpc` and appends the unwrapped payload to dst. Send-pack wraps the receive-pack request in an outer pkt-line stream so it can mark request boundaries without closing stdout; the HTTP body is the concatenated payload of those outer packets. Each payload is kept as its own chunk of dst rather than copied into one buffer, so the pack is held once.
Returns when the outer flush is seen. Special lengths 1/2 are tolerated and skipped (current send-pack doesn't emit them, but the shape is permissive). EOF before the flush is wrapped as io.ErrUnexpectedEOF — a truncated send-pack stdout is never a clean end.
func RemoveV2FetchFeature ¶ added in v0.11.5
RemoveV2FetchFeature drops feature from the fetch= capability in a v2 advertisement and leaves every other byte intact. A fetch line with no feature left becomes the bare key "fetch", which gitprotocol-v2(5) permits: capability = PKT-LINE(key[=value] LF). Advertisements without the feature come back unchanged. A server sends each capability key once, so a second fetch= line is an error.
func V2Command ¶
V2Command extracts the "command=" value from a v2 fetch / ls-refs / bundle-uri request message. The message is the bytes between two flushes (callers typically pass the buffer returned by ReadFlushTerminatedMessage). Returns "" when the message is malformed, truncated, or carries no command pktline.
Types ¶
type RequestBody ¶ added in v0.13.0
type RequestBody struct {
// contains filtered or unexported fields
}
RequestBody is a receive-pack request held as the chunks it was read in rather than one contiguous buffer. A push holds its pack in memory once: appending to a growing buffer would hold up to two copies while the buffer grows, and amending the agent capability would need a third.
The body is replayable — Reader returns a fresh io.ReadSeeker each time — so the transport's 401 retry and replica failover can resend it.
func ReadReceivePackRequest ¶
func ReadReceivePackRequest(r io.Reader) (*RequestBody, error)
ReadReceivePackRequest reads a v0/v1 git-receive-pack request body from r: a sequence of command pkt-lines terminated by a flush, optionally followed by a raw PACK stream when at least one command is not a delete (i.e. the new OID isn't all zeros).
The returned body is exactly what was on the wire — caller streams it to the remote without further framing. EOF before the flush is reported (treating it as a clean close would let a truncated request look like a clean delete-only push).
func (*RequestBody) ApplyTo ¶ added in v0.13.0
func (b *RequestBody) ApplyTo(req *http.Request)
ApplyTo declares the body's size on req and lets net/http rewind it. http.NewRequest infers both only for bytes and strings readers; without them the POST goes out chunked, and a redirect that must resend the body can't.
func (*RequestBody) Len ¶ added in v0.13.0
func (b *RequestBody) Len() int64
Len returns the body's size in bytes.
func (*RequestBody) ReadAt ¶ added in v0.13.0
func (b *RequestBody) ReadAt(p []byte, off int64) (int, error)
ReadAt implements io.ReaderAt over the chunks.
func (*RequestBody) ReadFrom ¶ added in v0.13.0
func (b *RequestBody) ReadFrom(r io.Reader) (int64, error)
ReadFrom appends everything r yields until io.EOF, in bodyChunkSize blocks. Like io.Copy, it treats only io.EOF itself as the end: any other error, io.ErrUnexpectedEOF and wrapped EOFs included, means the body is incomplete and is returned, even after a full block.
func (*RequestBody) Reader ¶ added in v0.13.0
func (b *RequestBody) Reader() *io.SectionReader
Reader returns a new reader positioned at the start of the body.