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

single.go 7.0 kB

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