Help us shape the future of Go! Take the 2026 Go Developer Survey

git

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package git provides centralized utilities and command builders for invoking git.

Index

Constants

View Source
const DefaultCommand = "git"

DefaultCommand is the default binary name for git.

Variables

This section is empty.

Functions

func FindMainRepo added in v0.4.0

func FindMainRepo(reposRoot, worktreeRoot, dir string) (string, error)

FindMainRepo resolves the root directory of the main git repository for dir. If dir is within a linked git worktree, it resolves the main repository. If dir is within a normal git repository, it returns that repository's root.

func FindMainRepoAndBranchFromGit added in v0.4.0

func FindMainRepoAndBranchFromGit(wtPath string) (string, string, error)

FindMainRepoAndBranchFromGit discovers the main repository path and branch name by reading the git worktree metadata (.git file and commondir) directly.

func FindRepoRoot added in v0.4.0

func FindRepoRoot(dir string) (string, error)

FindRepoRoot walks up from dir until it finds a directory containing a ".git" entry. Returns the absolute path to the repository root.

func IsRepo added in v0.3.0

func IsRepo(dir string) bool

IsRepo reports whether dir contains a ".git" entry (file or directory).

func ResolveCloneDir added in v0.3.0

func ResolveCloneDir(reposRoot, uri string) (string, error)

ResolveCloneDir resolves the absolute local directory path under reposRoot for uri.

func ResolveMainRepoFromWorktree added in v0.4.0

func ResolveMainRepoFromWorktree(reposRoot, worktreeRoot, wtPath string) (string, string, error)

ResolveMainRepoFromWorktree resolves the main repository path (repos_root/DIR) and branch name from a worktree path (worktree_root/DIR/branch).

func ResolveRepoRelPath added in v0.4.0

func ResolveRepoRelPath(reposRoot, repoDir string) (string, error)

ResolveRepoRelPath resolves the relative path of the git repository containing repoDir with respect to reposRoot (i.e. DIR in repos_root/DIR).

func ResolveUniqueMainRepos added in v0.4.0

func ResolveUniqueMainRepos(reposRoot, worktreeRoot string, wtPaths []string) ([]string, error)

ResolveUniqueMainRepos resolves the unique main repository paths under reposRoot corresponding to the given worktree paths under worktreeRoot, preserving order.

func ResolveWorktreePath added in v0.4.0

func ResolveWorktreePath(reposRoot, worktreeRoot, repoDir, branch string) (string, error)

ResolveWorktreePath computes worktree_root/DIR/branch for a repository at repoDir under reposRoot.

func URI2RelPath added in v0.3.0

func URI2RelPath(uri string) (string, error)

URI2RelPath converts a repository URI (HTTPS, HTTP, SSH, git://, SCP-like git@host:path, or host/path) into a relative directory path (<host>/<path> without trailing .git).

Types

type Command

type Command struct {
	// Bin is the path or name of the git binary. Defaults to DefaultCommand ("git") if empty.
	Bin string
	// Runner executes the command.
	Runner Runner
}

Command represents a git command configuration.

func New

func New(bin string, r Runner) *Command

New creates a new Command with the given binary and runner.

func (*Command) BranchDelete added in v0.4.0

func (c *Command) BranchDelete(ctx context.Context, repoDir, branch string, force bool, stderr io.Writer) error

BranchDelete deletes branch in repoDir. If force is true, it uses -D instead of -d.

func (*Command) BranchDeleteCmd added in v0.4.0

func (c *Command) BranchDeleteCmd(branch string, force bool) []string

BranchDeleteCmd constructs argv for "git branch -d <branch>" (or "-D" when force is true).

func (*Command) Checkout

func (c *Command) Checkout(ctx context.Context, dir, target string, stderr io.Writer) error

Checkout switches the branch in dir to target.

func (*Command) Clone added in v0.3.0

func (c *Command) Clone(ctx context.Context, uri, targetDir string, stderr io.Writer) error

Clone clones uri into targetDir, routing command output to stderr.

func (*Command) CloneCmd added in v0.3.0

func (c *Command) CloneCmd(uri, dir string) []string

CloneCmd constructs argv for "git clone -- <uri> <dir>" with the given URI and destination directory.

func (*Command) CurrentBranch

func (c *Command) CurrentBranch(ctx context.Context, dir string) (string, error)

CurrentBranch returns the currently checked-out branch in dir.

func (*Command) DiffCmd

func (c *Command) DiffCmd(interactive bool, flags ...string) []string

DiffCmd constructs argv for "git diff" with the given flags. If interactive is false, it injects "--no-pager" before "diff".

func (*Command) EnsureCloned added in v0.3.0

func (c *Command) EnsureCloned(ctx context.Context, reposRoot, uri string, stderr io.Writer) (string, error)

EnsureCloned resolves the local directory for uri under reposRoot and clones the repository if it does not already exist (as determined by IsRepo).

func (*Command) FetchCmd

func (c *Command) FetchCmd(flags ...string) []string

FetchCmd constructs argv for "git fetch" with the given flags.

func (*Command) GitBin

func (c *Command) GitBin() string

GitBin returns the git binary name/path.

func (*Command) GrepCmd

func (c *Command) GrepCmd(args ...string) []string

GrepCmd constructs argv for "git grep -H" with the given arguments.

func (*Command) IsDirty

func (c *Command) IsDirty(ctx context.Context, dir string) (bool, error)

IsDirty checks if the git repository in dir has uncommitted changes or untracked files.

func (*Command) IsMerged added in v0.4.0

func (c *Command) IsMerged(ctx context.Context, repoDir, branch, target string) (bool, error)

IsMerged reports whether branch has been merged into target in repoDir. It checks standard ancestor merges first (git merge-base --is-ancestor), and falls back to tree comparison via git merge-tree --write-tree to detect squash merges.

func (*Command) LsFilesCmd

func (c *Command) LsFilesCmd(flags ...string) []string

LsFilesCmd constructs argv for "git ls-files" with the given flags.

func (*Command) Output

func (c *Command) Output(ctx context.Context, dir string, args ...string) (string, error)

Output executes a git command in dir and returns its trimmed stdout string.

func (*Command) PullCmd

func (c *Command) PullCmd(flags ...string) []string

PullCmd constructs argv for "git pull" with the given flags.

func (*Command) RemoteURL

func (c *Command) RemoteURL(ctx context.Context, dir string) (string, error)

RemoteURL returns the remote URL for origin in dir, or an empty string if not found.

func (*Command) ResolveDefaultBranch

func (c *Command) ResolveDefaultBranch(ctx context.Context, dir string) (string, error)

ResolveDefaultBranch identifies the default branch for the repository in dir: 1. refs/remotes/origin/HEAD symbolic ref (e.g. "origin/main" -> "main") 2. init.defaultBranch config 3. refs/heads/main exists 4. refs/heads/master exists

func (*Command) Run

func (c *Command) Run(ctx context.Context, dir string, args []string, stdout, stderr io.Writer) error

Run executes a git command in dir with sub-arguments args.

func (*Command) StatusCmd

func (c *Command) StatusCmd(flags ...string) []string

StatusCmd constructs argv for "git status" with the given flags. If flags is empty, it defaults to []string{"--short"}.

func (*Command) WorktreeAdd added in v0.4.0

func (c *Command) WorktreeAdd(ctx context.Context, repoDir, wtPath, branch, newBranch string, stderr io.Writer) error

WorktreeAdd adds a worktree at wtPath for the repository in repoDir.

func (*Command) WorktreeAddCmd added in v0.4.0

func (c *Command) WorktreeAddCmd(wtPath, branch, newBranch string) []string

WorktreeAddCmd constructs argv for "git worktree add". If newBranch is non-empty, it constructs "git worktree add -b <newBranch> -- <wtPath> <branch>". Otherwise, it constructs "git worktree add -- <wtPath> <branch>".

func (*Command) WorktreePrune added in v0.4.0

func (c *Command) WorktreePrune(ctx context.Context, repoDir string, dryRun bool, stdout, stderr io.Writer) error

WorktreePrune runs "git worktree prune [--dry-run]" in repoDir.

func (*Command) WorktreePruneCmd added in v0.4.0

func (c *Command) WorktreePruneCmd(dryRun bool) []string

WorktreePruneCmd constructs argv for "git worktree prune [--dry-run]".

func (*Command) WorktreeRemove added in v0.4.0

func (c *Command) WorktreeRemove(ctx context.Context, repoDir, wtPath string, force, noForce bool, stderr io.Writer) error

WorktreeRemove removes the worktree at wtPath from the repository in repoDir.

func (*Command) WorktreeRemoveCmd added in v0.4.0

func (c *Command) WorktreeRemoveCmd(wtPath string, force, noForce bool) []string

WorktreeRemoveCmd constructs argv for "git worktree remove [--force] [--no-force] -- <wtPath>".

type Runner

type Runner interface {
	Run(ctx context.Context, dir string, command []string, stdout, stderr io.Writer) error
}

Runner executes a command in dir, writing its stdout/stderr to the provided writers.

Jump to

Keyboard shortcuts

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