single.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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() || len(f.Name) < 6 {
  72. continue
  73. } else if strings.ToLower(f.Name[:6]) == "readme" {
  74. readmeFile = f
  75. break
  76. }
  77. }
  78. if readmeFile != nil {
  79. // if file large than 1M not show it
  80. if readmeFile.Size > 1024*1024 || readmeFile.Filemode != git.FileModeBlob {
  81. ctx.Data["FileIsLarge"] = true
  82. } else if blob, err := readmeFile.LookupBlob(); err != nil {
  83. ctx.Data["FileIsLarge"] = true
  84. } else {
  85. ctx.Data["ReadmeContent"] = string(base.RenderMarkdown(blob.Contents()))
  86. }
  87. }
  88. ctx.Data["Paths"] = Paths
  89. ctx.Data["Treenames"] = treenames
  90. ctx.Data["IsRepoToolbarSource"] = true
  91. ctx.Data["Files"] = files
  92. ctx.Render.HTML(200, "repo/single", ctx.Data)
  93. }
  94. func Setting(ctx *middleware.Context, params martini.Params) {
  95. if !ctx.Repo.IsOwner {
  96. ctx.Render.Error(404)
  97. return
  98. }
  99. var title string
  100. if t, ok := ctx.Data["Title"].(string); ok {
  101. title = t
  102. }
  103. ctx.Data["Title"] = title + " - settings"
  104. ctx.Data["IsRepoToolbarSetting"] = true
  105. ctx.Render.HTML(200, "repo/setting", ctx.Data)
  106. }
  107. func Commits(ctx *middleware.Context) {
  108. ctx.Data["IsRepoToolbarCommits"] = true
  109. ctx.Render.HTML(200, "repo/commits", ctx.Data)
  110. }
  111. func Issues(ctx *middleware.Context) string {
  112. return "This is issues page"
  113. }
  114. func Pulls(ctx *middleware.Context) string {
  115. return "This is pulls page"
  116. }