single.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "strings"
  7. "github.com/codegangsta/martini"
  8. "github.com/gogits/git"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/middleware"
  12. )
  13. func Branches(ctx *middleware.Context, params martini.Params) {
  14. if !ctx.Repo.IsValid {
  15. return
  16. }
  17. ctx.Data["Username"] = params["username"]
  18. ctx.Data["Reponame"] = params["reponame"]
  19. brs, err := models.GetBranches(params["username"], params["reponame"])
  20. if err != nil {
  21. ctx.Handle(200, "repo.Branches", err)
  22. return
  23. }
  24. ctx.Data["Branches"] = brs
  25. ctx.Data["IsRepoToolbarBranches"] = true
  26. ctx.Render.HTML(200, "repo/branches", ctx.Data)
  27. }
  28. func Single(ctx *middleware.Context, params martini.Params) {
  29. if !ctx.Repo.IsValid {
  30. return
  31. }
  32. if params["branchname"] == "" {
  33. params["branchname"] = "master"
  34. }
  35. // Get tree path
  36. treename := params["_1"]
  37. // Directory and file list.
  38. files, err := models.GetReposFiles(params["username"], params["reponame"],
  39. params["branchname"], treename)
  40. if err != nil {
  41. ctx.Render.Error(404)
  42. return
  43. }
  44. ctx.Data["Username"] = params["username"]
  45. ctx.Data["Reponame"] = params["reponame"]
  46. ctx.Data["Branchname"] = params["branchname"]
  47. // Branches.
  48. brs, err := models.GetBranches(params["username"], params["reponame"])
  49. if err != nil {
  50. ctx.Render.Error(404)
  51. return
  52. }
  53. ctx.Data["Branches"] = brs
  54. var treenames []string
  55. Paths := make([]string, 0)
  56. if len(treename) > 0 {
  57. treenames = strings.Split(treename, "/")
  58. for i, _ := range treenames {
  59. Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
  60. }
  61. }
  62. // Get latest commit according username and repo name
  63. commit, err := models.GetLastestCommit(params["username"], params["reponame"])
  64. if err != nil {
  65. ctx.Render.Error(404)
  66. return
  67. }
  68. ctx.Data["LatestCommit"] = commit
  69. var readmeFile *models.RepoFile
  70. for _, f := range files {
  71. if !f.IsFile() {
  72. continue
  73. }
  74. if len(f.Name) < 6 {
  75. continue
  76. }
  77. if strings.ToLower(f.Name[:6]) == "readme" {
  78. readmeFile = f
  79. break
  80. }
  81. }
  82. if readmeFile != nil {
  83. // if file large than 1M not show it
  84. if readmeFile.Size > 1024*1024 || readmeFile.Filemode != git.FileModeBlob {
  85. ctx.Data["FileIsLarge"] = true
  86. } else if blob, err := readmeFile.LookupBlob(); err != nil {
  87. ctx.Data["FileIsLarge"] = true
  88. } else {
  89. ctx.Data["ReadmeContent"] = string(base.RenderMarkdown(blob.Contents()))
  90. }
  91. }
  92. ctx.Data["Paths"] = Paths
  93. ctx.Data["Treenames"] = treenames
  94. ctx.Data["IsRepoToolbarSource"] = true
  95. ctx.Data["Files"] = files
  96. ctx.Render.HTML(200, "repo/single", ctx.Data)
  97. }
  98. func Setting(ctx *middleware.Context, params martini.Params) {
  99. if !ctx.Repo.IsOwner {
  100. ctx.Render.Error(404)
  101. return
  102. }
  103. var title string
  104. if t, ok := ctx.Data["Title"].(string); ok {
  105. title = t
  106. }
  107. ctx.Data["Title"] = title + " - settings"
  108. ctx.Data["IsRepoToolbarSetting"] = true
  109. ctx.Render.HTML(200, "repo/setting", ctx.Data)
  110. }
  111. func Commits(ctx *middleware.Context) string {
  112. return "This is commits page"
  113. }
  114. func Issues(ctx *middleware.Context) string {
  115. return "This is issues page"
  116. }
  117. func Pulls(ctx *middleware.Context) string {
  118. return "This is pulls page"
  119. }