本站源代码
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.

111 satır
2.7KB

  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. "github.com/mcuadros/go-version"
  13. )
  14. func (repo *Repository) getTree(id SHA1) (*Tree, error) {
  15. gogitTree, err := repo.gogitRepo.TreeObject(id)
  16. if err != nil {
  17. return nil, err
  18. }
  19. tree := NewTree(repo, id)
  20. tree.gogitTree = gogitTree
  21. return tree, nil
  22. }
  23. // GetTree find the tree object in the repository.
  24. func (repo *Repository) GetTree(idStr string) (*Tree, error) {
  25. if len(idStr) != 40 {
  26. res, err := NewCommand("rev-parse", "--verify", idStr).RunInDir(repo.Path)
  27. if err != nil {
  28. return nil, err
  29. }
  30. if len(res) > 0 {
  31. idStr = res[:len(res)-1]
  32. }
  33. }
  34. id, err := NewIDFromString(idStr)
  35. if err != nil {
  36. return nil, err
  37. }
  38. resolvedID := id
  39. commitObject, err := repo.gogitRepo.CommitObject(id)
  40. if err == nil {
  41. id = SHA1(commitObject.TreeHash)
  42. }
  43. treeObject, err := repo.getTree(id)
  44. if err != nil {
  45. return nil, err
  46. }
  47. treeObject.ResolvedID = resolvedID
  48. return treeObject, nil
  49. }
  50. // CommitTreeOpts represents the possible options to CommitTree
  51. type CommitTreeOpts struct {
  52. Parents []string
  53. Message string
  54. KeyID string
  55. NoGPGSign bool
  56. AlwaysSign bool
  57. }
  58. // CommitTree creates a commit from a given tree id for the user with provided message
  59. func (repo *Repository) CommitTree(sig *Signature, tree *Tree, opts CommitTreeOpts) (SHA1, error) {
  60. binVersion, err := BinVersion()
  61. if err != nil {
  62. return SHA1{}, err
  63. }
  64. commitTimeStr := time.Now().Format(time.RFC3339)
  65. // Because this may call hooks we should pass in the environment
  66. env := append(os.Environ(),
  67. "GIT_AUTHOR_NAME="+sig.Name,
  68. "GIT_AUTHOR_EMAIL="+sig.Email,
  69. "GIT_AUTHOR_DATE="+commitTimeStr,
  70. "GIT_COMMITTER_NAME="+sig.Name,
  71. "GIT_COMMITTER_EMAIL="+sig.Email,
  72. "GIT_COMMITTER_DATE="+commitTimeStr,
  73. )
  74. cmd := NewCommand("commit-tree", tree.ID.String())
  75. for _, parent := range opts.Parents {
  76. cmd.AddArguments("-p", parent)
  77. }
  78. messageBytes := new(bytes.Buffer)
  79. _, _ = messageBytes.WriteString(opts.Message)
  80. _, _ = messageBytes.WriteString("\n")
  81. if version.Compare(binVersion, "1.7.9", ">=") && (opts.KeyID != "" || opts.AlwaysSign) {
  82. cmd.AddArguments(fmt.Sprintf("-S%s", opts.KeyID))
  83. }
  84. if version.Compare(binVersion, "2.0.0", ">=") && opts.NoGPGSign {
  85. cmd.AddArguments("--no-gpg-sign")
  86. }
  87. stdout := new(bytes.Buffer)
  88. stderr := new(bytes.Buffer)
  89. err = cmd.RunInDirTimeoutEnvFullPipeline(env, -1, repo.Path, stdout, stderr, messageBytes)
  90. if err != nil {
  91. return SHA1{}, concatenateError(err, stderr.String())
  92. }
  93. return NewIDFromString(strings.TrimSpace(stdout.String()))
  94. }
上海开阖软件有限公司 沪ICP备12045867号-1