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 ¶
- func AddFrom(repoDir, path, branch, base string) error
- func Available() bool
- func BranchExists(dir, branch string) bool
- func CommitAll(dir, message string) error
- func CurrentBranch(dir string) string
- func DefaultBase(repoDir string) string
- func DefaultWorktreePath(repoRoot, branch string) string
- func Diff(dir, path string) (string, error)
- func Fetch(dir string) (string, error)
- func HasRemote(dir string) bool
- func IsRepo(dir string) bool
- func Prune(repoDir string) (int, error)
- func Pull(dir string) (string, error)
- func Push(dir string) (string, error)
- func Remotes(dir string) []string
- func Remove(repoDir, path string, force bool) error
- func Root(dir string) (string, error)
- func UpstreamOf(dir string) string
- type Branch
- type FileChange
- type Status
- type Worktree
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddFrom ¶
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 BranchExists ¶
BranchExists reports whether a local branch is already present.
func CommitAll ¶
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 CurrentBranch ¶
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 ¶
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 ¶
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 Diff ¶
Diff returns a unified diff for one file, including untracked files, whose content is shown as an addition.
func Fetch ¶
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 ¶
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 Prune ¶
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 ¶
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 ¶
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 Remove ¶
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 ¶
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 UpstreamOf ¶
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.
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.
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 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 ¶
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
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.
func (Status) HasChanges ¶
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 ListDetailed ¶
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.