Documentation
¶
Overview ¶
Package git is a typed wrapper around the git command line. It shells out through pkg/exec; go-git is deliberately not used so behaviour matches the user's git exactly (hooks, config, worktrees, signing).
Index ¶
- Constants
- Variables
- func LockPath(err error) string
- type AddMode
- type Branch
- type Client
- func (c *Client) Add(ctx context.Context, repo Repo, mode AddMode) error
- func (c *Client) AddedPaths(ctx context.Context, repo Repo, from, to string) ([]string, error)
- func (c *Client) BranchExists(ctx context.Context, repo Repo, name string) (bool, error)
- func (c *Client) Branches(ctx context.Context, repo Repo) ([]Branch, error)
- func (c *Client) ChangedFiles(ctx context.Context, repo Repo) ([]string, error)
- func (c *Client) ChangedPaths(ctx context.Context, repo Repo, from, to string) ([]string, error)
- func (c *Client) CheckRefFormat(ctx context.Context, name string) error
- func (c *Client) Commit(ctx context.Context, repo Repo, o CommitOptions) (string, error)
- func (c *Client) CommitInfo(ctx context.Context, repo Repo, rev string) (CommitInfo, error)
- func (c *Client) CommitTree(ctx context.Context, repo Repo, tree, parent string, info CommitInfo) (string, error)
- func (c *Client) Commits(ctx context.Context, repo Repo, tip string, exclude ...string) ([]Commit, error)
- func (c *Client) ConfigAdd(ctx context.Context, repo Repo, scope Scope, key, value string) error
- func (c *Client) ConfigGet(ctx context.Context, repo Repo, scope Scope, key string) (string, bool, error)
- func (c *Client) ConfigGetAll(ctx context.Context, repo Repo, scope Scope, key string) ([]string, error)
- func (c *Client) ConfigGetRegexp(ctx context.Context, repo Repo, scope Scope, re string) ([]ConfigEntry, error)
- func (c *Client) ConfigSet(ctx context.Context, repo Repo, scope Scope, key, value string) error
- func (c *Client) ConfigUnset(ctx context.Context, repo Repo, scope Scope, key string) error
- func (c *Client) ConfigUnsetValue(ctx context.Context, repo Repo, scope Scope, key, value string) error
- func (c *Client) ConflictedFiles(ctx context.Context, repo Repo) ([]string, error)
- func (c *Client) CountCommits(ctx context.Context, repo Repo, from, to string) (int, error)
- func (c *Client) CreateBranch(ctx context.Context, repo Repo, name, start string) error
- func (c *Client) CurrentBranch(ctx context.Context, repo Repo) (string, error)
- func (c *Client) DefaultBranch(ctx context.Context, repo Repo) (string, bool, error)
- func (c *Client) DeleteBranch(ctx context.Context, repo Repo, name string) error
- func (c *Client) DiffRange(ctx context.Context, repo Repo, base, head string) (string, error)
- func (c *Client) Discover(ctx context.Context, dir string) (Repo, error)
- func (c *Client) Fetch(ctx context.Context, repo Repo, remote string) error
- func (c *Client) HasStagedChanges(ctx context.Context, repo Repo) (bool, error)
- func (c *Client) HasUnstagedChanges(ctx context.Context, repo Repo) (bool, error)
- func (c *Client) HasUntrackedFiles(ctx context.Context, repo Repo) (bool, error)
- func (c *Client) IndexLockPath(ctx context.Context, repo Repo) (string, error)
- func (c *Client) IsAncestor(ctx context.Context, repo Repo, ancestor, descendant string) (bool, error)
- func (c *Client) ListCmds(ctx context.Context, spec string) ([]string, error)
- func (c *Client) LocalChanges(ctx context.Context, repo Repo) ([]string, error)
- func (c *Client) MergeBase(ctx context.Context, repo Repo, a, b string) (string, error)
- func (c *Client) MergeFF(ctx context.Context, repo Repo, rev string) error
- func (c *Client) MergeTree(ctx context.Context, repo Repo, base, ours, theirs string) (MergeTreeResult, error)
- func (c *Client) Messages(ctx context.Context, repo Repo, from, to string) ([]string, error)
- func (c *Client) Passthrough(ctx context.Context, repo Repo, args ...string) error
- func (c *Client) PatchIDs(ctx context.Context, repo Repo, from, to string) (map[string]string, error)
- func (c *Client) PatchIDsExcluding(ctx context.Context, repo Repo, to string, exclude ...string) (map[string]string, error)
- func (c *Client) RebaseAbort(ctx context.Context, repo Repo) error
- func (c *Client) RebaseContinue(ctx context.Context, repo Repo) (stopped bool, err error)
- func (c *Client) RebaseInProgress(ctx context.Context, repo Repo) (bool, error)
- func (c *Client) RebaseMatches(ctx context.Context, repo Repo, branch, onto, tip string) (bool, error)
- func (c *Client) RebaseOnto(ctx context.Context, repo Repo, onto, upstream, branch string) (stopped bool, err error)
- func (c *Client) Reflog(ctx context.Context, repo Repo, ref string) ([]string, error)
- func (c *Client) RemoteFor(ctx context.Context, repo Repo, branch string) string
- func (c *Client) RemoteURL(ctx context.Context, repo Repo, name string) (string, error)
- func (c *Client) Replay(ctx context.Context, repo Repo, commits []string, onto string) (ReplayResult, error)
- func (c *Client) ResetHard(ctx context.Context, repo Repo, rev string) error
- func (c *Client) ResetKeep(ctx context.Context, repo Repo, rev string) error
- func (c *Client) RevList(ctx context.Context, repo Repo, from, to string) ([]string, error)
- func (c *Client) RevParse(ctx context.Context, repo Repo, rev string) (string, error)
- func (c *Client) Runner() exec.Runner
- func (c *Client) StagedDiff(ctx context.Context, repo Repo) (string, error)
- func (c *Client) Subject(ctx context.Context, repo Repo, rev string) (string, error)
- func (c *Client) Subjects(ctx context.Context, repo Repo, rev string, n int) ([]string, error)
- func (c *Client) Switch(ctx context.Context, repo Repo, name string) error
- func (c *Client) SwitchDetached(ctx context.Context, repo Repo, rev string) error
- func (c *Client) Tip(ctx context.Context, repo Repo, ref string) (string, bool, error)
- func (c *Client) TreeOf(ctx context.Context, repo Repo, rev string) (string, error)
- func (c *Client) Untracked(ctx context.Context, repo Repo) ([]string, error)
- func (c *Client) UpdateRefs(ctx context.Context, repo Repo, updates []RefUpdate) error
- func (c *Client) Worktrees(ctx context.Context, repo Repo) ([]Worktree, error)
- type Commit
- type CommitInfo
- type CommitOptions
- type ConfigEntry
- type Conflict
- type MergeTreeResult
- type Option
- type OverwriteError
- type RefUpdate
- type ReplayResult
- type Repo
- type Scope
- type SigningError
- type Worktree
Constants ¶
const DefaultLockRetryBudget = 2 * time.Second
DefaultLockRetryBudget is how long git commands wait for a held index lock before giving up. IDEs and agents take the lock for milliseconds at a time while refreshing status, which is plenty to make a bare `git add` fail.
Variables ¶
var ErrDetached = errors.New("HEAD is detached")
ErrDetached is returned when HEAD is not on a branch.
var ErrIndexLocked = errors.New("the index is locked by another git process")
ErrIndexLocked matches the error git commands return when another process held a worktree's index lock (index.lock) for longer than the retry budget. git-stack never removes a lock itself.
var ErrNotRepo = errors.New("not a git repository")
ErrNotRepo is returned by Discover outside a git repository.
var ErrRefLocked = errors.New("a ref is locked by another git process")
ErrRefLocked matches a command that failed because another process held a ref's lock file (refs/heads/main.lock, packed-refs.lock).
Functions ¶
Types ¶
type Branch ¶
type Branch struct {
Name string
// Head is the tip commit id.
Head string
// CommitTime is the committer date of the tip.
CommitTime time.Time
// Worktree is the path of the worktree the branch is checked out in, or
// empty.
Worktree string
// Upstream is the tracking branch (e.g. origin/feat), or empty.
Upstream string
}
Branch describes a local branch.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client runs git commands.
func (*Client) AddedPaths ¶ added in v0.1.4
AddedPaths lists the paths that exist in to's tree but not in from's.
func (*Client) BranchExists ¶
BranchExists reports whether a local branch exists.
func (*Client) ChangedFiles ¶ added in v0.2.0
ChangedFiles lists tracked files with staged or unstaged changes in the worktree at repo, relative to it. It reads with --no-optional-locks, as it may look at another worktree.
func (*Client) ChangedPaths ¶ added in v0.1.5
ChangedPaths lists the paths whose content differs between the trees of from and to.
func (*Client) CheckRefFormat ¶
CheckRefFormat validates name as a branch name.
func (*Client) Commit ¶
Commit creates or amends a commit and returns the new HEAD id. When the editor must open (no message and not NoEdit, or Edit), the command runs in passthrough mode.
func (*Client) CommitInfo ¶ added in v0.1.4
CommitInfo reads rev's author, message, tree and first parent.
func (*Client) CommitTree ¶ added in v0.1.4
func (c *Client) CommitTree(ctx context.Context, repo Repo, tree, parent string, info CommitInfo) (string, error)
CommitTree creates a commit of tree on parent with info's author, author date and message. The committer is the current user. The commit is not signed: unlike commit and rebase, commit-tree ignores commit.gpgsign and only signs when asked with -S, which we don't pass, so a replay never needs the signing key.
func (*Client) Commits ¶ added in v0.2.0
func (c *Client) Commits(ctx context.Context, repo Repo, tip string, exclude ...string) ([]Commit, error)
Commits lists the commits reachable from tip but from none of exclude, newest first. Revisions that don't resolve (a deleted parent branch, a recorded base that was garbage collected) are ignored rather than failing the read, and a missing tip lists nothing. It's a read that can run while another git process works in any worktree, so it takes no optional locks, and the output is NUL separated since a subject can hold anything.
func (*Client) ConfigGet ¶
func (c *Client) ConfigGet(ctx context.Context, repo Repo, scope Scope, key string) (string, bool, error)
ConfigGet returns the value of key. The boolean reports whether it was set.
func (*Client) ConfigGetAll ¶
func (c *Client) ConfigGetAll(ctx context.Context, repo Repo, scope Scope, key string) ([]string, error)
ConfigGetAll returns every value of a multi-valued key.
func (*Client) ConfigGetRegexp ¶
func (c *Client) ConfigGetRegexp(ctx context.Context, repo Repo, scope Scope, re string) ([]ConfigEntry, error)
ConfigGetRegexp returns every entry whose key matches the regexp.
func (*Client) ConfigUnset ¶
ConfigUnset removes every value of key. A missing key is not an error.
func (*Client) ConfigUnsetValue ¶
func (c *Client) ConfigUnsetValue(ctx context.Context, repo Repo, scope Scope, key, value string) error
ConfigUnsetValue removes the values of key that match the given value (exact match). A missing key or value is not an error.
func (*Client) ConflictedFiles ¶
ConflictedFiles lists paths with unresolved merge conflicts.
func (*Client) CountCommits ¶
CountCommits returns the number of commits in from..to.
func (*Client) CreateBranch ¶
CreateBranch creates name at start without checking it out.
func (*Client) CurrentBranch ¶
CurrentBranch returns the checked-out branch name.
func (*Client) DefaultBranch ¶
DefaultBranch guesses the repository's trunk the way gh-stack does: the branch origin/HEAD points at, else a local main or master. The boolean is false when nothing matches.
func (*Client) DeleteBranch ¶ added in v0.1.4
DeleteBranch deletes a local branch whether or not it is merged. git refuses when the branch is checked out in any worktree; callers check.
func (*Client) DiffRange ¶
DiffRange returns the diff between base and head (base...head semantics are not used: this is the exact tree difference, which is what a rebased branch shows against its parent).
func (*Client) Fetch ¶ added in v0.1.4
Fetch updates the remote-tracking refs of remote and prunes the ones whose branch is gone. Nothing local moves.
func (*Client) HasStagedChanges ¶
HasStagedChanges reports whether the index differs from HEAD. Like every read that may run against another worktree, it passes --no-optional-locks so it never takes (or trips over) that worktree's index lock just to refresh stat data.
func (*Client) HasUnstagedChanges ¶
HasUnstagedChanges reports whether the working tree has unstaged changes to tracked files.
func (*Client) HasUntrackedFiles ¶
HasUntrackedFiles reports whether untracked, non-ignored files exist.
func (*Client) IndexLockPath ¶ added in v0.2.0
IndexLockPath is where git keeps the index lock for the worktree at repo: .git/index.lock for the main one, .git/worktrees/<name>/index.lock for a linked one. It only names the file; nothing here ever removes it.
func (*Client) IsAncestor ¶
func (c *Client) IsAncestor(ctx context.Context, repo Repo, ancestor, descendant string) (bool, error)
IsAncestor reports whether ancestor is reachable from descendant.
func (*Client) ListCmds ¶
ListCmds returns `git --list-cmds=<spec>` (e.g. "builtins" or "main,others").
func (*Client) LocalChanges ¶ added in v0.1.5
LocalChanges lists the paths whose index or working tree content differs from HEAD, relative to the top level. Untracked files are not included; Untracked lists those.
func (*Client) MergeFF ¶ added in v0.1.4
MergeFF fast forwards the branch checked out in repo's worktree to rev and updates the tree. It refuses, changing nothing, when rev is not a descendant or a dirty file would be overwritten.
func (*Client) MergeTree ¶ added in v0.1.4
func (c *Client) MergeTree(ctx context.Context, repo Repo, base, ours, theirs string) (MergeTreeResult, error)
MergeTree merges ours and theirs with base as the common ancestor.
func (*Client) Passthrough ¶
Passthrough runs git with the terminal attached (editors, `add -p`).
func (*Client) PatchIDs ¶ added in v0.1.4
func (c *Client) PatchIDs(ctx context.Context, repo Repo, from, to string) (map[string]string, error)
PatchIDs maps the patch id of every non merge commit in from..to to that commit, so two histories can be compared for the same changes regardless of where they were rebased. The ids are verbatim: unlike --stable they count whitespace, so a whitespace only amend is a different change.
func (*Client) PatchIDsExcluding ¶ added in v0.1.4
func (c *Client) PatchIDsExcluding(ctx context.Context, repo Repo, to string, exclude ...string) (map[string]string, error)
PatchIDsExcluding is PatchIDs for the commits reachable from to but from none of exclude.
func (*Client) RebaseAbort ¶ added in v0.1.5
RebaseAbort abandons a rebase in progress; git puts the branch it was rebasing back and checks it out.
func (*Client) RebaseContinue ¶ added in v0.1.5
RebaseContinue resumes a stopped rebase after the conflicts were staged. stopped is true when it stopped again on unresolved conflicts; any other failure is an error.
func (*Client) RebaseInProgress ¶
RebaseInProgress reports whether git has a rebase in progress.
func (*Client) RebaseMatches ¶ added in v0.1.5
func (c *Client) RebaseMatches(ctx context.Context, repo Repo, branch, onto, tip string) (bool, error)
RebaseMatches reports whether the active rebase is for branch, from tip onto onto. Both git rebase backends record these values in the worktree's git dir; a rebase started after an earlier one was aborted can differ in any of them. It only reads the state, leaving the rebase and index alone.
func (*Client) RebaseOnto ¶ added in v0.1.5
func (c *Client) RebaseOnto(ctx context.Context, repo Repo, onto, upstream, branch string) (stopped bool, err error)
RebaseOnto runs `git rebase --onto onto upstream branch` without an editor. It checks branch out, so the working tree must be clean enough for git. stopped is true when git stopped on a conflict and left the rebase in progress for the caller to resolve; any other failure is an error.
func (*Client) Reflog ¶ added in v0.1.6
Reflog lists every commit ref has pointed at that the reflog still remembers, newest first. A ref with no reflog, or no ref at all, gives nothing.
func (*Client) RemoteFor ¶ added in v0.1.4
RemoteFor is the remote a branch tracks (branch.<name>.remote), or origin when it tracks nothing.
func (*Client) RemoteURL ¶ added in v0.2.0
RemoteURL is the URL git uses to fetch from the named remote, with url.<base>.insteadOf rewriting applied (git remote get-url). It only reads config; nothing touches the network.
func (*Client) Replay ¶ added in v0.1.4
func (c *Client) Replay(ctx context.Context, repo Repo, commits []string, onto string) (ReplayResult, error)
Replay recreates commits, oldest first, on top of onto. It never touches a working tree, the index or a ref: it only creates objects, so the caller decides what to do with Tip. A merge commit is replayed against its first parent, which linearises it. A commit whose changes are already in the new parent would come out empty and is dropped instead. A root commit is replayed with an empty merge base.
func (*Client) ResetHard ¶ added in v0.1.4
ResetHard moves the checked out branch, the index and the tree to rev. git deletes an untracked file in the way of a tracked one; callers check with AddedPaths and Untracked first.
func (*Client) ResetKeep ¶ added in v0.1.5
ResetKeep moves HEAD and the checked out branch to rev and updates the working tree, keeping local changes to files rev and HEAD agree on. git refuses, and nothing moves, when a locally changed file differs between the two. Staged new files are re-added afterwards because reset --keep drops them from the index; the caller refuses up front when the move adds the same path, so re-adding cannot clobber anything.
func (*Client) RevList ¶ added in v0.1.4
RevList lists the commits reachable from to but not from, oldest first.
func (*Client) Runner ¶
Runner exposes the underlying runner (used by adapters that run other tools with the same debug/TTY settings).
func (*Client) StagedDiff ¶
StagedDiff returns the diff of the index against HEAD.
func (*Client) Subjects ¶
Subjects returns the commit subjects of rev's history, newest first, at most n.
func (*Client) SwitchDetached ¶ added in v0.1.1
SwitchDetached checks out rev with a detached HEAD.
func (*Client) Tip ¶ added in v0.1.4
Tip resolves a full ref name (refs/heads/x, refs/remotes/origin/x) to a commit id. ok is false when the ref does not exist.
func (*Client) Untracked ¶ added in v0.1.4
Untracked lists the untracked, non ignored files in repo's worktree, relative to its top level.
func (*Client) UpdateRefs ¶ added in v0.1.4
UpdateRefs applies every update in one transaction: all move or none do.
type CommitInfo ¶ added in v0.1.4
type CommitInfo struct {
Author, Email string
// Date is the author date in strict ISO 8601 (git's %aI).
Date string
Message string
Tree string
// Parent is the first parent, "" for a root commit.
Parent string
}
CommitInfo is what it takes to recreate a commit somewhere else.
type CommitOptions ¶
type CommitOptions struct {
// Message paragraphs; each becomes a -m argument. Empty opens the editor
// unless NoEdit is set.
Message []string
Amend bool
NoEdit bool
// Edit forces the editor even with a message (or when amending).
Edit bool
ResetAuthor bool
NoVerify bool
// AllowEmpty permits a commit with no changes.
AllowEmpty bool
}
CommitOptions controls Commit.
type ConfigEntry ¶
ConfigEntry is one key/value pair.
type MergeTreeResult ¶ added in v0.1.4
MergeTreeResult is a three way merge computed without a working tree: the merged tree, and the conflicting paths when it was not clean (the tree then contains conflict markers and must not be committed).
type OverwriteError ¶ added in v0.2.0
type OverwriteError struct {
Changed, Untracked []string
}
OverwriteError is git refusing to move a checkout because doing so would overwrite files in it: local changes to tracked files (Changed) and untracked files where the update writes (Untracked). Paths are relative to the checkout. git touched nothing.
func (*OverwriteError) Error ¶ added in v0.2.0
func (e *OverwriteError) Error() string
type RefUpdate ¶ added in v0.1.4
type RefUpdate struct {
Ref, New, Old string
}
RefUpdate moves Ref to New. When Old is set the ref must currently be at Old, and the whole transaction fails if any Old is stale.
type ReplayResult ¶ added in v0.1.4
type ReplayResult struct {
// Tip is the new tip (onto itself when there was nothing to replay), or
// "" when Conflict is set.
Tip string
// Replayed counts the commits recreated.
Replayed int
// Skipped counts the commits dropped because their changes were already
// in the new parent, as git rebase drops them.
Skipped int
// Conflict is set when the replay stopped; the commits before it were
// recreated as unreferenced objects and nothing else happened.
Conflict *Conflict
}
ReplayResult is what Replay produced.
type Repo ¶
type Repo struct {
// TopLevel is the working tree root.
TopLevel string
// GitDir is the repository directory for this worktree
// (e.g. .git or .git/worktrees/<name>).
GitDir string
// CommonDir is the shared repository directory (.git of the main worktree).
CommonDir string
}
Repo locates a repository. All paths are absolute.
func (Repo) IsLinkedWorktree ¶
IsLinkedWorktree reports whether the repo is a linked worktree.
type SigningError ¶ added in v0.2.2
type SigningError struct {
// Format is gpg.format: ssh, openpgp (the default) or x509.
Format string
// Key is user.signingkey as configured, which for ssh is usually the
// path of the public key file; empty when unset.
Key string
// Detail is git's stderr, including the signing program's output.
Detail string
}
SigningError reports that git could not sign a commit. Nothing was committed. From Commit the index is untouched; from a rebase (RebaseOnto, RebaseContinue) the rebase is left in progress with the failed pick's changes staged, for RebaseContinue to commit once signing works or RebaseAbort to drop.
func (*SigningError) Error ¶ added in v0.2.2
func (e *SigningError) Error() string