Documentation
¶
Overview ¶
Package git implements a thin adapter that shells out to the git binary to retrieve repository status information.
It is intentionally minimal: no in-process libgit2, no CGo. We just parse the stable porcelain output of `git status --porcelain` and use `git rev-parse --abbrev-ref HEAD` for the branch name.
Errors from git (e.g. not a git repo) are returned to the caller so they can degrade gracefully rather than crashing the UI.
Caching ------- Source coalesces repeated Status and Diff calls within a short TTL so the 1Hz TUI snapshot loop doesn't fork+exec `git status` and two `git diff` processes every second on a quiet repo. See gitTTL and the cache fields below.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ActiveFile ¶
ActiveFile scans events from the livesession view (passed as summary strings) and returns the file_path argument from the most recent Edit/Write/MultiEdit tool call. Returns "" when no such event is found.
summaries is a slice of ap.Summary values from KindAssistant events, in chronological order (oldest first). The last matching entry wins.
func DiffSummary ¶
DiffSummary builds the "filename · +N −M" label from a slice of Hunks. Returns "" when hunks is nil/empty.
Types ¶
type DiffAdapter ¶
type DiffAdapter struct {
// contains filtered or unexported fields
}
DiffAdapter wraps Source and converts git.Hunk values to livesession.DiffHunk so the proto TUI layer's DiffSource interface can be satisfied without the proto package importing the adapters/git package directly.
func NewDiffAdapter ¶
func NewDiffAdapter() *DiffAdapter
NewDiffAdapter returns a DiffAdapter ready to use. Creates its own Source — for production code that needs to share a cache between LiveSessionAdapter and DiffAdapter, use NewDiffAdapterFor.
func NewDiffAdapterFor ¶
func NewDiffAdapterFor(src *Source) *DiffAdapter
NewDiffAdapterFor wraps an existing Source so the LiveSession and Diff adapters can share the same status/diff cache.
func (*DiffAdapter) Diff ¶
func (a *DiffAdapter) Diff(cwd, file string) ([]livesession.DiffHunk, error)
Diff implements proto.DiffSource. It calls git.Source.Diff and translates the result to livesession.DiffHunk values so the adapters/git package is not imported by the proto package.
type FileStatus ¶
type FileStatus struct {
// Path is the file path relative to the repo root.
Path string
// Status is the single-character code: 'M', 'A', 'D', 'R', '?', etc.
// For untracked files both XY columns are '?'; we normalise that to '?'.
Status rune
// Staged is true when the status character comes from the index (X) column.
Staged bool
}
FileStatus represents one entry from `git status --porcelain`.
type Hunk ¶
type Hunk struct {
// Header is the raw @@ line, e.g. "@@ -42,7 +42,18 @@ authenticate(token)".
Header string
// OldStart is the starting line number in the old file.
OldStart int
// OldCount is the number of lines from the old file shown in the hunk.
OldCount int
// NewStart is the starting line number in the new file.
NewStart int
// NewCount is the number of lines from the new file shown in the hunk.
NewCount int
// Lines are the individual diff lines (context, add, remove).
Lines []HunkLine
}
Hunk is one contiguous changed region from a unified diff.
type HunkLine ¶
type HunkLine struct {
// Type is ' ' for context, '+' for added, '-' for removed.
Type rune
// Text is the line content without the leading +/- marker.
Text string
}
HunkLine is a single line within a Hunk.
type LiveSessionAdapter ¶
type LiveSessionAdapter struct {
// contains filtered or unexported fields
}
LiveSessionAdapter wraps Source and implements livesession.GitSource.
func NewLiveSessionAdapter ¶
func NewLiveSessionAdapter() *LiveSessionAdapter
NewLiveSessionAdapter returns a LiveSessionAdapter that satisfies livesession.GitSource. Creates its own Source — for production code that needs to share a cache between LiveSessionAdapter and DiffAdapter, use NewLiveSessionAdapterFor.
func NewLiveSessionAdapterFor ¶
func NewLiveSessionAdapterFor(src *Source) *LiveSessionAdapter
NewLiveSessionAdapterFor wraps an existing Source so the LiveSession and Diff adapters can share the same status/diff cache.
func (*LiveSessionAdapter) StatusView ¶
func (a *LiveSessionAdapter) StatusView(cwd string) ([]livesession.GitFileStatus, error)
StatusView implements livesession.GitSource. It translates git.FileStatus values to livesession.GitFileStatus so the application layer has no dependency on the adapter package.
type Source ¶
type Source struct {
// contains filtered or unexported fields
}
Source shells out to git for status and diff information.
Method receivers are pointer-typed so the cache state can be shared across the wrapping adapters (LiveSessionAdapter, DiffAdapter) — both need to see the same cached results in production. Construct one Source per process and share it.
The zero value is usable directly (lazy default ttl/now/runGit) — useful for tests that don't care about caching.
func (*Source) Branch ¶
Branch runs `git rev-parse --abbrev-ref HEAD` in cwd. dirty is true when the working tree has uncommitted changes. Returns ("", false, nil) when cwd is not a git repo.
func (*Source) Diff ¶
Diff shells out to `git diff -U3 [<file>]` (and `git diff --cached -U3`) in the given working directory and returns parsed Hunks.
If file is non-empty, the diff is scoped to that single file. If empty, all changed files are included. At most maxDiffLines individual HunkLines are returned across all hunks to cap render time on very large diffs.
Successive calls with the same (cwd, file) within cacheTTL return the cached result, so the snapshot loop's 1Hz polling does not fork two `git diff` subprocesses every second on a quiet repo.
Returns (nil, nil) when cwd is not a git repository or git is unavailable.
func (*Source) Status ¶
func (s *Source) Status(cwd string) ([]FileStatus, error)
Status runs `git status --porcelain` in cwd and returns the list of changed files. Returns an empty slice (no error) when the directory is not a git repo or when git is not installed.
Successive calls within cacheTTL return the cached result. A newly-staged or newly-modified file may not appear immediately — the lag is bounded by gitTTL (3s by default).