tree.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2014 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. "bytes"
  7. "errors"
  8. "strings"
  9. "github.com/Unknwon/com"
  10. )
  11. var (
  12. ErrNotExist = errors.New("error not exist")
  13. )
  14. // A tree is a flat directory listing.
  15. type Tree struct {
  16. Id sha1
  17. repo *Repository
  18. // parent tree
  19. ptree *Tree
  20. entries Entries
  21. entriesParsed bool
  22. }
  23. // Parse tree information from the (uncompressed) raw
  24. // data from the tree object.
  25. func parseTreeData(tree *Tree, data []byte) ([]*TreeEntry, error) {
  26. entries := make([]*TreeEntry, 0, 10)
  27. l := len(data)
  28. pos := 0
  29. for pos < l {
  30. entry := new(TreeEntry)
  31. entry.ptree = tree
  32. step := 6
  33. switch string(data[pos : pos+step]) {
  34. case "100644":
  35. entry.mode = ModeBlob
  36. entry.Type = BLOB
  37. case "100755":
  38. entry.mode = ModeExec
  39. entry.Type = BLOB
  40. case "120000":
  41. entry.mode = ModeSymlink
  42. entry.Type = BLOB
  43. case "160000":
  44. entry.mode = ModeCommit
  45. entry.Type = COMMIT
  46. step = 8
  47. case "040000":
  48. entry.mode = ModeTree
  49. entry.Type = TREE
  50. default:
  51. return nil, errors.New("unknown type: " + string(data[pos:pos+step]))
  52. }
  53. pos += step + 6 // Skip string type of entry type.
  54. step = 40
  55. id, err := NewIdFromString(string(data[pos : pos+step]))
  56. if err != nil {
  57. return nil, err
  58. }
  59. entry.Id = id
  60. pos += step + 1 // Skip half of sha1.
  61. step = bytes.IndexByte(data[pos:], '\n')
  62. entry.name = string(data[pos : pos+step])
  63. pos += step + 1
  64. entries = append(entries, entry)
  65. }
  66. return entries, nil
  67. }
  68. func (t *Tree) SubTree(rpath string) (*Tree, error) {
  69. if len(rpath) == 0 {
  70. return t, nil
  71. }
  72. paths := strings.Split(rpath, "/")
  73. var err error
  74. var g = t
  75. var p = t
  76. var te *TreeEntry
  77. for _, name := range paths {
  78. te, err = p.GetTreeEntryByPath(name)
  79. if err != nil {
  80. return nil, err
  81. }
  82. g, err = t.repo.getTree(te.Id)
  83. if err != nil {
  84. return nil, err
  85. }
  86. g.ptree = p
  87. p = g
  88. }
  89. return g, nil
  90. }
  91. func (t *Tree) ListEntries(relpath string) (Entries, error) {
  92. if t.entriesParsed {
  93. return t.entries, nil
  94. }
  95. t.entriesParsed = true
  96. stdout, stderr, err := com.ExecCmdDirBytes(t.repo.Path,
  97. "git", "ls-tree", t.Id.String())
  98. if err != nil {
  99. if strings.Contains(err.Error(), "exit status 128") {
  100. return nil, errors.New(strings.TrimSpace(string(stderr)))
  101. }
  102. return nil, err
  103. }
  104. t.entries, err = parseTreeData(t, stdout)
  105. return t.entries, err
  106. }
  107. func NewTree(repo *Repository, id sha1) *Tree {
  108. tree := new(Tree)
  109. tree.Id = id
  110. tree.repo = repo
  111. return tree
  112. }