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.

repo_tree.go 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package git
  5. import (
  6. "bytes"
  7. "os"
  8. "strings"
  9. "time"
  10. )
  11. // CommitTreeOpts represents the possible options to CommitTree
  12. type CommitTreeOpts struct {
  13. Parents []string
  14. Message string
  15. KeyID string
  16. NoGPGSign bool
  17. AlwaysSign bool
  18. }
  19. // CommitTree creates a commit from a given tree id for the user with provided message
  20. func (repo *Repository) CommitTree(author, committer *Signature, tree *Tree, opts CommitTreeOpts) (SHA1, error) {
  21. commitTimeStr := time.Now().Format(time.RFC3339)
  22. // Because this may call hooks we should pass in the environment
  23. env := append(os.Environ(),
  24. "GIT_AUTHOR_NAME="+author.Name,
  25. "GIT_AUTHOR_EMAIL="+author.Email,
  26. "GIT_AUTHOR_DATE="+commitTimeStr,
  27. "GIT_COMMITTER_NAME="+committer.Name,
  28. "GIT_COMMITTER_EMAIL="+committer.Email,
  29. "GIT_COMMITTER_DATE="+commitTimeStr,
  30. )
  31. cmd := NewCommand(repo.Ctx, "commit-tree").AddDynamicArguments(tree.ID.String())
  32. for _, parent := range opts.Parents {
  33. cmd.AddArguments("-p").AddDynamicArguments(parent)
  34. }
  35. messageBytes := new(bytes.Buffer)
  36. _, _ = messageBytes.WriteString(opts.Message)
  37. _, _ = messageBytes.WriteString("\n")
  38. if opts.KeyID != "" || opts.AlwaysSign {
  39. cmd.AddOptionFormat("-S%s", opts.KeyID)
  40. }
  41. if opts.NoGPGSign {
  42. cmd.AddArguments("--no-gpg-sign")
  43. }
  44. stdout := new(bytes.Buffer)
  45. stderr := new(bytes.Buffer)
  46. err := cmd.Run(&RunOpts{
  47. Env: env,
  48. Dir: repo.Path,
  49. Stdin: messageBytes,
  50. Stdout: stdout,
  51. Stderr: stderr,
  52. })
  53. if err != nil {
  54. return SHA1{}, ConcatenateError(err, stderr.String())
  55. }
  56. return NewIDFromString(strings.TrimSpace(stdout.String()))
  57. }