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.

view.go 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 repo
  5. import (
  6. "bytes"
  7. "io/ioutil"
  8. "path"
  9. "path/filepath"
  10. "strings"
  11. "github.com/Unknwon/paginater"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/git"
  15. "github.com/gogits/gogs/modules/log"
  16. "github.com/gogits/gogs/modules/middleware"
  17. "github.com/gogits/gogs/modules/template"
  18. )
  19. const (
  20. HOME base.TplName = "repo/home"
  21. WATCHERS base.TplName = "repo/watchers"
  22. )
  23. func Home(ctx *middleware.Context) {
  24. ctx.Data["Title"] = ctx.Repo.Repository.Name
  25. ctx.Data["RequireHighlightJS"] = true
  26. branchName := ctx.Repo.BranchName
  27. userName := ctx.Repo.Owner.Name
  28. repoName := ctx.Repo.Repository.Name
  29. repoLink := ctx.Repo.RepoLink
  30. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  31. treeLink := branchLink
  32. rawLink := ctx.Repo.RepoLink + "/raw/" + branchName
  33. // Get tree path
  34. treename := ctx.Repo.TreeName
  35. if len(treename) > 0 {
  36. if treename[len(treename)-1] == '/' {
  37. ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1])
  38. return
  39. }
  40. treeLink += "/" + treename
  41. }
  42. ctx.Data["IsRepoToolbarSource"] = true
  43. isViewBranch := ctx.Repo.IsBranch
  44. ctx.Data["IsViewBranch"] = isViewBranch
  45. treePath := treename
  46. if len(treePath) != 0 {
  47. treePath = treePath + "/"
  48. }
  49. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treename)
  50. if err != nil && err != git.ErrNotExist {
  51. ctx.Handle(404, "GetTreeEntryByPath", err)
  52. return
  53. }
  54. if len(treename) != 0 && entry == nil {
  55. ctx.Handle(404, "repo.Home", nil)
  56. return
  57. }
  58. if entry != nil && !entry.IsDir() {
  59. blob := entry.Blob()
  60. if dataRc, err := blob.Data(); err != nil {
  61. ctx.Handle(404, "blob.Data", err)
  62. return
  63. } else {
  64. ctx.Data["FileSize"] = blob.Size()
  65. ctx.Data["IsFile"] = true
  66. ctx.Data["FileName"] = blob.Name()
  67. ext := path.Ext(blob.Name())
  68. if len(ext) > 0 {
  69. ext = ext[1:]
  70. }
  71. ctx.Data["FileExt"] = ext
  72. ctx.Data["FileLink"] = rawLink + "/" + treename
  73. buf := make([]byte, 1024)
  74. n, _ := dataRc.Read(buf)
  75. if n > 0 {
  76. buf = buf[:n]
  77. }
  78. _, isTextFile := base.IsTextFile(buf)
  79. _, isImageFile := base.IsImageFile(buf)
  80. ctx.Data["IsFileText"] = isTextFile
  81. switch {
  82. case isImageFile:
  83. ctx.Data["IsImageFile"] = true
  84. case isTextFile:
  85. d, _ := ioutil.ReadAll(dataRc)
  86. buf = append(buf, d...)
  87. readmeExist := base.IsMarkdownFile(blob.Name()) || base.IsReadmeFile(blob.Name())
  88. ctx.Data["ReadmeExist"] = readmeExist
  89. if readmeExist {
  90. ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, path.Dir(treeLink)))
  91. } else {
  92. if err, content := template.ToUtf8WithErr(buf); err != nil {
  93. if err != nil {
  94. log.Error(4, "Convert content encoding: %s", err)
  95. }
  96. ctx.Data["FileContent"] = string(buf)
  97. } else {
  98. ctx.Data["FileContent"] = content
  99. }
  100. }
  101. }
  102. }
  103. } else {
  104. // Directory and file list.
  105. tree, err := ctx.Repo.Commit.SubTree(treename)
  106. if err != nil {
  107. ctx.Handle(404, "SubTree", err)
  108. return
  109. }
  110. entries, err := tree.ListEntries(treename)
  111. if err != nil {
  112. ctx.Handle(500, "ListEntries", err)
  113. return
  114. }
  115. entries.Sort()
  116. files := make([][]interface{}, 0, len(entries))
  117. for _, te := range entries {
  118. if te.Type != git.COMMIT {
  119. c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name()))
  120. if err != nil {
  121. ctx.Handle(500, "GetCommitOfRelPath", err)
  122. return
  123. }
  124. files = append(files, []interface{}{te, c})
  125. } else {
  126. sm, err := ctx.Repo.Commit.GetSubModule(path.Join(treename, te.Name()))
  127. if err != nil {
  128. ctx.Handle(500, "GetSubModule", err)
  129. return
  130. }
  131. smUrl := ""
  132. if sm != nil {
  133. smUrl = sm.Url
  134. }
  135. c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name()))
  136. if err != nil {
  137. ctx.Handle(500, "GetCommitOfRelPath", err)
  138. return
  139. }
  140. files = append(files, []interface{}{te, git.NewSubModuleFile(c, smUrl, te.ID.String())})
  141. }
  142. }
  143. ctx.Data["Files"] = files
  144. var readmeFile *git.Blob
  145. for _, f := range entries {
  146. if f.IsDir() || !base.IsReadmeFile(f.Name()) {
  147. continue
  148. } else {
  149. readmeFile = f.Blob()
  150. break
  151. }
  152. }
  153. if readmeFile != nil {
  154. ctx.Data["ReadmeInList"] = true
  155. ctx.Data["ReadmeExist"] = true
  156. if dataRc, err := readmeFile.Data(); err != nil {
  157. ctx.Handle(404, "repo.SinglereadmeFile.LookupBlob", err)
  158. return
  159. } else {
  160. buf := make([]byte, 1024)
  161. n, _ := dataRc.Read(buf)
  162. if n > 0 {
  163. buf = buf[:n]
  164. }
  165. ctx.Data["FileSize"] = readmeFile.Size()
  166. ctx.Data["FileLink"] = rawLink + "/" + treename
  167. _, isTextFile := base.IsTextFile(buf)
  168. ctx.Data["FileIsText"] = isTextFile
  169. ctx.Data["FileName"] = readmeFile.Name()
  170. if isTextFile {
  171. d, _ := ioutil.ReadAll(dataRc)
  172. buf = append(buf, d...)
  173. switch {
  174. case base.IsMarkdownFile(readmeFile.Name()):
  175. buf = base.RenderMarkdown(buf, treeLink)
  176. default:
  177. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  178. }
  179. ctx.Data["FileContent"] = string(buf)
  180. }
  181. }
  182. }
  183. lastCommit := ctx.Repo.Commit
  184. if len(treePath) > 0 {
  185. c, err := ctx.Repo.Commit.GetCommitOfRelPath(treePath)
  186. if err != nil {
  187. ctx.Handle(500, "GetCommitOfRelPath", err)
  188. return
  189. }
  190. lastCommit = c
  191. }
  192. ctx.Data["LastCommit"] = lastCommit
  193. ctx.Data["LastCommitUser"] = models.ValidateCommitWithEmail(lastCommit)
  194. }
  195. ctx.Data["Username"] = userName
  196. ctx.Data["Reponame"] = repoName
  197. var treenames []string
  198. Paths := make([]string, 0)
  199. if len(treename) > 0 {
  200. treenames = strings.Split(treename, "/")
  201. for i, _ := range treenames {
  202. Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
  203. }
  204. ctx.Data["HasParentPath"] = true
  205. if len(Paths)-2 >= 0 {
  206. ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
  207. }
  208. }
  209. ctx.Data["Paths"] = Paths
  210. ctx.Data["TreeName"] = treename
  211. ctx.Data["Treenames"] = treenames
  212. ctx.Data["TreePath"] = treePath
  213. ctx.Data["BranchLink"] = branchLink
  214. ctx.HTML(200, HOME)
  215. }
  216. func renderItems(ctx *middleware.Context, total int, getter func(page int) ([]*models.User, error)) {
  217. page := ctx.QueryInt("page")
  218. if page <= 0 {
  219. page = 1
  220. }
  221. pager := paginater.New(total, models.ItemsPerPage, page, 5)
  222. ctx.Data["Page"] = pager
  223. items, err := getter(pager.Current())
  224. if err != nil {
  225. ctx.Handle(500, "getter", err)
  226. return
  227. }
  228. ctx.Data["Watchers"] = items
  229. ctx.HTML(200, WATCHERS)
  230. }
  231. func Watchers(ctx *middleware.Context) {
  232. ctx.Data["Title"] = ctx.Tr("repo.watchers")
  233. ctx.Data["PageIsWatchers"] = true
  234. renderItems(ctx, ctx.Repo.Repository.NumWatches, ctx.Repo.Repository.GetWatchers)
  235. }
  236. func Stars(ctx *middleware.Context) {
  237. ctx.Data["Title"] = ctx.Tr("repo.stargazers")
  238. ctx.Data["PageIsStargazers"] = true
  239. renderItems(ctx, ctx.Repo.Repository.NumStars, ctx.Repo.Repository.GetStargazers)
  240. }