tree.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // In case entry name is surrounded by double quotes(it happens only in git-shell).
  64. if entry.name[0] == '"' {
  65. entry.name = string(data[pos+1 : pos+step-1])
  66. entry.name = strings.Replace(entry.name, `\"`, `"`, -1)
  67. }
  68. pos += step + 1
  69. entries = append(entries, entry)
  70. }
  71. return entries, nil
  72. }
  73. func (t *Tree) SubTree(rpath string) (*Tree, error) {
  74. if len(rpath) == 0 {
  75. return t, nil
  76. }
  77. paths := strings.Split(rpath, "/")
  78. var err error
  79. var g = t
  80. var p = t
  81. var te *TreeEntry
  82. for _, name := range paths {
  83. te, err = p.GetTreeEntryByPath(name)
  84. if err != nil {
  85. return nil, err
  86. }
  87. g, err = t.repo.getTree(te.Id)
  88. if err != nil {
  89. return nil, err
  90. }
  91. g.ptree = p
  92. p = g
  93. }
  94. return g, nil
  95. }
  96. func (t *Tree) ListEntries(relpath string) (Entries, error) {
  97. if t.entriesParsed {
  98. return t.entries, nil
  99. }
  100. t.entriesParsed = true
  101. stdout, stderr, err := com.ExecCmdDirBytes(t.repo.Path,
  102. "git", "ls-tree", t.Id.String())
  103. if err != nil {
  104. if strings.Contains(err.Error(), "exit status 128") {
  105. return nil, errors.New(strings.TrimSpace(string(stderr)))
  106. }
  107. return nil, err
  108. }
  109. t.entries, err = parseTreeData(t, stdout)
  110. return t.entries, err
  111. }
  112. func NewTree(repo *Repository, id sha1) *Tree {
  113. tree := new(Tree)
  114. tree.Id = id
  115. tree.repo = repo
  116. return tree
  117. }