capabilities

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: 7 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidServiceLine indicates the service line format is invalid.
	ErrInvalidServiceLine = errors.New("invalid service line format")

	// ErrExpectedFlush indicates a flush packet was expected but not found.
	ErrExpectedFlush = errors.New("expected flush packet")

	// ErrUnknownFormat indicates an unknown advertisement format was encountered.
	ErrUnknownFormat = errors.New("unknown advertisement format")
)

Functions

This section is empty.

Types

type Advertisement struct {
	// Service is the service being advertised (e.g., "git-upload-pack")
	// In pure v2 format, this may be inferred from context
	Service string

	// Version is the protocol version (e.g., "2")
	Version string

	// Format indicates whether this is hybrid or pure v2 format
	Format AdvertisementFormat

	// Capabilities is an ordered map of capability names to their values.
	// The order is preserved as capabilities are inserted, which is critical for Git Protocol v2
	// where "version 2" must be the first capability.
	// For capabilities with '=' like "agent=git/2.39.0", the key is "agent" and value is "git/2.39.0".
	// For simple capabilities like "packfile-uris", the key is "packfile-uris" and value is "".
	Capabilities *util.OrderedMap[string, string]
}

Advertisement represents a Git Protocol v2 info/refs capabilities advertisement. Supports both hybrid format (with service line) and pure v2 format (without service line).

Hybrid format:

001e# service=git-upload-pack\n
0000 (flush)
000eversion 2\n
0017agent=git/2.39.0\n
0012ls-refs=unborn\n
001ffetch=shallow wait-for-done\n
0012server-option\n
0017object-format=sha1\n
0000 (flush)

Pure v2 format:

000eversion 2\n
0017agent=git/2.39.0\n
0012ls-refs=unborn\n
001ffetch=shallow wait-for-done\n
0012server-option\n
0017object-format=sha1\n
0000 (flush)

func Parse

func Parse(data []byte) (*Advertisement, error)

Parse parses a Git Protocol v2 capabilities advertisement from bytes. Automatically detects and parses both hybrid format (with service line) and pure v2 format.

func (*Advertisement) AddCapability

func (a *Advertisement) AddCapability(capability string) bool

AddCapability adds a capability with an empty value if it's not already present. Returns true if the capability was added, false if it already existed.

func (*Advertisement) GetCapability

func (a *Advertisement) GetCapability(capability string) (value string, exists bool)

GetCapability returns the value for a capability and whether it exists.

func (*Advertisement) HasCapability

func (a *Advertisement) HasCapability(capability string) bool

HasCapability checks if a capability is present in the advertisement.

func (*Advertisement) Marshal

func (a *Advertisement) Marshal() ([]byte, error)

Marshal converts the Advertisement back to Git Protocol v2 pkt-line format. Marshals according to the Format field (hybrid or pure v2).

func (*Advertisement) SetCapability

func (a *Advertisement) SetCapability(capability, value string)

SetCapability sets a capability with the given value.

type AdvertisementFormat

type AdvertisementFormat int

AdvertisementFormat represents the format of the capabilities advertisement.

const (
	// FormatHybrid is the hybrid format with service line:
	//   001e# service=git-upload-pack\n
	//   0000 (flush)
	//   000eversion 2\n
	//   <capabilities>
	//   0000 (flush)
	FormatHybrid AdvertisementFormat = iota

	// FormatPureV2 is the pure v2 format without service line:
	//   000eversion 2\n
	//   <capabilities>
	//   0000 (flush)
	FormatPureV2
)

Jump to

Keyboard shortcuts

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