Documentation
¶
Overview ¶
Package timemachine assembles the unified event stream that `gk timemachine` surfaces — HEAD reflog + per-branch reflogs + refs/gk/*-backup/ + (opt-in) stash and dangling commits.
Callers stay in the internal/reflog and internal/gitsafe layers for raw access; this package adds the normalization + merge + dedup on top. The shape is designed so the eventual bubbletea TUI and the `--no-tui --json` NDJSON renderer consume identical structs.
Index ¶
- func DiffPatch(ctx context.Context, r git.Runner, oid string) (string, error)
- func DiffStat(ctx context.Context, r git.Runner, oid string) (string, error)
- type DanglingOptions
- type DiffCache
- type Event
- func FilterByBranch(events []Event, branch string) []Event
- func FilterByKind(events []Event, allowed ...string) []Event
- func FilterBySince(events []Event, cutoff time.Time) []Event
- func FromBackupRef(b gitsafe.BackupRef) Event
- func FromReflogEntry(e reflog.Entry) Event
- func Limit(events []Event, n int) []Event
- func Merge(groups ...[]Event) []Event
- func ReadBackups(ctx context.Context, r git.Runner) ([]Event, error)
- func ReadBranches(ctx context.Context, r git.Runner, perBranchLimit int) ([]Event, error)
- func ReadDangling(ctx context.Context, r git.Runner, opts DanglingOptions) ([]Event, error)
- func ReadHEAD(ctx context.Context, r git.Runner, limit int) ([]Event, error)
- func ReadStash(ctx context.Context, r git.Runner) ([]Event, error)
- type EventKind
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DanglingOptions ¶
type DanglingOptions struct {
// Cap limits how many dangling commits are returned. 0 means unlimited.
// The SYN_v2.1 default is 500 — wired at the CLI layer, not here.
Cap int
}
DanglingOptions customize ReadDangling. The zero value is "no cap, no timeout, default branch-less HEAD flag". Cap <= 0 disables the cap; any timeout is carried in the ctx the caller passes in.
type DiffCache ¶
type DiffCache struct {
// contains filtered or unexported fields
}
DiffCache wraps DiffStat / DiffPatch with an LRU cache of recent results. Used by the TUI diff pane to keep keystroke latency low while the user arrows through the list.
Keyed by (mode, oid) so stat and patch caches never collide. The default capacity is 64; Set a larger value at construction if the TUI list is expected to exceed that viewport in active use.
func NewDiffCache ¶
NewDiffCache returns a DiffCache bound to the given runner. capacity <= 0 falls back to 64.
type Event ¶
type Event struct {
Kind EventKind
Ref string // "HEAD@{3}" | "refs/gk/undo-backup/main/1700000000"
OID string // full commit SHA
OldOID string // reflog previous tip; empty for backups
When time.Time // committer date (reflog) or parsed unix (backup)
Subject string // one-line human summary
// Reflog-only fields.
Action string // classified action ("commit", "reset", "rebase", ...)
// Backup-only fields.
BackupKind string // "undo" | "wipe" | "timemachine"
Branch string // branch segment from the backup ref path
}
Event is the unified timeline item shared by every consumer.
Fields are always populated in the same way:
- Kind, Ref, OID, When, Subject: always set.
- OldOID, Action: reflog-only (empty for backups).
- BackupKind, Branch: backup-only (empty for reflog).
Consumers that need raw source details can re-query the underlying reflog.Entry / gitsafe.BackupRef — this struct intentionally carries only the fields the TUI list pane and NDJSON renderer need.
func FilterByBranch ¶
FilterByBranch returns events that belong to the given branch name.
A reflog event belongs to a branch when its Ref is "refs/heads/<branch>@{N}" (per-branch reflog scan) or the short form "<branch>@{N}". HEAD reflog events match only when branch == "HEAD". Backup events match on the Branch field (the sanitized segment from the ref path). Stash events never match a specific branch filter.
Empty branch is a no-op (returns input unchanged).
func FilterByKind ¶
FilterByKind returns only events whose Kind.String() matches one of the given allowed strings. Empty `allowed` is a no-op (returns input). Unknown kind names silently filter everything out.
The result is a freshly allocated slice — the input is never mutated, so callers can invoke FilterByKind multiple times on the same events slice without inter-call interference.
func FilterBySince ¶
FilterBySince returns events whose When is at or after the cutoff. Events with zero When (missing timestamp) are dropped — a since-filter implies "recent only", and untimestamped entries cannot be proven recent.
Zero cutoff is a no-op (returns input unchanged).
func FromBackupRef ¶
FromBackupRef normalizes a gitsafe.BackupRef into an Event.
func FromReflogEntry ¶
FromReflogEntry normalizes a single reflog.Entry into an Event. The Ref field is set to the raw reflog ref string (e.g. "HEAD@{3}") and Subject falls back to the full Message when Summary is empty.
func Limit ¶
Limit truncates events to at most n items (newest-first assumed). n <= 0 is a no-op (returns the slice unchanged).
func Merge ¶
Merge concatenates several event slices and returns a newest-first sorted single slice. Events with zero When sort to the end so partial data never steals the top rows.
Merge does NOT dedupe by OID — a commit referenced from both a branch reflog and a backup ref shows up twice (with distinct Ref fields). The TUI list pane is expected to annotate the duplicates rather than collapse them; dedupe semantics are a later TM-05 bitset addition.
func ReadBackups ¶
ReadBackups wraps gitsafe.ListBackups and returns the result as []Event so all sources share the same slice type before Merge.
func ReadBranches ¶
ReadBranches collects reflog events from every local branch.
For each entry in `git for-each-ref refs/heads/`, reflog.Read is called to pull up to `perBranchLimit` entries (<= 0 for unlimited). All events are flattened into a single newest-first slice at the caller's next Merge step. Errors on individual branches propagate — typical failures are permission or a missing reflog for a freshly-pruned branch, both of which the caller probably wants to see.
func ReadDangling ¶
ReadDangling surfaces every commit unreachable from any ref (`git fsck --lost-found --no-reflogs`) as a KindDangling event, with the commit's committer-date and subject resolved via `git show`.
Performance warning: fsck is O(objects) and can take multiple seconds on large repos. Callers (TUI, CLI) should gate this source behind explicit opt-in (`--include-dangling`) and should pass a timeout-bearing context.
func ReadHEAD ¶
ReadHEAD returns the HEAD reflog as a slice of Events, newest first.
limit <= 0 is unlimited. On an empty reflog the returned slice is empty (not an error) — fresh repos may have zero entries before the first commit.
func ReadStash ¶
ReadStash returns the current stash stack as []Event.
A repo with zero stashes does not have refs/stash; git reflog errors with "unknown revision or ref". ReadStash treats that case as empty (not an error) — fresh repos routinely have no stashes, and callers should continue with other sources.
type EventKind ¶
type EventKind int
EventKind identifies the source pool an Event came from. The values are stable and exposed in the `--json` NDJSON output — changing the numeric ordering is fine, but the String() form is a user-visible contract.
const ( // KindReflog — an entry in HEAD or a branch reflog. KindReflog EventKind = iota + 1 // KindBackup — a gk-managed backup ref under refs/gk/*-backup/. KindBackup // KindStash — an entry in the stash reflog (refs/stash). KindStash // KindDangling — a dangling commit surfaced by `git fsck --lost-found`. // These are unreachable from any ref and will be pruned by `git gc` // unless recovered; timemachine surfaces them so users can rescue. KindDangling )