branchparent

package
v0.167.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 4 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 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateSet

func ValidateSet(ctx context.Context, c *git.Client, branch, parent string) error

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):

  1. parent is non-empty
  2. parent != branch (no self-parent)
  3. parent contains no '/' that resembles a remote prefix (origin/main)
  4. parent's ref name is well-formed (`git check-ref-format --branch`)
  5. refs/heads/<parent> exists locally (not a tag, not a remote-tracking)
  6. 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

func NewConfig(c *git.Client) *Config

NewConfig wraps a *git.Client. The client supplies the runner used for every git config invocation; nothing is cached.

func (*Config) AllParents

func (cfg *Config) AllParents(ctx context.Context) (map[string]string, error)

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

func (cfg *Config) GetParent(ctx context.Context, branch string) (string, error)

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

func (cfg *Config) SetParent(ctx context.Context, branch, parent string) error

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.

func (*Config) UnsetParent

func (cfg *Config) UnsetParent(ctx context.Context, branch string) error

UnsetParent removes the key. Idempotent: returns nil when the key was already absent, so callers don't need to check first.

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

func NewResolver(c *git.Client) *Resolver

NewResolver wraps a *git.Client. Inference is enabled; callers that feed resolved parents into merges should chain ExplicitOnly().

func (*Resolver) ExplicitOnly

func (r *Resolver) ExplicitOnly() *Resolver

ExplicitOnly returns the resolver restricted to explicit `gk-parent` config, with history inference switched off.

func (*Resolver) ResolveBase

func (r *Resolver) ResolveBase(ctx context.Context, branch, cfgBase string) string

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

func (r *Resolver) ResolveParent(ctx context.Context, branch string) (string, Source, bool)

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:

  1. `branch.<branch>.gk-parent` (explicit)
  2. 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"
)

func (Source) String

func (s Source) String() string

String makes Source play nicely with fmt %s.

Jump to

Keyboard shortcuts

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