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 ¶
const ( Commit = packfile.Commit Tree = packfile.Tree Blob = packfile.Blob Tag = packfile.Tag )
The four object types.
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 ¶
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 )
var ErrAlgo = errors.New("objects: unsupported hash algorithm")
ErrAlgo reports an algorithm this package does not implement.
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.
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.
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 ¶
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.
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 "".
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).
type Header ¶
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 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 ¶
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) IsSubmodule ¶
IsSubmodule reports whether the entry is a gitlink.
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 ¶
OffsetSeconds returns TZ as seconds east of UTC, "-0130" being -5400.
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 (*TagObject) Encode ¶
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.