command

package
v0.0.0-...-4c964c4 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NoRefUpdates denotes a command which will never update refs.
	NoRefUpdates = 1 << iota
	// NoEndOfOptions denotes a command which doesn't know --end-of-options.
	NoEndOfOptions
)
View Source
const (
	GitCommitterName  = "GIT_COMMITTER_NAME"
	GitCommitterEmail = "GIT_COMMITTER_EMAIL"
	GitCommitterDate  = "GIT_COMMITTER_DATE"
	GitAuthorName     = "GIT_AUTHOR_NAME"
	GitAuthorEmail    = "GIT_AUTHOR_EMAIL"
	GitAuthorDate     = "GIT_AUTHOR_DATE"

	GitTrace            = "GIT_TRACE"
	GitTracePack        = "GIT_TRACE_PACK_ACCESS"
	GitTracePackAccess  = "GIT_TRACE_PACKET"
	GitTracePerformance = "GIT_TRACE_PERFORMANCE"
	GitTraceSetup       = "GIT_TRACE_SETUP"
	GitExecPath         = "GIT_EXEC_PATH" // tells Git where to find its binaries.

	GitObjectDir           = "GIT_OBJECT_DIRECTORY"
	GitAlternateObjectDirs = "GIT_ALTERNATE_OBJECT_DIRECTORIES"
)

Variables

View Source
var (
	// ErrInvalidArg represent family of errors to report about bad argument used to make a call.
	ErrInvalidArg = errors.New("invalid argument")
)
View Source
var (
	GitExecutable = "git"
)

Functions

This section is empty.

Types

type CmdOptionFunc

type CmdOptionFunc func(c *Command)

func WithAction

func WithAction(action string) CmdOptionFunc

WithAction set the action of the Git command, e.g. "set-url" in `git remote set-url`.

func WithAlternateObjectDirs

func WithAlternateObjectDirs(dirs ...string) CmdOptionFunc

WithAlternateObjectDirs function sets alternates directories for object access.

func WithArg

func WithArg(args ...string) CmdOptionFunc

WithArg add arguments that shall be passed after all flags.

func WithAuthor

func WithAuthor(name, email string) CmdOptionFunc

WithAuthor sets given author to the command.

func WithAuthorAndDate

func WithAuthorAndDate(name, email string, date time.Time) CmdOptionFunc

WithAuthorAndDate sets given author and date to the command.

func WithCommitter

func WithCommitter(name, email string) CmdOptionFunc

WithCommitter sets given committer to the command.

func WithCommitterAndDate

func WithCommitterAndDate(name, email string, date time.Time) CmdOptionFunc

WithCommitterAndDate sets given committer and date to the command.

func WithConfig

func WithConfig(key, value string) CmdOptionFunc

WithConfig function sets key and value for config command.

func WithEnv

func WithEnv(keyValPairs ...string) CmdOptionFunc

WithEnv sets environment variable using key value pair for example: WithEnv("GIT_TRACE", "true").

func WithFlag

func WithFlag(flags ...string) CmdOptionFunc

WithFlag set optional flags to pass before positional arguments.

func WithGlobal

func WithGlobal(flags ...string) CmdOptionFunc

WithGlobal set the global optional flag of the Git command.

func WithPostSepArg

func WithPostSepArg(args ...string) CmdOptionFunc

WithPostSepArg set arguments that shall be passed as positional arguments after the `--`.

type Command

type Command struct {
	// Globals is the number of optional flags to pass before command name.
	// example: git --shallow-file pack-objects ...
	Globals []string

	// Name is the name of the Git command to run, e.g. "log", "cat-file" or "worktree".
	Name string

	// Action is the action of the Git command, e.g. "set-url" in `git remote set-url`
	Action string

	// Flags is the number of optional flags to pass before positional arguments, e.g.
	// `--oneline` or `--format=fuller`.
	Flags []string

	// Args is the arguments that shall be passed after all flags. These arguments must not be
	// flags and thus cannot start with `-`. Note that it may be unsafe to use this field in the
	// case where arguments are directly user-controlled. In that case it is advisable to use
	// `PostSepArgs` instead.
	Args []string

	// PostSepArgs is the arguments that shall be passed as positional arguments after the `--`
	// separator. Git recognizes that separator as the point where it should stop expecting any
	// options and treat the remaining arguments as positionals. This should be used when
	// passing user-controlled input of arbitrary form like for example paths, which may start
	// with a `-`.
	PostSepArgs []string

	// Git environment variables
	Envs Envs
	// contains filtered or unexported fields
}

Command contains options for running a git command.

func New

func New(name string, options ...CmdOptionFunc) *Command

New creates new command for interacting with the git process.

func Parse

func Parse(args ...string) *Command

Parse os args to Command object. This is very basic parser which doesn't care about flags or positional args values it just injects into proper slice of command struct. Every git command can contain globals:

git --help

command:

git version
git diff

action:

git remote set-url ...

command or action flags:

git diff --shortstat

command or action args:

git diff --shortstat main...dev

post args:

git diff main...dev -- file1

func (*Command) Add

func (c *Command) Add(options ...CmdOptionFunc) *Command

Add appends given options to the command.

func (*Command) Clone

func (c *Command) Clone() *Command

Clone clones the command object.

func (*Command) Run

func (c *Command) Run(ctx context.Context, opts ...RunOptionFunc) (err error)

Run executes the git command with optional configuration using WithXxx functions.

type Envs

type Envs map[string]string

Envs custom key value store for environment variables.

func (Envs) Args

func (e Envs) Args() []string

type Error

type Error struct {
	Err    error
	StdErr []byte
}

Error type with optional ExitCode and Stderr payload.

func AsError

func AsError(err error) (e *Error)

AsError unwraps Error otherwise return nil.

func NewError

func NewError(err error, stderr []byte) *Error

NewError creates error with source err and stderr payload.

func (*Error) Error

func (e *Error) Error() string

func (*Error) ExitCode

func (e *Error) ExitCode() int

func (*Error) IsAmbiguousArgErr

func (e *Error) IsAmbiguousArgErr() bool

func (*Error) IsExitCode

func (e *Error) IsExitCode(code int) bool

func (*Error) IsInvalidRefErr

func (e *Error) IsInvalidRefErr() bool

func (*Error) Unwrap

func (e *Error) Unwrap() error

type RunOption

type RunOption struct {
	// Dir is location of repo.
	Dir string
	// Stdin is the input to the command.
	Stdin io.Reader
	// Stdout is the outputs from the command.
	Stdout io.Writer
	// Stderr is the error output from the command.
	Stderr io.Writer
	// Envs is environments slice containing (final) immutable
	// environment pair "ENV=value"
	Envs []string
}

RunOption contains option for running a command.

type RunOptionFunc

type RunOptionFunc func(option *RunOption)

func WithDir

func WithDir(dir string) RunOptionFunc

WithDir set directory RunOption.Dir, this is repository dir where git command should be running.

func WithEnvs

func WithEnvs(envs ...string) RunOptionFunc

WithEnvs sets immutable values as slice, it is always added et the end of env slice.

func WithStderr

func WithStderr(stderr io.Writer) RunOptionFunc

WithStderr set RunOption.Stderr writer.

func WithStdin

func WithStdin(stdin io.Reader) RunOptionFunc

WithStdin set RunOption.Stdin reader.

func WithStdout

func WithStdout(stdout io.Writer) RunOptionFunc

WithStdout set RunOption.Stdout writer.

Jump to

Keyboard shortcuts

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