You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

single.go 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. urlPrefix := "http://" + base.Domain + branchLink
  115. ctx.Data["FileName"] = readmeFile.Name
  116. ctx.Data["FileContent"] = string(base.RenderMarkdown(blob.Contents(), urlPrefix))
  117. }
  118. }
  119. }
  120. ctx.Data["Username"] = params["username"]
  121. ctx.Data["Reponame"] = params["reponame"]
  122. ctx.Data["Branchname"] = params["branchname"]
  123. var treenames []string
  124. Paths := make([]string, 0)
  125. if len(treename) > 0 {
  126. treenames = strings.Split(treename, "/")
  127. for i, _ := range treenames {
  128. Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
  129. }
  130. ctx.Data["HasParentPath"] = true
  131. if len(Paths)-2 >= 0 {
  132. ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
  133. }
  134. }
  135. // Get latest commit according username and repo name
  136. commit, err := models.GetCommit(params["username"], params["reponame"],
  137. params["branchname"], params["commitid"])
  138. if err != nil {
  139. log.Error("repo.Single(GetCommit): %v", err)
  140. ctx.Error(404)
  141. return
  142. }
  143. ctx.Data["LastCommit"] = commit
  144. ctx.Data["Paths"] = Paths
  145. ctx.Data["Treenames"] = treenames
  146. ctx.Data["IsRepoToolbarSource"] = true
  147. ctx.Data["BranchLink"] = branchLink
  148. ctx.HTML(200, "repo/single", ctx.Data)
  149. }
  150. func Setting(ctx *middleware.Context, params martini.Params) {
  151. if !ctx.Repo.IsOwner {
  152. ctx.Error(404)
  153. return
  154. }
  155. // Branches.
  156. brs, err := models.GetBranches(params["username"], params["reponame"])
  157. if err != nil {
  158. log.Error("repo.Setting(GetBranches): %v", err)
  159. ctx.Error(404)
  160. return
  161. } else if len(brs) == 0 {
  162. ctx.Data["IsBareRepo"] = true
  163. ctx.HTML(200, "repo/setting", ctx.Data)
  164. return
  165. }
  166. var title string
  167. if t, ok := ctx.Data["Title"].(string); ok {
  168. title = t
  169. }
  170. ctx.Data["Title"] = title + " - settings"
  171. ctx.Data["IsRepoToolbarSetting"] = true
  172. ctx.HTML(200, "repo/setting", ctx.Data)
  173. }
  174. func Commits(ctx *middleware.Context, params martini.Params) {
  175. brs, err := models.GetBranches(params["username"], params["reponame"])
  176. if err != nil {
  177. ctx.Handle(200, "repo.Commits", err)
  178. return
  179. } else if len(brs) == 0 {
  180. ctx.Error(404)
  181. return
  182. }
  183. ctx.Data["IsRepoToolbarCommits"] = true
  184. commits, err := models.GetCommits(params["username"],
  185. params["reponame"], params["branchname"])
  186. if err != nil {
  187. ctx.Error(404)
  188. return
  189. }
  190. ctx.Data["Username"] = params["username"]
  191. ctx.Data["Reponame"] = params["reponame"]
  192. ctx.Data["CommitCount"] = commits.Len()
  193. ctx.Data["Commits"] = commits
  194. ctx.HTML(200, "repo/commits", ctx.Data)
  195. }
  196. func Issues(ctx *middleware.Context) {
  197. ctx.Data["IsRepoToolbarIssues"] = true
  198. ctx.HTML(200, "repo/issues", ctx.Data)
  199. }
  200. func Pulls(ctx *middleware.Context) {
  201. ctx.Data["IsRepoToolbarPulls"] = true
  202. ctx.HTML(200, "repo/pulls", ctx.Data)
  203. }
  204. func Action(ctx *middleware.Context, params martini.Params) {
  205. var err error
  206. switch params["action"] {
  207. case "watch":
  208. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, true)
  209. case "unwatch":
  210. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, false)
  211. }
  212. if err != nil {
  213. log.Error("repo.Action(%s): %v", params["action"], err)
  214. ctx.JSON(200, map[string]interface{}{
  215. "ok": false,
  216. "err": err.Error(),
  217. })
  218. return
  219. }
  220. ctx.JSON(200, map[string]interface{}{
  221. "ok": true,
  222. })
  223. }