objects

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: BSD-3-Clause Imports: 10 Imported by: ? 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 objects decodes the four git object types from their inflated content, with no git binary: commits, annotated tags, trees, and the hash of a blob (or of any object).

Formats (the pages opened for this package): the Git book chapter "Git Internals - Git Objects" (the `<type> <size>\0` header, the tree entry modes, the commit layout), gitformat-signature(5) (a tag signature is appended to the message; a commit signature is the multi-line "gpgsig" header whose continuation lines start with a space), git-cat-file(1) (the oracle in the tests) and git-hash-object(1).

The decoders are lenient where git is (legacy tree modes such as 100664 and 040000, a tag without a tagger, any extra commit header) and strict where leniency would be unsafe (a malformed id, a name that could escape a directory). Strict fsck rules (entry order, ".git" spellings) are the job of packfile.ParseObject, which this package does not repeat. Every decoder refuses input larger than MaxObjectBytes and a tree of more than MaxTreeEntries entries, and never panics.

Index

Constants

View Source
const (
	Commit = packfile.Commit
	Tree   = packfile.Tree
	Blob   = packfile.Blob
	Tag    = packfile.Tag
)

The four object types.

View Source
const (
	// MaxObjectBytes is the largest object a decoder accepts (it is also well
	// below math.MaxInt32, so a length fits an int on a 32-bit system).
	MaxObjectBytes = 64 << 20
	// MaxTreeEntries is the largest tree a decoder accepts.
	MaxTreeEntries = 1 << 20
	// MaxHeaders is the largest number of extra commit headers accepted.
	MaxHeaders = 1 << 16
)

Bounds. They are far above real objects and low enough that a decoder's memory stays within a small multiple of its input (at most 3.5 times for a tree of one-byte names, asserted by TestBombAmplification and TestBombTreeAtBound).

Variables

View Source
var (
	// ErrCorrupt reports malformed object content. It wraps packfile.ErrCorrupt.
	ErrCorrupt = packfile.ErrCorrupt
	// ErrLimit reports a bound exceeded. It wraps packfile.ErrLimit.
	ErrLimit = packfile.ErrLimit
)
View Source
var ErrAlgo = errors.New("objects: unsupported hash algorithm")

ErrAlgo reports an algorithm this package does not implement.

View Source
var ErrNonCanonical = errors.New("objects: non-canonical encoding")

ErrNonCanonical reports content that git may accept but that Encode would not write back byte for byte (a zero-padded date or mode, a missing blank line, a header without a space...). Such an error also wraps ErrCorrupt.

The decoders keep one invariant: for every input b, DecodeX(b) either fails or returns a value v with v.Encode() == b. Two different byte strings, which are two different object ids, therefore never decode to the same value, and a caller that re-encodes a decoded object hashes the object it was given. The price is that an object written in a legacy or sloppy form (a "040000" tree mode, a commit with no blank line) is refused: read it as bytes and use ComputeHash for its id.

View Source
var ErrSize = errors.New("objects: hashed bytes differ from the declared size")

ErrSize reports a Hasher that received other than the declared size, or a declared size below zero.

View Source
var ErrType = errors.New("objects: unknown object type")

ErrType reports a hash requested for a type that is not commit, tree, blob or tag: git has no such object, and hashing it under a made-up header would give an id nothing can read back.

Functions

func ObjectHeader

func ObjectHeader(t Type, size int64) ([]byte, error)

ObjectHeader returns "<type> <size>\0", the prefix git hashes before the content. It refuses a type that is not commit, tree, blob or tag (ErrType) and a negative size (ErrSize).

Types

type Algo

type Algo uint8

Algo names an object hash algorithm. Only SHA-1 exists today; ID is the 20-byte packfile.ID, so adding SHA-256 means a wider id type next to it, not a change to the callers that name an Algo.

const SHA1 Algo = 1

SHA1 is git's default object format.

func (Algo) String

func (a Algo) String() string

type CommitObject

type CommitObject struct {
	Tree      ID
	Parents   []ID
	Author    Signature
	Committer Signature
	// Headers are the headers after "committer", in order, verbatim.
	Headers []Header
	// Message is everything after the blank line that ends the headers; the
	// blank line is required (an object without it is refused as non-canonical).
	Message string
}

CommitObject is a decoded commit.

func DecodeCommit

func DecodeCommit(data []byte) (*CommitObject, error)

DecodeCommit decodes the inflated content of a commit object. The headers must come in git's order: tree, parent*, author, committer, then any others (gitformat-signature(5): the gpgsig header sits between the committer line and the blank line).

func (*CommitObject) Encode

func (c *CommitObject) Encode() ([]byte, error)

Encode returns the object content as git writes it. It refuses what DecodeCommit would not read back as the same value: an identity with '<', '>' or a control byte in a name or email, a bad time or zone, a header key that is empty or holds a space or LF, a NUL in a header, a second gpgsig or gpgsig-sha256 header, more parents or headers than the bounds, an object over MaxObjectBytes.

func (*CommitObject) GPGSig

func (c *CommitObject) GPGSig() string

GPGSig returns the "gpgsig" header (a PGP or SSH signature), or "".

func (*CommitObject) Header

func (c *CommitObject) Header(key string) (string, bool)

Header returns the value of the first extra header named key.

type Entry

type Entry struct {
	// Mode is the entry's mode as written, without a leading zero ("040000" is
	// refused as non-canonical, see ErrNonCanonical); "100664" gives
	// ModeDeprecated (use Mode.Canonical for what git writes today).
	Mode Mode
	Name string
	ID   ID
}

Entry is a tree entry.

type Hasher

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

Hasher computes an object id from a stream whose size is known up front, as `git hash-object` does for a file. It detects the SHA-1 collision attack the way git does (SHA-1DC, github.com/pjbgf/sha1cd).

func NewHasher

func NewHasher(a Algo, t Type, size int64) (*Hasher, error)

NewHasher starts the hash of an object of type t and size bytes.

func (*Hasher) Sum

func (h *Hasher) Sum() (ID, error)

Sum returns the id, ErrSize when the bytes written are not the declared size, and packfile.ErrCollision when the input carried a collision attack's mark.

func (*Hasher) Write

func (h *Hasher) Write(p []byte) (int, error)

Write adds content. It never fails; Sum checks the total.

type Header struct {
	Key   string
	Value string
}

Header is an extra header of a commit (gpgsig, mergetag, encoding, ...). Value holds the lines of a multi-line header joined with LF, the single space that starts each continuation line removed, so a gpgsig Value is the armored signature as signed; Encode puts the spaces back.

type ID

type ID = packfile.ID

ID is a SHA-1 object id, the type packfile uses.

func ComputeHash

func ComputeHash(a Algo, t Type, data []byte) (ID, error)

ComputeHash returns the id of an object: the hash of "<type> <len>\0" and data. For a blob it is what `git hash-object` prints.

type Mode

type Mode uint32

Mode is a tree entry's file mode, as git stores it.

const (
	ModeEmpty      Mode = 0
	ModeDir        Mode = 0o040000
	ModeRegular    Mode = 0o100644
	ModeDeprecated Mode = 0o100664 // written by very old git; read as ModeRegular
	ModeExecutable Mode = 0o100755
	ModeSymlink    Mode = 0o120000
	ModeSubmodule  Mode = 0o160000
)

File modes, as git writes them (git's own constants, cache.h).

func (Mode) Canonical

func (m Mode) Canonical() Mode

Canonical maps a legacy mode to the one git writes today, the way git's canon_mode does: a regular file is 100644 or 100755 by its owner-execute bit (100664 included), a symlink 120000, a directory 40000, a gitlink 160000. A mode of another kind is returned unchanged.

func (Mode) IsDir

func (m Mode) IsDir() bool

IsDir reports whether the entry is a subtree.

func (m Mode) IsGitlink() bool

IsGitlink is IsSubmodule.

func (Mode) IsRegular

func (m Mode) IsRegular() bool

IsRegular reports whether the entry is a regular file (executable or not).

func (Mode) IsSubmodule

func (m Mode) IsSubmodule() bool

IsSubmodule reports whether the entry is a gitlink.

func (m Mode) IsSymlink() bool

IsSymlink reports whether the entry is a symbolic link.

func (Mode) String

func (m Mode) String() string

String returns the octal mode without leading zeros: "40000" for a directory, the form git writes in a tree object.

type Signature

type Signature struct {
	Name  string
	Email string
	// When is the Unix time in seconds, as written.
	When int64
	// TZ is the zone exactly as written: five bytes, a sign and four digits
	// ("+0000", "-0130", "+0530"; git does not check the minutes are below 60,
	// and neither does this).
	TZ string
}

Signature is an author, committer or tagger line: "Name <email> time zone".

func (Signature) OffsetSeconds

func (s Signature) OffsetSeconds() int

OffsetSeconds returns TZ as seconds east of UTC, "-0130" being -5400.

func (Signature) String

func (s Signature) String() string

String renders the line as git writes it (without a trailing LF).

type TagObject

type TagObject struct {
	// Target is the tagged object and TargetType its type.
	Target     ID
	TargetType Type
	// Name is the tag's name.
	Name string
	// Tagger is nil for the old tags that have no tagger line.
	Tagger *Signature
	// Message is the text after the blank line, without the signature.
	Message string
	// Signature is the armored signature appended to the message, from its
	// BEGIN line to the end of the object, or "".
	Signature string
}

TagObject is a decoded annotated tag.

func DecodeTag

func DecodeTag(data []byte) (*TagObject, error)

DecodeTag decodes the inflated content of an annotated tag.

func (*TagObject) Encode

func (t *TagObject) Encode() ([]byte, error)

Encode returns the object content as git writes it. It refuses what DecodeTag would not read back as the same value: a TargetType that is not one of the four, an empty name or one holding a control byte, a bad tagger, a Message and Signature that do not split back at the same place (a Signature that does not begin at a marker line, or a marker line inside the Message after which a Signature follows), an object over MaxObjectBytes.

type TreeObject

type TreeObject struct {
	Entries []Entry
}

TreeObject is a decoded tree.

func DecodeTree

func DecodeTree(data []byte) (*TreeObject, error)

DecodeTree decodes the inflated content of a tree object. Entries stay in the order written. A name that is empty, ".", "..", or holds '/' or NUL is refused: it could make a checkout write outside its directory.

DecodeTree alone is NOT enough to check a tree out safely. It does not refuse duplicate or unsorted names, ".git" and its NTFS and HFS spellings ("GIT~1"), backslashes, or a mode other than the usual ones (a 100000 file, a 120777 link); packfile.ParseObject applies those fsck rules, and a checkout must run them too. The names of the entries are slices of one copy of the input, so a name kept alive keeps that copy alive.

func (*TreeObject) Encode

func (t *TreeObject) Encode() ([]byte, error)

Encode returns the object content as git writes it (modes without a leading zero). It refuses what DecodeTree refuses (a mode that is not a file, symlink, directory or gitlink, an empty name, ".", "..", a name holding '/' or NUL, too many entries or too large an object), so it only emits what DecodeTree reads back as the same value.

type Type

type Type = packfile.Type

Type is a git object type, the type packfile uses.

Jump to

Keyboard shortcuts

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