view.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. treeLink := branchLink
  28. rawLink := ctx.Repo.RepoLink + "/raw/" + branchName
  29. // Get tree path
  30. treename := ctx.Repo.TreeName
  31. if len(treename) > 0 {
  32. if treename[len(treename)-1] == '/' {
  33. ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1])
  34. return
  35. }
  36. treeLink += "/" + treename
  37. }
  38. ctx.Data["IsRepoToolbarSource"] = true
  39. isViewBranch := ctx.Repo.IsBranch
  40. ctx.Data["IsViewBranch"] = isViewBranch
  41. treePath := treename
  42. if len(treePath) != 0 {
  43. treePath = treePath + "/"
  44. }
  45. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treename)
  46. if err != nil && err != git.ErrNotExist {
  47. ctx.Handle(404, "GetTreeEntryByPath", err)
  48. return
  49. }
  50. if len(treename) != 0 && entry == nil {
  51. ctx.Handle(404, "repo.Home", nil)
  52. return
  53. }
  54. if entry != nil && !entry.IsDir() {
  55. blob := entry.Blob()
  56. if dataRc, err := blob.Data(); err != nil {
  57. ctx.Handle(404, "blob.Data", err)
  58. return
  59. } else {
  60. ctx.Data["FileSize"] = blob.Size()
  61. ctx.Data["IsFile"] = true
  62. ctx.Data["FileName"] = blob.Name()
  63. ext := path.Ext(blob.Name())
  64. if len(ext) > 0 {
  65. ext = ext[1:]
  66. }
  67. ctx.Data["FileExt"] = ext
  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 := base.IsMarkdownFile(blob.Name()) || base.IsReadmeFile(blob.Name())
  84. ctx.Data["ReadmeExist"] = readmeExist
  85. if readmeExist {
  86. ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, path.Dir(treeLink)))
  87. } else {
  88. if err, content := base.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(treename)
  107. if err != nil {
  108. ctx.Handle(500, "ListEntries", err)
  109. return
  110. }
  111. entries.Sort()
  112. files := make([][]interface{}, 0, len(entries))
  113. for _, te := range entries {
  114. if te.Type != git.COMMIT {
  115. c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name()))
  116. if err != nil {
  117. ctx.Handle(500, "GetCommitOfRelPath", err)
  118. return
  119. }
  120. files = append(files, []interface{}{te, c})
  121. } else {
  122. sm, err := ctx.Repo.Commit.GetSubModule(path.Join(treename, te.Name()))
  123. if err != nil {
  124. ctx.Handle(500, "GetSubModule", err)
  125. return
  126. }
  127. smUrl := ""
  128. if sm != nil {
  129. smUrl = sm.Url
  130. }
  131. c, err := ctx.Repo.Commit.GetCommitOfRelPath(filepath.Join(treePath, te.Name()))
  132. if err != nil {
  133. ctx.Handle(500, "GetCommitOfRelPath", err)
  134. return
  135. }
  136. files = append(files, []interface{}{te, git.NewSubModuleFile(c, smUrl, te.ID.String())})
  137. }
  138. }
  139. ctx.Data["Files"] = files
  140. var readmeFile *git.Blob
  141. for _, f := range entries {
  142. if f.IsDir() || !base.IsReadmeFile(f.Name()) {
  143. continue
  144. } else {
  145. readmeFile = f.Blob()
  146. break
  147. }
  148. }
  149. if readmeFile != nil {
  150. ctx.Data["ReadmeInList"] = true
  151. ctx.Data["ReadmeExist"] = true
  152. if dataRc, err := readmeFile.Data(); err != nil {
  153. ctx.Handle(404, "repo.SinglereadmeFile.LookupBlob", err)
  154. return
  155. } else {
  156. buf := make([]byte, 1024)
  157. n, _ := dataRc.Read(buf)
  158. if n > 0 {
  159. buf = buf[:n]
  160. }
  161. ctx.Data["FileSize"] = readmeFile.Size()
  162. ctx.Data["FileLink"] = rawLink + "/" + treename
  163. _, isTextFile := base.IsTextFile(buf)
  164. ctx.Data["FileIsText"] = isTextFile
  165. ctx.Data["FileName"] = readmeFile.Name()
  166. if isTextFile {
  167. d, _ := ioutil.ReadAll(dataRc)
  168. buf = append(buf, d...)
  169. switch {
  170. case base.IsMarkdownFile(readmeFile.Name()):
  171. buf = base.RenderMarkdown(buf, treeLink)
  172. default:
  173. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  174. }
  175. ctx.Data["FileContent"] = string(buf)
  176. }
  177. }
  178. }
  179. lastCommit := ctx.Repo.Commit
  180. if len(treePath) > 0 {
  181. c, err := ctx.Repo.Commit.GetCommitOfRelPath(treePath)
  182. if err != nil {
  183. ctx.Handle(500, "GetCommitOfRelPath", err)
  184. return
  185. }
  186. lastCommit = c
  187. }
  188. ctx.Data["LastCommit"] = lastCommit
  189. ctx.Data["LastCommitUser"] = models.ValidateCommitWithEmail(lastCommit)
  190. }
  191. ctx.Data["Username"] = userName
  192. ctx.Data["Reponame"] = repoName
  193. var treenames []string
  194. Paths := make([]string, 0)
  195. if len(treename) > 0 {
  196. treenames = strings.Split(treename, "/")
  197. for i, _ := range treenames {
  198. Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
  199. }
  200. ctx.Data["HasParentPath"] = true
  201. if len(Paths)-2 >= 0 {
  202. ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
  203. }
  204. }
  205. ctx.Data["Paths"] = Paths
  206. ctx.Data["TreeName"] = treename
  207. ctx.Data["Treenames"] = treenames
  208. ctx.Data["TreePath"] = treePath
  209. ctx.Data["BranchLink"] = branchLink
  210. ctx.HTML(200, HOME)
  211. }