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.

tree_entry.go 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. "sort"
  9. "strings"
  10. "gopkg.in/src-d/go-git.v4/plumbing"
  11. "gopkg.in/src-d/go-git.v4/plumbing/filemode"
  12. "gopkg.in/src-d/go-git.v4/plumbing/object"
  13. )
  14. // EntryMode the type of the object in the git tree
  15. type EntryMode int
  16. // There are only a few file modes in Git. They look like unix file modes, but they can only be
  17. // one of these.
  18. const (
  19. // EntryModeBlob
  20. EntryModeBlob EntryMode = 0100644
  21. // EntryModeExec
  22. EntryModeExec EntryMode = 0100755
  23. // EntryModeSymlink
  24. EntryModeSymlink EntryMode = 0120000
  25. // EntryModeCommit
  26. EntryModeCommit EntryMode = 0160000
  27. // EntryModeTree
  28. EntryModeTree EntryMode = 0040000
  29. )
  30. // TreeEntry the leaf in the git tree
  31. type TreeEntry struct {
  32. ID SHA1
  33. gogitTreeEntry *object.TreeEntry
  34. ptree *Tree
  35. size int64
  36. sized bool
  37. fullName string
  38. }
  39. // Name returns the name of the entry
  40. func (te *TreeEntry) Name() string {
  41. if te.fullName != "" {
  42. return te.fullName
  43. }
  44. return te.gogitTreeEntry.Name
  45. }
  46. // Mode returns the mode of the entry
  47. func (te *TreeEntry) Mode() EntryMode {
  48. return EntryMode(te.gogitTreeEntry.Mode)
  49. }
  50. // Type returns the type of the entry (commit, tree, blob)
  51. func (te *TreeEntry) Type() string {
  52. switch te.Mode() {
  53. case EntryModeCommit:
  54. return "commit"
  55. case EntryModeTree:
  56. return "tree"
  57. default:
  58. return "blob"
  59. }
  60. }
  61. // Size returns the size of the entry
  62. func (te *TreeEntry) Size() int64 {
  63. if te.IsDir() {
  64. return 0
  65. } else if te.sized {
  66. return te.size
  67. }
  68. file, err := te.ptree.gogitTree.TreeEntryFile(te.gogitTreeEntry)
  69. if err != nil {
  70. return 0
  71. }
  72. te.sized = true
  73. te.size = file.Size
  74. return te.size
  75. }
  76. // IsSubModule if the entry is a sub module
  77. func (te *TreeEntry) IsSubModule() bool {
  78. return te.gogitTreeEntry.Mode == filemode.Submodule
  79. }
  80. // IsDir if the entry is a sub dir
  81. func (te *TreeEntry) IsDir() bool {
  82. return te.gogitTreeEntry.Mode == filemode.Dir
  83. }
  84. // IsLink if the entry is a symlink
  85. func (te *TreeEntry) IsLink() bool {
  86. return te.gogitTreeEntry.Mode == filemode.Symlink
  87. }
  88. // IsRegular if the entry is a regular file
  89. func (te *TreeEntry) IsRegular() bool {
  90. return te.gogitTreeEntry.Mode == filemode.Regular
  91. }
  92. // Blob returns the blob object the entry
  93. func (te *TreeEntry) Blob() *Blob {
  94. encodedObj, err := te.ptree.repo.gogitRepo.Storer.EncodedObject(plumbing.AnyObject, te.gogitTreeEntry.Hash)
  95. if err != nil {
  96. return nil
  97. }
  98. return &Blob{
  99. ID: te.gogitTreeEntry.Hash,
  100. gogitEncodedObj: encodedObj,
  101. name: te.Name(),
  102. }
  103. }
  104. // FollowLink returns the entry pointed to by a symlink
  105. func (te *TreeEntry) FollowLink() (*TreeEntry, error) {
  106. if !te.IsLink() {
  107. return nil, ErrBadLink{te.Name(), "not a symlink"}
  108. }
  109. // read the link
  110. r, err := te.Blob().DataAsync()
  111. if err != nil {
  112. return nil, err
  113. }
  114. defer r.Close()
  115. buf := make([]byte, te.Size())
  116. _, err = io.ReadFull(r, buf)
  117. if err != nil {
  118. return nil, err
  119. }
  120. lnk := string(buf)
  121. t := te.ptree
  122. // traverse up directories
  123. for ; t != nil && strings.HasPrefix(lnk, "../"); lnk = lnk[3:] {
  124. t = t.ptree
  125. }
  126. if t == nil {
  127. return nil, ErrBadLink{te.Name(), "points outside of repo"}
  128. }
  129. target, err := t.GetTreeEntryByPath(lnk)
  130. if err != nil {
  131. if IsErrNotExist(err) {
  132. return nil, ErrBadLink{te.Name(), "broken link"}
  133. }
  134. return nil, err
  135. }
  136. return target, nil
  137. }
  138. // GetSubJumpablePathName return the full path of subdirectory jumpable ( contains only one directory )
  139. func (te *TreeEntry) GetSubJumpablePathName() string {
  140. if te.IsSubModule() || !te.IsDir() {
  141. return ""
  142. }
  143. tree, err := te.ptree.SubTree(te.Name())
  144. if err != nil {
  145. return te.Name()
  146. }
  147. entries, _ := tree.ListEntries()
  148. if len(entries) == 1 && entries[0].IsDir() {
  149. name := entries[0].GetSubJumpablePathName()
  150. if name != "" {
  151. return te.Name() + "/" + name
  152. }
  153. }
  154. return te.Name()
  155. }
  156. // Entries a list of entry
  157. type Entries []*TreeEntry
  158. type customSortableEntries struct {
  159. Comparer func(s1, s2 string) bool
  160. Entries
  161. }
  162. var sorter = []func(t1, t2 *TreeEntry, cmp func(s1, s2 string) bool) bool{
  163. func(t1, t2 *TreeEntry, cmp func(s1, s2 string) bool) bool {
  164. return (t1.IsDir() || t1.IsSubModule()) && !t2.IsDir() && !t2.IsSubModule()
  165. },
  166. func(t1, t2 *TreeEntry, cmp func(s1, s2 string) bool) bool {
  167. return cmp(t1.Name(), t2.Name())
  168. },
  169. }
  170. func (ctes customSortableEntries) Len() int { return len(ctes.Entries) }
  171. func (ctes customSortableEntries) Swap(i, j int) {
  172. ctes.Entries[i], ctes.Entries[j] = ctes.Entries[j], ctes.Entries[i]
  173. }
  174. func (ctes customSortableEntries) Less(i, j int) bool {
  175. t1, t2 := ctes.Entries[i], ctes.Entries[j]
  176. var k int
  177. for k = 0; k < len(sorter)-1; k++ {
  178. s := sorter[k]
  179. switch {
  180. case s(t1, t2, ctes.Comparer):
  181. return true
  182. case s(t2, t1, ctes.Comparer):
  183. return false
  184. }
  185. }
  186. return sorter[k](t1, t2, ctes.Comparer)
  187. }
  188. // Sort sort the list of entry
  189. func (tes Entries) Sort() {
  190. sort.Sort(customSortableEntries{func(s1, s2 string) bool {
  191. return s1 < s2
  192. }, tes})
  193. }
  194. // CustomSort customizable string comparing sort entry list
  195. func (tes Entries) CustomSort(cmp func(s1, s2 string) bool) {
  196. sort.Sort(customSortableEntries{cmp, tes})
  197. }