parser

package
v1.40.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package parser provides Git Protocol v2 parsing for Git requests.

This package parses Git pkt-line formatted Protocol v2 requests, extracting information like wanted commits, repository details, shallow clone depth, and various command-specific arguments. It supports all Git Protocol v2 commands including fetch, ls-refs, object-info, bundle-uri, and server-option.

Index

Constants

View Source
const (
	// FlushPkt signals end of message (0000).
	FlushPkt = ""
	// DelimPkt separates message sections (0001).
	DelimPkt = "\x01"
	// ResponseEndPkt marks response end in stateless connections (0002).
	ResponseEndPkt = "\x02"
)
View Source
const (
	// CommandFetch is the fetch command name.
	CommandFetch = "fetch"
	// CommandLsRefs is the ls-refs command name.
	CommandLsRefs = "ls-refs"
	// CommandObjectInfo is the object-info command name.
	CommandObjectInfo = "object-info"
	// CommandBundleURI is the bundle-uri command name.
	CommandBundleURI = "bundle-uri"
	// CommandServerOption is the server-option command name.
	CommandServerOption = "server-option"
)

Variables

View Source
var (
	// ErrUnknownCommand is returned when an unknown command is encountered.
	ErrUnknownCommand = errors.New("unknown command")
	// ErrInvalidRequest is returned when the request format is invalid.
	ErrInvalidRequest = errors.New("invalid request format")
)
View Source
var (
	// ErrPktLineParseError is returned when pkt-line parsing fails.
	ErrPktLineParseError = errors.New("pkt-line parse error")
)

Functions

func IsDelimPkt

func IsDelimPkt(pkt []byte) bool

IsDelimPkt returns true if the packet is a delimiter packet.

func IsFlushPkt

func IsFlushPkt(pkt []byte) bool

IsFlushPkt returns true if the packet is a flush packet.

Types

type BundleURICommand

type BundleURICommand struct {
	// Request context
	Context *GitUploadPackContext

	Arguments map[string][]string
	// contains filtered or unexported fields
}

BundleURICommand represents a bundle-uri command request.

func (*BundleURICommand) CacheKey

func (c *BundleURICommand) CacheKey() string

CacheKey returns empty string - not cacheable.

func (*BundleURICommand) Capabilities

func (c *BundleURICommand) Capabilities() *util.OrderedMap[string, string]

Capabilities returns the capabilities sent with the command.

func (*BundleURICommand) GetContext

func (c *BundleURICommand) GetContext() *GitUploadPackContext

GetContext returns the request context.

func (*BundleURICommand) IsCacheable

func (c *BundleURICommand) IsCacheable() bool

IsCacheable returns false - bundle URIs can change.

func (*BundleURICommand) Name

func (c *BundleURICommand) Name() string

Name returns the command name.

func (*BundleURICommand) NonCacheableReason

func (c *BundleURICommand) NonCacheableReason() string

NonCacheableReason returns why bundle-uri is not cacheable.

type Command

type Command interface {
	// Name returns the command name (fetch, ls-refs, etc.)
	Name() string

	// GetContext returns the request context
	GetContext() *GitUploadPackContext

	// Capabilities returns the capabilities sent with the command
	// These are key-value pairs sent before the delim packet, preserving insertion order
	Capabilities() *util.OrderedMap[string, string]

	// IsCacheable returns true if this command's response can be cached
	IsCacheable() bool

	// CacheKey generates a cache key for this command
	// Returns empty string if not cacheable
	CacheKey() string

	// NonCacheableReason returns why the command is not cacheable
	// Returns empty string if cacheable
	NonCacheableReason() string
}

Command represents a Git Protocol v2 command.

type FetchCommand

type FetchCommand struct {
	// Request context
	Context *GitUploadPackContext

	// Negotiation
	Wants []string // Object IDs wanted
	Haves []string // Object IDs already possessed
	Done  bool     // Negotiation complete

	// Shallow/Deepen options
	Shallow        []string  // Shallow boundaries
	Deepen         int       // Deepen by N commits
	DeepenSince    time.Time // Deepen since timestamp
	DeepenNot      []string  // Deepen not (exclude refs)
	DeepenRelative bool      // Relative to current depth

	// Filtering
	Filter *FilterSpec // Object filter (blob:none, etc.)

	// Options
	WantRef      []string // Want by ref name
	SidebandAll  bool     // Use sideband for all output
	PackfileURIs []string // Request packfile URIs
	ThinPack     bool     // Request thin pack
	NoProgress   bool     // Suppress progress
	IncludeTag   bool     // Include annotated tags
	OFSDelta     bool     // Use OFS_DELTA

	// Arguments (other command-specific args)
	Arguments map[string][]string
	// contains filtered or unexported fields
}

FetchCommand represents a fetch command request.

func ParseFetchCommandForTest

func ParseFetchCommandForTest(pl *pktline.Pktline, ctx *GitUploadPackContext) (*FetchCommand, error)

ParseFetchCommandForTest is a test helper that exposes parseFetchCommand for testing. This allows external packages to parse fetch commands from pkt-line format.

func (*FetchCommand) CacheKey

func (c *FetchCommand) CacheKey() string

CacheKey generates a cache key for this fetch request.

func (*FetchCommand) CacheKeyWithBase

func (c *FetchCommand) CacheKeyWithBase(basePackfileHash string) string

CacheKeyWithBase generates a cache key including the base packfile hash. This ensures requests with and without base packfile augmentation get separate cache entries.

func (*FetchCommand) Capabilities

func (c *FetchCommand) Capabilities() *util.OrderedMap[string, string]

Capabilities returns the capabilities sent with the command.

func (*FetchCommand) Clone

func (c *FetchCommand) Clone() *FetchCommand

Clone creates a deep copy of the FetchCommand.

func (*FetchCommand) GetContext

func (c *FetchCommand) GetContext() *GitUploadPackContext

GetContext returns the request context.

func (*FetchCommand) IsCacheable

func (c *FetchCommand) IsCacheable() bool

IsCacheable returns true if this fetch request can be cached.

func (*FetchCommand) Name

func (c *FetchCommand) Name() string

Name returns the command name.

func (*FetchCommand) NonCacheableReason

func (c *FetchCommand) NonCacheableReason() string

NonCacheableReason returns the reason why the fetch is not cacheable.

func (*FetchCommand) SerializeBody

func (c *FetchCommand) SerializeBody() ([]byte, error)

SerializeBody serializes the FetchCommand back to pkt-line format. This is used after augmenting the command with additional haves.

Git protocol v2 format (based on real Git trace data): - Command packet (NO newline): command=fetch - Capabilities (NO newlines): agent=..., object-format=... - Delim (0001) - Boolean options (NO newlines): thin-pack, ofs-delta, no-progress, etc. - Value arguments (NO newlines): deepen N, filter ..., etc. - Wants (WITH newlines): want <oid>\n - Haves (WITH newlines): have <oid>\n - Done (WITH newline): done\n - Flush (0000)

See https://git-scm.com/docs/gitprotocol-v2

func (*FetchCommand) SupportsPackfileURIsHTTPS

func (c *FetchCommand) SupportsPackfileURIsHTTPS() bool

SupportsPackfileURIsHTTPS checks if the client supports packfile-uris with HTTPS protocol.

type FilterSpec

type FilterSpec struct {
	Raw string // e.g. "blob:none", "blob:limit=1048576"
}

FilterSpec is an object filter kept as the spec string exactly as received, so SerializeBody can re-emit it verbatim. Build it with parseFilterSpec. Callers that need its structure parse Raw themselves.

type GitUploadPackContext

type GitUploadPackContext struct {
	// Repository is the repository path
	Repository string
}

GitUploadPackContext contains context for parsing git-upload-pack requests.

type LsRefsCommand

type LsRefsCommand struct {
	// Request context
	Context *GitUploadPackContext

	Symrefs   bool     // Include symbolic ref targets
	Peel      bool     // Peel tags to objects
	RefPrefix []string // Only refs matching prefixes
	Arguments map[string][]string
	// contains filtered or unexported fields
}

LsRefsCommand represents an ls-refs command request.

func (*LsRefsCommand) CacheKey

func (c *LsRefsCommand) CacheKey() string

CacheKey returns empty string - not cacheable.

func (*LsRefsCommand) Capabilities

func (c *LsRefsCommand) Capabilities() *util.OrderedMap[string, string]

Capabilities returns the capabilities sent with the command.

func (*LsRefsCommand) GetContext

func (c *LsRefsCommand) GetContext() *GitUploadPackContext

GetContext returns the request context.

func (*LsRefsCommand) IsCacheable

func (c *LsRefsCommand) IsCacheable() bool

IsCacheable returns false - ls-refs responses change frequently.

func (*LsRefsCommand) Name

func (c *LsRefsCommand) Name() string

Name returns the command name.

func (*LsRefsCommand) NonCacheableReason

func (c *LsRefsCommand) NonCacheableReason() string

NonCacheableReason returns why ls-refs is not cacheable.

type ObjectInfoCommand

type ObjectInfoCommand struct {
	// Request context
	Context *GitUploadPackContext

	Size      bool     // Request object sizes
	OIDs      []string // Object IDs to query
	Arguments map[string][]string
	// contains filtered or unexported fields
}

ObjectInfoCommand represents an object-info command request.

func (*ObjectInfoCommand) CacheKey

func (c *ObjectInfoCommand) CacheKey() string

CacheKey returns empty string - not cacheable.

func (*ObjectInfoCommand) Capabilities

func (c *ObjectInfoCommand) Capabilities() *util.OrderedMap[string, string]

Capabilities returns the capabilities sent with the command.

func (*ObjectInfoCommand) GetContext

func (c *ObjectInfoCommand) GetContext() *GitUploadPackContext

GetContext returns the request context.

func (*ObjectInfoCommand) IsCacheable

func (c *ObjectInfoCommand) IsCacheable() bool

IsCacheable returns false - object info is typically dynamic.

func (*ObjectInfoCommand) Name

func (c *ObjectInfoCommand) Name() string

Name returns the command name.

func (*ObjectInfoCommand) NonCacheableReason

func (c *ObjectInfoCommand) NonCacheableReason() string

NonCacheableReason returns why object-info is not cacheable.

type Request

type Request struct {
	// Version is the protocol version ("2")
	Version string

	// Command is the specific command being executed
	Command Command
}

Request represents a complete Git Protocol v2 request.

func ParseRequest

func ParseRequest(body io.Reader, requestContext *GitUploadPackContext) (*Request, error)

ParseRequest parses a Git Protocol v2 request.

type ServerOptionCommand

type ServerOptionCommand struct {
	// Request context
	Context *GitUploadPackContext

	Options   []string // Server-specific options
	Arguments map[string][]string
	// contains filtered or unexported fields
}

ServerOptionCommand represents server-option transmission.

func (*ServerOptionCommand) CacheKey

func (c *ServerOptionCommand) CacheKey() string

CacheKey returns empty string - not cacheable.

func (*ServerOptionCommand) Capabilities

func (c *ServerOptionCommand) Capabilities() *util.OrderedMap[string, string]

Capabilities returns the capabilities sent with the command.

func (*ServerOptionCommand) GetContext

func (c *ServerOptionCommand) GetContext() *GitUploadPackContext

GetContext returns the request context.

func (*ServerOptionCommand) IsCacheable

func (c *ServerOptionCommand) IsCacheable() bool

IsCacheable returns false - server options are metadata.

func (*ServerOptionCommand) Name

func (c *ServerOptionCommand) Name() string

Name returns the command name.

func (*ServerOptionCommand) NonCacheableReason

func (c *ServerOptionCommand) NonCacheableReason() string

NonCacheableReason returns why server-option is not cacheable.

Directories

Path Synopsis
Package capabilities provides parsing and marshalling for Git Protocol v2 capabilities advertisements in pkt-line format.
Package capabilities provides parsing and marshalling for Git Protocol v2 capabilities advertisements in pkt-line format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL