store

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const OopsDir = ".oops"

OopsDir is the name of a local store directory.

Variables

This section is empty.

Functions

func FileKey added in v0.4.0

func FileKey(abs string) string

FileKey names a file's directory inside a store.

func GlobalRoot added in v0.4.0

func GlobalRoot() (string, error)

GlobalRoot is the store in the user's home directory.

func HashBytes added in v0.4.0

func HashBytes(b []byte) string

HashBytes is the content address of data.

func NormalizePath added in v0.4.0

func NormalizePath(abs string) string

NormalizePath makes one spelling per file: forward slashes, and on Windows (case-insensitive file system) the whole path in lower case.

Types

type Code added in v0.4.0

type Code string

Code classifies an error so a caller can react without parsing text.

const (
	CodeNotTracked      Code = "not_tracked"
	CodeVersionNotFound Code = "version_not_found"
	CodeFileNotFound    Code = "file_not_found"
	CodeLockTimeout     Code = "lock_timeout"
	CodeAlreadyTracked  Code = "already_tracked"
)

func CodeOf added in v0.4.0

func CodeOf(err error) Code

CodeOf returns the code of err, or "" when err carries none.

type Error added in v0.4.0

type Error struct {
	Code Code
	Msg  string
}

Error is an error with a stable code.

func (*Error) Error added in v0.4.0

func (e *Error) Error() string

type Index added in v0.4.0

type Index struct {
	Format   int       `json:"format"`
	Path     string    `json:"path"`
	Next     int       `json:"next"`
	Versions []Version `json:"versions"`
}

Index is a file's version list.

func (*Index) Find added in v0.4.0

func (ix *Index) Find(n int) *Version

Find returns version n, nil when it does not exist.

func (*Index) HasHash added in v0.4.0

func (ix *Index) HasHash(h string) bool

HasHash reports whether any version has this content.

func (*Index) Latest added in v0.4.0

func (ix *Index) Latest() *Version

Latest is the newest version, nil when there is none.

type Kind added in v0.4.0

type Kind string

Kind tells how a version was made; retention treats them differently.

const (
	KindManual Kind = "manual"
	KindAuto   Kind = "auto"
)

type MoveResult added in v0.4.0

type MoveResult struct {
	From      string `json:"from"`
	To        string `json:"to"`
	MovedFile bool   `json:"movedFile"`
}

MoveResult reports a move. MovedFile is false when only the history moved.

type PrunePolicy added in v0.4.0

type PrunePolicy struct {
	MaxAge   time.Duration
	MaxBytes int64
	DryRun   bool
}

PrunePolicy limits what a store keeps. A zero field is no limit.

type PruneReport added in v0.4.0

type PruneReport struct {
	DryRun       bool             `json:"dryRun"`
	Removed      []RemovedVersion `json:"removed"`
	FreedBytes   int64            `json:"freedBytes"`
	TotalBytes   int64            `json:"totalBytes"`
	OverCapBytes int64            `json:"overCapBytes"`
	Unreadable   []Unreadable     `json:"unreadable"`
}

PruneReport is the outcome of a prune.

type RemovedVersion added in v0.4.0

type RemovedVersion struct {
	Path   string `json:"path"`
	N      int    `json:"n"`
	Reason string `json:"reason"`
}

RemovedVersion names a version prune removed (or would remove).

type RestoreResult added in v0.4.0

type RestoreResult struct {
	Path        string   `json:"path"`
	Restored    int      `json:"restored"`
	SavedBefore *Version `json:"savedBefore"`
}

RestoreResult reports a restore. SavedBefore is the version that kept the file's unsaved content, nil when there was nothing to keep.

type SaveOptions added in v0.4.0

type SaveOptions struct {
	Kind  Kind
	Actor string
	Label string
	Meta  map[string]string
}

SaveOptions describes the version a save creates.

type SaveResult added in v0.4.0

type SaveResult struct {
	Saved   bool    `json:"saved"`
	Reason  string  `json:"reason,omitempty"`
	Path    string  `json:"path"`
	Version Version `json:"version"`
}

SaveResult reports a save. Saved is false when the content equals the latest version.

type Status added in v0.4.0

type Status struct {
	Path    string `json:"path"`
	Exists  bool   `json:"exists"`
	Latest  int    `json:"latest"`
	Current int    `json:"current"`
	Changed bool   `json:"changed"`
}

Status compares a file on disk with its history.

type Store

type Store struct {
	Root        string
	LockTimeout time.Duration
	// contains filtered or unexported fields
}

Store is a directory holding the version history of any number of files.

func Open added in v0.4.0

func Open(root string) *Store

Open returns the store rooted at root. Nothing is created until a version is saved.

func OpenLocal added in v0.4.0

func OpenLocal(base string) *Store

OpenLocal returns the store kept in base/.oops. Files under base are recorded by their path relative to base, so renaming or moving the folder keeps its history.

func (*Store) Content added in v0.4.0

func (s *Store) Content(file string, n int) ([]byte, error)

Content returns the bytes of version n.

func (*Store) Files added in v0.4.0

func (s *Store) Files() ([]*Index, []Unreadable, error)

Files lists every history in the store, and separately the ones whose index cannot be read.

func (*Store) History

func (s *Store) History(file string) (*Index, error)

History returns the file's versions, oldest first.

func (*Store) Move added in v0.4.0

func (s *Store) Move(from, to string) (MoveResult, error)

Move carries a file's history to a new path, moving the file too when it is still at the old path. Both histories are locked for the whole move and the history is carried file by file (content is linked, or copied where a link is not possible), never by renaming its directory — so no lock is released early, and at every point one complete index describes the file: the new one is written before the old one is cleared, and a failure clears the new one and leaves the old history as it was.

func (*Store) Orphans added in v0.4.0

func (s *Store) Orphans() ([]*Index, error)

Orphans lists histories whose file no longer exists on disk.

func (*Store) Prune added in v0.4.0

func (s *Store) Prune(p PrunePolicy) (PruneReport, error)

Prune applies the retention policy to the whole store. Only auto versions are removed.

func (*Store) Remove added in v0.4.0

func (s *Store) Remove(file string) error

Remove deletes every version of the file.

func (*Store) RemoveHistory added in v0.4.0

func (s *Store) RemoveHistory(ix *Index) error

RemoveHistory deletes the history an index describes.

func (*Store) Restore added in v0.4.0

func (s *Store) Restore(file string, n int, o SaveOptions) (RestoreResult, error)

Restore writes version n back to the file. Content on disk that matches no version is first kept as a saved (manual) version, so a restore never loses work and prune never removes that copy.

func (*Store) Save

func (s *Store) Save(file string, o SaveOptions) (SaveResult, error)

Save records the file's current content as a new version. Saving content equal to the latest version is a successful no-op (Saved=false, Reason "unchanged").

func (*Store) Status added in v0.4.0

func (s *Store) Status(file string) (Status, error)

Status compares the file on disk with its history. Current is the newest version whose content equals the file, 0 when none does.

type Unreadable added in v0.4.0

type Unreadable struct {
	Dir   string `json:"dir"`
	Error string `json:"error"`
}

Unreadable names a history whose index could not be read.

type Version added in v0.4.0

type Version struct {
	N     int               `json:"n"`
	Hash  string            `json:"hash"`
	Size  int64             `json:"size"`
	Time  time.Time         `json:"time"`
	Kind  Kind              `json:"kind"`
	Actor string            `json:"actor,omitempty"`
	Label string            `json:"label,omitempty"`
	Meta  map[string]string `json:"meta,omitempty"`
}

Version is one saved state of a file.

Jump to

Keyboard shortcuts

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