gitrepo

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 13 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 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

Constants

This section is empty.

Variables

View Source
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

func (g Git) BranchTip(ctx context.Context, repo, branch string) (string, error)

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

func (g Git) CommonDir(ctx context.Context, dir string) (string, error)

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

func (g Git) DeleteBranch(ctx context.Context, repo, branch, expectSHA string) error

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

func (g Git) Inspect(ctx context.Context, path string) (LocalState, error)

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

func (Git) IsRepo(dir string) bool

IsRepo reports whether dir/.git exists, as the directory of a main worktree or the file of a linked one.

func (Git) Remotes

func (g Git) Remotes(ctx context.Context, repo string) ([]Remote, error)

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

func (g Git) RemoveWorktree(ctx context.Context, repo, path string) error

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.

func (Git) Worktrees

func (g Git) Worktrees(ctx context.Context, repo string) ([]Worktree, error)

Worktrees lists the worktrees of repo, main worktree first.

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 Remote

type Remote struct {
	Name string
	URL  string
}

Remote is one configured remote.<name>.url value.

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`.

Jump to

Keyboard shortcuts

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