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 ¶
- Variables
- func Slug(branch string) string
- func ValidateBranch(branch string) error
- type CommandError
- type ExecRunner
- type Git
- func (g *Git) AddWorktree(ctx context.Context, repo, path, branch, base string) error
- func (g *Git) BranchExists(ctx context.Context, repo, branch string) bool
- func (g *Git) Dirs(ctx context.Context, dir string) (gitDir, commonDir string, err error)
- func (g *Git) RemoveWorktree(ctx context.Context, repo, path string, force bool) error
- func (g *Git) Root(ctx context.Context, dir string) (string, error)
- func (g *Git) Status(ctx context.Context, dir string) (*Status, error)
- func (g *Git) Worktrees(ctx context.Context, repo string) ([]Worktree, error)
- type Runner
- type Status
- type Watcher
- type WatcherOptions
- type Worktree
Constants ¶
This section is empty.
Variables ¶
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 ¶
Slug turns a branch name into a single directory name: feature/login becomes feature-login.
func ValidateBranch ¶
ValidateBranch rejects branch names git would refuse or that could be mistaken for options.
Types ¶
type CommandError ¶
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 Git ¶
type Git struct {
Runner Runner
}
Git answers questions about repositories.
func (*Git) AddWorktree ¶
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 ¶
BranchExists reports whether a local branch exists.
func (*Git) Dirs ¶
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 ¶
RemoveWorktree removes the worktree at path. Without force, git refuses when it has changes; that is reported as ErrDirty.
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.
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 ¶
Run processes filesystem events and periodic refreshes until ctx ends, then releases the watcher.
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.