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

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