single.go 6.6 KB

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