view.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/git-shell"
  13. "github.com/gogits/gogs/models"
  14. "github.com/gogits/gogs/modules/base"
  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. 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. ext := path.Ext(blob.Name())
  67. if len(ext) > 0 {
  68. ext = ext[1:]
  69. }
  70. ctx.Data["FileExt"] = ext
  71. ctx.Data["FileLink"] = rawLink + "/" + treename
  72. buf := make([]byte, 1024)
  73. n, _ := dataRc.Read(buf)
  74. if n > 0 {
  75. buf = buf[:n]
  76. }
  77. _, isTextFile := base.IsTextFile(buf)
  78. _, isImageFile := base.IsImageFile(buf)
  79. ctx.Data["IsFileText"] = isTextFile
  80. switch {
  81. case isImageFile:
  82. ctx.Data["IsImageFile"] = true
  83. case isTextFile:
  84. d, _ := ioutil.ReadAll(dataRc)
  85. buf = append(buf, d...)
  86. readmeExist := base.IsMarkdownFile(blob.Name()) || base.IsReadmeFile(blob.Name())
  87. ctx.Data["ReadmeExist"] = readmeExist
  88. if readmeExist {
  89. ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
  90. } else {
  91. if err, content := template.ToUtf8WithErr(buf); err != nil {
  92. if err != nil {
  93. log.Error(4, "Convert content encoding: %s", err)
  94. }
  95. ctx.Data["FileContent"] = string(buf)
  96. } else {
  97. ctx.Data["FileContent"] = content
  98. }
  99. }
  100. }
  101. }
  102. } else {
  103. // Directory and file list.
  104. tree, err := ctx.Repo.Commit.SubTree(treename)
  105. if err != nil {
  106. ctx.Handle(404, "SubTree", err)
  107. return
  108. }
  109. entries, err := tree.ListEntries()
  110. if err != nil {
  111. ctx.Handle(500, "ListEntries", err)
  112. return
  113. }
  114. entries.Sort()
  115. files := make([][]interface{}, 0, len(entries))
  116. for _, te := range entries {
  117. if te.Type != git.OBJECT_COMMIT {
  118. c, err := ctx.Repo.Commit.GetCommitByPath(filepath.Join(treePath, te.Name()))
  119. if err != nil {
  120. ctx.Handle(500, "GetCommitByPath", err)
  121. return
  122. }
  123. files = append(files, []interface{}{te, c})
  124. } else {
  125. sm, err := ctx.Repo.Commit.GetSubModule(path.Join(treename, te.Name()))
  126. if err != nil {
  127. ctx.Handle(500, "GetSubModule", err)
  128. return
  129. }
  130. smUrl := ""
  131. if sm != nil {
  132. smUrl = sm.Url
  133. }
  134. c, err := ctx.Repo.Commit.GetCommitByPath(filepath.Join(treePath, te.Name()))
  135. if err != nil {
  136. ctx.Handle(500, "GetCommitByPath", err)
  137. return
  138. }
  139. files = append(files, []interface{}{te, git.NewSubModuleFile(c, smUrl, te.ID.String())})
  140. }
  141. }
  142. ctx.Data["Files"] = files
  143. var readmeFile *git.Blob
  144. for _, f := range entries {
  145. if f.IsDir() || !base.IsReadmeFile(f.Name()) {
  146. continue
  147. } else {
  148. readmeFile = f.Blob()
  149. break
  150. }
  151. }
  152. if readmeFile != nil {
  153. ctx.Data["ReadmeInList"] = true
  154. ctx.Data["ReadmeExist"] = true
  155. if dataRc, err := readmeFile.Data(); err != nil {
  156. ctx.Handle(404, "repo.SinglereadmeFile.Data", err)
  157. return
  158. } else {
  159. buf := make([]byte, 1024)
  160. n, _ := dataRc.Read(buf)
  161. if n > 0 {
  162. buf = buf[:n]
  163. }
  164. ctx.Data["FileSize"] = readmeFile.Size()
  165. ctx.Data["FileLink"] = rawLink + "/" + treename
  166. _, isTextFile := base.IsTextFile(buf)
  167. ctx.Data["FileIsText"] = isTextFile
  168. ctx.Data["FileName"] = readmeFile.Name()
  169. if isTextFile {
  170. d, _ := ioutil.ReadAll(dataRc)
  171. buf = append(buf, d...)
  172. switch {
  173. case base.IsMarkdownFile(readmeFile.Name()):
  174. buf = base.RenderMarkdown(buf, treeLink, ctx.Repo.Repository.ComposeMetas())
  175. default:
  176. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  177. }
  178. ctx.Data["FileContent"] = string(buf)
  179. }
  180. }
  181. }
  182. lastCommit := ctx.Repo.Commit
  183. if len(treePath) > 0 {
  184. c, err := ctx.Repo.Commit.GetCommitByPath(treePath)
  185. if err != nil {
  186. ctx.Handle(500, "GetCommitByPath", err)
  187. return
  188. }
  189. lastCommit = c
  190. }
  191. ctx.Data["LastCommit"] = lastCommit
  192. ctx.Data["LastCommitUser"] = models.ValidateCommitWithEmail(lastCommit)
  193. }
  194. ctx.Data["Username"] = userName
  195. ctx.Data["Reponame"] = repoName
  196. var treenames []string
  197. Paths := make([]string, 0)
  198. if len(treename) > 0 {
  199. treenames = strings.Split(treename, "/")
  200. for i, _ := range treenames {
  201. Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
  202. }
  203. ctx.Data["HasParentPath"] = true
  204. if len(Paths)-2 >= 0 {
  205. ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
  206. }
  207. }
  208. ctx.Data["Paths"] = Paths
  209. ctx.Data["TreeName"] = treename
  210. ctx.Data["Treenames"] = treenames
  211. ctx.Data["TreePath"] = treePath
  212. ctx.Data["BranchLink"] = branchLink
  213. ctx.HTML(200, HOME)
  214. }
  215. func renderItems(ctx *middleware.Context, total int, getter func(page int) ([]*models.User, error)) {
  216. page := ctx.QueryInt("page")
  217. if page <= 0 {
  218. page = 1
  219. }
  220. pager := paginater.New(total, models.ItemsPerPage, page, 5)
  221. ctx.Data["Page"] = pager
  222. items, err := getter(pager.Current())
  223. if err != nil {
  224. ctx.Handle(500, "getter", err)
  225. return
  226. }
  227. ctx.Data["Watchers"] = items
  228. ctx.HTML(200, WATCHERS)
  229. }
  230. func Watchers(ctx *middleware.Context) {
  231. ctx.Data["Title"] = ctx.Tr("repo.watchers")
  232. ctx.Data["PageIsWatchers"] = true
  233. renderItems(ctx, ctx.Repo.Repository.NumWatches, ctx.Repo.Repository.GetWatchers)
  234. }
  235. func Stars(ctx *middleware.Context) {
  236. ctx.Data["Title"] = ctx.Tr("repo.stargazers")
  237. ctx.Data["PageIsStargazers"] = true
  238. renderItems(ctx, ctx.Repo.Repository.NumStars, ctx.Repo.Repository.GetStargazers)
  239. }
  240. func Forks(ctx *middleware.Context) {
  241. ctx.Data["Title"] = ctx.Tr("repos.forks")
  242. forks, err := ctx.Repo.Repository.GetForks()
  243. if err != nil {
  244. ctx.Handle(500, "GetForks", err)
  245. return
  246. }
  247. for _, fork := range forks {
  248. if err = fork.GetOwner(); err != nil {
  249. ctx.Handle(500, "GetOwner", err)
  250. return
  251. }
  252. }
  253. ctx.Data["Forks"] = forks
  254. ctx.HTML(200, FORKS)
  255. }