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.

136 lines
2.8 KiB

9 years ago
10 years ago
10 years ago
  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. }