object

package
v0.0.0-...-2ee2aa9 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package object contains methods and objects to work with git objects

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrObjectUnknown represents an error thrown when encoutering an
	// unknown object
	ErrObjectUnknown = errors.New("invalid object type")

	// ErrObjectInvalid represents an error thrown when an object contains
	// unexpected data or when the wrong object is provided to a method.
	// Ex. Inserting a ObjectDeltaOFS in a tree
	// Ex.2 Creating a tag using a commit with no ID (commit not persisted
	// 	to the odb)
	ErrObjectInvalid = errors.New("invalid object")

	// ErrTreeInvalid represents an error thrown when parsing an invalid
	// tree object
	ErrTreeInvalid = errors.New("invalid tree")

	// ErrCommitInvalid represents an error thrown when parsing an invalid
	// commit object
	ErrCommitInvalid = errors.New("invalid commit")

	// ErrTagInvalid represents an error thrown when parsing an invalid
	// tag object
	ErrTagInvalid = errors.New("invalid tag")
)
View Source
var ErrSignatureInvalid = errors.New("commit signature is invalid")

ErrSignatureInvalid is an error thrown when the signature of a commit couldn't be parsed

Functions

This section is empty.

Types

type Blob

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

Blob represents a blob object

func NewBlob

func NewBlob(o *Object) *Blob

NewBlob returns a new Blob object from a git Object

func (*Blob) Bytes

func (b *Blob) Bytes() []byte

Bytes returns the blob's contents

func (*Blob) BytesCopy

func (b *Blob) BytesCopy() []byte

BytesCopy returns a copy of blob's contents

func (*Blob) ID

func (b *Blob) ID() ginternals.Oid

ID returns the blob's ID

func (*Blob) Size

func (b *Blob) Size() int

Size returns the size of the blob

func (*Blob) ToObject

func (b *Blob) ToObject() *Object

ToObject returns the Blob's underlying Object

type Commit

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

Commit represents a commit object

func NewCommit

func NewCommit(treeID ginternals.Oid, author Signature, opts *CommitOptions) *Commit

NewCommit creates a new Commit object Any provided Oids won't be check

func NewCommitFromObject

func NewCommitFromObject(o *Object) (*Commit, error)

NewCommitFromObject creates a commit from a raw object

A commit has following format:

tree {sha} parent {sha} author {author_name} <{author_email}> {author_date_seconds} {author_date_timezone} committer {committer_name} <{committer_email}> {committer_date_seconds} {committer_date_timezone} gpgsig -----BEGIN PGP SIGNATURE----- {gpg key over multiple lines}

-----END PGP SIGNATURE-----

{a blank line} {commit message}

Note:

  • A commit can have 0, 1, or many parents lines The very first commit of a repo has no parents A regular commit as 1 parent A merge commit has 2 or more parents
  • The gpgsig is optional

func (*Commit) Author

func (c *Commit) Author() Signature

Author returns the Signature of the person that made the changes

func (*Commit) Committer

func (c *Commit) Committer() Signature

Committer returns the Signature of the person that created the commit

func (*Commit) GPGSig

func (c *Commit) GPGSig() string

GPGSig returns the GPG signature of the commit, if any

func (*Commit) ID

func (c *Commit) ID() ginternals.Oid

ID returns the SHA of the commit object

func (*Commit) Message

func (c *Commit) Message() string

Message returns the commit's message

func (*Commit) ParentIDs

func (c *Commit) ParentIDs() []ginternals.Oid

ParentIDs returns the list of SHA of the parent commits (if any) - The first commit of an orphan branch has 0 parents - A regular commit or the result of a fast-forward merge has 1 parent - A true merge (no fast-forward) as 2 or more parents

func (*Commit) ToObject

func (c *Commit) ToObject() *Object

ToObject returns the underlying Object

func (*Commit) TreeID

func (c *Commit) TreeID() ginternals.Oid

TreeID returns the SHA of the commit's tree

type CommitOptions

type CommitOptions struct {
	Message string
	GPGSig  string
	// Committer represent the person creating the commit.
	// If not provided, the author will be used as committer
	Committer Signature
	ParentsID []ginternals.Oid
}

CommitOptions represents all the optional data available to create a commit

type Object

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

Object represents a git object. An object can be of multiple types but they all share similarities (same storage system, same header, etc.). Object are stored in .git/objects, and may be stored in a packfile (kind of an optimized git database) located in .git/objects/packs https://git-scm.com/book/en/v2/Git-Internals-Git-Objects

func New

func New(typ Type, content []byte) *Object

New creates a new git object of the given type

func (*Object) AsBlob

func (o *Object) AsBlob() *Blob

AsBlob parses the object as Blob

func (*Object) AsCommit

func (o *Object) AsCommit() (*Commit, error)

AsCommit parses the object as Commit

func (*Object) AsTag

func (o *Object) AsTag() (*Tag, error)

AsTag parses the object as Tag

func (*Object) AsTree

func (o *Object) AsTree() (*Tree, error)

AsTree parses the object as Tree

func (*Object) Bytes

func (o *Object) Bytes() []byte

Bytes returns the object's contents

func (*Object) Compress

func (o *Object) Compress() (data []byte, err error)

Compress return the object zlib compressed, alongside its oid. The format of the compressed data is: [type] [size][NULL][content] The type in ascii, followed by a space, followed by the size in ascii, followed by a null character (0), followed by the object data maybe we can move some code around

func (*Object) ID

func (o *Object) ID() ginternals.Oid

ID returns the ID of the object.

func (*Object) Size

func (o *Object) Size() int

Size returns the size of the object

func (*Object) Type

func (o *Object) Type() Type

Type returns the Type for this object

type Signature

type Signature struct {
	Time  time.Time
	Name  string
	Email string
}

Signature represents the author/committer and time of a commit

func NewSignature

func NewSignature(name, email string) Signature

NewSignature generates a signature at the current date and time

func NewSignatureFromBytes

func NewSignatureFromBytes(b []byte) (Signature, error)

NewSignatureFromBytes returns a signature from an array of byte

A signature has the following format: User Name <user.email@domain.tld> timestamp timezone Ex: Melvin Laplanche <melvin.wont.reply@gmail.com> 1566115917 -0700

func (Signature) IsZero

func (s Signature) IsZero() bool

IsZero returns whether the signature has Zero value

func (Signature) String

func (s Signature) String() string

String returns a stringified version of the Signature

type Tag

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

Tag represents a Tag object

func NewTag

func NewTag(p *TagParams) *Tag

NewTag creates a new Tag object

func NewTagFromObject

func NewTagFromObject(o *Object) (*Tag, error)

NewTagFromObject creates a new Tag from a raw git object

A tag has following format:

object {sha} type {target_object_type} tag {tag_name} tagger {author_name} <{author_email}> {author_date_seconds} {author_date_timezone} gpgsig -----BEGIN PGP SIGNATURE----- {gpg key over multiple lines}

-----END PGP SIGNATURE-----

{a blank line} {tag message}

Note: - The gpgsig is optional

func (*Tag) GPGSig

func (t *Tag) GPGSig() string

GPGSig returns the GPG signature of the tag, if any

func (*Tag) ID

func (t *Tag) ID() ginternals.Oid

ID returns the SHA of the tag object

func (*Tag) Message

func (t *Tag) Message() string

Message returns the tag's message

func (*Tag) Name

func (t *Tag) Name() string

Name returns the tag's name

func (*Tag) Tagger

func (t *Tag) Tagger() Signature

Tagger returns the Signature of the person that created the tag

func (*Tag) Target

func (t *Tag) Target() ginternals.Oid

Target returns the ID of the object targeted by the tag

func (*Tag) ToObject

func (t *Tag) ToObject() *Object

ToObject returns the underlying Object

func (*Tag) Type

func (t *Tag) Type() Type

Type returns the type of the targeted object

type TagParams

type TagParams struct {
	Target    *Object
	Name      string
	Tagger    Signature
	Message   string
	OptGPGSig string
}

TagParams represents all the data needed to create a Tag Params starting by Opt are optionals

type Tree

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

Tree represents a git tree object

func NewTree

func NewTree(entries []TreeEntry) *Tree

NewTree returns a new tree with the given entries

func NewTreeFromObject

func NewTreeFromObject(o *Object) (*Tree, error)

NewTreeFromObject returns a new tree from an object

A tree has following format:

{octal_mode} {path_name}\0{encoded_sha}

Note: - a Tree may have multiple entries

func (*Tree) Entries

func (t *Tree) Entries() []TreeEntry

Entries returns a copy of tree entries

func (*Tree) Entry

func (t *Tree) Entry(path string) (entry TreeEntry, ok bool)

Entry returns an entry from its path

func (*Tree) ID

func (t *Tree) ID() ginternals.Oid

ID returns the object's ID ginternals.NullOid is returned if the object doesn't have an ID yet

func (*Tree) ToObject

func (t *Tree) ToObject() *Object

ToObject returns an Object representing the tree

type TreeEntry

type TreeEntry struct {
	Path string
	ID   ginternals.Oid
	Mode TreeObjectMode
}

TreeEntry represents an entry inside a git tree

type TreeObjectMode

type TreeObjectMode int32

TreeObjectMode represents the mode of an object inside a tree Non-standard modes (like 0o100664) are not supported

const (
	// ModeFile represents the mode to use for a regular file
	ModeFile TreeObjectMode = 0o100644
	// ModeExecutable represents the mode to use for a executable file
	ModeExecutable TreeObjectMode = 0o100755
	// ModeDirectory represents the mode to use for a directory
	ModeDirectory TreeObjectMode = 0o040000
	// ModeSymLink represents the mode to use for a symbolic link
	ModeSymLink TreeObjectMode = 0o120000
	// ModeGitLink represents the mode to use for a gitlink (submodule)
	ModeGitLink TreeObjectMode = 0o160000
)

func (TreeObjectMode) IsValid

func (m TreeObjectMode) IsValid() bool

IsValid returns whether the mode is a supported mode or not

func (TreeObjectMode) ObjectType

func (m TreeObjectMode) ObjectType() Type

ObjectType returns the object type associated to a mode

type Type

type Type int8

Type represents the type of an object as stored in a packfile

const (
	TypeCommit Type = 1
	TypeTree   Type = 2
	TypeBlob   Type = 3
	TypeTag    Type = 4
	// 5 is reserved for future use
	ObjectDeltaOFS Type = 6
	ObjectDeltaRef Type = 7
)

List of all the possible object types

func NewTypeFromString

func NewTypeFromString(t string) (Type, error)

NewTypeFromString returns an Type from its string representation

func (Type) IsValid

func (t Type) IsValid() bool

IsValid check id the object type is an existing type

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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