gitpack

package
v0.26.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gitpack reads git pack files and v2 pack indexes by ranged reads.

Index

Constants

View Source
const (
	MaxObjectBytes = 16 << 20

	// VerifyWorkBytes bounds the working memory of Verify: the base cache, the object kept past it, and the delta, its
	// base and its result while one object resolves.
	VerifyWorkBytes = verifyCacheBytes + 4*MaxObjectBytes
)
View Source
const IdxHeadBytes = idxHead

IdxHeadBytes is the size of the header and the fanout table of a v2 index.

View Source
const MaxUnpackBytes = 64 << 20

MaxUnpackBytes bounds the inflated bytes that Unpack reads from entries and the bytes of the objects that it returns, each.

View Source
const MaxUnpackObjects = 100000

MaxUnpackObjects bounds the objects of one pack that Unpack reads.

Variables

View Source
var (
	ErrCorrupt  = errors.New("gitpack: corrupt pack or index")
	ErrTooLarge = errors.New("gitpack: object too large")
)
View Source
var ErrNotFound = errors.New("object not found")

Functions

func BuildIdx added in v0.10.0

func BuildIdx(entries []IdxEntry, packSum [20]byte) []byte

BuildIdx writes the v2 index of a pack from entries sorted by name. packSum is the trailer of the pack.

func BuildSize added in v0.5.0

func BuildSize(objs []Object) int

BuildSize bounds the bytes of the pack body of objs: zlib stores an incompressible object with 5 bytes of overhead for each 64 KiB block, and each object adds a header and a zlib frame of at most 32 bytes.

func EncodeOfs added in v0.12.0

func EncodeOfs(dist int64) []byte

EncodeOfs is the encoded form of the distance to the base of an offset delta.

func EntryBound added in v0.13.0

func EntryBound(data []byte) (int64, bool)

EntryBound is the most bytes that the pack entry at the start of data takes: its header, the base name of a ref delta, and its deflate stream even if the stream stores the data. It reports false when data does not hold the header.

func IdxCount added in v0.10.0

func IdxCount(head []byte) (int, error)

IdxCount returns the number of objects of a v2 index from its first IdxHeadBytes bytes.

Types

type IdxEntry added in v0.10.0

type IdxEntry struct {
	Name SHA
	CRC  uint32
	Off  uint32
}

IdxEntry is one object of a pack index: its name, the CRC-32 of its entry and its offset.

type IdxIter added in v0.10.0

type IdxIter struct {
	// contains filtered or unexported fields
}

IdxIter reads the names and offsets of a v2 index in name order with ranged reads, so a caller can merge indexes of any size in bounded memory.

func NewIdxIter added in v0.10.0

func NewIdxIter(ctx context.Context, n int, get func(ctx context.Context, off, n int64) ([]byte, error)) *IdxIter

NewIdxIter returns an iterator over the n entries of an index. get reads a range of the index, and the rest of it when n is negative.

func (*IdxIter) Next added in v0.10.0

func (it *IdxIter) Next() (SHA, int64, bool, error)

Next returns the next name and its offset in the pack, and false at the end.

type Object

type Object struct {
	Type Type
	Data []byte
}

func Unpack added in v0.6.0

func Unpack(ctx context.Context, pack []byte, external func(SHA) (Type, []byte, error)) ([]Object, error)

Unpack reads the whole pack and returns its objects with every delta resolved. The pack may be thin: external resolves the base of a ref delta that the pack does not hold.

func UnpackHeld added in v0.12.0

func UnpackHeld(ctx context.Context, pack []byte, external func(SHA) (Type, []byte, error), held func(int64) error) ([]Object, error)

UnpackHeld is Unpack that calls held with the inflated bytes that it holds, before it allocates them, each time that count grows. An error of held stops the unpack.

type Pack

type Pack struct {
	// contains filtered or unexported fields
}

func NewAt added in v0.10.0

func NewAt(pack ReaderAt) *Pack

NewAt returns a Pack that holds no index. It finds a name through the function that WithLookup sets, so a caller that indexes many packs in one structure reads each pack with no index of its own.

func NewPack

func NewPack(idx []byte, pack ReaderAt) (*Pack, error)

NewPack checks the structure and the checksum of a v2 index before it trusts a byte of it.

func (*Pack) Bind

func (p *Pack) Bind(r ReaderAt) *Pack

Bind returns a view of the pack that reads through r and shares its index and object cache, so a request can bring its own context to the reads.

func (*Pack) Bytes

func (p *Pack) Bytes() int

Bytes is the size of the index that the pack keeps in memory.

func (*Pack) CachedBytes

func (p *Pack) CachedBytes() int

CachedBytes is the size of the resolved objects the pack holds.

func (*Pack) Count

func (p *Pack) Count() int

func (*Pack) Footprint added in v0.12.0

func (p *Pack) Footprint(s SHA) (int64, error)

Footprint is the bytes that Get of s holds at most for its result: the size of an object that the pack stores whole, and MaxObjectBytes for a delta, because its base is inflated first and can be of any size.

func (*Pack) Get

func (p *Pack) Get(s SHA) (Type, []byte, error)

func (*Pack) GetDepth

func (p *Pack) GetDepth(s SHA) (Type, []byte, int, error)

GetDepth is Get that also returns the length of the delta chain that produced the object; zero is a whole object.

func (*Pack) Has

func (p *Pack) Has(s SHA) bool

func (*Pack) Index added in v0.16.0

func (p *Pack) Index(s SHA) (int, bool)

Index returns the position of the object s in the order of the index.

func (*Pack) Lookup added in v0.10.0

func (p *Pack) Lookup(s SHA) (int64, bool)

Lookup returns the offset of the object s in the pack.

func (*Pack) Names added in v0.15.0

func (p *Pack) Names() iter.Seq[SHA]

Names returns the names of the objects of the pack, in the order of the index.

func (*Pack) OnCacheChange

func (p *Pack) OnCacheChange(f func())

OnCacheChange sets f to run, with no lock held, after an object enters the cache. Call it before the pack is shared.

func (*Pack) Size

func (p *Pack) Size(s SHA) (int64, Type, error)

Size returns the object size and type without inflating the whole object.

func (*Pack) SizeAt added in v0.16.0

func (p *Pack) SizeAt(off int64) (int64, error)

SizeAt returns the inflated size of the entry at off, whole or delta, without resolving a base.

func (*Pack) Verify

func (p *Pack) Verify(ctx context.Context, idx, pack []byte) error

Verify checks that pack is the pack that the index describes and that every object hashes to its name. It resolves the objects in pack order so that each delta chain is read once, and it stops at a budget of inflated bytes or when ctx ends.

func (*Pack) WholeType added in v0.15.0

func (p *Pack) WholeType(s SHA) (Type, int64, bool)

WholeType returns the type of the object s and its size when the pack stores it whole, and false for a delta or an object that the pack lacks. It reads the header of the entry only.

func (*Pack) WithLookup added in v0.10.0

func (p *Pack) WithLookup(f func(SHA) (int64, bool)) *Pack

WithLookup returns a view of the pack that finds a name with f. The view shares the object cache.

type Raw added in v0.16.0

type Raw struct {
	Type  Type
	Size  int64
	Delta bool
	Zlib  []byte
}

Raw is a stored pack entry of a whole object that a writer can copy as it is: the type, the inflated size that the entry header names, and the zlib stream. A delta has Delta set and no stream.

func RawEntry added in v0.16.0

func RawEntry(data []byte) (r Raw, n int, need int64, err error)

RawEntry parses the pack entry at the start of data. For a whole object it returns the entry, whose stream is a slice of data, and the bytes that the entry takes. When data ends inside the entry, it returns the most that the entry can take as need and the size from the header in r. An entry that is not a valid zlib stream of the size that its header names is ErrCorrupt.

type ReaderAt

type ReaderAt interface {
	io.ReaderAt
	Size() int64
}

ReaderAt is the ranged-read seam over the bytes of one pack.

type SHA

type SHA [20]byte

func Build

func Build(objs []Object) (pack, idx []byte, names []SHA)

Build writes objects as a pack without deltas, and its v2 index. The returned names are the object names in the order of objs. An object that repeats an earlier one is stored once.

func Hash

func Hash(t Type, data []byte) SHA

func ParseSHA

func ParseSHA(h string) (SHA, error)

func (SHA) String

func (s SHA) String() string

type Span added in v0.12.0

type Span struct {
	Off, End int64
	// SizeLen is the length of the type and size varint. HdrLen is the length of the whole header, which adds the
	// base of a delta.
	SizeLen, HdrLen int
	// Delta is 0 for a whole entry, 6 for an offset delta and 7 for a reference delta.
	Delta    uint8
	Type     Type
	BaseOff  int64
	BaseName SHA
}

Span is the extent of one entry of a pack and what its header says.

func Scan added in v0.12.0

func Scan(pack []byte) ([]Span, error)

Scan lists the entries of a checked pack in order, without keeping their data.

type Type

type Type uint8
const (
	Commit Type = 1
	Tree   Type = 2
	Blob   Type = 3
	Tag    Type = 4
)

func ParseType

func ParseType(name string) Type

ParseType maps a type name to a Type, or 0.

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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