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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // NewCommandNoGlobals creates and returns a new Git Command based on given command and arguments only with the specify args and don't care global command args
  46. func NewCommandNoGlobals(args ...string) *Command {
  47. return &Command{
  48. name: GitExecutable,
  49. args: args,
  50. }
  51. }
  52. // AddArguments adds new argument(s) to the command.
  53. func (c *Command) AddArguments(args ...string) *Command {
  54. c.args = append(c.args, args...)
  55. return c
  56. }
  57. // RunInDirTimeoutEnvPipeline executes the command in given directory with given timeout,
  58. // it pipes stdout and stderr to given io.Writer.
  59. func (c *Command) RunInDirTimeoutEnvPipeline(env []string, timeout time.Duration, dir string, stdout, stderr io.Writer) error {
  60. return c.RunInDirTimeoutEnvFullPipeline(env, timeout, dir, stdout, stderr, nil)
  61. }
  62. // RunInDirTimeoutEnvFullPipeline executes the command in given directory with given timeout,
  63. // it pipes stdout and stderr to given io.Writer and passes in an io.Reader as stdin.
  64. func (c *Command) RunInDirTimeoutEnvFullPipeline(env []string, timeout time.Duration, dir string, stdout, stderr io.Writer, stdin io.Reader) error {
  65. return c.RunInDirTimeoutEnvFullPipelineFunc(env, timeout, dir, stdout, stderr, stdin, nil)
  66. }
  67. // RunInDirTimeoutEnvFullPipelineFunc executes the command in given directory with given timeout,
  68. // 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.
  69. 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 {
  70. if timeout == -1 {
  71. timeout = DefaultCommandExecutionTimeout
  72. }
  73. if len(dir) == 0 {
  74. log(c.String())
  75. } else {
  76. log("%s: %v", dir, c)
  77. }
  78. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  79. defer cancel()
  80. cmd := exec.CommandContext(ctx, c.name, c.args...)
  81. if env == nil {
  82. cmd.Env = append(os.Environ(), fmt.Sprintf("LC_ALL=%s", DefaultLocale))
  83. } else {
  84. cmd.Env = env
  85. cmd.Env = append(cmd.Env, fmt.Sprintf("LC_ALL=%s", DefaultLocale))
  86. }
  87. cmd.Dir = dir
  88. cmd.Stdout = stdout
  89. cmd.Stderr = stderr
  90. cmd.Stdin = stdin
  91. if err := cmd.Start(); err != nil {
  92. return err
  93. }
  94. pid := process.GetManager().Add(fmt.Sprintf("%s %s %s [repo_path: %s]", GitExecutable, c.name, strings.Join(c.args, " "), dir), cmd)
  95. defer process.GetManager().Remove(pid)
  96. if fn != nil {
  97. fn(ctx, cancel)
  98. }
  99. if err := cmd.Wait(); err != nil {
  100. return err
  101. }
  102. return ctx.Err()
  103. }
  104. // RunInDirTimeoutPipeline executes the command in given directory with given timeout,
  105. // it pipes stdout and stderr to given io.Writer.
  106. func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, stdout, stderr io.Writer) error {
  107. return c.RunInDirTimeoutEnvPipeline(nil, timeout, dir, stdout, stderr)
  108. }
  109. // RunInDirTimeoutFullPipeline executes the command in given directory with given timeout,
  110. // it pipes stdout and stderr to given io.Writer, and stdin from the given io.Reader
  111. func (c *Command) RunInDirTimeoutFullPipeline(timeout time.Duration, dir string, stdout, stderr io.Writer, stdin io.Reader) error {
  112. return c.RunInDirTimeoutEnvFullPipeline(nil, timeout, dir, stdout, stderr, stdin)
  113. }
  114. // RunInDirTimeout executes the command in given directory with given timeout,
  115. // and returns stdout in []byte and error (combined with stderr).
  116. func (c *Command) RunInDirTimeout(timeout time.Duration, dir string) ([]byte, error) {
  117. return c.RunInDirTimeoutEnv(nil, timeout, dir)
  118. }
  119. // RunInDirTimeoutEnv executes the command in given directory with given timeout,
  120. // and returns stdout in []byte and error (combined with stderr).
  121. func (c *Command) RunInDirTimeoutEnv(env []string, timeout time.Duration, dir string) ([]byte, error) {
  122. stdout := new(bytes.Buffer)
  123. stderr := new(bytes.Buffer)
  124. if err := c.RunInDirTimeoutEnvPipeline(env, timeout, dir, stdout, stderr); err != nil {
  125. return nil, concatenateError(err, stderr.String())
  126. }
  127. if stdout.Len() > 0 {
  128. log("stdout:\n%s", stdout.Bytes()[:1024])
  129. }
  130. return stdout.Bytes(), nil
  131. }
  132. // RunInDirPipeline executes the command in given directory,
  133. // it pipes stdout and stderr to given io.Writer.
  134. func (c *Command) RunInDirPipeline(dir string, stdout, stderr io.Writer) error {
  135. return c.RunInDirFullPipeline(dir, stdout, stderr, nil)
  136. }
  137. // RunInDirFullPipeline executes the command in given directory,
  138. // it pipes stdout and stderr to given io.Writer.
  139. func (c *Command) RunInDirFullPipeline(dir string, stdout, stderr io.Writer, stdin io.Reader) error {
  140. return c.RunInDirTimeoutFullPipeline(-1, dir, stdout, stderr, stdin)
  141. }
  142. // RunInDirBytes executes the command in given directory
  143. // and returns stdout in []byte and error (combined with stderr).
  144. func (c *Command) RunInDirBytes(dir string) ([]byte, error) {
  145. return c.RunInDirTimeout(-1, dir)
  146. }
  147. // RunInDir executes the command in given directory
  148. // and returns stdout in string and error (combined with stderr).
  149. func (c *Command) RunInDir(dir string) (string, error) {
  150. return c.RunInDirWithEnv(dir, nil)
  151. }
  152. // RunInDirWithEnv executes the command in given directory
  153. // and returns stdout in string and error (combined with stderr).
  154. func (c *Command) RunInDirWithEnv(dir string, env []string) (string, error) {
  155. stdout, err := c.RunInDirTimeoutEnv(env, -1, dir)
  156. if err != nil {
  157. return "", err
  158. }
  159. return string(stdout), nil
  160. }
  161. // RunTimeout executes the command in default working directory with given timeout,
  162. // and returns stdout in string and error (combined with stderr).
  163. func (c *Command) RunTimeout(timeout time.Duration) (string, error) {
  164. stdout, err := c.RunInDirTimeout(timeout, "")
  165. if err != nil {
  166. return "", err
  167. }
  168. return string(stdout), nil
  169. }
  170. // Run executes the command in default working directory
  171. // and returns stdout in string and error (combined with stderr).
  172. func (c *Command) Run() (string, error) {
  173. return c.RunTimeout(-1)
  174. }