fastpack

package module
v0.0.0-...-3dfeec9 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: MIT Imports: 10 Imported by: 0

README

git-fastpack Go Reference

A "fast" Golang parser for git packfiles, operating exclusively on byte slices.

package main

import (
	"log"
	"os"

	fastpack "github.com/bored-engineer/git-fastpack"
)

func main() {
	// Create a scanner and set the internal LRU cache size
	scanner, err := fastpack.NewScanner(10000)
	if err != nil {
		log.Fatalf("fastpack.NewScanner failed: %v", err)
	}

	// Read the entire packfile into memory (hint: use mmap)
	b, err := os.ReadFile(os.Args[1])
	if err != nil {
		log.Fatalf("os.ReadFile failed: %v", err)
	}
	scanner.Reset(b)

	// Loop over the packfile contents
	_, objects, err := scanner.Header()
	if err != nil {
		log.Fatalf("(*fastpack.Scanner).Header failed: %v", err)
	}
	for range objects {
		ot, buf, err := scanner.Object()
		if err != nil {
			log.Fatalf("(*fastpack.Scanner).Object failed: %v", err)
		}
		oid := fastpack.OID(ot, buf)
		log.Printf("%x: %s(%d bytes)", oid, ot, len(buf))
	}
	if _, err := scanner.Trailer(); err != nil {
		log.Fatalf("(*fastpack.Scanner).Trailer failed: %v", err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OID

func OID(ot ObjectType, b []byte) [sha1.Size]byte

OID returns the git OID for a given ObjectType and content

Types

type ObjectType

type ObjectType int8

ObjectType is the type of an object within a packfile.

const (
	InvalidObject ObjectType = 0
	CommitObject  ObjectType = 1
	TreeObject    ObjectType = 2
	BlobObject    ObjectType = 3
	TagObject     ObjectType = 4
	// 5 reserved for future expansion
	OFSDeltaObject ObjectType = 6
	REFDeltaObject ObjectType = 7
)

func (ObjectType) String

func (ot ObjectType) String() string

String returns the string representation of the ObjectType.

func (ObjectType) Valid

func (ot ObjectType) Valid() bool

Valid returns true if the ObjectType is a valid Git object type.

type Scanner

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

Scanner iterates through the objects in a packfile, returning their type and decompressed data.

func NewScanner

func NewScanner(lruSize int) (*Scanner, error)

NewScanner creates a new Scanner for the given packfile data. The packfile data is not copied, so the caller must ensure it remains valid for the lifetime of the Scanner.

func (*Scanner) Header

func (s *Scanner) Header() (version uint32, objects uint32, err error)

Header parses the packfile header

func (*Scanner) Object

func (s *Scanner) Object() (ObjectType, []byte, error)

Object reads the next object from the packfile, returning its type and decompressed data.

func (*Scanner) Reset

func (s *Scanner) Reset(packfile []byte)

Reset initializes the Scanner with the given packfile data.

func (*Scanner) Trailer

func (s *Scanner) Trailer() ([sha1.Size]byte, error)

Trailer parses the packfile trailer and verifies the checksum.

Jump to

Keyboard shortcuts

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