git

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotARepo = errors.New("not a git repository")

ErrNotARepo is returned when the path is not inside a Git repository.

Functions

func LogFormatFlag

func LogFormatFlag() string

LogFormatFlag returns the --format flag for git log.

Types

type Branch

type Branch struct {
	Name      string
	IsCurrent bool
	IsRemote  bool
	Upstream  string
	Hash      string
	Subject   string
	Ahead     int
	Behind    int
}

Branch represents a local or remote branch.

func ParseBranchOutput

func ParseBranchOutput(out string) []Branch

ParseBranchOutput parses `git branch -a --format=...`.

type CLIService

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

CLIService implements Service by shelling out to the git CLI. Optimised for large monorepos:

  • GIT_OPTIONAL_LOCKS=0 env var on all read commands (no lock contention)
  • Context-based timeouts prevent hangs
  • Stdout/Stderr separated — stderr noise doesn't corrupt output

func NewCLIService

func NewCLIService(path string) (*CLIService, error)

NewCLIService opens a Git repository at the given path.

func (*CLIService) AheadBehind

func (s *CLIService) AheadBehind() (int, int, error)

AheadBehind returns how many commits ahead/behind the upstream.

func (*CLIService) BisectBad

func (s *CLIService) BisectBad() error

BisectBad marks the current commit as bad.

func (*CLIService) BisectGood

func (s *CLIService) BisectGood() error

BisectGood marks the current commit as good.

func (*CLIService) BisectLog

func (s *CLIService) BisectLog() (string, error)

BisectLog returns the bisect log.

func (*CLIService) BisectReset

func (s *CLIService) BisectReset() error

BisectReset resets the bisect session.

func (*CLIService) BisectStart

func (s *CLIService) BisectStart(bad, good string) error

BisectStart starts a git bisect.

func (*CLIService) Branches

func (s *CLIService) Branches() ([]Branch, error)

Branches returns all branches.

func (*CLIService) Commit

func (s *CLIService) Commit(message string) error

Commit creates a new commit with the given message.

func (*CLIService) CommitAmend

func (s *CLIService) CommitAmend(message string) error

CommitAmend amends the last commit with the given message.

func (*CLIService) ConflictFiles

func (s *CLIService) ConflictFiles() ([]string, error)

ConflictFiles returns paths with merge conflicts.

func (*CLIService) CreateBranch

func (s *CLIService) CreateBranch(name string) error

CreateBranch creates a new branch.

func (*CLIService) DeleteBranch

func (s *CLIService) DeleteBranch(name string, force bool) error

DeleteBranch deletes the given branch.

func (*CLIService) Diff

func (s *CLIService) Diff(staged bool, path string) (string, error)

Diff returns the diff for a path.

func (*CLIService) DiffRange

func (s *CLIService) DiffRange(from, to string) (string, error)

DiffRange returns the diff between two refs.

func (*CLIService) Discard

func (s *CLIService) Discard(paths ...string) error

Discard discards changes for the given paths.

func (*CLIService) Fetch

func (s *CLIService) Fetch(remote string) error

Fetch fetches from the given remote.

func (*CLIService) GitDir

func (s *CLIService) GitDir() string

GitDir returns the path to the .git directory.

func (*CLIService) Head

func (s *CLIService) Head() (string, error)

Head returns the current HEAD ref.

func (*CLIService) IsClean

func (s *CLIService) IsClean() (bool, error)

IsClean reports whether the worktree is clean.

func (*CLIService) IsMerging

func (s *CLIService) IsMerging() bool

IsMerging reports whether a merge is in progress.

func (*CLIService) IsRebasing

func (s *CLIService) IsRebasing() bool

IsRebasing reports whether a rebase is in progress.

func (*CLIService) Log

func (s *CLIService) Log(limit int, args ...string) ([]Commit, error)

Log returns the commit log.

func (*CLIService) LogGraph

func (s *CLIService) LogGraph(limit int) ([]GraphEntry, error)

LogGraph returns the commit log with ASCII graph.

func (*CLIService) MarkResolved

func (s *CLIService) MarkResolved(path string) error

MarkResolved marks a conflict as resolved.

func (*CLIService) MergeBranch

func (s *CLIService) MergeBranch(name string) error

MergeBranch merges the given branch into the current branch.

func (*CLIService) Pull

func (s *CLIService) Pull(remote, branch string) error

Pull pulls from the given remote and branch.

func (*CLIService) Push

func (s *CLIService) Push(remote, branch string, force bool) error

Push pushes to the given remote and branch.

func (*CLIService) RebaseAbort

func (s *CLIService) RebaseAbort() error

RebaseAbort aborts a rebase in progress.

func (*CLIService) RebaseContinue

func (s *CLIService) RebaseContinue() error

RebaseContinue continues a rebase in progress.

func (*CLIService) RebaseInteractive

func (s *CLIService) RebaseInteractive(onto string) error

RebaseInteractive starts an interactive rebase.

func (*CLIService) Remotes

func (s *CLIService) Remotes() ([]Remote, error)

Remotes returns all configured remotes.

func (*CLIService) RenameBranch

func (s *CLIService) RenameBranch(oldName, newName string) error

RenameBranch renames a branch.

func (*CLIService) RepoRoot

func (s *CLIService) RepoRoot() string

RepoRoot returns the repository root path.

func (*CLIService) Show

func (s *CLIService) Show(hash string) (*Commit, string, error)

Show returns the commit details and diff for a given hash.

func (*CLIService) Stage

func (s *CLIService) Stage(paths ...string) error

Stage stages the given paths.

func (*CLIService) StageAll

func (s *CLIService) StageAll() error

StageAll stages all changes.

func (*CLIService) StashApply

func (s *CLIService) StashApply(index int) error

StashApply applies the stash at the given index.

func (*CLIService) StashDrop

func (s *CLIService) StashDrop(index int) error

StashDrop drops the stash at the given index.

func (*CLIService) StashList

func (s *CLIService) StashList() ([]StashEntry, error)

StashList returns stash entries.

func (*CLIService) StashPop

func (s *CLIService) StashPop(index int) error

StashPop pops the stash at the given index.

func (*CLIService) StashSave

func (s *CLIService) StashSave(message string) error

StashSave saves a new stash entry.

func (*CLIService) StashShow

func (s *CLIService) StashShow(index int) (string, error)

StashShow shows the diff for a stash entry.

func (*CLIService) Status

func (s *CLIService) Status() (*StatusResult, error)

Status returns the current working tree status.

func (*CLIService) SwitchBranch

func (s *CLIService) SwitchBranch(name string) error

SwitchBranch switches to the given branch.

func (*CLIService) Unstage

func (s *CLIService) Unstage(paths ...string) error

Unstage unstages the given paths.

func (*CLIService) UnstageAll

func (s *CLIService) UnstageAll() error

UnstageAll unstages all changes.

func (*CLIService) Upstream

func (s *CLIService) Upstream() string

Upstream returns the upstream tracking branch name.

func (*CLIService) WorktreeAdd

func (s *CLIService) WorktreeAdd(path, branch string) error

WorktreeAdd adds a new worktree.

func (*CLIService) WorktreeList

func (s *CLIService) WorktreeList() ([]Worktree, error)

WorktreeList returns all worktrees.

func (*CLIService) WorktreeRemove

func (s *CLIService) WorktreeRemove(path string) error

WorktreeRemove removes a worktree.

type CachedService

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

CachedService wraps a Service implementation with a TTL-based cache for expensive read operations. Write operations (Stage, Commit, etc.) automatically invalidate the cache so the next read is fresh.

This is critical for monorepo performance: multiple views and the status bar all request overlapping data (Status, Head, AheadBehind, etc.) within the same refresh cycle. Without caching, a single refresh event could spawn 15+ git subprocesses. With caching, it spawns ~5.

The cache is bounded by maxCacheEntries to prevent unbounded memory growth across long-running sessions or multiple instances.

func NewCachedService

func NewCachedService(inner Service, ttl time.Duration) *CachedService

NewCachedService wraps an existing Service with a TTL cache. Recommended TTL: 1-2 seconds. This ensures that within a single refresh cycle (which triggers multiple git queries), each query only hits git once.

func (*CachedService) AheadBehind

func (c *CachedService) AheadBehind() (int, int, error)

AheadBehind delegates to the inner service (cached).

func (*CachedService) BisectBad

func (c *CachedService) BisectBad() error

BisectBad marks current commit as bad and invalidates the cache.

func (*CachedService) BisectGood

func (c *CachedService) BisectGood() error

BisectGood marks current commit as good and invalidates the cache.

func (*CachedService) BisectLog

func (c *CachedService) BisectLog() (string, error)

BisectLog delegates to the inner service (not cached).

func (*CachedService) BisectReset

func (c *CachedService) BisectReset() error

BisectReset resets bisect and invalidates the cache.

func (*CachedService) BisectStart

func (c *CachedService) BisectStart(bad, good string) error

BisectStart starts bisect and invalidates the cache.

func (*CachedService) Branches

func (c *CachedService) Branches() ([]Branch, error)

Branches delegates to the inner service (cached).

func (*CachedService) Commit

func (c *CachedService) Commit(message string) error

Commit creates a commit and invalidates the cache.

func (*CachedService) CommitAmend

func (c *CachedService) CommitAmend(message string) error

CommitAmend amends the last commit and invalidates the cache.

func (*CachedService) ConflictFiles

func (c *CachedService) ConflictFiles() ([]string, error)

ConflictFiles delegates to the inner service (cached).

func (*CachedService) CreateBranch

func (c *CachedService) CreateBranch(name string) error

CreateBranch creates a branch and invalidates the cache.

func (*CachedService) DeleteBranch

func (c *CachedService) DeleteBranch(name string, force bool) error

DeleteBranch deletes a branch and invalidates the cache.

func (*CachedService) Diff

func (c *CachedService) Diff(staged bool, path string) (string, error)

Diff delegates to the inner service (not cached).

func (*CachedService) DiffRange

func (c *CachedService) DiffRange(from, to string) (string, error)

DiffRange delegates to the inner service (not cached).

func (*CachedService) Discard

func (c *CachedService) Discard(paths ...string) error

Discard discards changes in paths and invalidates the cache.

func (*CachedService) Fetch

func (c *CachedService) Fetch(remote string) error

Fetch fetches from remote and invalidates the cache.

func (*CachedService) GitDir

func (c *CachedService) GitDir() string

GitDir delegates to the inner service.

func (*CachedService) Head

func (c *CachedService) Head() (string, error)

Head returns the current HEAD ref (cached).

func (*CachedService) Invalidate

func (c *CachedService) Invalidate()

Invalidate clears all cached entries. Called after any write operation.

func (*CachedService) IsClean

func (c *CachedService) IsClean() (bool, error)

IsClean reports whether the worktree is clean (cached).

func (*CachedService) IsMerging

func (c *CachedService) IsMerging() bool

IsMerging delegates to the inner service (cached).

func (*CachedService) IsRebasing

func (c *CachedService) IsRebasing() bool

IsRebasing delegates to the inner service (cached).

func (*CachedService) Log

func (c *CachedService) Log(limit int, args ...string) ([]Commit, error)

Log delegates to the inner service (not cached).

func (*CachedService) LogGraph

func (c *CachedService) LogGraph(limit int) ([]GraphEntry, error)

LogGraph delegates to the inner service (not cached).

func (*CachedService) MarkResolved

func (c *CachedService) MarkResolved(path string) error

MarkResolved marks a conflict as resolved and invalidates the cache.

func (*CachedService) MergeBranch

func (c *CachedService) MergeBranch(name string) error

MergeBranch merges a branch and invalidates the cache.

func (*CachedService) Pull

func (c *CachedService) Pull(remote, branch string) error

Pull pulls from remote and invalidates the cache.

func (*CachedService) Push

func (c *CachedService) Push(remote, branch string, force bool) error

Push pushes to remote and invalidates the cache.

func (*CachedService) RebaseAbort

func (c *CachedService) RebaseAbort() error

RebaseAbort aborts rebase and invalidates the cache.

func (*CachedService) RebaseContinue

func (c *CachedService) RebaseContinue() error

RebaseContinue continues rebase and invalidates the cache.

func (*CachedService) RebaseInteractive

func (c *CachedService) RebaseInteractive(onto string) error

RebaseInteractive starts interactive rebase and invalidates the cache.

func (*CachedService) Remotes

func (c *CachedService) Remotes() ([]Remote, error)

Remotes delegates to the inner service (cached).

func (*CachedService) RenameBranch

func (c *CachedService) RenameBranch(oldName, newName string) error

RenameBranch renames a branch and invalidates the cache.

func (*CachedService) RepoRoot

func (c *CachedService) RepoRoot() string

RepoRoot delegates to the inner service.

func (*CachedService) Show

func (c *CachedService) Show(hash string) (*Commit, string, error)

Show delegates to the inner service (not cached).

func (*CachedService) Stage

func (c *CachedService) Stage(paths ...string) error

Stage stages paths and invalidates the cache.

func (*CachedService) StageAll

func (c *CachedService) StageAll() error

StageAll stages all changes and invalidates the cache.

func (*CachedService) StashApply

func (c *CachedService) StashApply(index int) error

StashApply applies a stash entry and invalidates the cache.

func (*CachedService) StashDrop

func (c *CachedService) StashDrop(index int) error

StashDrop drops a stash entry and invalidates the cache.

func (*CachedService) StashList

func (c *CachedService) StashList() ([]StashEntry, error)

StashList delegates to the inner service (cached).

func (*CachedService) StashPop

func (c *CachedService) StashPop(index int) error

StashPop pops a stash entry and invalidates the cache.

func (*CachedService) StashSave

func (c *CachedService) StashSave(message string) error

StashSave saves to stash and invalidates the cache.

func (*CachedService) StashShow

func (c *CachedService) StashShow(index int) (string, error)

StashShow delegates to the inner service (not cached).

func (*CachedService) Status

func (c *CachedService) Status() (*StatusResult, error)

Status delegates to the inner service (cached).

func (*CachedService) SwitchBranch

func (c *CachedService) SwitchBranch(name string) error

SwitchBranch switches to a branch and invalidates the cache.

func (*CachedService) Unstage

func (c *CachedService) Unstage(paths ...string) error

Unstage unstages paths and invalidates the cache.

func (*CachedService) UnstageAll

func (c *CachedService) UnstageAll() error

UnstageAll unstages all paths and invalidates the cache.

func (*CachedService) Upstream

func (c *CachedService) Upstream() string

Upstream delegates to the inner service (cached).

func (*CachedService) WorktreeAdd

func (c *CachedService) WorktreeAdd(path, branch string) error

WorktreeAdd adds a worktree and invalidates the cache.

func (*CachedService) WorktreeList

func (c *CachedService) WorktreeList() ([]Worktree, error)

WorktreeList delegates to the inner service (cached).

func (*CachedService) WorktreeRemove

func (c *CachedService) WorktreeRemove(path string) error

WorktreeRemove removes a worktree and invalidates the cache.

type Commit

type Commit struct {
	Hash        string
	ShortHash   string
	Author      string
	AuthorEmail string
	Date        time.Time
	RelDate     string
	Subject     string
	Body        string
	Parents     []string
	Refs        []Ref
}

Commit represents a single Git commit.

func ParseLogOutput

func ParseLogOutput(out string) []Commit

ParseLogOutput parses the raw output of git log using our custom format. Optimised: uses IndexByte scanning instead of Split to avoid allocating a large []string for repos with thousands of commits.

type FileStatus

type FileStatus struct {
	Staging  StatusCode
	Worktree StatusCode
	Path     string
	OrigPath string // Only set for renames/copies.
	IsStaged bool
}

FileStatus represents the status of a single file in the working tree or index.

type GraphEntry

type GraphEntry struct {
	Graph  string  // e.g. "* ", "| * "
	Commit *Commit // nil for graph-only lines (merge lines, etc.)
}

GraphEntry pairs a commit with its ASCII graph decoration.

func ParseGraphOutput

func ParseGraphOutput(out string) []GraphEntry

ParseGraphOutput parses `git log --graph` with our custom format.

type Ref

type Ref struct {
	Name   string
	Type   RefType
	Remote string
}

Ref is a Git reference (branch, tag, HEAD, etc.).

func ParseRefs

func ParseRefs(raw string) []Ref

ParseRefs parses the %D decoration string into typed Ref values.

type RefType

type RefType int

RefType classifies a Git reference.

const (
	RefBranch RefType = iota
	RefRemoteBranch
	RefTag
	RefHead
	RefStash
)

Git reference types.

type Remote

type Remote struct {
	Name     string
	FetchURL string
	PushURL  string
}

Remote represents a configured Git remote.

func ParseRemoteOutput

func ParseRemoteOutput(out string) []Remote

ParseRemoteOutput parses `git remote -v`.

type Service

type Service interface {
	// ── Repository info ──────────────────────────────────────────────
	RepoRoot() string
	GitDir() string
	Head() (string, error)
	IsClean() (bool, error)
	IsMerging() bool
	IsRebasing() bool
	AheadBehind() (ahead, behind int, err error)
	Upstream() string

	// ── Status & staging ─────────────────────────────────────────────
	Status() (*StatusResult, error)
	Stage(paths ...string) error
	StageAll() error
	Unstage(paths ...string) error
	UnstageAll() error
	Discard(paths ...string) error

	// ── Commits ──────────────────────────────────────────────────────
	Commit(message string) error
	CommitAmend(message string) error
	Log(limit int, args ...string) ([]Commit, error)
	LogGraph(limit int) ([]GraphEntry, error)
	Show(hash string) (*Commit, string, error)

	// ── Diff ─────────────────────────────────────────────────────────
	Diff(staged bool, path string) (string, error)
	DiffRange(from, to string) (string, error)

	// ── Branches ─────────────────────────────────────────────────────
	Branches() ([]Branch, error)
	CreateBranch(name string) error
	SwitchBranch(name string) error
	DeleteBranch(name string, force bool) error
	MergeBranch(name string) error
	RenameBranch(oldName, newName string) error

	// ── Stash ────────────────────────────────────────────────────────
	StashList() ([]StashEntry, error)
	StashSave(message string) error
	StashPop(index int) error
	StashApply(index int) error
	StashDrop(index int) error
	StashShow(index int) (string, error)

	// ── Remotes ──────────────────────────────────────────────────────
	Remotes() ([]Remote, error)
	Fetch(remote string) error
	Pull(remote, branch string) error
	Push(remote, branch string, force bool) error

	// ── Worktrees ────────────────────────────────────────────────────
	WorktreeList() ([]Worktree, error)
	WorktreeAdd(path, branch string) error
	WorktreeRemove(path string) error

	// ── Rebase ───────────────────────────────────────────────────────
	RebaseInteractive(onto string) error
	RebaseContinue() error
	RebaseAbort() error

	// ── Bisect ───────────────────────────────────────────────────────
	BisectStart(bad, good string) error
	BisectGood() error
	BisectBad() error
	BisectReset() error
	BisectLog() (string, error)

	// ── Conflict resolution ──────────────────────────────────────────
	ConflictFiles() ([]string, error)
	MarkResolved(path string) error
}

Service defines the contract for all Git operations. Every TUI view depends on this interface, never on exec.Command directly. This makes the application testable via mock implementations.

type StashEntry

type StashEntry struct {
	Index   int
	Message string
	Branch  string
}

StashEntry represents a single stash entry.

func ParseStashList

func ParseStashList(out string) []StashEntry

ParseStashList parses `git stash list`.

type StatusCode

type StatusCode byte

StatusCode represents a single-character Git status indicator.

const (
	StatusUnmodified  StatusCode = ' '
	StatusModified    StatusCode = 'M'
	StatusTypeChanged StatusCode = 'T'
	StatusAdded       StatusCode = 'A'
	StatusDeleted     StatusCode = 'D'
	StatusRenamed     StatusCode = 'R'
	StatusCopied      StatusCode = 'C'
	StatusUnmerged    StatusCode = 'U'
	StatusUntracked   StatusCode = '?'
	StatusIgnored     StatusCode = '!'
)

Git status codes as single-byte indicators.

func (StatusCode) Label

func (s StatusCode) Label() string

Label returns a human-readable description of the status.

func (StatusCode) String

func (s StatusCode) String() string

String returns the single-character representation.

type StatusResult

type StatusResult struct {
	Staged    []FileStatus
	Unstaged  []FileStatus
	Untracked []FileStatus
	Conflicts []FileStatus
}

StatusResult holds the categorised status of the entire repository.

func ParseStatusOutput

func ParseStatusOutput(out string) *StatusResult

ParseStatusOutput parses `git status --porcelain=v1 -z`. NUL-delimited scanning avoids allocating a massive []string for repos with thousands of changed files.

func (*StatusResult) TotalCount

func (sr *StatusResult) TotalCount() int

TotalCount returns the total number of files across all categories.

type Worktree

type Worktree struct {
	Path   string
	Head   string
	Branch string
	Bare   bool
}

Worktree represents a linked working tree.

func ParseWorktreeList

func ParseWorktreeList(out string) []Worktree

ParseWorktreeList parses `git worktree list --porcelain`.

Jump to

Keyboard shortcuts

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