object

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2024 License: Apache-2.0 Imports: 11 Imported by: 1

Documentation

Overview

Package object provides types for Git objects and functions for parsing and serializing those objects. For an overview, see https://git-scm.com/book/en/v2/Git-Internals-Git-Objects

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendPrefix

func AppendPrefix(dst []byte, typ Type, n int64) []byte

AppendPrefix appends a Git object prefix (e.g. "blob 42\x00") to a byte slice.

func BlobSum

func BlobSum(r io.Reader, size int64) (githash.SHA1, error)

BlobSum computes the Git SHA-1 object ID of the blob with the given content. This includes the Git object prefix as part of the hash input. It returns an error if the blob does not match the provided size in bytes.

Types

type Commit

type Commit struct {
	// Tree is the hash of the commit's tree object.
	Tree githash.SHA1
	// Parents are the hashes of the commit's parents.
	Parents []githash.SHA1

	// Author identifies the person who wrote the code.
	Author User
	// AuthorTime is the time the code was written.
	// The Location is significant,
	// and its name may be used for serialization if it meets the correct format.
	AuthorTime time.Time

	// Committer identifies the person who committed the code to the repository.
	Committer User
	// CommitTime is the time the code was committed to the repository.
	// The Location is significant,
	// and its name may be used for serialization if it meets the correct format.
	CommitTime time.Time

	// Extra stores any lines in the commit object
	// between the committer line and the gpgsig line.
	// It will never begin or end with a newline,
	// nor will it contain blank lines.
	Extra CommitFields

	// If GPGSignature is not empty, then it is the ASCII-armored signature of
	// the commit.
	GPGSignature []byte

	// Message is the commit message.
	Message string
}

A Commit is a parsed Git commit object.

func ParseCommit

func ParseCommit(data []byte) (*Commit, error)

ParseCommit deserializes a commit in the Git object format. It is the same as calling *Commit.UnmarshalBinary on a new commit.

func (*Commit) MarshalBinary added in v0.9.0

func (c *Commit) MarshalBinary() ([]byte, error)

MarshalBinary serializes a commit into the Git object format.

func (*Commit) MarshalText

func (c *Commit) MarshalText() ([]byte, error)

MarshalText serializes a commit into the Git object format. It is the same as calling *Commit.MarshalBinary.

func (*Commit) SHA1

func (c *Commit) SHA1() githash.SHA1

SHA1 computes the SHA-1 hash of the commit object. This is commonly known as the "commit hash" and uniquely identifies the commit.

func (*Commit) Summary

func (c *Commit) Summary() string

Summary returns the first line of the message.

func (*Commit) UnmarshalBinary added in v0.9.0

func (c *Commit) UnmarshalBinary(data []byte) error

UnmarshalBinary deserializes a commit from the Git object format.

func (*Commit) UnmarshalText

func (c *Commit) UnmarshalText(data []byte) error

UnmarshalText deserializes a commit from the Git object format. It is the same as calling *Commit.UnmarshalBinary.

type CommitFields added in v0.10.0

type CommitFields string

CommitFields is a block of lines, conventionally in the form "key value". A value may span multiple lines by starting each continuation line with a space.

func (CommitFields) Cut added in v0.10.0

func (fields CommitFields) Cut() (head, tail CommitFields)

Cut slices fields around the first field.

func (CommitFields) First added in v0.10.0

func (fields CommitFields) First() (key, value string)

First returns the first field's key and value.

func (CommitFields) Get added in v0.10.0

func (fields CommitFields) Get(key string) string

Get returns the value of the first field in fields with the given key, or the empty string if no such field exists.

func (CommitFields) IsValid added in v0.10.0

func (fields CommitFields) IsValid() bool

IsValid reports whether fields can be serialized into a Git commit. Valid fields do not begin or end with a newline and do not contain blank lines.

type Mode

type Mode uint32

Mode is a tree entry file mode. It is similar to fs.FileMode, but is limited to a specific set of modes.

const (
	// ModePlain indicates a non-executable file.
	ModePlain Mode = 0o100644
	// ModeExecutable indicates an executable file.
	ModeExecutable Mode = 0o100755
	// ModeDir indicates a subdirectory.
	ModeDir Mode = 0o040000
	// ModeSymlink indicates a symbolic link.
	ModeSymlink Mode = 0o120000
	// ModeGitlink indicates a Git submodule.
	ModeGitlink Mode = 0o160000

	// ModePlainGroupWritable indicates a non-executable file.
	// This is equivalent to ModePlain, but was sometimes generated by
	// older versions of Git.
	ModePlainGroupWritable Mode = 0o100664
)

Git tree entry modes.

func (Mode) FileMode

func (m Mode) FileMode() (f fs.FileMode, ok bool)

FileMode converts the Git mode into an fs.FileMode, if possible. ModeGitlink will have both fs.ModeDir and fs.ModeSymlink set.

func (Mode) Format

func (m Mode) Format(f fmt.State, c rune)

Format implements fmt.Formatter to make %x and %X format the number rather than the string.

func (Mode) IsDir

func (m Mode) IsDir() bool

IsDir reports whether m describes a directory.

func (Mode) IsRegular

func (m Mode) IsRegular() bool

IsRegular reports whether m describes a file.

func (Mode) String

func (m Mode) String() string

String formats the mode as zero-padded octal.

type Prefix added in v0.9.0

type Prefix struct {
	Type Type
	Size int64
}

Prefix is a parsed Git object prefix like "blob 42\x00".

func (Prefix) MarshalBinary added in v0.9.0

func (p Prefix) MarshalBinary() ([]byte, error)

MarshalBinary returns the result of AppendPrefix.

func (Prefix) String added in v0.9.0

func (p Prefix) String() string

String returns the prefix without the trailing NUL byte.

func (*Prefix) UnmarshalBinary added in v0.9.0

func (p *Prefix) UnmarshalBinary(data []byte) error

UnmarshalBinary parses an object prefix.

type Tag

type Tag struct {
	// ObjectID is the hash of the object that the tag refers to.
	ObjectID githash.SHA1
	// ObjectType is the type of the object that the tag refers to.
	ObjectType Type

	// Name is the name of the tag.
	Name string

	// Tagger identifies the person who created the tag.
	Tagger User
	// Time is the time the tag was created.
	// The Location is significant.
	Time time.Time

	// Message is the tag message.
	Message string
}

A Tag is a parsed Git tag object. These are referred to as "annotated tags" in the Git documentation.

func ParseTag

func ParseTag(data []byte) (*Tag, error)

ParseTag deserializes a tag in the Git object format. It is the same as calling UnmarshalText on a new tag.

func (*Tag) MarshalBinary added in v0.9.0

func (t *Tag) MarshalBinary() ([]byte, error)

MarshalBinary serializes a tag into the Git object format.

func (*Tag) MarshalText

func (t *Tag) MarshalText() ([]byte, error)

MarshalText serializes a tag into the Git object format. It is the same as calling MarshalBinary.

func (*Tag) SHA1

func (t *Tag) SHA1() githash.SHA1

SHA1 computes the SHA-1 hash of the tag object.

func (*Tag) Summary

func (t *Tag) Summary() string

Summary returns the first line of the message.

func (*Tag) UnmarshalBinary added in v0.9.0

func (t *Tag) UnmarshalBinary(data []byte) error

UnmarshalBinary deserializes a tag from the Git object format.

func (*Tag) UnmarshalText

func (t *Tag) UnmarshalText(data []byte) error

UnmarshalText deserializes a tag from the Git object format. It is the same as calling UnmarshalBinary.

type Tree

type Tree []*TreeEntry

A Tree is a Git tree object: a flat list of files in a directory. The zero value is an empty tree.

Tree methods generally assume elements of a Tree are sorted and contain no duplicates. Use *Tree.Sort to sort a tree and check for duplicates.

func ParseTree

func ParseTree(src []byte) (Tree, error)

ParseTree deserializes a tree in the Git object format. It is the same as calling UnmarshalBinary on a new tree.

func (Tree) Len

func (tree Tree) Len() int

Len returns the number of entries in the tree.

Len is a part of sort.Interface.

func (Tree) Less

func (tree Tree) Less(i, j int) bool

Less reports whether the i'th entry is ordered before the j'th entry in Git path ordering. Git path ordering is lexicographical, but directories are treated as if their name ends in a slash ("/").

Less is a part of sort.Interface.

func (Tree) MarshalBinary

func (tree Tree) MarshalBinary() ([]byte, error)

MarshalBinary serializes the tree into the Git tree object format. It returns an error if the tree is not sorted or contains duplicates.

func (Tree) SHA1

func (tree Tree) SHA1() githash.SHA1

SHA1 computes the SHA-1 hash of the tree object. It panics if the tree is not sorted or contains duplicates.

func (Tree) Search

func (tree Tree) Search(name string) *TreeEntry

Search returns the entry with the given name in the tree or nil if not found. It may return incorrect results if the tree is not sorted.

func (Tree) Sort

func (tree Tree) Sort() error

Sort sorts the tree, returning an error if there are any duplicates. See *Tree.Less for details regarding the sort order.

func (Tree) String

func (tree Tree) String() string

String formats the tree in an ASCII-clean debugging format.

func (Tree) Swap

func (tree Tree) Swap(i, j int)

Swap swaps the i'th entry with the j'th entry.

Swap is a part of sort.Interface.

func (*Tree) UnmarshalBinary

func (tree *Tree) UnmarshalBinary(src []byte) error

UnmarshalBinary deserializes a tree from the Git object format. If UnmarshalBinary does not return an error, the tree will always be sorted.

type TreeEntry

type TreeEntry struct {
	Name     string
	Mode     Mode
	ObjectID githash.SHA1
}

A TreeEntry represents a single file in a Git tree object.

func (*TreeEntry) String

func (ent *TreeEntry) String() string

String formats the entry in an ASCII-clean format similar to the Git tree object format.

type Type

type Type string

Type is an enumeration of Git object types.

const (
	TypeBlob   Type = "blob"
	TypeTree   Type = "tree"
	TypeCommit Type = "commit"
	TypeTag    Type = "tag"
)

Object types.

func (Type) IsValid

func (typ Type) IsValid() bool

IsValid reports whether typ is one of the known constants.

type User

type User string

User identifies an author or committer as a string like "Octocat <octocat@example.com>".

func MakeUser

func MakeUser(name, email string) (User, error)

MakeUser constructs a User string from a name and an email address.

func (User) Email

func (u User) Email() string

Email returns the user's email address, if they have one.

func (User) Name

func (u User) Name() string

Name returns the user's name.

Jump to

Keyboard shortcuts

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