git

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2022 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package git implements internal git objects and related APIs.

Index

Constants

View Source
const (
	// AuthorName is tbe name of the author used as part of metadata of a commit.
	AuthorName string = "Shyamsunder Rathi"
	// AuthorEmail is the email of the author used as part of metadata of a commit.
	AuthorEmail string = "sxxxxxx@gmail.com"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Blob added in v0.2.0

type Blob struct {
	Repository *Repo
	*Object
}

Blob is a git object to represent the data of a single file.

func NewBlob

func NewBlob(repo *Repo, obj *Object) (*Blob, error)

NewBlob creates a new blob object by parsing a Object.

func NewBlobFromFile

func NewBlobFromFile(repo *Repo, file string) (*Blob, error)

NewBlobFromFile creates a new blob object by reading data from a file.

func (*Blob) DataSize added in v0.2.0

func (blob *Blob) DataSize() int

DataSize returns the size of the data of a blob object.

func (*Blob) Print added in v0.2.0

func (blob *Blob) Print() string

Print returns a string representation of a blob object.

func (*Blob) Type added in v0.2.0

func (blob *Blob) Type() string

Type returns the type string of a blob object.

type Commit added in v0.2.0

type Commit struct {
	Repository *Repo
	*Object
	Entries entryMap
	// Keep the keys to maintain the insertion order.
	Keys []string
	Msg  string
}

Commit is a object with a map of "commit" entries, commit msg and a git object.

func NewCommit

func NewCommit(repo *Repo, obj *Object) (*Commit, error)

NewCommit creates a new commit object by parsing a Object.

func NewCommitFromParams

func NewCommitFromParams(repo *Repo, treeHash, parentHash, msg string) (*Commit, error)

NewCommitFromParams builds a commit object using a 'tree' and optionall a 'parent' hash, and a given commit message. This can be used by CLI commands such as "gogit commit-tree".

func (*Commit) Author added in v0.3.0

func (commit *Commit) Author() (name, email string)

Author returns the name and email of the author of the given commit.

func (*Commit) DataSize added in v0.2.0

func (commit *Commit) DataSize() int

DataSize returns the size of the data of a commit object.

func (*Commit) Parents added in v0.2.0

func (commit *Commit) Parents() []string

Parents returns a list of parents of the given commit. If there are no parent (base commit), then it returns an empty list.

func (*Commit) ParseData added in v0.2.0

func (commit *Commit) ParseData() error

ParseData parses a commit object's bytes and prepares a dictionary of its components.

func (*Commit) PrettyPrint added in v0.2.0

func (commit *Commit) PrettyPrint() (string, error)

PrettyPrint prints a commit object in a human readable format, similar to what is shown by "git log" output.

func (*Commit) Print added in v0.2.0

func (commit *Commit) Print() string

Print returns a string representation of a commit object.

func (*Commit) TreeHash added in v0.2.0

func (commit *Commit) TreeHash() string

TreeHash returns the "tree" object hash inside the given commit object. Each commit object has only one "tree" object inside it.

func (*Commit) Type added in v0.2.0

func (commit *Commit) Type() string

Type returns the type string of a commit object.

type ObjIntf added in v0.3.0

type ObjIntf interface {
	Print() string
	Type() string
	DataSize() int
}

ObjIntf is a common interface shared by all type of git objects.

type Object added in v0.2.0

type Object struct {
	ObjType string
	ObjData []byte
}

Object is a struct holding the raw data for any git object type. ObjType can be one of "commit", "blob", "tree" or "tag".

func NewObject

func NewObject(objType string, data []byte) *Object

NewObject returns a new git object of given type and with given data bytes.

type RefEntry added in v0.2.0

type RefEntry struct {
	Name    string
	RefHash string
}

RefEntry keeps a mapping of a reference object with its associated reference.

type Repo

type Repo struct {
	GitDir   string
	WorkTree string
}

Repo structure to hold the current repository details.

func GetRepo

func GetRepo(path string) (*Repo, error)

GetRepo is used by all commands other than "gogit init" to work on an existing repo. .git directory can be at given path, or can be at any parent up to rootdir.

func NewRepo

func NewRepo(path string) (*Repo, error)

NewRepo is used by 'gogit init' to create a fresh repo.

func (*Repo) DirPath

func (r *Repo) DirPath(create bool, paths ...string) (string, error)

DirPath gets (and optionally creates) a directory path inside .git in the repo. Example: ["objects", "1e", "ab123"] returns ".git/objects/1e/ab123"

func (*Repo) FilePath

func (r *Repo) FilePath(create bool, paths ...string) (string, error)

FilePath gets a file path inside .git in the repo. Optionally create needed directories in the path. Last item in 'paths' is the file name.

func (*Repo) GetRefs added in v0.2.0

func (r *Repo) GetRefs(pattern string, getHead bool) ([]RefEntry, error)

GetRefs gets all the references inside the .git directory. This can be used by commands such as "gogit show-ref".

func (*Repo) NameResolve added in v0.2.0

func (r *Repo) NameResolve(name string) ([]string, error)

NameResolve resolves a given reference string to one or more equivalent hashes. Useful to:

  • Convert a short hash to a list of matching full size hashes.
  • Convert a symbolic, head or tag reference to a list of matching full size hashes.

func (*Repo) ObjectParse

func (r *Repo) ObjectParse(objHash string) (*Object, error)

ObjectParse finds the data referred by the given sha1 hash and add the data to the object as per "Git" specifications.

func (*Repo) ObjectWrite

func (r *Repo) ObjectWrite(obj *Object, write bool) (string, error)

ObjectWrite calculates the sha1 of a git object and optionally write it to a file as per "Git" specifications.

func (*Repo) RefResolve

func (r *Repo) RefResolve(path string) (string, string, error)

RefResolve converts a symbolic reference to its object hash.

func (*Repo) UniqueNameResolve added in v0.2.0

func (r *Repo) UniqueNameResolve(name string) (string, error)

UniqueNameResolve converts a given name to a unique valid full object hash. It returns an error if there are less or more than 1 matching objects to the given name. This can be used by many commands to act on a single unique hash after taking a possible ambiguous name from the user.

func (*Repo) UpdateRef added in v0.3.0

func (r *Repo) UpdateRef(ref string, newValue string) error

UpdateRef updates the given strict reference to the full hash by resolving the value given by newValue. If 'ref' is a symbolic reference (such as HEAD), then the target reference is updated instaed (if HEAD is pointing to master, then master is updated).

func (*Repo) ValidateRef added in v0.2.0

func (r *Repo) ValidateRef(ref string) (string, error)

ValidateRef strictly validates if a given reference is a valid reference in the local repository (or is HEAD). Returns the resolved object hash. This can be used by commands such as "gogit show-ref -verify".

type Tree added in v0.2.0

type Tree struct {
	Repository *Repo
	*Object
	Entries []TreeEntry
}

Tree is a object with a list of "tree" entries and a git object.

func NewTree

func NewTree(repo *Repo, obj *Object) (*Tree, error)

NewTree creates a new tree object by parsing a Object.

func NewTreeFromInput

func NewTreeFromInput(repo *Repo, input string) (*Tree, error)

NewTreeFromInput parses the given string and create a tree from it.

func (*Tree) Checkout added in v0.2.0

func (tree *Tree) Checkout(path string) error

Checkout recreates an entire worktree in a given path by recursively reading the blobs and trees inside this tree object.

func (*Tree) DataSize added in v0.2.0

func (tree *Tree) DataSize() int

DataSize returns the size of the data of a tree object.

func (*Tree) ParseData added in v0.2.0

func (tree *Tree) ParseData() error

ParseData parses a tree object's bytes and prepares a list of its components.

func (*Tree) Print added in v0.2.0

func (tree *Tree) Print() string

Print returns a string representation of a tree object.

func (*Tree) Type added in v0.2.0

func (tree *Tree) Type() string

Type returns the type string of a tree object.

type TreeEntry

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

TreeEntry is a struct holding components of a tree object.

Jump to

Keyboard shortcuts

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