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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "fmt"
  8. "os"
  9. "strings"
  10. "time"
  11. )
  12. func (repo *Repository) getTree(id SHA1) (*Tree, error) {
  13. gogitTree, err := repo.gogitRepo.TreeObject(id)
  14. if err != nil {
  15. return nil, err
  16. }
  17. tree := NewTree(repo, id)
  18. tree.gogitTree = gogitTree
  19. return tree, nil
  20. }
  21. // GetTree find the tree object in the repository.
  22. func (repo *Repository) GetTree(idStr string) (*Tree, error) {
  23. if len(idStr) != 40 {
  24. res, err := NewCommand("rev-parse", "--verify", idStr).RunInDir(repo.Path)
  25. if err != nil {
  26. return nil, err
  27. }
  28. if len(res) > 0 {
  29. idStr = res[:len(res)-1]
  30. }
  31. }
  32. id, err := NewIDFromString(idStr)
  33. if err != nil {
  34. return nil, err
  35. }
  36. resolvedID := id
  37. commitObject, err := repo.gogitRepo.CommitObject(id)
  38. if err == nil {
  39. id = SHA1(commitObject.TreeHash)
  40. }
  41. treeObject, err := repo.getTree(id)
  42. if err != nil {
  43. return nil, err
  44. }
  45. treeObject.ResolvedID = resolvedID
  46. return treeObject, nil
  47. }
  48. // CommitTreeOpts represents the possible options to CommitTree
  49. type CommitTreeOpts struct {
  50. Parents []string
  51. Message string
  52. KeyID string
  53. NoGPGSign bool
  54. }
  55. // CommitTree creates a commit from a given tree id for the user with provided message
  56. func (repo *Repository) CommitTree(sig *Signature, tree *Tree, opts CommitTreeOpts) (SHA1, error) {
  57. commitTimeStr := time.Now().Format(time.RFC3339)
  58. // Because this may call hooks we should pass in the environment
  59. env := append(os.Environ(),
  60. "GIT_AUTHOR_NAME="+sig.Name,
  61. "GIT_AUTHOR_EMAIL="+sig.Email,
  62. "GIT_AUTHOR_DATE="+commitTimeStr,
  63. "GIT_COMMITTER_NAME="+sig.Name,
  64. "GIT_COMMITTER_EMAIL="+sig.Email,
  65. "GIT_COMMITTER_DATE="+commitTimeStr,
  66. )
  67. cmd := NewCommand("commit-tree", tree.ID.String())
  68. for _, parent := range opts.Parents {
  69. cmd.AddArguments("-p", parent)
  70. }
  71. cmd.AddArguments("-m", opts.Message)
  72. if opts.KeyID != "" {
  73. cmd.AddArguments(fmt.Sprintf("-S%s", opts.KeyID))
  74. }
  75. if opts.NoGPGSign {
  76. cmd.AddArguments("--no-gpg-sign")
  77. }
  78. res, err := cmd.RunInDirWithEnv(repo.Path, env)
  79. if err != nil {
  80. return SHA1{}, err
  81. }
  82. return NewIDFromString(strings.TrimSpace(res))
  83. }