repo.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. "os"
  7. "path"
  8. "github.com/Unknwon/com"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/git"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/middleware"
  15. )
  16. const (
  17. CREATE base.TplName = "repo/create"
  18. MIGRATE base.TplName = "repo/migrate"
  19. )
  20. func Create(ctx *middleware.Context) {
  21. ctx.Data["Title"] = ctx.Tr("new_repo")
  22. ctx.Data["PageIsRepoCreate"] = true
  23. // Give default value for template to render.
  24. ctx.Data["gitignore"] = "0"
  25. ctx.Data["license"] = "0"
  26. ctx.Data["Gitignores"] = models.Gitignores
  27. ctx.Data["Licenses"] = models.Licenses
  28. ctxUser := ctx.User
  29. // orgId := com.StrTo(ctx.Query("org")).MustInt64()
  30. // if orgId > 0 {
  31. // org, err := models.GetUserById(orgId)
  32. // if err != nil && err != models.ErrUserNotExist {
  33. // ctx.Handle(500, "home.Dashboard(GetUserById)", err)
  34. // return
  35. // }
  36. // ctxUser = org
  37. // }
  38. ctx.Data["ContextUser"] = ctxUser
  39. // if err := ctx.User.GetOrganizations(); err != nil {
  40. // ctx.Handle(500, "home.Dashboard(GetOrganizations)", err)
  41. // return
  42. // }
  43. // ctx.Data["AllUsers"] = append([]*models.User{ctx.User}, ctx.User.Orgs...)
  44. ctx.HTML(200, CREATE)
  45. }
  46. func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) {
  47. ctx.Data["Title"] = ctx.Tr("new_repo")
  48. ctx.Data["PageIsRepoCreate"] = true
  49. ctx.Data["Gitignores"] = models.Gitignores
  50. ctx.Data["Licenses"] = models.Licenses
  51. ctxUser := ctx.User
  52. // orgId := com.StrTo(ctx.Query("org")).MustInt64()
  53. // if orgId > 0 {
  54. // org, err := models.GetUserById(orgId)
  55. // if err != nil && err != models.ErrUserNotExist {
  56. // ctx.Handle(500, "home.Dashboard(GetUserById)", err)
  57. // return
  58. // }
  59. // ctxUser = org
  60. // }
  61. ctx.Data["ContextUser"] = ctxUser
  62. // if err := ctx.User.GetOrganizations(); err != nil {
  63. // ctx.Handle(500, "home.CreatePost(GetOrganizations)", err)
  64. // return
  65. // }
  66. // ctx.Data["Orgs"] = ctx.User.Orgs
  67. if ctx.HasError() {
  68. ctx.HTML(200, CREATE)
  69. return
  70. }
  71. u := ctx.User
  72. // Not equal means current user is an organization.
  73. if u.Id != form.Uid {
  74. var err error
  75. u, err = models.GetUserById(form.Uid)
  76. if err != nil {
  77. if err == models.ErrUserNotExist {
  78. ctx.Handle(404, "home.CreatePost(GetUserById)", err)
  79. } else {
  80. ctx.Handle(500, "home.CreatePost(GetUserById)", err)
  81. }
  82. return
  83. }
  84. // Check ownership of organization.
  85. if !u.IsOrgOwner(ctx.User.Id) {
  86. ctx.Error(403)
  87. return
  88. }
  89. }
  90. repo, err := models.CreateRepository(u, form.RepoName, form.Description,
  91. form.Gitignore, form.License, form.Private, false, form.InitReadme)
  92. if err == nil {
  93. log.Trace("Repository created: %s/%s", u.Name, form.RepoName)
  94. ctx.Redirect("/" + u.Name + "/" + form.RepoName)
  95. return
  96. } else if err == models.ErrRepoAlreadyExist {
  97. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), CREATE, &form)
  98. return
  99. } else if err == models.ErrRepoNameIllegal {
  100. ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), CREATE, &form)
  101. return
  102. }
  103. if repo != nil {
  104. if errDelete := models.DeleteRepository(u.Id, repo.Id, u.Name); errDelete != nil {
  105. log.Error(4, "DeleteRepository: %v", errDelete)
  106. }
  107. }
  108. ctx.Handle(500, "CreateRepository", err)
  109. }
  110. // func Migrate(ctx *middleware.Context) {
  111. // ctx.Data["Title"] = "Migrate repository"
  112. // ctx.Data["PageIsNewRepo"] = true
  113. // if err := ctx.User.GetOrganizations(); err != nil {
  114. // ctx.Handle(500, "home.Migrate(GetOrganizations)", err)
  115. // return
  116. // }
  117. // ctx.Data["Orgs"] = ctx.User.Orgs
  118. // ctx.HTML(200, MIGRATE)
  119. // }
  120. // func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) {
  121. // ctx.Data["Title"] = "Migrate repository"
  122. // ctx.Data["PageIsNewRepo"] = true
  123. // if err := ctx.User.GetOrganizations(); err != nil {
  124. // ctx.Handle(500, "home.MigratePost(GetOrganizations)", err)
  125. // return
  126. // }
  127. // ctx.Data["Orgs"] = ctx.User.Orgs
  128. // if ctx.HasError() {
  129. // ctx.HTML(200, MIGRATE)
  130. // return
  131. // }
  132. // u := ctx.User
  133. // // Not equal means current user is an organization.
  134. // if u.Id != form.Uid {
  135. // var err error
  136. // u, err = models.GetUserById(form.Uid)
  137. // if err != nil {
  138. // if err == models.ErrUserNotExist {
  139. // ctx.Handle(404, "home.MigratePost(GetUserById)", err)
  140. // } else {
  141. // ctx.Handle(500, "home.MigratePost(GetUserById)", err)
  142. // }
  143. // return
  144. // }
  145. // }
  146. // authStr := strings.Replace(fmt.Sprintf("://%s:%s",
  147. // form.AuthUserName, form.AuthPasswd), "@", "%40", -1)
  148. // url := strings.Replace(form.Url, "://", authStr+"@", 1)
  149. // repo, err := models.MigrateRepository(u, form.RepoName, form.Description, form.Private,
  150. // form.Mirror, url)
  151. // if err == nil {
  152. // log.Trace("%s Repository migrated: %s/%s", ctx.Req.RequestURI, u.LowerName, form.RepoName)
  153. // ctx.Redirect("/" + u.Name + "/" + form.RepoName)
  154. // return
  155. // } else if err == models.ErrRepoAlreadyExist {
  156. // ctx.RenderWithErr("Repository name has already been used", MIGRATE, &form)
  157. // return
  158. // } else if err == models.ErrRepoNameIllegal {
  159. // ctx.RenderWithErr(models.ErrRepoNameIllegal.Error(), MIGRATE, &form)
  160. // return
  161. // }
  162. // if repo != nil {
  163. // if errDelete := models.DeleteRepository(u.Id, repo.Id, u.Name); errDelete != nil {
  164. // log.Error("repo.MigratePost(DeleteRepository): %v", errDelete)
  165. // }
  166. // }
  167. // if strings.Contains(err.Error(), "Authentication failed") {
  168. // ctx.RenderWithErr(err.Error(), MIGRATE, &form)
  169. // return
  170. // }
  171. // ctx.Handle(500, "repo.Migrate(MigrateRepository)", err)
  172. // }
  173. // func Action(ctx *middleware.Context, params martini.Params) {
  174. // var err error
  175. // switch params["action"] {
  176. // case "watch":
  177. // err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, true)
  178. // case "unwatch":
  179. // err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, false)
  180. // case "desc":
  181. // if !ctx.Repo.IsOwner {
  182. // ctx.Error(404)
  183. // return
  184. // }
  185. // ctx.Repo.Repository.Description = ctx.Query("desc")
  186. // ctx.Repo.Repository.Website = ctx.Query("site")
  187. // err = models.UpdateRepository(ctx.Repo.Repository)
  188. // }
  189. // if err != nil {
  190. // log.Error("repo.Action(%s): %v", params["action"], err)
  191. // ctx.JSON(200, map[string]interface{}{
  192. // "ok": false,
  193. // "err": err.Error(),
  194. // })
  195. // return
  196. // }
  197. // ctx.JSON(200, map[string]interface{}{
  198. // "ok": true,
  199. // })
  200. // }
  201. func Download(ctx *middleware.Context) {
  202. ext := "." + ctx.Params(":ext")
  203. var archivePath string
  204. switch ext {
  205. case ".zip":
  206. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  207. case ".tar.gz":
  208. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
  209. default:
  210. ctx.Error(404)
  211. return
  212. }
  213. if !com.IsDir(archivePath) {
  214. if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
  215. ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err)
  216. return
  217. }
  218. }
  219. archivePath = path.Join(archivePath, ctx.Repo.CommitId+ext)
  220. if !com.IsFile(archivePath) {
  221. if err := ctx.Repo.Commit.CreateArchive(archivePath, git.ZIP); err != nil {
  222. ctx.Handle(500, "Download -> CreateArchive "+archivePath, err)
  223. return
  224. }
  225. }
  226. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(ctx.Repo.CommitId)+ext)
  227. }