gitx

package
v0.3.4-rc.6 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 18 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 gitx is Flockdeck's use of git: the status a pane header shows, the review panel's file list, diffs and commit (changes.go, commit.go), push, pull and fetch (remote.go), and the worktrees that let several agents work in parallel without fighting over one checkout (worktree.go). Every command goes through run.go, which gives it a deadline and shapes what it says when it fails.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFrom

func AddFrom(repoDir, path, branch, base string) error

AddFrom creates a worktree at path.

When branch names an existing local branch it is checked out; otherwise a new branch is created, starting at base when one is given and at the current HEAD when it is not. The path and the starting point both arrive from the window, so they are put after a "--": without it git reads anything beginning with a dash as one of its own options. A base of "--force" was not refused, it was obeyed, and the new worktree quietly started from HEAD with a force checkout instead of from wherever the user had named.

func AddNewBranch added in v0.3.3

func AddNewBranch(repoDir, path, branch string) error

AddNewBranch creates a worktree at path on a new branch, starting at the current HEAD, and never checks out a branch that is already there.

AddFrom checks out a branch that exists, which is what a person naming one means. A caller that means to make a branch of its own -- and later to delete it again, as a fan-out does for an agent that never started -- has to know it did: handed a branch somebody else made in the meantime, it would be deleting their branch. A branch that exists is refused instead.

`git worktree add -b` makes the branch before the checkout, and leaves it behind when the checkout then fails -- on a directory already at path, say. That branch is this call's own, since it was not there a moment before, with no worktree and nothing on it, so it is deleted again rather than left for somebody to wonder about.

func Available

func Available() bool

Available reports whether git is installed.

func BranchExists

func BranchExists(dir, branch string) bool

BranchExists reports whether a local branch is already present.

func CommitAll

func CommitAll(dir, message string) error

CommitAll stages everything and commits it.

Both steps run the user's own programs -- a clean filter such as LFS for the add, the pre-commit hooks for the commit, which as often as not lint or run the tests -- so neither is held to the deadline for reading the repository. A hook still going at twenty seconds was killed there, and the commit it guarded reported as a hang.

func CommitReviewed added in v0.3.3

func CommitReviewed(dir, message string, r Reviewed) error

CommitReviewed commits the files somebody was shown, and refuses when they are no longer what a commit would record.

The review panel lists the tree when it is read and commits when a button is pressed, and agents go on writing in between. CommitAll stages whatever is there at that moment, so a file written after the list was read went into a commit nobody had looked at. When the files git would now record are not those listed -- one has arrived, one has gone, or one has been written to again since its Stamp was taken -- nothing is staged, and the error says how many moved. IsMoved tells that refusal apart from any other.

What is staged is then exactly what was checked, rather than "add --all" again a moment later: a file written in between is left for the next commit.

func CommonDir added in v0.3.3

func CommonDir(dir string) (string, error)

CommonDir returns the git directory shared by every worktree of the repository containing dir: the main checkout's .git, whichever worktree dir is in. It is what tells two paths apart as the same repository or not, where Root answers with the worktree each is in.

func CurrentBranch

func CurrentBranch(dir string) string

CurrentBranch returns the checked-out branch, or "" when detached.

symbolic-ref reads the branch HEAD points at without needing a commit on it, where `rev-parse --abbrev-ref HEAD` fails outright in a repository nobody has committed to yet -- which made a fresh repository look detached, and its first push report "cannot push a detached HEAD".

func DefaultBase

func DefaultBase(repoDir string) string

DefaultBase returns a sensible starting point for a new branch: the current branch of the main worktree.

A rebase in progress there has to be stepped around. It detaches HEAD while it replays commits, so there is no current branch to offer and "HEAD" means whichever commit the replay happens to be sitting on -- a starting point nobody means, and one that will not exist as anything once the rebase finishes. The branch being rebased still points at where it was before the rebase started, which is a real place to branch from.

A branch nobody has committed to yet is not one either: it names no commit, and the panel's pre-filled base made every new worktree in a fresh repository fail with "invalid reference: main". Offering nothing leaves git to start the new branch empty, as it does when no base is given.

func DefaultWorktreePath

func DefaultWorktreePath(repoRoot, branch string) string

DefaultWorktreePath suggests where a new worktree for a branch should live: a sibling of the repository, named after it, so checkouts stay grouped together without nesting inside the repository itself.

A name that is already taken is stepped around rather than suggested. That means two things, and only one of them is visible in the file system: a directory sitting there, which `git worktree add` refuses, and a worktree git still has a record of. The second is what someone who deleted a checkout in their file manager leaves behind, and it fails differently -- "a missing but already registered worktree" -- for a path that looks free.

func DeleteBranch added in v0.3.3

func DeleteBranch(repoDir, branch string) error

DeleteBranch deletes a local branch whether or not it has been merged. git refuses one that is checked out in any worktree.

func Diff

func Diff(dir, path string) (string, error)

Diff returns a unified diff for one file, including untracked files, whose content is shown as an addition.

func Exited added in v0.3.4

func Exited(err error) <-chan struct{}

Exited returns a channel that is closed once every process a git command failed with err started has exited.

A command given up on at its deadline is answered as soon as it has been ended, so that a pane header can say at once that its checkout did not answer, but ending a process only starts its end: one stuck on a network drive that has gone away ends only when the drive gives up. A caller about to ask the same checkout again waits for this first, rather than start another git beside the one still stuck there. For any other error, and for none, it is closed already.

func Fetch

func Fetch(dir string) (string, error)

Fetch updates the remote-tracking branches.

A bare fetch goes to the current branch's remote, which for a branch tracking a local one -- `git checkout -b feature --track main` -- is the repository itself: "From . * branch main -> FETCH_HEAD", nothing brought down from anywhere, and a toast that read like a fetch. Such a branch fetches from the remote a first push would go to instead; every other branch keeps git's own choice.

func HasRemote

func HasRemote(dir string) bool

HasRemote reports whether the repository has anywhere to push to. A substring test for "origin" used to be enough here, but it also matched a remote merely named "my-origin" and missed a repository whose only remote is called something else entirely.

func IsMoved added in v0.3.4

func IsMoved(err error) bool

IsMoved reports whether err is CommitReviewed refusing a tree that moved after it was listed.

func IsRepo

func IsRepo(dir string) bool

IsRepo reports whether dir is inside a git repository.

func Prune

func Prune(repoDir string) (int, error)

Prune removes administrative records for worktrees whose directories have been deleted behind git's back, and reports how many went.

The count is what lets the panel say whether the button it just offered did anything. git names each record it removes on stderr, one to a line, so they are counted as lines rather than looked for by the word they start with -- which is translated when git is speaking anything but English.

func Pull

func Pull(dir string) (string, error)

Pull fast-forwards from the upstream. A merge that cannot fast-forward is left for the user to resolve deliberately rather than started here.

The two ways that commonly fails are told apart by asking the repository rather than by reading git's words, which change with its language: a branch with no upstream has nothing to pull from, and one that has moved on as well as fallen behind cannot be fast-forwarded. Both used to arrive as git's advice, cut down to the lines that did not say what to do.

func Push

func Push(dir string) (string, error)

Push sends the current branch to its remote, setting the upstream the first time so a new worktree's branch does not need a separate command.

func RefreshIndex added in v0.3.4

func RefreshIndex(dir string) error

RefreshIndex writes down what git found about files that were touched without changing, which the pane headers' status never does.

That status takes no lock (GIT_OPTIONAL_LOCKS=0), so that polling the headers never holds the index lock an agent's commit needs, and so it never writes back what it learned either. A checkout whose files were all touched at once -- by a build, or by a checkout another tool made -- had every one of them read again by every status after, five and a half seconds a time on a checkout of sixty thousand files, until something else refreshed the index. This refreshes it. git takes the lock only when nobody holds it, and refuses rather than waits when somebody does.

It has the long deadline, not the short one: git killed while writing the index leaves its lock behind, and every git after it then fails.

func Remotes

func Remotes(dir string) []string

Remotes lists the configured remote names.

func Remove

func Remove(repoDir, path string, force bool) error

Remove deletes a worktree. force discards uncommitted changes in it.

When the directory has already been deleted by hand -- which is how these usually disappear -- git refuses to remove a path that is not there. Pruning the record it left behind is what the person pressing remove meant, and it is the only thing left to do.

That prune is why the path is checked against the repository's own list first. It is not selective: it discards the record of every worktree whose directory is missing, including one sitting on a drive that happens to be unplugged. A path git has never heard of should not be able to set that off, and reporting "removed" for it was a lie besides.

func Root

func Root(dir string) (string, error)

Root returns the top level of the working tree containing dir.

git answers with forward slashes even on Windows, so the path is cleaned to the platform's own form: List does the same, and the two are compared against each other to tell the main worktree from the linked ones.

func Stamp added in v0.3.4

func Stamp(dir, path string) string

Stamp is what a changed file looks like on disk, taken when the file is listed so a commit can tell whether it has been written to since: its size, its modification time and its mode, or "gone" for one that is not there. It is "" for what it cannot speak for -- a directory, which is how a repository inside this one is listed -- and that is not checked.

It is not a hash of the content. Reading every file on every listing is what the panel cannot afford, and an edit that leaves both the size and the modification time as they were -- possible where the file system keeps times to the second or coarser -- goes unnoticed.

func UpstreamOf

func UpstreamOf(dir string) string

UpstreamOf names the remote branch the checked-out branch tracks, or "" when it tracks nothing.

Push is the only thing that needs this, and asking StatusOf made git walk the entire working tree to answer it -- 190ms on a checkout with 20,000 untracked files, against 44ms here -- for a fact that has nothing to do with what is in the working tree. It also meant a status that timed out on a large checkout read as "no upstream", and Push would then set one: on a branch tracking something other than origin, that quietly repoints it.

func WorktreePaths added in v0.3.3

func WorktreePaths(repoRoot string, branches []string) []string

WorktreePaths is DefaultWorktreePath for several branches at once, whose worktrees are about to be created side by side: each path steps around the ones chosen before it as well as around what is already there.

Chosen one at a time, while the worktrees were being created together, two could be given the same path. Every choice looks only at what exists, and the sibling that would have taken a name has not made its directory yet -- so "agent/fix", stepping around a stray repo-agent-fix to repo-agent-fix-2, and "agent/fix-2", whose own name that is, both chose it, and one of the two tasks failed on a directory the other had just made.

Types

type Branch

type Branch struct {
	Name      string
	Upstream  string
	Current   bool
	CheckedIn string // path of the worktree that has it checked out, if any
}

Branch is a local branch and where it stands against its upstream.

func Branches

func Branches(dir string) ([]Branch, error)

Branches lists local branches, most recently used first, so the worktree picker can offer them without the user typing a name.

type FileChange

type FileChange struct {
	Path string
	// Status is the two-letter porcelain code, e.g. " M", "A ", "??".
	Status string
	// Label is that code rendered for people: "modified", "new", …
	Label     string
	Staged    bool
	Unstaged  bool
	Untracked bool
	Added     int
	Removed   int
}

FileChange is one modified file in a working tree.

func Changes

func Changes(dir string) ([]FileChange, error)

Changes lists the files that differ from HEAD, with line counts.

type Reviewed added in v0.3.4

type Reviewed struct {
	// Files are the paths the panel listed, spelled as the window has them.
	Files []string
	// Stamps holds each listed file's Stamp from when it was listed. A file
	// with none is held only to being listed.
	Stamps map[string]string
	// Unlisted counts the changed files the list left out.
	Unlisted int
}

Reviewed is what the review panel showed of a working tree, as a commit made from it sends back.

type Status

type Status struct {
	Branch   string
	Head     string // short commit id
	Detached bool
	Upstream string
	Ahead    int
	Behind   int
	// Unborn marks a branch with no commits on it yet, where Head is empty
	// because there is nothing to point at.
	Unborn bool
	// Operation names what a detached checkout is in the middle of --
	// "rebasing" or "bisecting" -- when Branch was read from that operation's
	// state rather than from HEAD, which names no branch while it runs. A
	// rebase started from a detached HEAD has no branch to read, and is
	// "rebasing" with Branch empty.
	Operation string
	// Dirty counts tracked files with changes; Untracked counts new files.
	Dirty     int
	Untracked int
}

Status summarises a working tree: what is checked out, whether it has uncommitted work, and how it stands against its upstream.

func StatusOf

func StatusOf(dir string) Status

StatusOf reads a working tree's state in a single git call.

`--porcelain=v2 --branch` reports the branch, upstream and ahead/behind counts alongside the file entries, which avoids the several invocations the same information would otherwise take.

func StatusWithin added in v0.2.10

func StatusWithin(dir string, timeout time.Duration) (Status, error)

StatusWithin is StatusOf for a caller that gives git less time and needs to know when it ran out: the pane headers, which are refreshed every few seconds and would rather say a checkout did not answer than wait on it. The whole of the read, the extra call for a detached checkout included, has timeout to finish in. When it does not, git is stopped, the Status is empty, and errors.Is(err, context.DeadlineExceeded) holds.

It does not look inside submodules. Finding work left uncommitted in one means a git status of its own inside every submodule, every refresh, and that work cannot be committed from here anyway; the review panel, which reads StatusOf, still says so. A submodule moved to another commit is this repository's change, and is still counted.

func (Status) HasChanges

func (s Status) HasChanges() bool

HasChanges reports whether anything is uncommitted.

type Worktree

type Worktree struct {
	Path     string
	Head     string
	Branch   string // short branch name, empty when detached
	Bare     bool
	Detached bool
	Locked   bool
	// Main reports whether this is the repository's primary worktree.
	Main bool
	// Prunable reports that the worktree's directory is gone -- deleted by
	// hand, or on a drive that is not there -- and only git's record of it is
	// left. Its Status is empty, which is not the same thing as clean.
	Prunable bool
	// Status is filled in by ListDetailed.
	Status Status
}

Worktree is one entry from `git worktree list`.

func List

func List(dir string) ([]Worktree, error)

List returns every worktree of the repository containing dir.

func ListDetailed

func ListDetailed(dir string) ([]Worktree, error)

ListDetailed returns every worktree with its status filled in.

Each worktree needs its own status call, so they are run concurrently: a repository with several worktrees would otherwise make the panel wait for them one after another. Only a few run at a time, though. A fan-out leaves a dozen worktrees or more behind, and a whole-tree git status started for every one of them in the same instant contends with the agents working in them for answers that arrive no sooner.

func (Worktree) Label

func (w Worktree) Label() string

Label returns a human-readable name for the worktree.

Jump to

Keyboard shortcuts

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