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.

272 lines
6.7 KiB

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