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

git

package
v0.1.0-alpha.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 6 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

This section is empty.

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) 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) 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) 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) 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"}.

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