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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 = 360 * 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. parentContext context.Context
  29. desc string
  30. }
  31. func (c *Command) String() string {
  32. if len(c.args) == 0 {
  33. return c.name
  34. }
  35. return fmt.Sprintf("%s %s", c.name, strings.Join(c.args, " "))
  36. }
  37. // NewCommand creates and returns a new Git Command based on given command and arguments.
  38. func NewCommand(args ...string) *Command {
  39. return NewCommandContext(DefaultContext, args...)
  40. }
  41. // NewCommandContext creates and returns a new Git Command based on given command and arguments.
  42. func NewCommandContext(ctx context.Context, args ...string) *Command {
  43. // Make an explicit copy of GlobalCommandArgs, otherwise append might overwrite it
  44. cargs := make([]string, len(GlobalCommandArgs))
  45. copy(cargs, GlobalCommandArgs)
  46. return &Command{
  47. name: GitExecutable,
  48. args: append(cargs, args...),
  49. parentContext: ctx,
  50. }
  51. }
  52. // 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
  53. func NewCommandNoGlobals(args ...string) *Command {
  54. return NewCommandContextNoGlobals(DefaultContext, args...)
  55. }
  56. // NewCommandContextNoGlobals 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
  57. func NewCommandContextNoGlobals(ctx context.Context, args ...string) *Command {
  58. return &Command{
  59. name: GitExecutable,
  60. args: args,
  61. parentContext: ctx,
  62. }
  63. }
  64. // SetParentContext sets the parent context for this command
  65. func (c *Command) SetParentContext(ctx context.Context) *Command {
  66. c.parentContext = ctx
  67. return c
  68. }
  69. // SetDescription sets the description for this command which be returned on
  70. // c.String()
  71. func (c *Command) SetDescription(desc string) *Command {
  72. c.desc = desc
  73. return c
  74. }
  75. // AddArguments adds new argument(s) to the command.
  76. func (c *Command) AddArguments(args ...string) *Command {
  77. c.args = append(c.args, args...)
  78. return c
  79. }
  80. // RunInDirTimeoutEnvPipeline executes the command in given directory with given timeout,
  81. // it pipes stdout and stderr to given io.Writer.
  82. func (c *Command) RunInDirTimeoutEnvPipeline(env []string, timeout time.Duration, dir string, stdout, stderr io.Writer) error {
  83. return c.RunInDirTimeoutEnvFullPipeline(env, timeout, dir, stdout, stderr, nil)
  84. }
  85. // RunInDirTimeoutEnvFullPipeline executes the command in given directory with given timeout,
  86. // it pipes stdout and stderr to given io.Writer and passes in an io.Reader as stdin.
  87. func (c *Command) RunInDirTimeoutEnvFullPipeline(env []string, timeout time.Duration, dir string, stdout, stderr io.Writer, stdin io.Reader) error {
  88. return c.RunInDirTimeoutEnvFullPipelineFunc(env, timeout, dir, stdout, stderr, stdin, nil)
  89. }
  90. // RunInDirTimeoutEnvFullPipelineFunc executes the command in given directory with given timeout,
  91. // 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.
  92. 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) error {
  93. if timeout == -1 {
  94. timeout = DefaultCommandExecutionTimeout
  95. }
  96. if len(dir) == 0 {
  97. log(c.String())
  98. } else {
  99. log("%s: %v", dir, c)
  100. }
  101. ctx, cancel := context.WithTimeout(c.parentContext, timeout)
  102. defer cancel()
  103. cmd := exec.CommandContext(ctx, c.name, c.args...)
  104. if env == nil {
  105. cmd.Env = append(os.Environ(), fmt.Sprintf("LC_ALL=%s", DefaultLocale))
  106. } else {
  107. cmd.Env = env
  108. cmd.Env = append(cmd.Env, fmt.Sprintf("LC_ALL=%s", DefaultLocale))
  109. }
  110. cmd.Env = append(cmd.Env, "GODEBUG=asyncpreemptoff=1")
  111. cmd.Dir = dir
  112. cmd.Stdout = stdout
  113. cmd.Stderr = stderr
  114. cmd.Stdin = stdin
  115. if err := cmd.Start(); err != nil {
  116. return err
  117. }
  118. desc := c.desc
  119. if desc == "" {
  120. desc = fmt.Sprintf("%s %s %s [repo_path: %s]", GitExecutable, c.name, strings.Join(c.args, " "), dir)
  121. }
  122. pid := process.GetManager().Add(desc, cancel)
  123. defer process.GetManager().Remove(pid)
  124. if fn != nil {
  125. err := fn(ctx, cancel)
  126. if err != nil {
  127. cancel()
  128. return err
  129. }
  130. }
  131. if err := cmd.Wait(); err != nil && ctx.Err() != context.DeadlineExceeded {
  132. return err
  133. }
  134. return ctx.Err()
  135. }
  136. // RunInDirTimeoutPipeline executes the command in given directory with given timeout,
  137. // it pipes stdout and stderr to given io.Writer.
  138. func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, stdout, stderr io.Writer) error {
  139. return c.RunInDirTimeoutEnvPipeline(nil, timeout, dir, stdout, stderr)
  140. }
  141. // RunInDirTimeoutFullPipeline executes the command in given directory with given timeout,
  142. // it pipes stdout and stderr to given io.Writer, and stdin from the given io.Reader
  143. func (c *Command) RunInDirTimeoutFullPipeline(timeout time.Duration, dir string, stdout, stderr io.Writer, stdin io.Reader) error {
  144. return c.RunInDirTimeoutEnvFullPipeline(nil, timeout, dir, stdout, stderr, stdin)
  145. }
  146. // RunInDirTimeout executes the command in given directory with given timeout,
  147. // and returns stdout in []byte and error (combined with stderr).
  148. func (c *Command) RunInDirTimeout(timeout time.Duration, dir string) ([]byte, error) {
  149. return c.RunInDirTimeoutEnv(nil, timeout, dir)
  150. }
  151. // RunInDirTimeoutEnv executes the command in given directory with given timeout,
  152. // and returns stdout in []byte and error (combined with stderr).
  153. func (c *Command) RunInDirTimeoutEnv(env []string, timeout time.Duration, dir string) ([]byte, error) {
  154. stdout := new(bytes.Buffer)
  155. stderr := new(bytes.Buffer)
  156. if err := c.RunInDirTimeoutEnvPipeline(env, timeout, dir, stdout, stderr); err != nil {
  157. return nil, concatenateError(err, stderr.String())
  158. }
  159. if stdout.Len() > 0 {
  160. log("stdout:\n%s", stdout.Bytes()[:1024])
  161. }
  162. return stdout.Bytes(), nil
  163. }
  164. // RunInDirPipeline executes the command in given directory,
  165. // it pipes stdout and stderr to given io.Writer.
  166. func (c *Command) RunInDirPipeline(dir string, stdout, stderr io.Writer) error {
  167. return c.RunInDirFullPipeline(dir, stdout, stderr, nil)
  168. }
  169. // RunInDirFullPipeline executes the command in given directory,
  170. // it pipes stdout and stderr to given io.Writer.
  171. func (c *Command) RunInDirFullPipeline(dir string, stdout, stderr io.Writer, stdin io.Reader) error {
  172. return c.RunInDirTimeoutFullPipeline(-1, dir, stdout, stderr, stdin)
  173. }
  174. // RunInDirBytes executes the command in given directory
  175. // and returns stdout in []byte and error (combined with stderr).
  176. func (c *Command) RunInDirBytes(dir string) ([]byte, error) {
  177. return c.RunInDirTimeout(-1, dir)
  178. }
  179. // RunInDir executes the command in given directory
  180. // and returns stdout in string and error (combined with stderr).
  181. func (c *Command) RunInDir(dir string) (string, error) {
  182. return c.RunInDirWithEnv(dir, nil)
  183. }
  184. // RunInDirWithEnv executes the command in given directory
  185. // and returns stdout in string and error (combined with stderr).
  186. func (c *Command) RunInDirWithEnv(dir string, env []string) (string, error) {
  187. stdout, err := c.RunInDirTimeoutEnv(env, -1, dir)
  188. if err != nil {
  189. return "", err
  190. }
  191. return string(stdout), nil
  192. }
  193. // RunTimeout executes the command in default working directory with given timeout,
  194. // and returns stdout in string and error (combined with stderr).
  195. func (c *Command) RunTimeout(timeout time.Duration) (string, error) {
  196. stdout, err := c.RunInDirTimeout(timeout, "")
  197. if err != nil {
  198. return "", err
  199. }
  200. return string(stdout), nil
  201. }
  202. // Run executes the command in default working directory
  203. // and returns stdout in string and error (combined with stderr).
  204. func (c *Command) Run() (string, error) {
  205. return c.RunTimeout(-1)
  206. }