repo.go 6.5 KB

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