You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

view.go 6.3 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.Repo.TreeName
  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, branchLink))
  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. // Render issue index links.
  132. for _, f := range files {
  133. switch c := f[1].(type) {
  134. case *git.Commit:
  135. c.CommitMessage = string(base.RenderIssueIndexPattern([]byte(c.CommitMessage), ctx.Repo.RepoLink))
  136. case *git.SubModuleFile:
  137. c.CommitMessage = string(base.RenderIssueIndexPattern([]byte(c.CommitMessage), ctx.Repo.RepoLink))
  138. }
  139. }
  140. ctx.Data["Files"] = files
  141. var readmeFile *git.Blob
  142. for _, f := range entries {
  143. if f.IsDir() || !base.IsReadmeFile(f.Name()) {
  144. continue
  145. } else {
  146. readmeFile = f.Blob()
  147. break
  148. }
  149. }
  150. if readmeFile != nil {
  151. ctx.Data["ReadmeInList"] = true
  152. ctx.Data["ReadmeExist"] = true
  153. if dataRc, err := readmeFile.Data(); err != nil {
  154. ctx.Handle(404, "repo.SinglereadmeFile.LookupBlob", err)
  155. return
  156. } else {
  157. buf := make([]byte, 1024)
  158. n, _ := dataRc.Read(buf)
  159. if n > 0 {
  160. buf = buf[:n]
  161. }
  162. ctx.Data["FileSize"] = readmeFile.Size()
  163. ctx.Data["FileLink"] = rawLink + "/" + treename
  164. _, isTextFile := base.IsTextFile(buf)
  165. ctx.Data["FileIsText"] = isTextFile
  166. ctx.Data["FileName"] = readmeFile.Name()
  167. if isTextFile {
  168. d, _ := ioutil.ReadAll(dataRc)
  169. buf = append(buf, d...)
  170. switch {
  171. case base.IsMarkdownFile(readmeFile.Name()):
  172. buf = base.RenderMarkdown(buf, branchLink)
  173. default:
  174. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  175. }
  176. ctx.Data["FileContent"] = string(buf)
  177. }
  178. }
  179. }
  180. lastCommit := ctx.Repo.Commit
  181. lastCommit.CommitMessage = string(base.RenderIssueIndexPattern([]byte(lastCommit.CommitMessage), ctx.Repo.RepoLink))
  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. }