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.

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