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