git

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package git provides functionality for reading and writing git repositories.

Index

Constants

This section is empty.

Variables

View Source
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 Blob

type Blob struct {
	Data []byte
}

Blob represents a git blob (file content).

func ParseBlob

func ParseBlob(obj *Object) (*Blob, error)

ParseBlob parses a blob from an Object.

func (*Blob) ToObject

func (b *Blob) ToObject() *Object

ToObject converts a Blob to an Object.

type Commit

type Commit struct {
	TreeHash  Hash
	Parents   []Hash
	Author    Signature
	Committer Signature
	Message   string
}

Commit represents a git commit.

func ParseCommit

func ParseCommit(obj *Object) (*Commit, error)

ParseCommit parses a commit from an Object.

func (*Commit) ToObject

func (c *Commit) ToObject() *Object

ToObject converts a Commit to an Object.

type CommitOptions

type CommitOptions struct {
	Author    Signature
	Committer Signature
	Message   string
	Parents   []Hash
}

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.

func ParseHash

func ParseHash(s string) (Hash, error)

ParseHash parses a hex-encoded hash string.

func (Hash) IsZero

func (h Hash) IsZero() bool

IsZero returns true if the hash is all zeros.

func (Hash) String

func (h Hash) String() string

String returns the hex-encoded hash.

type Object

type Object struct {
	Type ObjectType
	Data []byte
}

Object represents a git object.

func DecodeObject

func DecodeObject(data []byte) (*Object, error)

DecodeObject parses a compressed git object.

func (*Object) Encode

func (o *Object) Encode() ([]byte, error)

Encode returns the compressed git object data.

func (*Object) Hash

func (o *Object) Hash() Hash

Hash computes the SHA-1 hash of the 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) Head

func (r *Repository) Head() (Hash, error)

Head returns the hash of HEAD.

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.

func (*Repository) WriteTree

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

WriteTree writes a tree to the repository.

type Signature

type Signature struct {
	Name  string
	Email string
	When  time.Time
}

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

func ParseSignature(line string) (*Signature, error)

ParseSignature parses a signature from git format.

func SignatureAt

func SignatureAt(name, email string, when time.Time) Signature

SignatureAt creates a signature with a specific time.

func (*Signature) String

func (s *Signature) String() string

String returns the signature in git format.

type Tree

type Tree struct {
	Entries []TreeEntry
}

Tree represents a git tree (directory listing).

func ParseTree

func ParseTree(obj *Object) (*Tree, error)

ParseTree parses a tree from an Object.

func (*Tree) ToObject

func (t *Tree) ToObject() *Object

ToObject converts a Tree to an Object.

type TreeBuilder

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

TreeBuilder helps construct tree objects.

func NewTreeBuilder

func NewTreeBuilder() *TreeBuilder

NewTreeBuilder creates a new TreeBuilder.

func (*TreeBuilder) AddBlob

func (tb *TreeBuilder) AddBlob(name string, hash Hash, executable bool) error

AddBlob adds a blob entry to the tree.

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.

type TreeEntry

type TreeEntry struct {
	Mode uint32
	Name string
	Hash Hash
}

TreeEntry represents a single entry in a tree.

func (*TreeEntry) IsDir

func (e *TreeEntry) IsDir() bool

IsDir returns true if the entry is a directory (tree).

Jump to

Keyboard shortcuts

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