gitdir

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package gitdir asks the installed git binary where a worktree and its admin directories are.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoGit = errors.New("git is not installed")

ErrNoGit means the git binary is not installed.

Functions

func CleanEnv

func CleanEnv(env []string) []string

CleanEnv drops repository-local git variables from env and pins output to the C locale, so git's answer depends only on the directory it runs in.

func WorktreeRoot

func WorktreeRoot(gitDir string) (root string, ok bool, err error)

WorktreeRoot returns where the worktree of a per-worktree admin dir lives now. git keeps this current on `git worktree move`, so it is the reliable way back from an admin dir to its worktree. ok is false when the admin dir is gone (worktree removed and pruned).

Types

type Git

type Git struct {
	Path string // git binary; empty means "git" from PATH
}

Git runs the git binary.

func (Git) AddWorktree

func (g Git) AddWorktree(ctx context.Context, dir, path, branch, newBranch, base string) error

AddWorktree adds a worktree at path. With newBranch it creates that branch from base (empty: the current HEAD); otherwise it checks out branch, which git also finds on a remote.

func (Git) Branch

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

Branch returns the checked out branch of the worktree at dir, or "" for a detached HEAD.

func (Git) DeleteBranch

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

DeleteBranch deletes a local branch, merged or not.

func (Git) Dirty

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

Dirty reports whether the worktree at dir has uncommitted changes or untracked files.

func (Git) Find

func (g Git) Find(ctx context.Context, dir string) (info Info, ok bool, err error)

Find returns the worktree containing dir. ok is false when dir is not inside a worktree.

func (Git) RemoveWorktree

func (g Git) RemoveWorktree(ctx context.Context, dir, path string, force bool) error

RemoveWorktree removes the worktree at path; force also drops uncommitted changes.

func (Git) Worktrees

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

Worktrees lists the worktrees registered in a repository, identified by its common dir.

type Info

type Info struct {
	Root      string // worktree root
	GitDir    string // per-worktree admin dir: <root>/.git or <common>/worktrees/<id>
	CommonDir string // shared admin dir of the repository
}

Info describes the worktree containing a path.

func (Info) Linked

func (i Info) Linked() bool

Linked reports whether the worktree was added with `git worktree add`.

func (Info) MainName

func (i Info) MainName() string

MainName is the directory name of the main worktree, used as the default project name. For a bare repository it is the repository directory without a ".git" suffix.

func (Info) MainRoot

func (i Info) MainRoot() string

MainRoot is the directory of the main worktree, or of a bare repository.

type Worktree

type Worktree struct {
	Path     string
	Prunable bool // git knows the worktree directory is gone
}

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