gitcmd

package
v3.1.32 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultLocale = "C"

DefaultLocale is the default LC_ALL to run git commands in.

Variables

View Source
var ErrBrokenCommand = errors.New("git command is broken")
View Source
var GitExecutable = "git" // the command name of git, will be updated to an absolute path during initialization

Functions

func CommonCmdServEnvs

func CommonCmdServEnvs() []string

CommonCmdServEnvs is like CommonGitCmdEnvs, but it only returns minimal required environment variables for the "gitea serv" command

func CommonGitCmdEnvs

func CommonGitCmdEnvs() []string

CommonGitCmdEnvs returns the common environment variables for a "git" command.

func ConcatenateError

func ConcatenateError(err error, stderr string) error

ConcatenateError concatenats an error with stderr string

func HomeDir

func HomeDir() string

HomeDir is the home dir for git to store the global config file used by Gitea internally

func IsErrorExitCode

func IsErrorExitCode(err error, code int) bool

func SetDefaultCommandExecutionTimeout

func SetDefaultCommandExecutionTimeout(timeout time.Duration)

func SetExecutablePath

func SetExecutablePath(path string) error

SetExecutablePath changes the path of git executable and checks the file permission and version.

Types

type Command

type Command struct {
	// contains filtered or unexported fields
}

Command represents a command with its subcommands or arguments.

func NewCommand

func NewCommand(args ...internal.CmdArg) *Command

NewCommand creates and returns a new Git Command based on given command and arguments. Each argument should be safe to be trusted. User-provided arguments should be passed to AddDynamicArguments instead.

func (*Command) AddArguments

func (c *Command) AddArguments(args ...internal.CmdArg) *Command

AddArguments adds new git arguments (option/value) to the command. It only accepts string literals, or trusted CmdArg. Type CmdArg is in the internal package, so it can not be used outside of this package directly, it makes sure that user-provided arguments won't cause RCE risks. User-provided arguments should be passed by other AddXxx functions

func (*Command) AddConfig

func (c *Command) AddConfig(key, value string) *Command

func (*Command) AddDashesAndList

func (c *Command) AddDashesAndList(list ...string) *Command

AddDashesAndList adds the "--" and then add the list as arguments, it's usually for adding file list At the moment, this function can be only called once, maybe in future it can be refactored to support multiple calls (if necessary)

func (*Command) AddDynamicArguments

func (c *Command) AddDynamicArguments(args ...string) *Command

AddDynamicArguments adds new dynamic argument values to the command. The arguments may come from user input and can not be trusted, so no leading '-' is allowed to avoid passing options. TODO: in the future, this function can be renamed to AddArgumentValues

func (*Command) AddOptionFormat

func (c *Command) AddOptionFormat(opt string, args ...any) *Command

AddOptionFormat adds a new option with a format string and arguments For example: AddOptionFormat("--opt=%s %s", val1, val2) means 1 argument: {"--opt=val1 val2"}.

func (*Command) AddOptionValues

func (c *Command) AddOptionValues(opt internal.CmdArg, args ...string) *Command

AddOptionValues adds a new option with a list of non-option values For example: AddOptionValues("--opt", val) means 2 arguments: {"--opt", val}. The values are treated as dynamic argument values. It equals to: AddArguments("--opt") then AddDynamicArguments(val).

func (*Command) LogString

func (c *Command) LogString() string

func (*Command) ProcessState

func (c *Command) ProcessState() string

func (*Command) Run

func (c *Command) Run(ctx context.Context) error

Run runs the command

func (*Command) RunStdBytes

func (c *Command) RunStdBytes(ctx context.Context) (stdout, stderr []byte, runErr RunStdError)

RunStdBytes runs the command and returns stdout/stderr as bytes. and store stderr to returned error (err combined with stderr).

func (*Command) RunStdString

func (c *Command) RunStdString(ctx context.Context) (stdout, stderr string, runErr RunStdError)

RunStdString runs the command and returns stdout/stderr as string. and store stderr to returned error (err combined with stderr).

func (*Command) WithDir

func (c *Command) WithDir(dir string) *Command

func (*Command) WithEnv

func (c *Command) WithEnv(env []string) *Command

func (*Command) WithParentCallerInfo

func (c *Command) WithParentCallerInfo(optInfo ...string) *Command

WithParentCallerInfo can be used to set the caller info (usually function name) of the parent function of the caller. For most cases, "Run" family functions can get its caller info automatically But if you need to call "Run" family functions in a wrapper function: "FeatureFunc -> GeneralWrapperFunc -> RunXxx", then you can to call this function in GeneralWrapperFunc to set the caller info of FeatureFunc. The caller info can only be set once.

func (*Command) WithPipelineFunc

func (c *Command) WithPipelineFunc(f func(context.Context, context.CancelFunc) error) *Command

func (*Command) WithStderr

func (c *Command) WithStderr(stderr io.Writer) *Command

func (*Command) WithStdin

func (c *Command) WithStdin(stdin io.Reader) *Command

func (*Command) WithStdout

func (c *Command) WithStdout(stdout io.Writer) *Command

func (*Command) WithTimeout

func (c *Command) WithTimeout(timeout time.Duration) *Command

func (*Command) WithUseContextTimeout

func (c *Command) WithUseContextTimeout(useContextTimeout bool) *Command

type RunStdError

type RunStdError interface {
	error
	Unwrap() error
	Stderr() string
}

type TrustedCmdArgs

type TrustedCmdArgs []internal.CmdArg

TrustedCmdArgs returns the trusted arguments for git command. It's mainly for passing user-provided and trusted arguments to git command In most cases, it shouldn't be used. Use AddXxx function instead

func ToTrustedCmdArgs

func ToTrustedCmdArgs(args []string) TrustedCmdArgs

ToTrustedCmdArgs converts a list of strings (trusted as argument) to TrustedCmdArgs In most cases, it shouldn't be used. Use NewCommand().AddXxx() function instead

Source Files

  • command.go
  • env.go
  • utils.go

Jump to

Keyboard shortcuts

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