gitobj

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: GPL-3.0, LGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package gitobj reads git objects as the store holds them: the exact bytes git takes the SHA-1 of, "<kind> <size>\x00<content>", under a key.GitSHA1 key. It knows the framing, the hash, and the links one object holds to others, which is all the store needs: it does not check an object the way git fsck does. See architecture/git-objects.md.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMalformed means a payload is not a git object the store can walk.
	ErrMalformed = errors.New("gitobj: malformed object")
	// ErrMismatch means a payload does not hash to the key it was given under.
	ErrMismatch = errors.New("gitobj: payload does not match its key")
	// ErrCollision means a payload carries the marks of a SHA-1 collision attack.
	// Its SHA-1 says nothing about it, so it must not be stored under one.
	ErrCollision = errors.New("gitobj: SHA-1 collision attack detected")
)

Functions

func Encode

func Encode(kind Kind, content []byte) []byte

Encode frames content as an object of the given kind: the bytes a store holds, and the bytes git takes the SHA-1 of.

func KeyOf

func KeyOf(data []byte) (key.Key, error)

KeyOf returns the key of a payload: its SHA-1, as NewGitSHA1 lays it out. It does not look at the frame. A payload that carries the marks of a collision attack has no key, and gets ErrCollision.

func Verify

func Verify(k key.Key, data []byte) error

Verify reports whether data is the object k names: not a collision, hashing to k, and framed as a git object. It leaves the content alone. git does not parse an object it only reaches as a file, and neither does the store: an object that will not parse is refused by the walk that has to read it (Links), which comes before a reference to it is accepted, and by nothing short of that.

Types

type Kind

type Kind uint8

Kind is what git calls an object's type.

const (
	Blob Kind = iota
	Tree
	Commit
	Tag
)

func Parse

func Parse(data []byte) (Kind, []byte, error)

Parse splits a payload into its kind and content, holding the frame to what git reads: a known kind, one space, a decimal size without leading zeros, a NUL, and exactly that many bytes. The content is a part of data.

func (Kind) String

func (k Kind) String() string

String returns the name git frames an object with.

type Link struct {
	Key  key.Key
	Leaf bool
}

Link is one object's reference to another. Leaf says what the referring object knows of the target: it is a blob, which has no links, so a walk may count it without reading it. Git's own walks trust an entry's mode the same way.

func Links(data []byte) ([]Link, error)

Links returns the objects a payload refers to, in the order it names them: a commit's tree and then its parents, a tree's entries, a tag's target. A blob has none. A tree entry that holds a submodule's commit is no link, since that commit lives in another repository; git does not need it here either.

Jump to

Keyboard shortcuts

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