Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

repo.go 7.0KB

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