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.

view.go 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. "fmt"
  8. gotemplate "html/template"
  9. "io/ioutil"
  10. "path"
  11. "strings"
  12. "github.com/Unknwon/paginater"
  13. "github.com/gogits/git-module"
  14. "github.com/gogits/gogs/models"
  15. "github.com/gogits/gogs/modules/base"
  16. "github.com/gogits/gogs/modules/context"
  17. "github.com/gogits/gogs/modules/log"
  18. "github.com/gogits/gogs/modules/markdown"
  19. "github.com/gogits/gogs/modules/setting"
  20. "github.com/gogits/gogs/modules/template"
  21. "github.com/gogits/gogs/modules/template/highlight"
  22. )
  23. const (
  24. HOME base.TplName = "repo/home"
  25. WATCHERS base.TplName = "repo/watchers"
  26. FORKS base.TplName = "repo/forks"
  27. )
  28. func Home(ctx *context.Context) {
  29. title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
  30. if len(ctx.Repo.Repository.Description) > 0 {
  31. title += ": " + ctx.Repo.Repository.Description
  32. }
  33. ctx.Data["Title"] = title
  34. ctx.Data["PageIsViewCode"] = true
  35. ctx.Data["RequireHighlightJS"] = true
  36. branchName := ctx.Repo.BranchName
  37. userName := ctx.Repo.Owner.Name
  38. repoName := ctx.Repo.Repository.Name
  39. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  40. treeLink := branchLink
  41. rawLink := ctx.Repo.RepoLink + "/raw/" + branchName
  42. // newFileLink := ctx.Repo.RepoLink + "/_new/" + branchName
  43. // uploadFileLink := ctx.Repo.RepoLink + "/upload/" + branchName
  44. treePath := ctx.Repo.TreePath
  45. if len(treePath) > 0 {
  46. treeLink += "/" + treePath
  47. }
  48. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treePath)
  49. if err != nil {
  50. if git.IsErrNotExist(err) {
  51. ctx.Handle(404, "GetTreeEntryByPath", err)
  52. } else {
  53. ctx.Handle(500, "GetTreeEntryByPath", err)
  54. }
  55. return
  56. }
  57. if !entry.IsDir() {
  58. blob := entry.Blob()
  59. dataRc, err := blob.Data()
  60. if err != nil {
  61. ctx.Handle(404, "blob.Data", err)
  62. return
  63. }
  64. ctx.Data["FileSize"] = blob.Size()
  65. ctx.Data["IsFile"] = true
  66. ctx.Data["FileName"] = blob.Name()
  67. ctx.Data["HighlightClass"] = highlight.FileNameToHighlightClass(blob.Name())
  68. ctx.Data["FileLink"] = rawLink + "/" + treePath
  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. _, isPDFFile := base.IsPDFFile(buf)
  77. ctx.Data["IsFileText"] = isTextFile
  78. // Assume file is not editable first.
  79. if !isTextFile {
  80. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_non_text_files")
  81. }
  82. switch {
  83. case isPDFFile:
  84. ctx.Data["IsPDFFile"] = true
  85. case isImageFile:
  86. ctx.Data["IsImageFile"] = true
  87. case isTextFile:
  88. if blob.Size() >= setting.UI.MaxDisplayFileSize {
  89. ctx.Data["IsFileTooLarge"] = true
  90. } else {
  91. d, _ := ioutil.ReadAll(dataRc)
  92. buf = append(buf, d...)
  93. isMarkdown := markdown.IsMarkdownFile(blob.Name())
  94. ctx.Data["IsMarkdown"] = isMarkdown
  95. readmeExist := isMarkdown || markdown.IsReadmeFile(blob.Name())
  96. ctx.Data["ReadmeExist"] = readmeExist
  97. if readmeExist {
  98. // TODO: don't need to render if it's a README but not Markdown file.
  99. ctx.Data["FileContent"] = string(markdown.Render(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
  100. } else {
  101. // Building code view blocks with line number on server side.
  102. var filecontent string
  103. if err, content := template.ToUTF8WithErr(buf); err != nil {
  104. if err != nil {
  105. log.Error(4, "ToUTF8WithErr: %s", err)
  106. }
  107. filecontent = string(buf)
  108. } else {
  109. filecontent = content
  110. }
  111. var output bytes.Buffer
  112. lines := strings.Split(filecontent, "\n")
  113. for index, line := range lines {
  114. output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(line)) + "\n")
  115. }
  116. ctx.Data["FileContent"] = gotemplate.HTML(output.String())
  117. output.Reset()
  118. for i := 0; i < len(lines); i++ {
  119. output.WriteString(fmt.Sprintf(`<span id="L%d">%d</span>`, i+1, i+1))
  120. }
  121. ctx.Data["LineNums"] = gotemplate.HTML(output.String())
  122. }
  123. }
  124. if ctx.Repo.IsWriter() && ctx.Repo.IsViewBranch {
  125. ctx.Data["CanEditFile"] = true
  126. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
  127. } else if !ctx.Repo.IsViewBranch {
  128. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
  129. } else if !ctx.Repo.IsWriter() {
  130. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit")
  131. }
  132. }
  133. if ctx.Repo.IsWriter() && ctx.Repo.IsViewBranch {
  134. ctx.Data["CanDeleteFile"] = true
  135. ctx.Data["FileDeleteLinkTooltip"] = ctx.Tr("repo.delete_this_file")
  136. } else {
  137. if !ctx.Repo.IsViewBranch {
  138. ctx.Data["FileDeleteLinkTooltip"] = ctx.Tr("repo.must_be_on_branch")
  139. } else if !ctx.Repo.IsWriter() {
  140. ctx.Data["FileDeleteLinkTooltip"] = ctx.Tr("repo.must_be_writer")
  141. }
  142. }
  143. } else {
  144. // Directory and file list.
  145. tree, err := ctx.Repo.Commit.SubTree(treePath)
  146. if err != nil {
  147. ctx.Handle(404, "SubTree", err)
  148. return
  149. }
  150. entries, err := tree.ListEntries()
  151. if err != nil {
  152. ctx.Handle(500, "ListEntries", err)
  153. return
  154. }
  155. entries.Sort()
  156. ctx.Data["Files"], err = entries.GetCommitsInfo(ctx.Repo.Commit, treePath)
  157. if err != nil {
  158. ctx.Handle(500, "GetCommitsInfo", err)
  159. return
  160. }
  161. var readmeFile *git.Blob
  162. for _, f := range entries {
  163. if f.IsDir() || !markdown.IsReadmeFile(f.Name()) {
  164. continue
  165. } else {
  166. readmeFile = f.Blob()
  167. break
  168. }
  169. }
  170. if readmeFile != nil {
  171. ctx.Data["ReadmeInList"] = true
  172. ctx.Data["ReadmeExist"] = true
  173. if dataRc, err := readmeFile.Data(); err != nil {
  174. ctx.Handle(404, "repo.SinglereadmeFile.Data", err)
  175. return
  176. } else {
  177. buf := make([]byte, 1024)
  178. n, _ := dataRc.Read(buf)
  179. if n > 0 {
  180. buf = buf[:n]
  181. }
  182. ctx.Data["FileSize"] = readmeFile.Size()
  183. ctx.Data["FileLink"] = rawLink + "/" + treePath
  184. _, isTextFile := base.IsTextFile(buf)
  185. ctx.Data["FileIsText"] = isTextFile
  186. ctx.Data["FileName"] = readmeFile.Name()
  187. if isTextFile {
  188. d, _ := ioutil.ReadAll(dataRc)
  189. buf = append(buf, d...)
  190. switch {
  191. case markdown.IsMarkdownFile(readmeFile.Name()):
  192. ctx.Data["IsMarkdown"] = true
  193. buf = markdown.Render(buf, treeLink, ctx.Repo.Repository.ComposeMetas())
  194. default:
  195. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  196. }
  197. ctx.Data["FileContent"] = string(buf)
  198. }
  199. }
  200. }
  201. lastCommit := ctx.Repo.Commit
  202. if len(treePath) > 0 {
  203. c, err := ctx.Repo.Commit.GetCommitByPath(treePath)
  204. if err != nil {
  205. ctx.Handle(500, "GetCommitByPath", err)
  206. return
  207. }
  208. lastCommit = c
  209. }
  210. ctx.Data["LastCommit"] = lastCommit
  211. ctx.Data["LastCommitUser"] = models.ValidateCommitWithEmail(lastCommit)
  212. // if ctx.Repo.IsWriter() && ctx.Repo.IsViewBranch {
  213. // ctx.Data["NewFileLink"] = newFileLink + "/" + treePath
  214. // if setting.Repository.Upload.Enabled {
  215. // ctx.Data["UploadFileLink"] = uploadFileLink + "/" + treePath
  216. // }
  217. // }
  218. }
  219. ctx.Data["Username"] = userName
  220. ctx.Data["Reponame"] = repoName
  221. ec, err := ctx.Repo.GetEditorconfig()
  222. if err != nil && !git.IsErrNotExist(err) {
  223. ctx.Handle(500, "ErrGettingEditorconfig", err)
  224. return
  225. }
  226. ctx.Data["Editorconfig"] = ec
  227. var treenames []string
  228. paths := make([]string, 0)
  229. if len(treePath) > 0 {
  230. treenames = strings.Split(treePath, "/")
  231. for i := range treenames {
  232. paths = append(paths, strings.Join(treenames[0:i+1], "/"))
  233. }
  234. ctx.Data["HasParentPath"] = true
  235. if len(paths)-2 >= 0 {
  236. ctx.Data["ParentPath"] = "/" + paths[len(paths)-2]
  237. }
  238. }
  239. ctx.Data["Paths"] = paths
  240. ctx.Data["TreePath"] = treePath
  241. ctx.Data["TreeLink"] = treeLink
  242. ctx.Data["Treenames"] = treenames
  243. ctx.Data["BranchLink"] = branchLink
  244. ctx.HTML(200, HOME)
  245. }
  246. func RenderUserCards(ctx *context.Context, total int, getter func(page int) ([]*models.User, error), tpl base.TplName) {
  247. page := ctx.QueryInt("page")
  248. if page <= 0 {
  249. page = 1
  250. }
  251. pager := paginater.New(total, models.ItemsPerPage, page, 5)
  252. ctx.Data["Page"] = pager
  253. items, err := getter(pager.Current())
  254. if err != nil {
  255. ctx.Handle(500, "getter", err)
  256. return
  257. }
  258. ctx.Data["Cards"] = items
  259. ctx.HTML(200, tpl)
  260. }
  261. func Watchers(ctx *context.Context) {
  262. ctx.Data["Title"] = ctx.Tr("repo.watchers")
  263. ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers")
  264. ctx.Data["PageIsWatchers"] = true
  265. RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, ctx.Repo.Repository.GetWatchers, WATCHERS)
  266. }
  267. func Stars(ctx *context.Context) {
  268. ctx.Data["Title"] = ctx.Tr("repo.stargazers")
  269. ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers")
  270. ctx.Data["PageIsStargazers"] = true
  271. RenderUserCards(ctx, ctx.Repo.Repository.NumStars, ctx.Repo.Repository.GetStargazers, WATCHERS)
  272. }
  273. func Forks(ctx *context.Context) {
  274. ctx.Data["Title"] = ctx.Tr("repos.forks")
  275. forks, err := ctx.Repo.Repository.GetForks()
  276. if err != nil {
  277. ctx.Handle(500, "GetForks", err)
  278. return
  279. }
  280. for _, fork := range forks {
  281. if err = fork.GetOwner(); err != nil {
  282. ctx.Handle(500, "GetOwner", err)
  283. return
  284. }
  285. }
  286. ctx.Data["Forks"] = forks
  287. ctx.HTML(200, FORKS)
  288. }