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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. "gopkg.in/src-d/go-git.v4/plumbing"
  8. )
  9. func (repo *Repository) getTree(id SHA1) (*Tree, error) {
  10. gogitTree, err := repo.gogitRepo.TreeObject(plumbing.Hash(id))
  11. if err != nil {
  12. return nil, err
  13. }
  14. tree := NewTree(repo, id)
  15. tree.gogitTree = gogitTree
  16. return tree, nil
  17. }
  18. // GetTree find the tree object in the repository.
  19. func (repo *Repository) GetTree(idStr string) (*Tree, error) {
  20. if len(idStr) != 40 {
  21. res, err := NewCommand("rev-parse", idStr).RunInDir(repo.Path)
  22. if err != nil {
  23. return nil, err
  24. }
  25. if len(res) > 0 {
  26. idStr = res[:len(res)-1]
  27. }
  28. }
  29. id, err := NewIDFromString(idStr)
  30. if err != nil {
  31. return nil, err
  32. }
  33. commitObject, err := repo.gogitRepo.CommitObject(plumbing.Hash(id))
  34. if err != nil {
  35. return nil, err
  36. }
  37. treeObject, err := repo.getTree(SHA1(commitObject.TreeHash))
  38. if err != nil {
  39. return nil, err
  40. }
  41. treeObject.CommitID = id
  42. return treeObject, nil
  43. }