You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

command.go 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "bytes"
  7. "context"
  8. "fmt"
  9. "io"
  10. "os"
  11. "os/exec"
  12. "strings"
  13. "time"
  14. "code.gitea.io/gitea/modules/process"
  15. )
  16. var (
  17. // GlobalCommandArgs global command args for external package setting
  18. GlobalCommandArgs []string
  19. // DefaultCommandExecutionTimeout default command execution timeout duration
  20. DefaultCommandExecutionTimeout = 60 * time.Second
  21. )
  22. // DefaultLocale is the default LC_ALL to run git commands in.
  23. const DefaultLocale = "C"
  24. // Command represents a command with its subcommands or arguments.
  25. type Command struct {
  26. name string
  27. args []string
  28. }
  29. func (c *Command) String() string {
  30. if len(c.args) == 0 {
  31. return c.name
  32. }
  33. return fmt.Sprintf("%s %s", c.name, strings.Join(c.args, " "))
  34. }
  35. // NewCommand creates and returns a new Git Command based on given command and arguments.
  36. func NewCommand(args ...string) *Command {
  37. // Make an explicit copy of GlobalCommandArgs, otherwise append might overwrite it
  38. cargs := make([]string, len(GlobalCommandArgs))
  39. copy(cargs, GlobalCommandArgs)
  40. return &Command{
  41. name: GitExecutable,
  42. args: append(cargs, args...),
  43. }
  44. }
  45. // AddArguments adds new argument(s) to the command.
  46. func (c *Command) AddArguments(args ...string) *Command {
  47. c.args = append(c.args, args...)
  48. return c
  49. }
  50. // RunInDirTimeoutEnvPipeline executes the command in given directory with given timeout,
  51. // it pipes stdout and stderr to given io.Writer.
  52. func (c *Command) RunInDirTimeoutEnvPipeline(env []string, timeout time.Duration, dir string, stdout, stderr io.Writer) error {
  53. return c.RunInDirTimeoutEnvFullPipeline(env, timeout, dir, stdout, stderr, nil)
  54. }
  55. // RunInDirTimeoutEnvFullPipeline executes the command in given directory with given timeout,
  56. // it pipes stdout and stderr to given io.Writer and passes in an io.Reader as stdin.
  57. func (c *Command) RunInDirTimeoutEnvFullPipeline(env []string, timeout time.Duration, dir string, stdout, stderr io.Writer, stdin io.Reader) error {
  58. return c.RunInDirTimeoutEnvFullPipelineFunc(env, timeout, dir, stdout, stderr, stdin, nil)
  59. }
  60. // RunInDirTimeoutEnvFullPipelineFunc executes the command in given directory with given timeout,
  61. // it pipes stdout and stderr to given io.Writer and passes in an io.Reader as stdin. Between cmd.Start and cmd.Wait the passed in function is run.
  62. func (c *Command) RunInDirTimeoutEnvFullPipelineFunc(env []string, timeout time.Duration, dir string, stdout, stderr io.Writer, stdin io.Reader, fn func(context.Context, context.CancelFunc)) error {
  63. if timeout == -1 {
  64. timeout = DefaultCommandExecutionTimeout
  65. }
  66. if len(dir) == 0 {
  67. log(c.String())
  68. } else {
  69. log("%s: %v", dir, c)
  70. }
  71. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  72. defer cancel()
  73. cmd := exec.CommandContext(ctx, c.name, c.args...)
  74. if env == nil {
  75. cmd.Env = append(os.Environ(), fmt.Sprintf("LC_ALL=%s", DefaultLocale))
  76. } else {
  77. cmd.Env = env
  78. cmd.Env = append(cmd.Env, fmt.Sprintf("LC_ALL=%s", DefaultLocale))
  79. }
  80. cmd.Dir = dir
  81. cmd.Stdout = stdout
  82. cmd.Stderr = stderr
  83. cmd.Stdin = stdin
  84. if err := cmd.Start(); err != nil {
  85. return err
  86. }
  87. pid := process.GetManager().Add(fmt.Sprintf("%s %s %s [repo_path: %s]", GitExecutable, c.name, strings.Join(c.args, " "), dir), cmd)
  88. defer process.GetManager().Remove(pid)
  89. if fn != nil {
  90. fn(ctx, cancel)
  91. }
  92. if err := cmd.Wait(); err != nil {
  93. return err
  94. }
  95. return ctx.Err()
  96. }
  97. // RunInDirTimeoutPipeline executes the command in given directory with given timeout,
  98. // it pipes stdout and stderr to given io.Writer.
  99. func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, stdout, stderr io.Writer) error {
  100. return c.RunInDirTimeoutEnvPipeline(nil, timeout, dir, stdout, stderr)
  101. }
  102. // RunInDirTimeoutFullPipeline executes the command in given directory with given timeout,
  103. // it pipes stdout and stderr to given io.Writer, and stdin from the given io.Reader
  104. func (c *Command) RunInDirTimeoutFullPipeline(timeout time.Duration, dir string, stdout, stderr io.Writer, stdin io.Reader) error {
  105. return c.RunInDirTimeoutEnvFullPipeline(nil, timeout, dir, stdout, stderr, stdin)
  106. }
  107. // RunInDirTimeout executes the command in given directory with given timeout,
  108. // and returns stdout in []byte and error (combined with stderr).
  109. func (c *Command) RunInDirTimeout(timeout time.Duration, dir string) ([]byte, error) {
  110. return c.RunInDirTimeoutEnv(nil, timeout, dir)
  111. }
  112. // RunInDirTimeoutEnv executes the command in given directory with given timeout,
  113. // and returns stdout in []byte and error (combined with stderr).
  114. func (c *Command) RunInDirTimeoutEnv(env []string, timeout time.Duration, dir string) ([]byte, error) {
  115. stdout := new(bytes.Buffer)
  116. stderr := new(bytes.Buffer)
  117. if err := c.RunInDirTimeoutEnvPipeline(env, timeout, dir, stdout, stderr); err != nil {
  118. return nil, concatenateError(err, stderr.String())
  119. }
  120. if stdout.Len() > 0 {
  121. log("stdout:\n%s", stdout.Bytes()[:1024])
  122. }
  123. return stdout.Bytes(), nil
  124. }
  125. // RunInDirPipeline executes the command in given directory,
  126. // it pipes stdout and stderr to given io.Writer.
  127. func (c *Command) RunInDirPipeline(dir string, stdout, stderr io.Writer) error {
  128. return c.RunInDirFullPipeline(dir, stdout, stderr, nil)
  129. }
  130. // RunInDirFullPipeline executes the command in given directory,
  131. // it pipes stdout and stderr to given io.Writer.
  132. func (c *Command) RunInDirFullPipeline(dir string, stdout, stderr io.Writer, stdin io.Reader) error {
  133. return c.RunInDirTimeoutFullPipeline(-1, dir, stdout, stderr, stdin)
  134. }
  135. // RunInDirBytes executes the command in given directory
  136. // and returns stdout in []byte and error (combined with stderr).
  137. func (c *Command) RunInDirBytes(dir string) ([]byte, error) {
  138. return c.RunInDirTimeout(-1, dir)
  139. }
  140. // RunInDir executes the command in given directory
  141. // and returns stdout in string and error (combined with stderr).
  142. func (c *Command) RunInDir(dir string) (string, error) {
  143. return c.RunInDirWithEnv(dir, nil)
  144. }
  145. // RunInDirWithEnv executes the command in given directory
  146. // and returns stdout in string and error (combined with stderr).
  147. func (c *Command) RunInDirWithEnv(dir string, env []string) (string, error) {
  148. stdout, err := c.RunInDirTimeoutEnv(env, -1, dir)
  149. if err != nil {
  150. return "", err
  151. }
  152. return string(stdout), nil
  153. }
  154. // RunTimeout executes the command in default working directory with given timeout,
  155. // and returns stdout in string and error (combined with stderr).
  156. func (c *Command) RunTimeout(timeout time.Duration) (string, error) {
  157. stdout, err := c.RunInDirTimeout(timeout, "")
  158. if err != nil {
  159. return "", err
  160. }
  161. return string(stdout), nil
  162. }
  163. // Run executes the command in default working directory
  164. // and returns stdout in string and error (combined with stderr).
  165. func (c *Command) Run() (string, error) {
  166. return c.RunTimeout(-1)
  167. }