mingit

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package mingit is a utility library for programmatically generating minimally-functional local Git repositories.

This is intended for the narrow use-case of using a Git repository to represent some data generated entirely by a program, such as a log of changes made to some data objects over the course of several steps. The goal is only to generate as easily as possible a bare-bones git repository directory with just enough content for normal git tools to be able to understand it.

There are lots of things that mingit doesn't do, including but not limited to: reading from existing repositories, generating pack files and index files, and interacting with git's network protocols. If you need git functionality not available here then consider using github.com/go-git/go-git instead.

Index

Constants

View Source
const (
	// ModeRegular is the file mode Git uses for regular (non-executable) files.
	// The target must be a blob in this case.
	ModeRegular os.FileMode = 0100644

	// ModeExecutable is the file mode Git uses for executable files.
	// The target must be a blob in this case.
	ModeExecutable os.FileMode = 0100755

	// ModeDir is the file mode Git uses for directories.
	// The target must be a tree in this case.
	ModeDir os.FileMode = 040000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Commit

type Commit struct {
	TreeID    ObjectID
	ParentIDs []ObjectID
	Author    *Identity
	Committer *Identity
	Message   string
}

Commit represents the data for a commit, which you can then encode into a Git object using the Object method.

func (*Commit) ForStorage

func (c *Commit) ForStorage() []byte

ForStorage returns a byte slice containing the commit data in the raw format that git would use inside a generated object.

func (*Commit) Object

func (c *Commit) Object() Object

Object encodes the data from the Commit into a Git object ready to be stored into a repository.

type Identity

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

Identity represents a name, address, and timestamp that can appear as either the author or the committer of a commit object.

type Object

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

Object represents an object that can be stored in a git repository.

func NewBlob

func NewBlob(content []byte) Object

NewBlob encodes and returns a new blob object.

func (Object) ID

func (o Object) ID() ObjectID

ID returns the id of the object, which is currently always a SHA-1 hash of the object's encoded content.

type ObjectID

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

ObjectID represents the ID of a Git object

func (ObjectID) String

func (oid ObjectID) String() string

type Repository

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

Repository represents a git repository under construction.

func NewRepository

func NewRepository(gitDir string) (*Repository, error)

NewRepository creates a new directory at the given path and populates it with just enough for git to consider it to be an empty repository.

The given directory must not already exist. Note that this creates a bare repository, like "git init --bare", so the repository won't have an associated work tree. You can clone the generated repository directory with "git clone" in order to create a copy with a work tree where you could potentially create new commits, etc.

func (*Repository) SetRef

func (r *Repository) SetRef(name string, target ObjectID) error

SetRef updates the given ref name to refer to the given object ID. Refs typically refer to either commits or tags, but in principle they can refer to objects of any type.

SetRef doesn't validate the given ref name in any way, so it's the caller's responsibility to pass something sensible. Most refs start with the prefix "refs/", but there are a few common ones that don't such as "HEAD" and "FETCH_HEAD". The ref name is a path to a file in the repository directory, so you should avoid passing a path that traverses out of the repository or that would overwrite a non-ref file in the repository.

Most normal git implementations also check whether the given target object is a suitable, safe replacement for the currently-present ref, but mingit does not: it just unconditionally overwrites any file already present at the given location.

func (*Repository) SetRefSymbolic

func (r *Repository) SetRefSymbolic(updateRef, targetRef string) error

SetRefSymbolic is similar to SetRef, but rather than setting the given refer to refer to a particular object it will instead refer to another ref.

In typical git use it's conventional for the HEAD ref to be a symbolic ref referring to the currently-selected branch. NewRepository initially sets HEAD to refer to refs/heads/main, so if you intend to use that as your main branch name then you don't need to explicitly reset it using this function.

func (*Repository) WriteBlob

func (r *Repository) WriteBlob(data []byte) (ObjectID, error)

WriteBlob writes the given byte slice into the repository as a blob object, and returns the id of the object that was created.

func (*Repository) WriteCommit

func (r *Repository) WriteCommit(commit *Commit) (ObjectID, error)

WriteCommit writes the given commit data into the repository as a commit object, and returns the id of the object that was created.

func (*Repository) WriteObject

func (r *Repository) WriteObject(obj Object) (ObjectID, error)

WriteObject writes the given object into the object store of the repository.

This is the low-level implementation of WriteBlob, WriteTree, and WriteCommit. You only need to do this if you're constructing your objects externally.

func (*Repository) WriteTree

func (r *Repository) WriteTree(tree Tree) (ObjectID, error)

WriteTree writes the given tree data into the repository as a tree object, and returns the id of the object that was created.

type Tree

type Tree []TreeItem

Tree represents the data for a git tree, which you can then encode into a Git object using the Object method.

func (Tree) Object

func (t Tree) Object() Object

Object encodes the data from the Tree into a Git object ready to be stored into a repository.

type TreeItem

type TreeItem struct {
	Mode     os.FileMode
	Name     string
	TargetID ObjectID
}

TreeItem represents a single item in a tree.

Jump to

Keyboard shortcuts

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