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.

238 lines
5.7 KiB

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