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.9KB

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