Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "strings"
  7. )
  8. // Tree represents a flat directory listing.
  9. type Tree struct {
  10. ID SHA1
  11. repo *Repository
  12. // parent tree
  13. ptree *Tree
  14. entries Entries
  15. entriesParsed bool
  16. entriesRecursive Entries
  17. entriesRecursiveParsed bool
  18. }
  19. // NewTree create a new tree according the repository and commit id
  20. func NewTree(repo *Repository, id SHA1) *Tree {
  21. return &Tree{
  22. ID: id,
  23. repo: repo,
  24. }
  25. }
  26. // SubTree get a sub tree by the sub dir path
  27. func (t *Tree) SubTree(rpath string) (*Tree, error) {
  28. if len(rpath) == 0 {
  29. return t, nil
  30. }
  31. paths := strings.Split(rpath, "/")
  32. var (
  33. err error
  34. g = t
  35. p = t
  36. te *TreeEntry
  37. )
  38. for _, name := range paths {
  39. te, err = p.GetTreeEntryByPath(name)
  40. if err != nil {
  41. return nil, err
  42. }
  43. g, err = t.repo.getTree(te.ID)
  44. if err != nil {
  45. return nil, err
  46. }
  47. g.ptree = p
  48. p = g
  49. }
  50. return g, nil
  51. }
  52. // ListEntries returns all entries of current tree.
  53. func (t *Tree) ListEntries() (Entries, error) {
  54. if t.entriesParsed {
  55. return t.entries, nil
  56. }
  57. stdout, err := NewCommand("ls-tree", t.ID.String()).RunInDirBytes(t.repo.Path)
  58. if err != nil {
  59. return nil, err
  60. }
  61. t.entries, err = parseTreeEntries(stdout, t)
  62. if err == nil {
  63. t.entriesParsed = true
  64. }
  65. return t.entries, err
  66. }
  67. // ListEntriesRecursive returns all entries of current tree recursively including all subtrees
  68. func (t *Tree) ListEntriesRecursive() (Entries, error) {
  69. if t.entriesRecursiveParsed {
  70. return t.entriesRecursive, nil
  71. }
  72. stdout, err := NewCommand("ls-tree", "-t", "-r", t.ID.String()).RunInDirBytes(t.repo.Path)
  73. if err != nil {
  74. return nil, err
  75. }
  76. t.entriesRecursive, err = parseTreeEntries(stdout, t)
  77. if err == nil {
  78. t.entriesRecursiveParsed = true
  79. }
  80. return t.entriesRecursive, err
  81. }