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.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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", ctx.Data)
  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. // 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", ctx.Data)
  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. readmeExist := base.IsMarkdownFile(repoFile.Name) || base.IsReadmeFile(repoFile.Name)
  79. ctx.Data["ReadmeExist"] = readmeExist
  80. if readmeExist {
  81. ctx.Data["FileContent"] = string(base.RenderMarkdown(blob.Contents(), ""))
  82. } else {
  83. ctx.Data["FileContent"] = string(blob.Contents())
  84. }
  85. }
  86. } else {
  87. // Directory and file list.
  88. files, err := models.GetReposFiles(params["username"], params["reponame"],
  89. params["branchname"], params["commitid"], treename)
  90. if err != nil {
  91. log.Error("repo.Single(GetReposFiles): %v", err)
  92. ctx.Error(404)
  93. return
  94. }
  95. ctx.Data["Files"] = files
  96. var readmeFile *models.RepoFile
  97. for _, f := range files {
  98. if !f.IsFile() || !base.IsReadmeFile(f.Name) {
  99. continue
  100. } else {
  101. readmeFile = f
  102. break
  103. }
  104. }
  105. if readmeFile != nil {
  106. ctx.Data["ReadmeExist"] = true
  107. // if file large than 1M not show it
  108. if readmeFile.Size > 1024*1024 || readmeFile.Filemode != git.FileModeBlob {
  109. ctx.Data["FileIsLarge"] = true
  110. } else if blob, err := readmeFile.LookupBlob(); err != nil {
  111. log.Error("repo.Single(readmeFile.LookupBlob): %v", err)
  112. ctx.Error(404)
  113. return
  114. } else {
  115. // current repo branch link
  116. urlPrefix := "http://" + base.Domain + branchLink
  117. ctx.Data["FileName"] = readmeFile.Name
  118. ctx.Data["FileContent"] = string(base.RenderMarkdown(blob.Contents(), urlPrefix))
  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["IsRepoToolbarSource"] = true
  149. ctx.Data["BranchLink"] = branchLink
  150. ctx.HTML(200, "repo/single", ctx.Data)
  151. }
  152. func Http(ctx *middleware.Context, params martini.Params) {
  153. /*if !ctx.Repo.IsValid {
  154. return
  155. }*/
  156. // TODO: access check
  157. username := params["username"]
  158. reponame := params["reponame"]
  159. if strings.HasSuffix(reponame, ".git") {
  160. reponame = reponame[:len(reponame)-4]
  161. }
  162. prefix := path.Join("/", username, params["reponame"])
  163. server := &webdav.Server{
  164. Fs: webdav.Dir(models.RepoPath(username, reponame)),
  165. TrimPrefix: prefix,
  166. Listings: true,
  167. }
  168. server.ServeHTTP(ctx.ResponseWriter, ctx.Req)
  169. }
  170. func Setting(ctx *middleware.Context, params martini.Params) {
  171. if !ctx.Repo.IsOwner {
  172. ctx.Error(404)
  173. return
  174. }
  175. // Branches.
  176. brs, err := models.GetBranches(params["username"], params["reponame"])
  177. if err != nil {
  178. log.Error("repo.Setting(GetBranches): %v", err)
  179. ctx.Error(404)
  180. return
  181. } else if len(brs) == 0 {
  182. ctx.Data["IsBareRepo"] = true
  183. ctx.HTML(200, "repo/setting", ctx.Data)
  184. return
  185. }
  186. var title string
  187. if t, ok := ctx.Data["Title"].(string); ok {
  188. title = t
  189. }
  190. ctx.Data["Title"] = title + " - settings"
  191. ctx.Data["IsRepoToolbarSetting"] = true
  192. ctx.HTML(200, "repo/setting", ctx.Data)
  193. }
  194. func Commits(ctx *middleware.Context, params martini.Params) {
  195. brs, err := models.GetBranches(params["username"], params["reponame"])
  196. if err != nil {
  197. ctx.Handle(200, "repo.Commits", err)
  198. return
  199. } else if len(brs) == 0 {
  200. ctx.Error(404)
  201. return
  202. }
  203. ctx.Data["IsRepoToolbarCommits"] = true
  204. commits, err := models.GetCommits(params["username"],
  205. params["reponame"], params["branchname"])
  206. if err != nil {
  207. ctx.Error(404)
  208. return
  209. }
  210. ctx.Data["Username"] = params["username"]
  211. ctx.Data["Reponame"] = params["reponame"]
  212. ctx.Data["CommitCount"] = commits.Len()
  213. ctx.Data["Commits"] = commits
  214. ctx.HTML(200, "repo/commits", ctx.Data)
  215. }
  216. func Issues(ctx *middleware.Context) {
  217. ctx.Data["IsRepoToolbarIssues"] = true
  218. ctx.HTML(200, "repo/issues", ctx.Data)
  219. }
  220. func Pulls(ctx *middleware.Context) {
  221. ctx.Data["IsRepoToolbarPulls"] = true
  222. ctx.HTML(200, "repo/pulls", ctx.Data)
  223. }
  224. func Action(ctx *middleware.Context, params martini.Params) {
  225. var err error
  226. switch params["action"] {
  227. case "watch":
  228. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, true)
  229. case "unwatch":
  230. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, false)
  231. }
  232. if err != nil {
  233. log.Error("repo.Action(%s): %v", params["action"], err)
  234. ctx.JSON(200, map[string]interface{}{
  235. "ok": false,
  236. "err": err.Error(),
  237. })
  238. return
  239. }
  240. ctx.JSON(200, map[string]interface{}{
  241. "ok": true,
  242. })
  243. }