backend

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package backend defines the VCS backend interface and shared types. New VCS backends implement the Backend interface and register themselves via Register; the tool then uses them for detection, status, and dispatch.

Index

Constants

View Source
const (
	PriorityJj  = 20
	PriorityGit = 10
)

Variables

View Source
var ErrNoArgs = errors.New("no arguments provided")

ErrNoArgs is returned by Backend.Run when called with no arguments.

Functions

func ComputeBookmarkState

func ComputeBookmarkState(bookmark *BookmarkStatus)

ComputeBookmarkState derives the RefState for a single BookmarkStatus from its Ahead/Behind counts and whether a remote is configured.

func DetectDir

func DetectDir(path, marker string) (bool, error)

DetectDir returns true if the directory at path contains a directory with the given marker name (e.g. ".git" or ".jj").

func ExtractExitCode

func ExtractExitCode(err error) (int, bool)

ExtractExitCode returns the exit code from an exec.ExitError. Returns ok=false if err is nil or not an ExitError.

func Names added in v0.10.0

func Names() []string

Names returns all registered backend names, in priority order.

func Register

func Register(backend Backend) error

Register adds a backend to the global registry. Detection priority follows Backend.Priority() order (higher first).

func ResetDetectCache added in v0.12.0

func ResetDetectCache()

ResetDetectCache clears the memoized Detect results. Used by tests and by long-lived callers (TUI refresh) that want fresh detection.

Types

type Backend

type Backend interface {
	// Name returns the canonical backend identifier, e.g. "git" or "jj".
	Name() string

	// Priority returns the detection priority. Higher values are checked first.
	// For colocated repos, the backend with the highest priority wins.
	Priority() int

	// Detect returns true if the directory at path is managed by this VCS.
	// It must not return an error for non-matching dirs; only real I/O
	// failures warrant an error.
	Detect(path string) (bool, error)

	// Status returns the unified status for the repo at path.
	Status(ctx context.Context, path string) (RepoStatus, error)

	// SubcommandArgs returns the full argument list for a VCS operation.
	// For example, git returns ["fetch"] but jj returns ["git", "fetch"]
	// because jj wraps git operations under the "git" subcommand.
	SubcommandArgs(op string) []string

	// Subcommands returns available subcommands for the VCS tool, e.g.
	// ["add", "branch", "log", "status"] for git. Returns an empty slice
	// when subcommands cannot be loaded (tool not found, parse error, etc).
	Subcommands(ctx context.Context) ([]string, error)

	// Run executes the given args using this VCS tool in path.
	// When interactive is true, the caller has already arranged for the
	// subprocess to inherit the terminal; Run must not capture output.
	Run(ctx context.Context, path string, args []string, interactive bool) (RunResult, error)
}

Backend is the interface every VCS backend must implement. Backends are stateless; all repo-specific state is passed per call.

func ByName

func ByName(name string) (Backend, error)

ByName returns the backend with the given name, or an error if not found.

func Detect

func Detect(path string) (Backend, error)

Detect returns the highest-priority backend that claims the directory. Priority follows DetectAll semantics — jj wins over git for colocated repos. Results (including failures) are memoized per absolute path; use ResetDetectCache to invalidate.

func DetectAll

func DetectAll(path string) ([]Backend, error)

DetectAll returns all backends that claim the directory, in priority order with jj first when both jj and git are present.

type BookmarkStatus

type BookmarkStatus struct {
	Name string

	// Remote name (e.g. "origin"). Empty when no remote.
	Remote string

	Ahead  int
	Behind int

	// True when jj reports a bookmark conflict (diverged push).
	// For git this is always false; git represents this as a diverged state.
	Conflict bool

	// Computed sync state derived from Ahead/Behind/Remote.
	State RefState
}

BookmarkStatus holds tracking information for a single bookmark (jj) or the current branch (git). Both backends populate this struct so the UI can render them identically.

type RefState

type RefState int

RefState describes the relationship between a local ref and its remote.

const (
	// RefStateUnknown means the bookmark has no tracking remote.
	RefStateUnknown RefState = iota
	// RefStateSynced means local matches remote.
	RefStateSynced
	// RefStateAhead means local is ahead of remote.
	RefStateAhead
	// RefStateBehind means local is behind remote.
	RefStateBehind
	// RefStateDiverged means local and remote have diverged.
	RefStateDiverged
	// RefStateNoRemote means no remote is configured.
	RefStateNoRemote
	// RefStateGone means remote ref was deleted.
	RefStateGone
)

func WorstState

func WorstState(bookmarks []BookmarkStatus, hasConflict bool) RefState

WorstState returns the most severe RefState across all bookmarks. Severity order: Conflict > Diverged > Behind > Ahead > NoRemote > Synced.

func (RefState) Severity

func (s RefState) Severity() int

Severity order: Conflict > Diverged > Behind > Ahead > NoRemote > Synced.

func (RefState) String

func (s RefState) String() string

type RepoStatus

type RepoStatus struct {
	// Ref is the human-readable current position: branch name for git,
	// change ID short for jj.
	Ref string

	// Bookmarks holds tracking state for all local bookmarks/branches.
	// Git populates exactly one entry (the current branch).
	// jj populates one entry per local bookmark at or near @.
	Bookmarks []BookmarkStatus

	// OverallState is the worst-case RefState across all Bookmarks,
	// used for sorting and the summary colour in the ll table.
	OverallState RefState

	Dirty bool

	// True when the repo has unresolved VCS conflicts
	// (jj conflict markers, git merge conflicts).
	Conflict bool

	CommitMsg string

	// Relative commit time (e.g. "3 days ago").
	CommitTime string

	// Working copy (@) commits ahead of HEAD bookmark. Only populated by
	// the jj backend (git's working copy is always at the branch tip).
	LocalAhead int
}

RepoStatus is the unified status schema populated by each backend.

type RunResult

type RunResult struct {
	// Output is the combined stdout+stderr of the subprocess, captured
	// when running in parallel. In interactive mode this is empty.
	Output   string
	ExitCode int
}

RunResult holds the outcome of a single repo dispatch.

func RunCommand

func RunCommand(
	ctx context.Context,
	binary string,
	path string,
	args []string,
	interactive bool,
) (RunResult, error)

RunCommand executes a binary with args in the given directory. When interactive, it passes through stdin/stdout/stderr to the terminal. Otherwise it captures combined stdout+stderr into the returned RunResult. ExitError is unwrapped: the exit code is set on the result and the error is cleared (non-zero exit is not considered an infrastructure failure).

Jump to

Keyboard shortcuts

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