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.1KB

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