Documentation
¶
Overview ¶
Package git provides functionality for reading and writing git repositories.
Index ¶
- Variables
- type Blob
- type Commit
- type CommitOptions
- type Hash
- type Object
- type ObjectType
- type Repository
- func (r *Repository) CommitOnBranch(treeHash Hash, branch string, opts CommitOptions) (Hash, error)
- func (r *Repository) CreateCommit(treeHash Hash, opts CommitOptions) (Hash, error)
- func (r *Repository) GetHeadRef() (string, Hash, error)
- func (r *Repository) Head() (Hash, error)
- func (r *Repository) ListFilesAtCommit(commitHash Hash) ([]string, error)
- func (r *Repository) ListFilesAtTree(treeHash Hash, prefix string) ([]string, error)
- func (r *Repository) ReadBlob(hash Hash) (*Blob, error)
- func (r *Repository) ReadCommit(hash Hash) (*Commit, error)
- func (r *Repository) ReadFileAtCommit(commitHash Hash, path string) ([]byte, error)
- func (r *Repository) ReadFileAtTree(treeHash Hash, path string) ([]byte, error)
- func (r *Repository) ReadObject(hash Hash) (*Object, error)
- func (r *Repository) ReadTree(hash Hash) (*Tree, error)
- func (r *Repository) ResolveRef(ref string) (Hash, error)
- func (r *Repository) TreeFromMap(files map[string][]byte) (Hash, error)
- func (r *Repository) UpdateRef(ref string, hash Hash) error
- func (r *Repository) WriteBlob(data []byte) (Hash, error)
- func (r *Repository) WriteCommit(commit *Commit) (Hash, error)
- func (r *Repository) WriteObject(obj *Object) (Hash, error)
- func (r *Repository) WriteTree(tree *Tree) (Hash, error)
- type Signature
- type Tree
- type TreeBuilder
- type TreeEntry
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("object not found") ErrInvalidObject = errors.New("invalid object format") ErrNotARepository = errors.New("not a git repository") ErrInvalidRef = errors.New("invalid reference") )
Common errors.
Functions ¶
This section is empty.
Types ¶
type Commit ¶
type Commit struct {
TreeHash Hash
Parents []Hash
Author Signature
Committer Signature
Message string
}
Commit represents a git commit.
func ParseCommit ¶
ParseCommit parses a commit from an Object.
type CommitOptions ¶
CommitOptions contains options for creating a commit.
type Hash ¶
type Hash [20]byte
Hash represents a git object hash (SHA-1, 20 bytes).
var ZeroHash Hash
ZeroHash is the zero-value hash.
type Object ¶
type Object struct {
Type ObjectType
Data []byte
}
Object represents a git object.
func DecodeObject ¶
DecodeObject parses a compressed git object.
type ObjectType ¶
type ObjectType string
ObjectType represents the type of a git object.
const ( ObjectTypeBlob ObjectType = "blob" ObjectTypeTree ObjectType = "tree" ObjectTypeCommit ObjectType = "commit" ObjectTypeTag ObjectType = "tag" )
type Repository ¶
type Repository struct {
// Path is the path to the repository root (containing .git).
Path string
// GitDir is the path to the .git directory.
GitDir string
}
Repository represents a local git repository.
func Open ¶
func Open(path string) (*Repository, error)
Open opens an existing git repository at the given path. It searches for a .git directory in the given path or any parent directory.
func OpenAt ¶
func OpenAt(path string) (*Repository, error)
OpenAt opens a git repository at exactly the given path. Unlike Open, it does not search parent directories.
func (*Repository) CommitOnBranch ¶
func (r *Repository) CommitOnBranch(treeHash Hash, branch string, opts CommitOptions) (Hash, error)
CommitOnBranch creates a commit and updates the branch ref. If branch is empty, it updates HEAD directly (detached HEAD mode).
func (*Repository) CreateCommit ¶
func (r *Repository) CreateCommit(treeHash Hash, opts CommitOptions) (Hash, error)
CreateCommit creates a new commit with the given tree and options. It writes the commit object and optionally updates a ref.
func (*Repository) GetHeadRef ¶
func (r *Repository) GetHeadRef() (string, Hash, error)
GetHeadRef returns the reference that HEAD points to (e.g., "refs/heads/main"). If HEAD is detached, it returns an empty string and the hash.
func (*Repository) ListFilesAtCommit ¶
func (r *Repository) ListFilesAtCommit(commitHash Hash) ([]string, error)
ListFilesAtCommit lists all files at a given commit.
func (*Repository) ListFilesAtTree ¶
func (r *Repository) ListFilesAtTree(treeHash Hash, prefix string) ([]string, error)
ListFilesAtTree lists all files in a tree recursively.
func (*Repository) ReadBlob ¶
func (r *Repository) ReadBlob(hash Hash) (*Blob, error)
ReadBlob reads a blob by its hash.
func (*Repository) ReadCommit ¶
func (r *Repository) ReadCommit(hash Hash) (*Commit, error)
ReadCommit reads a commit by its hash.
func (*Repository) ReadFileAtCommit ¶
func (r *Repository) ReadFileAtCommit(commitHash Hash, path string) ([]byte, error)
ReadFileAtCommit reads a file from the repository at a given commit.
func (*Repository) ReadFileAtTree ¶
func (r *Repository) ReadFileAtTree(treeHash Hash, path string) ([]byte, error)
ReadFileAtTree reads a file from a tree, following the path.
func (*Repository) ReadObject ¶
func (r *Repository) ReadObject(hash Hash) (*Object, error)
ReadObject reads an object by its hash.
func (*Repository) ReadTree ¶
func (r *Repository) ReadTree(hash Hash) (*Tree, error)
ReadTree reads a tree by its hash.
func (*Repository) ResolveRef ¶
func (r *Repository) ResolveRef(ref string) (Hash, error)
ResolveRef resolves a reference to a hash. Supports both full refs (refs/heads/main) and short refs (HEAD, main).
func (*Repository) TreeFromMap ¶
func (r *Repository) TreeFromMap(files map[string][]byte) (Hash, error)
TreeFromMap creates a tree from a map of path to file content. This is a convenience function for creating simple trees. Paths must use forward slashes (/) as separators. All files are created with mode 0o100644 (non-executable).
func (*Repository) UpdateRef ¶
func (r *Repository) UpdateRef(ref string, hash Hash) error
UpdateRef updates a reference to point to the given hash.
func (*Repository) WriteBlob ¶
func (r *Repository) WriteBlob(data []byte) (Hash, error)
WriteBlob writes a blob to the repository.
func (*Repository) WriteCommit ¶
func (r *Repository) WriteCommit(commit *Commit) (Hash, error)
WriteCommit writes a commit to the repository.
func (*Repository) WriteObject ¶
func (r *Repository) WriteObject(obj *Object) (Hash, error)
WriteObject writes an object to the repository and returns its hash.
type Signature ¶
Signature represents the author or committer of a commit.
func DefaultSignature ¶
func DefaultSignature() Signature
DefaultSignature creates a signature with a default name and email.
func ParseSignature ¶
ParseSignature parses a signature from git format.
func SignatureAt ¶
SignatureAt creates a signature with a specific time.
type Tree ¶
type Tree struct {
Entries []TreeEntry
}
Tree represents a git tree (directory listing).
type TreeBuilder ¶
type TreeBuilder struct {
// contains filtered or unexported fields
}
TreeBuilder helps construct tree objects.
func (*TreeBuilder) AddBlob ¶
func (tb *TreeBuilder) AddBlob(name string, hash Hash, executable bool) error
AddBlob adds a blob entry to the tree.
func (*TreeBuilder) AddSymlink ¶
func (tb *TreeBuilder) AddSymlink(name string, hash Hash) error
AddSymlink adds a symbolic link entry.
func (*TreeBuilder) AddTree ¶
func (tb *TreeBuilder) AddTree(name string, hash Hash) error
AddTree adds a subtree entry.
func (*TreeBuilder) Build ¶
func (tb *TreeBuilder) Build() *Tree
Build creates the Tree, sorting entries as git does.