gitx

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package gitx is the binary's one door to git. The reads mirror the shell-outs bin/f10-load-project.sh makes - the bash script is the semantics oracle, and shelling out identically keeps the two comparable. The writes are `f10 start`'s - a branch (new, or tracking origin's), a worktree, and the config entries that record what a branch depends on - and `f10 finish`'s, which undoes them once the branch has merged: the pull, the worktree removal, the branch deletion. `f10 drive` adds one: the push of a branch it caught up before merging.

Everything goes through Exec, a variable so a test can answer git without a checkout. No go-git.

Index

Constants

This section is empty.

Variables

View Source
var Exec = func(ctx context.Context, dir string, args ...string) (string, error) {
	cmd := exec.CommandContext(ctx, "git", args...)
	cmd.Dir = dir

	var stderr strings.Builder

	cmd.Stderr = &stderr

	out, err := cmd.Output()
	if err != nil {
		if exit, ok := errors.AsType[*exec.ExitError](err); ok && ctx.Err() == nil {
			return "", &ExitError{Args: args, Code: exit.ExitCode(), Stderr: strings.TrimSpace(stderr.String())}
		}

		if msg := strings.TrimSpace(stderr.String()); msg != "" {
			return "", fmt.Errorf("git %s: %s", args[0], msg)
		}

		return "", fmt.Errorf("git %s: %w", args[0], err)
	}

	return strings.TrimSpace(string(out)), nil
}

Exec runs git with args in dir and returns trimmed stdout. A non-zero exit is an *ExitError carrying git's own stderr, which names the cause better than any wrapper could, and the code, which Read needs.

Functions

func AddWorktree

func AddWorktree(ctx context.Context, dir, path, branch, base string, create bool) error

AddWorktree checks branch out at path. With create, the branch is made from base - git's HEAD when base is "" - and must not exist yet; otherwise it must.

func CommitTree added in v0.42.0

func CommitTree(ctx context.Context, dir, tree string, parents []string, message ...string) (string, error)

CommitTree writes a commit of tree on parents with a message of one paragraph per part, and returns its sha. No ref moves.

func Config added in v0.46.0

func Config(ctx context.Context, dir, key string) (string, error)

Config is one local config value; absent is "" with no error.

func CurrentBranch

func CurrentBranch(ctx context.Context, dir string) string

CurrentBranch is the branch dir has checked out, or "" when detached.

func DeleteBranch

func DeleteBranch(ctx context.Context, dir, branch string) error

DeleteBranch deletes the local branch whether or not git sees it as merged: a squash or rebase merge leaves no trace git recognises, so the caller's proof is the host's, not git's. The branch's config section goes with it.

func DeleteRemoteRef added in v0.33.0

func DeleteRemoteRef(ctx context.Context, dir, branch string) error

DeleteRemoteRef drops the local copy of origin's branch, which a branch deleted through the host's API leaves behind until a pruning fetch. git deletes a ref that is not there without complaint.

func Fetch added in v0.35.0

func Fetch(ctx context.Context, dir string) error

Fetch updates every remote-tracking branch from origin.

func FetchInto

func FetchInto(ctx context.Context, dir, branch string) error

FetchInto updates local branch from origin's branch of the same name without touching the checkout: git fast-forwards a branch that is not checked out and refuses anything else.

func IsAncestor

func IsAncestor(ctx context.Context, dir, commit, ref string) (bool, error)

IsAncestor reports whether commit is reachable from ref: git answers with exit 0 or 1. An unknown object is a no too, since a recorded sha may name a commit squashed away; anything else is an error.

func MergeTree added in v0.42.0

func MergeTree(ctx context.Context, dir, base, head string) (string, error)

MergeTree is the tree of head merged into base, built in memory as MergesCleanly tries it; a conflict is an error.

func MergesCleanly added in v0.33.0

func MergesCleanly(ctx context.Context, dir, base, head string) (bool, error)

MergesCleanly reports whether head merges into base without a conflict, tried in memory the way a host tries a pull request: nothing is checked out or written. git exits 1 both for a conflict, reported on stdout, and for a ref it cannot merge, reported on stderr, which is an error. Not --quiet: git 2.56 exits 0 with it on a conflict.

func Out

func Out(ctx context.Context, dir string, args ...string) string

Out runs git and returns trimmed stdout, or "" on any error. It cannot tell "not there" from "git failed", so it is for display only: a read that decides what f10 does next goes through Read.

func PullFF

func PullFF(ctx context.Context, dir string) error

PullFF fast-forwards the checked-out branch from its upstream, and fails rather than merge when it cannot.

func Push added in v0.33.0

func Push(ctx context.Context, dir, branch, lease string) error

Push sends HEAD to origin's branch. A lease (the sha origin's branch had when the caller read it) forces the push only while origin still has that sha, which a rebased branch needs; "" pushes a fast-forward and nothing else.

func Read added in v0.46.0

func Read(ctx context.Context, dir string, args ...string) (string, bool, error)

Read runs a quiet git read: exit 0 is the value and present, exit 1 is absent with no error, and anything else - a lock, a broken worktree, a killed git - is the error. A form whose "not there" exits 128 (a rev-parse --verify without -q) reads absence as an error, which halts rather than proceeds.

func Ref added in v0.46.0

func Ref(ctx context.Context, dir, ref string) (string, error)

Ref is the sha ref names, "" with no error when there is no such ref.

func Refs

func Refs(ctx context.Context, dir string, patterns ...string) ([]string, error)

Refs lists the refs matching patterns, in git's own order, as branch names: `refs/heads/` and `refs/remotes/` stripped, so a remote branch reads `origin/x`. The full refname is asked for rather than the short one, which git disambiguates to `heads/x` when a tag shares the name. A pattern is what for-each-ref takes: a literal ref, which also matches everything under it at a slash, or a glob.

func RemoteBranchExists

func RemoteBranchExists(ctx context.Context, dir, branch string) (bool, error)

RemoteBranchExists asks origin whether it still has branch, which is the one git read here that reaches the network.

func RemoteBranches added in v0.45.0

func RemoteBranches(ctx context.Context, dir string) (map[string]string, error)

RemoteBranches is every branch origin has, name to tip sha, in one network read. A checkout with no origin has none.

func RemoveWorktree

func RemoveWorktree(ctx context.Context, dir, path string) error

RemoveWorktree removes the checkout at path. A dirty tree makes git refuse, which callers rule out first.

func Resolved added in v0.46.0

func Resolved(p string) string

Resolved is the path with symlinks resolved. A component that does not exist - a removed or never-created path - keeps its name below the longest prefix that does, so two spellings of one place still compare.

func SamePath added in v0.46.0

func SamePath(a, b string) bool

SamePath compares two worktree paths with symlinks resolved where they exist.

func SetConfig

func SetConfig(ctx context.Context, dir, key, value string) error

SetConfig writes one entry to the repository's local config, replacing any value the key held. Local config never leaves the machine, which is what makes it the place for a per-branch fact in stealth mode.

func Status

func Status(ctx context.Context, dir string) ([]string, error)

Status lists what `status --porcelain` reports for dir: one line per changed or untracked path, empty for a clean tree. Ignored files are not listed, so an excluded .f10/ never counts.

func Switch added in v0.35.0

func Switch(ctx context.Context, dir, branch string) error

Switch checks out branch in dir. A dirty tree can make git refuse, which callers rule out first.

func TrackBranch added in v0.35.0

func TrackBranch(ctx context.Context, dir, branch string, force bool) error

TrackBranch creates local branch from origin's branch of the same name, tracking it, so a pull or push in its worktree needs no upstream set. With force an existing branch is moved there, which git refuses while a worktree has it checked out.

func UnsetConfig

func UnsetConfig(ctx context.Context, dir, key string) error

UnsetConfig removes one entry from the repository's local config.

func UpdateRef added in v0.42.0

func UpdateRef(ctx context.Context, dir, ref, sha, old string) error

UpdateRef moves ref to sha while it still points at old.

Types

type ConfigEntry

type ConfigEntry struct {
	Key   string
	Value string
}

A ConfigEntry is one key and its value as `config --get-regexp` lists them.

func ConfigEntries

func ConfigEntries(ctx context.Context, dir, pattern string) ([]ConfigEntry, error)

ConfigEntries lists the local config entries whose key matches pattern, in git's order. No match is an empty list, not an error: git exits 1 for it.

type ExitError added in v0.46.0

type ExitError struct {
	Args   []string
	Code   int
	Stderr string
}

An ExitError is git having run and exited non-zero.

func (*ExitError) Error added in v0.46.0

func (e *ExitError) Error() string

type Worktree

type Worktree struct {
	Path   string
	Branch string // short name; "" when detached or bare
}

A Worktree is one checkout git knows about.

func Worktrees

func Worktrees(ctx context.Context, dir string) ([]Worktree, error)

Worktrees lists every checkout of the repo dir belongs to, the main one first, as `git worktree list --porcelain` reports them.

Jump to

Keyboard shortcuts

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