single.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. "fmt"
  7. "strings"
  8. "github.com/codegangsta/martini"
  9. "github.com/gogits/git"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/log"
  13. "github.com/gogits/gogs/modules/middleware"
  14. )
  15. func Branches(ctx *middleware.Context, params martini.Params) {
  16. if !ctx.Repo.IsValid {
  17. return
  18. }
  19. brs, err := models.GetBranches(params["username"], params["reponame"])
  20. if err != nil {
  21. ctx.Handle(200, "repo.Branches", err)
  22. return
  23. } else if len(brs) == 0 {
  24. ctx.Error(404)
  25. return
  26. }
  27. ctx.Data["Username"] = params["username"]
  28. ctx.Data["Reponame"] = params["reponame"]
  29. ctx.Data["Branchname"] = brs[0]
  30. ctx.Data["Branches"] = brs
  31. ctx.Data["IsRepoToolbarBranches"] = true
  32. ctx.HTML(200, "repo/branches", ctx.Data)
  33. }
  34. func Single(ctx *middleware.Context, params martini.Params) {
  35. if !ctx.Repo.IsValid {
  36. return
  37. }
  38. if params["branchname"] == "" {
  39. params["branchname"] = "master"
  40. }
  41. // Get tree path
  42. treename := params["_1"]
  43. if len(treename) > 0 && treename[len(treename)-1] == '/' {
  44. ctx.Redirect("/"+ctx.Repo.Owner.LowerName+"/"+
  45. ctx.Repo.Repository.Name+"/tree/"+params["branchname"]+"/"+treename[:len(treename)-1], 302)
  46. return
  47. }
  48. // Branches.
  49. brs, err := models.GetBranches(params["username"], params["reponame"])
  50. if err != nil {
  51. log.Error("repo.Single(GetBranches): %v", err)
  52. ctx.Error(404)
  53. return
  54. } else if len(brs) == 0 {
  55. ctx.Data["IsBareRepo"] = true
  56. ctx.HTML(200, "repo/single", ctx.Data)
  57. return
  58. }
  59. ctx.Data["Branches"] = brs
  60. // Directory and file list.
  61. files, err := models.GetReposFiles(params["username"], params["reponame"],
  62. params["branchname"], params["commitid"], treename)
  63. if err != nil {
  64. log.Error("repo.Single(GetReposFiles): %v", err)
  65. ctx.Error(404)
  66. return
  67. }
  68. ctx.Data["Username"] = params["username"]
  69. ctx.Data["Reponame"] = params["reponame"]
  70. ctx.Data["Branchname"] = params["branchname"]
  71. var treenames []string
  72. Paths := make([]string, 0)
  73. if len(treename) > 0 {
  74. treenames = strings.Split(treename, "/")
  75. for i, _ := range treenames {
  76. Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
  77. }
  78. ctx.Data["HasParentPath"] = true
  79. if len(Paths)-2 >= 0 {
  80. ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
  81. }
  82. }
  83. // Get latest commit according username and repo name
  84. commit, err := models.GetCommit(params["username"], params["reponame"],
  85. params["branchname"], params["commitid"])
  86. if err != nil {
  87. log.Error("repo.Single(GetCommit): %v", err)
  88. ctx.Error(404)
  89. return
  90. }
  91. ctx.Data["LastCommit"] = commit
  92. var readmeFile *models.RepoFile
  93. for _, f := range files {
  94. if !f.IsFile() || len(f.Name) < 6 {
  95. continue
  96. } else if strings.ToLower(f.Name[:6]) == "readme" {
  97. readmeFile = f
  98. break
  99. }
  100. }
  101. if readmeFile != nil {
  102. ctx.Data["ReadmeExist"] = true
  103. // if file large than 1M not show it
  104. if readmeFile.Size > 1024*1024 || readmeFile.Filemode != git.FileModeBlob {
  105. ctx.Data["FileIsLarge"] = true
  106. } else if blob, err := readmeFile.LookupBlob(); err != nil {
  107. ctx.Data["ReadmeExist"] = false
  108. } else {
  109. // current repo branch link
  110. urlPrefix := "http://" + base.Domain + "/" + ctx.Repo.Owner.LowerName + "/" +
  111. ctx.Repo.Repository.Name + "/tree/" + params["branchname"]
  112. ctx.Data["ReadmeContent"] = string(base.RenderMarkdown(blob.Contents(), urlPrefix))
  113. }
  114. }
  115. log.Trace("repo.Single: Paths: %s", strings.Join(Paths, ", "))
  116. ctx.Data["Paths"] = Paths
  117. ctx.Data["Treenames"] = treenames
  118. ctx.Data["IsRepoToolbarSource"] = true
  119. ctx.Data["Files"] = files
  120. ctx.HTML(200, "repo/single", ctx.Data)
  121. }
  122. func Setting(ctx *middleware.Context, params martini.Params) {
  123. if !ctx.Repo.IsOwner {
  124. ctx.Error(404)
  125. return
  126. }
  127. // Branches.
  128. brs, err := models.GetBranches(params["username"], params["reponame"])
  129. if err != nil {
  130. log.Error("repo.Setting(GetBranches): %v", err)
  131. ctx.Error(404)
  132. return
  133. } else if len(brs) == 0 {
  134. ctx.Data["IsBareRepo"] = true
  135. ctx.HTML(200, "repo/setting", ctx.Data)
  136. return
  137. }
  138. var title string
  139. if t, ok := ctx.Data["Title"].(string); ok {
  140. title = t
  141. }
  142. ctx.Data["Title"] = title + " - settings"
  143. ctx.Data["IsRepoToolbarSetting"] = true
  144. ctx.HTML(200, "repo/setting", ctx.Data)
  145. }
  146. func Commits(ctx *middleware.Context, params martini.Params) {
  147. brs, err := models.GetBranches(params["username"], params["reponame"])
  148. if err != nil {
  149. ctx.Handle(200, "repo.Commits", err)
  150. return
  151. } else if len(brs) == 0 {
  152. ctx.Error(404)
  153. return
  154. }
  155. ctx.Data["IsRepoToolbarCommits"] = true
  156. commits, err := models.GetCommits(params["username"],
  157. params["reponame"], params["branchname"])
  158. if err != nil {
  159. ctx.Error(404)
  160. return
  161. }
  162. ctx.Data["Username"] = params["username"]
  163. ctx.Data["Reponame"] = params["reponame"]
  164. ctx.Data["CommitCount"] = commits.Len()
  165. ctx.Data["Commits"] = commits
  166. ctx.HTML(200, "repo/commits", ctx.Data)
  167. }
  168. func Issues(ctx *middleware.Context) {
  169. ctx.Data["IsRepoToolbarIssues"] = true
  170. ctx.HTML(200, "repo/issues", ctx.Data)
  171. }
  172. func Pulls(ctx *middleware.Context) {
  173. ctx.Data["IsRepoToolbarPulls"] = true
  174. ctx.HTML(200, "repo/pulls", ctx.Data)
  175. }
  176. func Action(ctx *middleware.Context, params martini.Params) {
  177. var err error
  178. switch params["action"] {
  179. case "watch":
  180. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, true)
  181. case "unwatch":
  182. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, false)
  183. }
  184. if err != nil {
  185. log.Error("repo.Action(%s): %v", params["action"], err)
  186. ctx.JSON(200, map[string]interface{}{
  187. "ok": false,
  188. "err": err.Error(),
  189. })
  190. return
  191. }
  192. ctx.JSON(200, map[string]interface{}{
  193. "ok": true,
  194. })
  195. }