git

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package git reads repository state for environments (branch, ahead and behind, dirty files) and manages the worktrees Hive creates for agents. It drives the git CLI rather than reimplementing git, so it always agrees with what the user sees in their shell.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotRepository means the directory is not inside a git working tree.
	ErrNotRepository = errors.New("not a git repository")
	// ErrDirty means git refused because the worktree has changes.
	ErrDirty = errors.New("the worktree has uncommitted changes")
)

Functions

func Slug

func Slug(branch string) string

Slug turns a branch name into a single directory name: feature/login becomes feature-login.

func ValidateBranch

func ValidateBranch(branch string) error

ValidateBranch rejects branch names git would refuse or that could be mistaken for options.

Types

type CommandError

type CommandError struct {
	Args   []string
	Stderr string
	Err    error
}

CommandError is a git command that failed, with what git said.

func (*CommandError) Error

func (e *CommandError) Error() string

func (*CommandError) Unwrap

func (e *CommandError) Unwrap() error

type ExecRunner

type ExecRunner struct{}

ExecRunner runs the git binary on PATH.

func (ExecRunner) Run

func (ExecRunner) Run(ctx context.Context, dir string, args ...string) ([]byte, error)

type Git

type Git struct {
	Runner Runner
}

Git answers questions about repositories.

func New

func New() *Git

New returns a Git using the git binary.

func (*Git) AddWorktree

func (g *Git) AddWorktree(ctx context.Context, repo, path, branch, base string) error

AddWorktree checks out branch at path. A branch that does not exist yet is created from base (the current HEAD when base is empty).

func (*Git) BranchExists

func (g *Git) BranchExists(ctx context.Context, repo, branch string) bool

BranchExists reports whether a local branch exists.

func (*Git) Dirs

func (g *Git) Dirs(ctx context.Context, dir string) (gitDir, commonDir string, err error)

Dirs returns the working tree's git directory and the repository's common directory (they differ for linked worktrees). Changes there (HEAD, index, refs) are what the watcher listens for.

func (*Git) RemoveWorktree

func (g *Git) RemoveWorktree(ctx context.Context, repo, path string, force bool) error

RemoveWorktree removes the worktree at path. Without force, git refuses when it has changes; that is reported as ErrDirty.

func (*Git) Root

func (g *Git) Root(ctx context.Context, dir string) (string, error)

Root returns the top of the working tree containing dir.

func (*Git) Status

func (g *Git) Status(ctx context.Context, dir string) (*Status, error)

Status returns the state of the working tree containing dir.

func (*Git) Worktrees

func (g *Git) Worktrees(ctx context.Context, repo string) ([]Worktree, error)

Worktrees lists the repository's worktrees, the main one first.

type Runner

type Runner interface {
	// Run runs `git -C dir args...` and returns its standard output.
	Run(ctx context.Context, dir string, args ...string) ([]byte, error)
}

Runner runs git. It is a port so tests can fake git's answers.

type Status

type Status struct {
	// Root is the top of the working tree.
	Root string `json:"root"`
	// Branch is the checked-out branch; empty when Detached.
	Branch   string `json:"branch,omitempty"`
	Head     string `json:"head,omitempty"` // abbreviated commit, empty before the first commit
	Detached bool   `json:"detached,omitempty"`
	Upstream string `json:"upstream,omitempty"`
	Ahead    int    `json:"ahead,omitempty"`
	Behind   int    `json:"behind,omitempty"`
	// Staged, Modified, Untracked and Conflicts count paths in each state.
	Staged    int `json:"staged,omitempty"`
	Modified  int `json:"modified,omitempty"`
	Untracked int `json:"untracked,omitempty"`
	Conflicts int `json:"conflicts,omitempty"`
}

Status is a working tree's state.

func (*Status) Dirty

func (s *Status) Dirty() bool

Dirty reports whether the working tree has any change.

type Watcher

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

Watcher keeps the status of watched directories current. Changes in a repository's git directory (HEAD, index, refs) are noticed through the filesystem within a moment; edits to working-tree files are noticed by a periodic refresh, because watching every file of a large tree costs more than it saves.

func NewWatcher

func NewWatcher(g *Git, onChange func(dir string, st *Status), opts WatcherOptions) (*Watcher, error)

NewWatcher returns a watcher calling onChange whenever a watched directory's status changes (st is nil when it stopped being a repository).

func (*Watcher) Run

func (w *Watcher) Run(ctx context.Context)

Run processes filesystem events and periodic refreshes until ctx ends, then releases the watcher.

func (*Watcher) Status

func (w *Watcher) Status(dir string) *Status

Status returns the last known status of dir.

func (*Watcher) Unwatch

func (w *Watcher) Unwatch(dir string)

Unwatch stops tracking dir.

func (*Watcher) Watch

func (w *Watcher) Watch(ctx context.Context, dir string) *Status

Watch starts tracking dir and returns its current status (nil when it is not a repository; it is still refreshed periodically, in case one is created).

type WatcherOptions

type WatcherOptions struct {
	// Interval is the periodic refresh (default 5s).
	Interval time.Duration
	// Debounce groups bursts of filesystem events (default 250ms).
	Debounce time.Duration
	Logger   *slog.Logger
}

WatcherOptions tunes a Watcher. Zero values use defaults.

type Worktree

type Worktree struct {
	Path     string `json:"path"`
	Head     string `json:"head,omitempty"`
	Branch   string `json:"branch,omitempty"`
	Detached bool   `json:"detached,omitempty"`
	Bare     bool   `json:"bare,omitempty"`
	Locked   bool   `json:"locked,omitempty"`
	// Main is the repository's main working tree.
	Main bool `json:"main,omitempty"`
}

Worktree is one checkout of a repository.

Jump to

Keyboard shortcuts

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