Documentation
¶
Overview ¶
Package branchparent persists and resolves the parent branch of a local git branch — the branch from which it forked and against which divergence should be reported. It is the single source of truth for the gk-parent metadata; status, switch, and worktree consume it via Resolver.
Storage is per-branch git config: `branch.<name>.gk-parent`. The choice of git config (over an external file) means parent metadata travels with the repo's local clone, no synchronization with the working tree is needed, and other gk versions that don't recognize the key simply ignore it.
Index ¶
- func ValidateSet(ctx context.Context, c *git.Client, branch, parent string) error
- type Config
- func (cfg *Config) AllParents(ctx context.Context) (map[string]string, error)
- func (cfg *Config) GetParent(ctx context.Context, branch string) (string, error)
- func (cfg *Config) SetParent(ctx context.Context, branch, parent string) error
- func (cfg *Config) UnsetParent(ctx context.Context, branch string) error
- type Issue
- type Resolver
- func (r *Resolver) ExplicitOnly() *Resolver
- func (r *Resolver) ResolveBase(ctx context.Context, branch, cfgBase string) string
- func (r *Resolver) ResolveBaseExplained(ctx context.Context, branch, cfgBase string) (base string, source Source, parentResolved bool)
- func (r *Resolver) ResolveBaseWithIssues(ctx context.Context, branch, cfgBase string) (base string, source Source, issues []Issue)
- func (r *Resolver) ResolveParent(ctx context.Context, branch string) (string, Source, bool)
- type Source
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidateSet ¶
ValidateSet checks whether `branch` may have its parent set to `parent`. Returns nil when the assignment is safe; a descriptive error otherwise.
Validations (in order — earliest cheapest):
- parent is non-empty
- parent != branch (no self-parent)
- parent contains no '/' that resembles a remote prefix (origin/main)
- parent's ref name is well-formed (`git check-ref-format --branch`)
- refs/heads/<parent> exists locally (not a tag, not a remote-tracking)
- assigning parent does not create a cycle (walk existing chain)
The CLI write path runs this BEFORE Config.SetParent. Validation lives here, not in Config, so unit-tested business rules stay decoupled from the dumb git-config wrapper.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config is the write-path API for the gk-parent metadata. Reads also live here so callers that only need the explicit value (no inference, no fallback) avoid pulling in Resolver. SetParent does NOT validate; that's validateParent's job — Config is the dumb wrapper around git config.
func NewConfig ¶
NewConfig wraps a *git.Client. The client supplies the runner used for every git config invocation; nothing is cached.
func (*Config) AllParents ¶
AllParents reads every `branch.<name>.gk-parent` in a single git call and returns a branch→parent map. List consumers (switch picker, worktree table) use this instead of GetParent-per-branch so the subprocess count stays flat on branch-heavy repos. Values are returned verbatim — the parent ref may no longer exist; callers that care must check (Resolver re-checks on read for exactly this reason).
func (*Config) GetParent ¶
GetParent reads `branch.<branch>.gk-parent`. Returns ("", nil) when unset, matching git's documented exit-1-for-missing-key contract. A non-nil error only surfaces real failures (broken config, missing repo) — callers can safely treat ("", nil) as "no parent configured".
func (*Config) SetParent ¶
SetParent writes the parent. The value is stored verbatim; no normalization (e.g., stripping `refs/heads/`). Validate before calling — see ValidateSet in this package, which the CLI write path runs first.
Empty parent is rejected: storing "" would create a key whose value reads back as "" on GetParent, indistinguishable from "unset". Callers wanting to remove the metadata must use UnsetParent.
type Issue ¶
type Issue struct {
// Code is a stable machine-readable identifier ("parent-missing",
// "parent-is-tag", etc.). Reserved for future expansion; Phase 1
// only emits "parent-missing".
Code string
// Parent is the offending value (the explicit gk-parent that no
// longer resolves to a local ref).
Parent string
// Message is a human-readable, single-line description suitable
// for stderr — already including the parent name.
Message string
}
Issue describes a recoverable problem encountered during resolution that the caller should surface (typically a one-line stderr warning). The resolver itself never prints — it has no writer — but it tells the caller what to say.
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver is the read-path API: it knows about explicit config, inference, and ref existence. It is the single entry point used by status/switch/ worktree so base-resolution logic does not fork across commands.
Resolver does NOT validate writes — that's validateParent in this package. It only reads and decides.
func NewResolver ¶
NewResolver wraps a *git.Client. Inference is enabled; callers that feed resolved parents into merges should chain ExplicitOnly().
func (*Resolver) ExplicitOnly ¶
ExplicitOnly returns the resolver restricted to explicit `gk-parent` config, with history inference switched off.
func (*Resolver) ResolveBase ¶
ResolveBase returns the effective comparison base for branch: the parent when one resolves, otherwise cfgBase. Designed as a drop-in replacement for callers that previously passed cfgBase directly to rev-list.
cfgBase is whatever the caller would have used absent parent metadata — typically status's resolveBaseForStatus output. ResolveBase never returns "" unless the caller passes "" — it only swaps cfgBase out when a parent is resolvable.
func (*Resolver) ResolveBaseExplained ¶
func (r *Resolver) ResolveBaseExplained(ctx context.Context, branch, cfgBase string) (base string, source Source, parentResolved bool)
ResolveBaseExplained is like ResolveBase but also returns the source so status can print verbose diagnostics ("from feat/A (parent of feat/sub)") without re-querying. base = parent when ok, else cfgBase + Source="".
func (*Resolver) ResolveBaseWithIssues ¶
func (r *Resolver) ResolveBaseWithIssues(ctx context.Context, branch, cfgBase string) (base string, source Source, issues []Issue)
ResolveBaseWithIssues is the noisy variant of ResolveBase: it returns the effective base AND any problems that justified a fallback. Status uses this to print a one-line warning when the user has set a gk-parent that no longer exists, instead of silently using cfgBase and leaving them in the dark.
When no explicit parent is configured, issues is empty even if inference would have failed — absent inference is the normal path, not a problem.
func (*Resolver) ResolveParent ¶
ResolveParent attempts to find the parent of branch. Returns (parent, source, ok). ok=false means no parent is configured AND no inference succeeded — callers should fall back to their default base.
Resolution order:
- `branch.<branch>.gk-parent` (explicit)
- inferParent (reflog branchpoint containment; skipped on ExplicitOnly)
When the explicit value names a ref that does not exist locally, ResolveParent returns ("", "", false) so callers fall back. The caller is responsible for surfacing a one-line warning; we don't print here because Resolver has no writer.
type Source ¶
type Source string
Source describes where a resolved parent value came from. Callers display it for transparency (e.g., `gk status -v` showing "inferred from reflog") and use it to decide whether to warn — explicit values get less verbose fallback messaging than inferred ones.
const ( // SourceExplicit means the parent came from `branch.<name>.gk-parent`. SourceExplicit Source = "explicit" // SourceInferred means the parent was deduced from git history: the // branch's creation point (oldest reflog entry) is contained by exactly // one other local branch. Only unambiguous single-candidate results are // ever inferred; anything else falls back. SourceInferred Source = "inferred" )