gitbranch

package
v0.0.0-...-9208c1c Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 11 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 gitbranch builds branch names from task data and manages branches, worktrees, statuses and commit hooks through the git CLI (contract C2).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotRepo       = errors.New("not a git repository")
	ErrBranchExists  = errors.New("branch already exists")
	ErrDetached      = errors.New("HEAD is detached")
	ErrCurrentBranch = errors.New("cannot delete the current branch")
	ErrNotMerged     = errors.New("branch is not fully merged")
	ErrHookExists    = errors.New("foreign prepare-commit-msg hook exists")
	ErrWorktreeBusy  = errors.New("branch is checked out in another worktree")
)

Errors returned by Repo methods.

Functions

func CommitMessage

func CommitMessage(template string, it Item) (string, error)

CommitMessage fills {identifier}, {sequence}, {title} (single line) and {slug}; an unknown placeholder is an error.

func Render

func Render(template string, it Item) (string, error)

Render fills the template placeholders {identifier}, {sequence}, {slug} and validates the resulting name.

func Slug

func Slug(title string, max int) string

Slug converts a title to a branch-safe slug of at most max characters (max <= 0 means 40). An empty result is "task".

func ValidName

func ValidName(name string) error

ValidName checks name by the rules of `git check-ref-format --branch` (go-git, no process per call).

func WorktreePath

func WorktreePath(template, toplevel, branch string) string

WorktreePath expands {repo} (base name of toplevel) and {branch} (name with "/" replaced by "-"); relative results are taken from toplevel. The result is absolute and cleaned.

Types

type BranchStatus

type BranchStatus struct {
	Name          string
	Local         bool   // refs/heads/<name>
	Remote        bool   // refs/remotes/<remote>/<name>
	Current       bool   // branch of HEAD in this working tree
	Worktree      string // other worktree where the branch is checked out
	Ahead, Behind int    // commits relative to base; 0, 0 if unknown
}

BranchStatus describes where a branch lives and how far it is from base.

type Brancher

type Brancher interface {
	Create(name string, o Options) (Result, error)
	Switch(name string) (Result, error)
}

Brancher is the interface consumed by the TUI.

type Git

type Git interface {
	Brancher
	Exists(name string) (bool, error)
	Dirty() (bool, error)
	Toplevel() (string, error)
	Current() (string, error)
	Branches() ([]string, error)
	Statuses(names []string, remote, base string) (map[string]BranchStatus, error)
	AddWorktree(name, path string, o Options) (Result, error)
	Delete(name string, force bool) error
	WriteCommitMsg(branch, msg string) error
	ReadCommitMsg(branch string) (msg string, ok bool, err error)
	InstallHook() (string, bool, error)
	Push(remote, name string) error
	RemoteURL(remote string) (string, error)
	DiffStat(base, branch string) (Stat, error)
}

Git is the full repository interface for app, tui and cli.

type Item

type Item struct {
	Identifier string
	Sequence   int
	Title      string
}

Item is the task data for the branch name template.

func ParseBranch

func ParseBranch(template, name string) (Item, bool)

ParseBranch extracts the task identifier and sequence from a branch name: first by the branch template, then by a generic ID-N pattern. The identifier is upper-cased; Title is empty.

type Options

type Options struct {
	Base     string // empty: current HEAD
	Checkout bool
}

Options controls branch creation.

type Repo

type Repo struct{ Dir string }

Repo is a git repository working directory.

func Open

func Open(dir string) (*Repo, error)

Open verifies that dir is inside a git repository.

func (*Repo) AddWorktree

func (r *Repo) AddWorktree(name, path string, o Options) (Result, error)

AddWorktree checks out name in a separate worktree at path. A missing branch is created from o.Base (empty: HEAD). If the branch is already checked out somewhere, the result carries that path and ErrWorktreeBusy.

func (*Repo) Branches

func (r *Repo) Branches() ([]string, error)

Branches lists local branches sorted by name.

func (*Repo) Create

func (r *Repo) Create(name string, o Options) (Result, error)

Create makes a new branch; if it exists, returns ErrBranchExists.

func (*Repo) Current

func (r *Repo) Current() (string, error)

Current returns the branch checked out in this working tree.

func (*Repo) Delete

func (r *Repo) Delete(name string, force bool) error

Delete removes a local branch (`git branch -d`, or -D with force).

func (*Repo) DiffStat

func (r *Repo) DiffStat(base, branch string) (Stat, error)

DiffStat returns `git diff --shortstat base...branch`. Empty base is resolved as in Statuses (remote HEAD, main, master).

func (*Repo) Dirty

func (r *Repo) Dirty() (bool, error)

Dirty reports whether the working tree has uncommitted changes.

func (*Repo) Exists

func (r *Repo) Exists(name string) (bool, error)

Exists reports whether a local branch with that name exists.

func (*Repo) InstallHook

func (r *Repo) InstallHook() (path string, installed bool, err error)

InstallHook installs the prepare-commit-msg hook. installed is false when the same hook was already in place; a foreign hook gives ErrHookExists.

func (*Repo) Push

func (r *Repo) Push(remote, name string) error

Push publishes name to remote and sets upstream (`git push -u`).

func (*Repo) ReadCommitMsg

func (r *Repo) ReadCommitMsg(branch string) (msg string, ok bool, err error)

ReadCommitMsg reads the stored commit message; a missing file gives ok=false.

func (*Repo) RemoteURL

func (r *Repo) RemoteURL(remote string) (string, error)

RemoteURL returns the URL of remote.

func (*Repo) Statuses

func (r *Repo) Statuses(names []string, remote, base string) (map[string]BranchStatus, error)

Statuses returns statuses for names (empty: all local branches). base empty means refs/remotes/<remote>/HEAD, then main, then master.

func (*Repo) Switch

func (r *Repo) Switch(name string) (Result, error)

Switch checks out an existing branch.

func (*Repo) Toplevel

func (r *Repo) Toplevel() (string, error)

Toplevel returns the root of the working tree.

func (*Repo) WriteCommitMsg

func (r *Repo) WriteCommitMsg(branch, msg string) error

WriteCommitMsg stores the commit message for branch (atomic write).

type Result

type Result struct {
	Name     string
	Created  bool
	Switched bool
	Dirty    bool
	Path     string // worktree path; empty for ordinary branches
}

Result describes what was done.

type Stat

type Stat struct{ Files, Insertions, Deletions int }

Stat is the size of a branch's change relative to its base.

Directories

Path Synopsis
Package gitbranchtest provides an in-memory gitbranch.Git for tests.
Package gitbranchtest provides an in-memory gitbranch.Git for tests.

Jump to

Keyboard shortcuts

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