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
- type Commit
- type Identity
- type Object
- type ObjectID
- type Repository
- func (r *Repository) SetRef(name string, target ObjectID) error
- func (r *Repository) SetRefSymbolic(updateRef, targetRef string) error
- func (r *Repository) WriteBlob(data []byte) (ObjectID, error)
- func (r *Repository) WriteCommit(commit *Commit) (ObjectID, error)
- func (r *Repository) WriteObject(obj Object) (ObjectID, error)
- func (r *Repository) WriteTree(tree Tree) (ObjectID, error)
- type Tree
- type TreeItem
Constants ¶
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 ¶
ForStorage returns a byte slice containing the commit data in the raw format that git would use inside a generated object.
type Identity ¶
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.
type ObjectID ¶
type ObjectID struct {
// contains filtered or unexported fields
}
ObjectID represents the ID of a Git object
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.