single.go 6.6 KB

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