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.

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