本站源代码
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

131 lines
2.5KB

  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. "io"
  8. "strings"
  9. "gopkg.in/src-d/go-git.v4/plumbing"
  10. "gopkg.in/src-d/go-git.v4/plumbing/object"
  11. )
  12. // Tree represents a flat directory listing.
  13. type Tree struct {
  14. ID SHA1
  15. ResolvedID SHA1
  16. repo *Repository
  17. gogitTree *object.Tree
  18. // parent tree
  19. ptree *Tree
  20. }
  21. // NewTree create a new tree according the repository and tree id
  22. func NewTree(repo *Repository, id SHA1) *Tree {
  23. return &Tree{
  24. ID: id,
  25. repo: repo,
  26. }
  27. }
  28. // SubTree get a sub tree by the sub dir path
  29. func (t *Tree) SubTree(rpath string) (*Tree, error) {
  30. if len(rpath) == 0 {
  31. return t, nil
  32. }
  33. paths := strings.Split(rpath, "/")
  34. var (
  35. err error
  36. g = t
  37. p = t
  38. te *TreeEntry
  39. )
  40. for _, name := range paths {
  41. te, err = p.GetTreeEntryByPath(name)
  42. if err != nil {
  43. return nil, err
  44. }
  45. g, err = t.repo.getTree(te.ID)
  46. if err != nil {
  47. return nil, err
  48. }
  49. g.ptree = p
  50. p = g
  51. }
  52. return g, nil
  53. }
  54. func (t *Tree) loadTreeObject() error {
  55. gogitTree, err := t.repo.gogitRepo.TreeObject(t.ID)
  56. if err != nil {
  57. return err
  58. }
  59. t.gogitTree = gogitTree
  60. return nil
  61. }
  62. // ListEntries returns all entries of current tree.
  63. func (t *Tree) ListEntries() (Entries, error) {
  64. if t.gogitTree == nil {
  65. err := t.loadTreeObject()
  66. if err != nil {
  67. return nil, err
  68. }
  69. }
  70. entries := make([]*TreeEntry, len(t.gogitTree.Entries))
  71. for i, entry := range t.gogitTree.Entries {
  72. entries[i] = &TreeEntry{
  73. ID: entry.Hash,
  74. gogitTreeEntry: &t.gogitTree.Entries[i],
  75. ptree: t,
  76. }
  77. }
  78. return entries, nil
  79. }
  80. // ListEntriesRecursive returns all entries of current tree recursively including all subtrees
  81. func (t *Tree) ListEntriesRecursive() (Entries, error) {
  82. if t.gogitTree == nil {
  83. err := t.loadTreeObject()
  84. if err != nil {
  85. return nil, err
  86. }
  87. }
  88. var entries []*TreeEntry
  89. seen := map[plumbing.Hash]bool{}
  90. walker := object.NewTreeWalker(t.gogitTree, true, seen)
  91. for {
  92. fullName, entry, err := walker.Next()
  93. if err == io.EOF {
  94. break
  95. }
  96. if err != nil {
  97. return nil, err
  98. }
  99. if seen[entry.Hash] {
  100. continue
  101. }
  102. convertedEntry := &TreeEntry{
  103. ID: entry.Hash,
  104. gogitTreeEntry: &entry,
  105. ptree: t,
  106. fullName: fullName,
  107. }
  108. entries = append(entries, convertedEntry)
  109. }
  110. return entries, nil
  111. }
上海开阖软件有限公司 沪ICP备12045867号-1