objects

package
v0.0.0-...-8022555 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2019 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

package objects deals with Git object format.

It implements low-level accessors to read and parse loose objects and packfiles in Git repositories, and defines appropriate data types representing the three basic object types of Git: blobs, trees and commits.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Blob

type Blob struct {
	Hash Hash
	Data []byte
}

A Blob is an object representing a chunk of data.

func (Blob) ID

func (b Blob) ID() Hash

func (Blob) Type

func (b Blob) Type() ObjType

func (Blob) WriteTo

func (b Blob) WriteTo(w io.Writer) error

type Commit

type Commit struct {
	Hash          Hash
	Tree          Hash   // The tree object pointed to by this commit.
	Parents       []Hash // The parents of this commit.
	Author        string // The email address of the commit author.
	AuthorTime    time.Time
	Committer     string // The email address of the committer.
	CommitterTime time.Time
	Message       []byte // The commit description.
}

A Commit represents the metadata stored in a Git commit.

func (Commit) ID

func (c Commit) ID() Hash

func (Commit) Type

func (c Commit) Type() ObjType

func (Commit) WriteTo

func (c Commit) WriteTo(w io.Writer) error

type Hash

type Hash [20]byte

func NewHash

func NewHash(s []byte) (b Hash)

func (Hash) String

func (h Hash) String() string

type Mode

type Mode uint16

A Git mode is (type<<12|unixperm).

const (
	ModeRegular Mode = 8 << 12
	ModeDir     Mode = 4 << 12
	ModeSymlink Mode = 2 << 12

	ModeGitlink = ModeDir | ModeSymlink
)

type ObjType

type ObjType uint8

ObjType enumerates the three possible object types: blob, tree, commit.

const (
	BLOB ObjType = iota
	TREE
	COMMIT
)

func (ObjType) String

func (t ObjType) String() string

type Object

type Object interface {
	// ID return the hash of the object.
	ID() Hash
	// Type returns the object type (BLOB, TREE, COMMIT).
	Type() ObjType
	// WriteTo serializes the object: if the Writer is a sha1 Hash
	// this will produce the hash for this object, if the Writer is
	// a compressor from compress/flate, this is equivalent to the
	// loose object format.
	WriteTo(io.Writer) error
}

func ParseLoose

func ParseLoose(r io.ReadCloser) (Object, error)

ParseLoose reads a loose object as stored in the objects/ subdirectory of a git repository.

Example
f, err := os.Open(".git/objects/4b/b670aefc9ea81bede6b019b11d0c5d2f1871c2")
if err != nil {
	return
}
// ParseLoose closes f.
obj, err := ParseLoose(f)
if err != nil {
	return
}
switch obj.(type) {
case Commit:
	// object is a commit.
case Blob:
	// object is a blob.
case Tree:
	// object is a tree.
}
Output:

type PackReader

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

A PackReader implements access to Git pack files and indexes.

func NewPackReader

func NewPackReader(pack, idx *io.SectionReader) (*PackReader, error)

NewPackReader creates a PackReader from files pointing to a packfile and its index.

func (*PackReader) Extract

func (pk *PackReader) Extract(h Hash) (Object, error)

Extract finds and parses an object from a pack.

Example
base := ".git/objects/pack/pack-bb4afc76654154e3a9f198723ba89873ecb14293"
fpack, err := os.Open(base + ".pack")
if err != nil {
	log.Fatal(err)
}
fidx, err := os.Open(base + ".idx")
if err != nil {
	log.Fatal(err)
}
defer fpack.Close()
defer fidx.Close()

packsize, err1 := fpack.Seek(0, os.SEEK_END)
idxsize, err2 := fidx.Seek(0, os.SEEK_END)
if err1 != nil || err2 != nil {
	log.Fatal(err1, err2)
}
pk, err := NewPackReader(
	io.NewSectionReader(fpack, 0, packsize),
	io.NewSectionReader(fidx, 0, idxsize))
if err != nil {
	log.Fatal(err)
}
var hash Hash
hex.Decode(hash[:], []byte("2e16bbf779131a90346eab585e9e5c4736d3aeac"))
obj, err := pk.Extract(hash)
if err != nil {
	log.Fatal(err)
}
log.Printf("%+v", obj)
Output:

func (*PackReader) Objects

func (pk *PackReader) Objects() ([]Hash, error)

Objects returns the list of hashes of objects stored in this pack.

type Tree

type Tree struct {
	Hash    Hash
	Entries []TreeElem
}

A Tree represents a collection of blobs and trees in a directory-like fashion.

func (Tree) ID

func (t Tree) ID() Hash

func (Tree) Type

func (t Tree) Type() ObjType

func (Tree) WriteTo

func (t Tree) WriteTo(w io.Writer) error

type TreeElem

type TreeElem struct {
	Name string
	Mode os.FileMode
	Hash Hash
}

Jump to

Keyboard shortcuts

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