本站源代码
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

238 lines
5.4KB

  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. // IsExecutable if the entry is an executable file (not necessarily binary)
  93. func (te *TreeEntry) IsExecutable() bool {
  94. return te.gogitTreeEntry.Mode == filemode.Executable
  95. }
  96. // Blob returns the blob object the entry
  97. func (te *TreeEntry) Blob() *Blob {
  98. encodedObj, err := te.ptree.repo.gogitRepo.Storer.EncodedObject(plumbing.AnyObject, te.gogitTreeEntry.Hash)
  99. if err != nil {
  100. return nil
  101. }
  102. return &Blob{
  103. ID: te.gogitTreeEntry.Hash,
  104. gogitEncodedObj: encodedObj,
  105. name: te.Name(),
  106. }
  107. }
  108. // FollowLink returns the entry pointed to by a symlink
  109. func (te *TreeEntry) FollowLink() (*TreeEntry, error) {
  110. if !te.IsLink() {
  111. return nil, ErrBadLink{te.Name(), "not a symlink"}
  112. }
  113. // read the link
  114. r, err := te.Blob().DataAsync()
  115. if err != nil {
  116. return nil, err
  117. }
  118. defer r.Close()
  119. buf := make([]byte, te.Size())
  120. _, err = io.ReadFull(r, buf)
  121. if err != nil {
  122. return nil, err
  123. }
  124. lnk := string(buf)
  125. t := te.ptree
  126. // traverse up directories
  127. for ; t != nil && strings.HasPrefix(lnk, "../"); lnk = lnk[3:] {
  128. t = t.ptree
  129. }
  130. if t == nil {
  131. return nil, ErrBadLink{te.Name(), "points outside of repo"}
  132. }
  133. target, err := t.GetTreeEntryByPath(lnk)
  134. if err != nil {
  135. if IsErrNotExist(err) {
  136. return nil, ErrBadLink{te.Name(), "broken link"}
  137. }
  138. return nil, err
  139. }
  140. return target, nil
  141. }
  142. // GetSubJumpablePathName return the full path of subdirectory jumpable ( contains only one directory )
  143. func (te *TreeEntry) GetSubJumpablePathName() string {
  144. if te.IsSubModule() || !te.IsDir() {
  145. return ""
  146. }
  147. tree, err := te.ptree.SubTree(te.Name())
  148. if err != nil {
  149. return te.Name()
  150. }
  151. entries, _ := tree.ListEntries()
  152. if len(entries) == 1 && entries[0].IsDir() {
  153. name := entries[0].GetSubJumpablePathName()
  154. if name != "" {
  155. return te.Name() + "/" + name
  156. }
  157. }
  158. return te.Name()
  159. }
  160. // Entries a list of entry
  161. type Entries []*TreeEntry
  162. type customSortableEntries struct {
  163. Comparer func(s1, s2 string) bool
  164. Entries
  165. }
  166. var sorter = []func(t1, t2 *TreeEntry, cmp func(s1, s2 string) bool) bool{
  167. func(t1, t2 *TreeEntry, cmp func(s1, s2 string) bool) bool {
  168. return (t1.IsDir() || t1.IsSubModule()) && !t2.IsDir() && !t2.IsSubModule()
  169. },
  170. func(t1, t2 *TreeEntry, cmp func(s1, s2 string) bool) bool {
  171. return cmp(t1.Name(), t2.Name())
  172. },
  173. }
  174. func (ctes customSortableEntries) Len() int { return len(ctes.Entries) }
  175. func (ctes customSortableEntries) Swap(i, j int) {
  176. ctes.Entries[i], ctes.Entries[j] = ctes.Entries[j], ctes.Entries[i]
  177. }
  178. func (ctes customSortableEntries) Less(i, j int) bool {
  179. t1, t2 := ctes.Entries[i], ctes.Entries[j]
  180. var k int
  181. for k = 0; k < len(sorter)-1; k++ {
  182. s := sorter[k]
  183. switch {
  184. case s(t1, t2, ctes.Comparer):
  185. return true
  186. case s(t2, t1, ctes.Comparer):
  187. return false
  188. }
  189. }
  190. return sorter[k](t1, t2, ctes.Comparer)
  191. }
  192. // Sort sort the list of entry
  193. func (tes Entries) Sort() {
  194. sort.Sort(customSortableEntries{func(s1, s2 string) bool {
  195. return s1 < s2
  196. }, tes})
  197. }
  198. // CustomSort customizable string comparing sort entry list
  199. func (tes Entries) CustomSort(cmp func(s1, s2 string) bool) {
  200. sort.Sort(customSortableEntries{cmp, tes})
  201. }
上海开阖软件有限公司 沪ICP备12045867号-1