single.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. ctx.Data["ReadmeExist"] = true
  80. // if file large than 1M not show it
  81. if readmeFile.Size > 1024*1024 || readmeFile.Filemode != git.FileModeBlob {
  82. ctx.Data["FileIsLarge"] = true
  83. } else if blob, err := readmeFile.LookupBlob(); err != nil {
  84. ctx.Data["FileIsLarge"] = true
  85. } else {
  86. ctx.Data["ReadmeContent"] = string(base.RenderMarkdown(blob.Contents()))
  87. }
  88. }
  89. ctx.Data["Paths"] = Paths
  90. ctx.Data["Treenames"] = treenames
  91. ctx.Data["IsRepoToolbarSource"] = true
  92. ctx.Data["Files"] = files
  93. ctx.Render.HTML(200, "repo/single", ctx.Data)
  94. }
  95. func Setting(ctx *middleware.Context, params martini.Params) {
  96. if !ctx.Repo.IsOwner {
  97. ctx.Render.Error(404)
  98. return
  99. }
  100. var title string
  101. if t, ok := ctx.Data["Title"].(string); ok {
  102. title = t
  103. }
  104. ctx.Data["Title"] = title + " - settings"
  105. ctx.Data["IsRepoToolbarSetting"] = true
  106. ctx.Render.HTML(200, "repo/setting", ctx.Data)
  107. }
  108. func Commits(ctx *middleware.Context, params martini.Params) {
  109. ctx.Data["IsRepoToolbarCommits"] = true
  110. commits, err := models.GetCommits(params["username"],
  111. params["reponame"], params["branchname"])
  112. if err != nil {
  113. ctx.Render.Error(404)
  114. return
  115. }
  116. ctx.Data["Commits"] = commits
  117. ctx.Render.HTML(200, "repo/commits", ctx.Data)
  118. }
  119. func Issues(ctx *middleware.Context) string {
  120. return "This is issues page"
  121. }
  122. func Pulls(ctx *middleware.Context) string {
  123. return "This is pulls page"
  124. }