gitcmd

package
v1.25.0-rc0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2025 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, opts *RunOpts) error

Run runs the command with the RunOpts

func (*Command) RunStdBytes

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

RunStdBytes runs the command with options 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, opts *RunOpts) (stdout, stderr string, runErr RunStdError)

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

type RunOpts

type RunOpts struct {
	Env               []string
	Timeout           time.Duration
	UseContextTimeout bool

	// Dir is the working dir for the git command, however:
	// FIXME: this could be incorrect in many cases, for example:
	// * /some/path/.git
	// * /some/path/.git/gitea-data/data/repositories/user/repo.git
	// If "user/repo.git" is invalid/broken, then running git command in it will use "/some/path/.git", and produce unexpected results
	// The correct approach is to use `--git-dir" global argument
	Dir string

	Stdout, Stderr io.Writer

	// Stdin is used for passing input to the command
	// The caller must make sure the Stdin writer is closed properly to finish the Run function.
	// Otherwise, the Run function may hang for long time or forever, especially when the Git's context deadline is not the same as the caller's.
	// Some common mistakes:
	// * `defer stdinWriter.Close()` then call `cmd.Run()`: the Run() would never return if the command is killed by timeout
	// * `go { case <- parentContext.Done(): stdinWriter.Close() }` with `cmd.Run(DefaultTimeout)`: the command would have been killed by timeout but the Run doesn't return until stdinWriter.Close()
	// * `go { if stdoutReader.Read() err != nil: stdinWriter.Close() }` with `cmd.Run()`: the stdoutReader may never return error if the command is killed by timeout
	// In the future, ideally the git module itself should have full control of the stdin, to avoid such problems and make it easier to refactor to a better architecture.
	Stdin io.Reader

	PipelineFunc func(context.Context, context.CancelFunc) error
}

RunOpts represents parameters to run the command. If UseContextTimeout is specified, then Timeout is ignored.

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

Jump to

Keyboard shortcuts

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