timemachine

package
v0.151.2 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 11 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 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiffPatch

func DiffPatch(ctx context.Context, r git.Runner, oid string) (string, error)

DiffPatch returns the full patch (`git show --patch --stat` output).

func DiffStat

func DiffStat(ctx context.Context, r git.Runner, oid string) (string, error)

DiffStat returns the `git show --stat` summary for the given OID. The output is trimmed — leading/trailing blank lines are stripped so callers can embed it directly in TUI panels without extra gaps.

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

func NewDiffCache(r git.Runner, capacity int) *DiffCache

NewDiffCache returns a DiffCache bound to the given runner. capacity <= 0 falls back to 64.

func (*DiffCache) Len

func (c *DiffCache) Len() int

Len returns the current number of cached entries. Intended for tests.

func (*DiffCache) Patch

func (c *DiffCache) Patch(ctx context.Context, oid string) (string, error)

Patch returns the cached --patch output or fetches + caches it.

func (*DiffCache) Stat

func (c *DiffCache) Stat(ctx context.Context, oid string) (string, error)

Stat returns the cached --stat output or fetches + caches it.

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

func FilterByBranch(events []Event, branch string) []Event

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

func FilterByKind(events []Event, allowed ...string) []Event

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

func FilterBySince(events []Event, cutoff time.Time) []Event

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

func FromBackupRef(b gitsafe.BackupRef) Event

FromBackupRef normalizes a gitsafe.BackupRef into an Event.

func FromReflogEntry

func FromReflogEntry(e reflog.Entry) Event

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

func Limit(events []Event, n int) []Event

Limit truncates events to at most n items (newest-first assumed). n <= 0 is a no-op (returns the slice unchanged).

func Merge

func Merge(groups ...[]Event) []Event

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

func ReadBackups(ctx context.Context, r git.Runner) ([]Event, error)

ReadBackups wraps gitsafe.ListBackups and returns the result as []Event so all sources share the same slice type before Merge.

func ReadBranches

func ReadBranches(ctx context.Context, r git.Runner, perBranchLimit int) ([]Event, error)

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

func ReadDangling(ctx context.Context, r git.Runner, opts DanglingOptions) ([]Event, error)

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

func ReadHEAD(ctx context.Context, r git.Runner, limit int) ([]Event, error)

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

func ReadStash(ctx context.Context, r git.Runner) ([]Event, error)

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
)

func (EventKind) String

func (k EventKind) String() string

String returns the stable lowercase name of the EventKind (used in NDJSON output and TUI filter chips).

Jump to

Keyboard shortcuts

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