single.go 6.6 KB

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