repo.go 6.9 KB

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