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

gitrun

package
v1.3.15 Latest Latest
Warning

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

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

Documentation

Overview

Package gitrun is the one runner for a one-shot git child.

Every git this repository starts for an answer goes through here: it is bounded by the caller's context or by a named default (subproc.GitBudget, 60 s; subproc.GitLongBudget, 300 s for a command that goes to the network), it carries WaitDelay so a killed git cannot hang its caller on the pipe that git's own child (an ssh, a credential helper, a pager) holds open, and it reports a kill as a *subproc.TimeoutError. The environment is the caller's choice: a caller that scrubs it passes the scrubbed list in Options.Env, and a caller that must leave it intact (a hook that was handed GIT_INDEX_FILE) leaves Env nil. A caller whose repository is the one it names with C (a tool reading its own store while a git that called it exported GIT_DIR) sets OwnRepo, which drops the variables that say where a repository is.

Index

Constants

View Source
const DefaultGitIdentity = "nova-sprint <nova-sprint@example.invalid>"

DefaultGitIdentity is the identity the sprint machine commits as when the sprint row names none: a generic machine identity, which the coordinator sets to the fleet's own.

View Source
const DefaultTimeout = subproc.GitBudget

DefaultTimeout is how long one local git may run when the caller's context has no sooner deadline and Options.Timeout is zero; a network command gets subproc.GitLongBudget (see subproc.GitBudgetFor).

Variables

View Source
var RepoVars = []string{
	"GIT_DIR", "GIT_WORK_TREE", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES",
	"GIT_INDEX_FILE", "GIT_COMMON_DIR", "GIT_NAMESPACE", "GIT_PREFIX", "GIT_CEILING_DIRECTORIES",
}

RepoVars are the variables with which git is told where a repository, its objects, its index or its work tree are. A git that starts a helper (a credential helper, a hook) exports them, and a git the helper starts reads the helper's repository, not its own.

Functions

func Combined

func Combined(ctx context.Context, o Options, args ...string) ([]byte, error)

Combined runs git and returns its stdout and stderr interleaved, as one stream.

func Command

func Command(ctx context.Context, o Options, args ...string) (*exec.Cmd, context.CancelFunc)

Command builds the git child: -C, directory, environment, deadline and WaitDelay applied, nothing started. The returned cancel is called once the child is waited for.

func Output

func Output(ctx context.Context, o Options, args ...string) (string, error)

Output runs git and returns its stdout with its leading and trailing blanks trimmed. A failure is an *Error (which unwraps to the cause, so a *subproc.TimeoutError is still found).

func Prepare

func Prepare(ctx context.Context, o Options, args ...string) subproc.Bounded

Prepare is Command for a caller that reads the stream itself and must tell a deadline kill from its own failure: the Bounded carries the bound context, whose subproc.Expired says the deadline ended the child.

func WithoutRepoVars

func WithoutRepoVars(env []string) []string

WithoutRepoVars returns env without the RepoVars; every other entry, GIT_TERMINAL_PROMPT among them, is kept in order. The argument is not changed.

Types

type Error

type Error struct {
	Args   []string
	Err    error
	Stderr string
}

Error is a failed git as Output reports it: the command, the cause and git's own stderr.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type GitIdentity added in v1.3.14

type GitIdentity struct {
	Name, Email string
}

GitIdentity is one parsed identity: the name and email of "Name <email>".

func ParseGitIdentity added in v1.3.14

func ParseGitIdentity(v string) (GitIdentity, bool)

ParseGitIdentity parses a git identity as "Name <email>": the name is everything before the last " <", the email the text between the last "<" and ">". ok is false when the value is empty or does not spell a name and an email.

func (GitIdentity) Env added in v1.3.14

func (id GitIdentity) Env() []string

Env is the GIT_AUTHOR_*/GIT_COMMITTER_* environment this identity sets, so a commit takes the configured name and email whatever the host's global git config or the caller's environment held.

type Options

type Options struct {
	// Bin is the git program; empty is "git" on PATH.
	Bin string
	// C, when set, is passed as `git -C C`.
	C string
	// Dir, when set, is the child's working directory.
	Dir string
	// Env is the child's whole environment; nil inherits the caller's.
	Env []string
	// OwnRepo says the repository is the one C names, whatever the caller's environment
	// says: the variables in RepoVars are dropped from the child's environment (Env, or the
	// caller's when Env is nil) and every other variable is kept.
	OwnRepo bool
	// Stdin, when set, is the child's standard input.
	Stdin io.Reader
	// Timeout overrides the default budget when positive.
	Timeout time.Duration
	// WaitDelay overrides subproc.WaitDelay when positive.
	WaitDelay time.Duration
}

Options is how one git runs.

type Result

type Result struct {
	Stdout, Stderr []byte
}

Result is the two streams of one finished git.

func Run

func Run(ctx context.Context, o Options, args ...string) (Result, error)

Run runs git and returns its stdout and stderr apart. A git killed at its deadline returns a *subproc.TimeoutError.

Jump to

Keyboard shortcuts

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