Documentation
¶
Overview ¶
Package gitrepo drives the git binary to list a repository's worktrees, inspect a worktree for anything its removal would lose, and remove worktrees and branches.
Index ¶
- Variables
- type Git
- func (g Git) BranchTip(ctx context.Context, repo, branch string) (string, error)
- func (g Git) CommonDir(ctx context.Context, dir string) (string, error)
- func (g Git) DeleteBranch(ctx context.Context, repo, branch, expectSHA string) error
- func (g Git) Inspect(ctx context.Context, path string) (LocalState, error)
- func (Git) IsRepo(dir string) bool
- func (g Git) Remotes(ctx context.Context, repo string) ([]Remote, error)
- func (g Git) RemoveWorktree(ctx context.Context, repo, path string) error
- func (g Git) Worktrees(ctx context.Context, repo string) ([]Worktree, error)
- type LocalState
- type Remote
- type Worktree
Constants ¶
This section is empty.
Variables ¶
var ErrBranchMoved = errors.New("branch moved")
ErrBranchMoved reports that a branch no longer points at the commit the caller decided to delete it for, or no longer exists.
Functions ¶
This section is empty.
Types ¶
type Git ¶
type Git struct {
// Binary is the git executable; empty means "git" looked up in PATH.
Binary string
}
Git runs the git binary. The zero value runs "git" from PATH. A Git holds no state between calls, so one value serves concurrent calls on different repositories.
func (Git) BranchTip ¶
BranchTip returns the full SHA of the commit local branch points at, or "" with a nil error when there is no such branch. The branch is read by its full refname, so a tag of the same name is never read in its place.
func (Git) CommonDir ¶
CommonDir returns the absolute, symlink-resolved git directory shared by every worktree of the repository dir belongs to, so two worktrees of one repository return the same value.
func (Git) DeleteBranch ¶
DeleteBranch deletes local branch if it still points at expectSHA, and otherwise returns an error wrapping ErrBranchMoved without deleting it. git refuses to delete a branch checked out in any worktree; that refusal is returned as the error.
func (Git) Inspect ¶
Inspect reports the local state of the worktree rooted at path. A path that is not a worktree root is an error rather than a report on whichever worktree contains it, and so is a HEAD that names no commit, such as an unborn branch.
func (Git) IsRepo ¶
IsRepo reports whether dir/.git exists, as the directory of a main worktree or the file of a linked one.
func (Git) Remotes ¶
Remotes returns the repository's configured remote URLs in configuration order, one entry per remote.<name>.url value, as written rather than as rewritten by url.<base>.insteadOf.
func (Git) RemoveWorktree ¶
RemoveWorktree runs `git worktree remove` without --force. git refuses a locked worktree, one holding a populated submodule, and one where git status reports staged changes, unstaged changes to tracked files, or untracked files, even with status.showUntrackedFiles=no configured; the refusal is returned as the error. Whatever git status does not report is deleted without complaint: ignored files, and changes to paths marked skip-worktree or assume-unchanged.
type LocalState ¶
type LocalState struct {
// Head is the full SHA of the checked-out commit.
Head string
// Tracked lists paths with staged or unstaged changes, including
// unmerged paths and modified submodules. A rename or copy is listed
// once, under its new path.
Tracked []string
// Untracked lists every untracked file individually; a directory
// appears, with a trailing slash, only when it is a nested repository.
Untracked []string
// Ignored lists ignored paths. A directory an ignore pattern matches
// appears once with a trailing slash and its contents are not listed;
// an ignored file in a directory that is not itself ignored appears by
// its own path.
Ignored []string
// SkipFlagged lists index entries marked assume-unchanged or
// skip-worktree, whose changes git status does not report.
SkipFlagged []string
// HasSubmodules reports a .gitmodules file at the worktree root.
HasSubmodules bool
// OperationInProgress reports a rebase, merge, cherry-pick, revert, or
// bisect under way in the worktree.
OperationInProgress bool
// LastActivity is the newest modification time among the HEAD, index,
// and logs/HEAD files of the worktree's own git directory; zero when
// none exists.
LastActivity time.Time
}
LocalState is what a worktree holds that its commit does not. Paths are relative to the worktree root.
type Worktree ¶
type Worktree struct {
// Path is the worktree's absolute directory.
Path string
// Head is the full SHA of the checked-out commit; empty for a bare
// repository.
Head string
// Branch is the checked-out branch's short name; empty when detached or
// bare.
Branch string
// Main marks the repository's main worktree, which git lists first.
Main bool
Bare bool
Detached bool
// Locked is set by `git worktree lock`, with or without a reason.
Locked bool
// Prunable means git considers the worktree gone, typically because
// its directory was deleted.
Prunable bool
LockReason string
PrunableReason string
}
Worktree is one entry of `git worktree list`.