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 7.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. // Not equal means current user is an organization.
  122. if form.Uid != ctx.User.Id {
  123. org, err := models.GetUserById(form.Uid)
  124. if err != nil {
  125. ctx.Handle(500, "GetUserById", err)
  126. return
  127. }
  128. ctxUser = org
  129. }
  130. ctx.Data["ContextUser"] = ctxUser
  131. if err := ctx.User.GetOrganizations(); err != nil {
  132. ctx.Handle(500, "GetOrganizations", err)
  133. return
  134. }
  135. ctx.Data["Orgs"] = ctx.User.Orgs
  136. if ctx.HasError() {
  137. ctx.HTML(200, MIGRATE)
  138. return
  139. }
  140. if ctxUser.IsOrganization() {
  141. // Check ownership of organization.
  142. if !ctxUser.IsOrgOwner(ctx.User.Id) {
  143. ctx.Error(403)
  144. return
  145. }
  146. }
  147. authStr := strings.Replace(fmt.Sprintf("://%s:%s",
  148. form.AuthUserName, form.AuthPasswd), "@", "%40", -1)
  149. url := strings.Replace(form.HttpsUrl, "://", authStr+"@", 1)
  150. repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private,
  151. form.Mirror, url)
  152. if err == nil {
  153. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  154. ctx.Redirect("/" + ctxUser.Name + "/" + form.RepoName)
  155. return
  156. } else if err == models.ErrRepoAlreadyExist {
  157. ctx.Data["Err_RepoName"] = true
  158. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), MIGRATE, &form)
  159. return
  160. } else if err == models.ErrRepoNameIllegal {
  161. ctx.Data["Err_RepoName"] = true
  162. ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), MIGRATE, &form)
  163. return
  164. }
  165. if repo != nil {
  166. if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil {
  167. log.Error(4, "DeleteRepository: %v", errDelete)
  168. }
  169. }
  170. if strings.Contains(err.Error(), "Authentication failed") {
  171. ctx.Data["Err_Auth"] = true
  172. ctx.RenderWithErr(ctx.Tr("form.auth_failed", err), MIGRATE, &form)
  173. return
  174. }
  175. ctx.Handle(500, "MigratePost", err)
  176. }
  177. func Action(ctx *middleware.Context) {
  178. var err error
  179. switch ctx.Params(":action") {
  180. case "watch":
  181. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, true)
  182. case "unwatch":
  183. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, false)
  184. case "star":
  185. err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.Id, true)
  186. case "unstar":
  187. err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.Id, false)
  188. case "desc":
  189. if !ctx.Repo.IsOwner {
  190. ctx.Error(404)
  191. return
  192. }
  193. ctx.Repo.Repository.Description = ctx.Query("desc")
  194. ctx.Repo.Repository.Website = ctx.Query("site")
  195. err = models.UpdateRepository(ctx.Repo.Repository)
  196. }
  197. if err != nil {
  198. log.Error(4, "Action(%s): %v", ctx.Params(":action"), err)
  199. ctx.JSON(200, map[string]interface{}{
  200. "ok": false,
  201. "err": err.Error(),
  202. })
  203. return
  204. }
  205. ctx.Redirect(ctx.Repo.RepoLink)
  206. return
  207. ctx.JSON(200, map[string]interface{}{
  208. "ok": true,
  209. })
  210. }
  211. func Download(ctx *middleware.Context) {
  212. ext := "." + ctx.Params(":ext")
  213. var archivePath string
  214. switch ext {
  215. case ".zip":
  216. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  217. case ".tar.gz":
  218. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
  219. default:
  220. ctx.Error(404)
  221. return
  222. }
  223. if !com.IsDir(archivePath) {
  224. if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
  225. ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err)
  226. return
  227. }
  228. }
  229. archivePath = path.Join(archivePath, ctx.Repo.CommitId+ext)
  230. if !com.IsFile(archivePath) {
  231. if err := ctx.Repo.Commit.CreateArchive(archivePath, git.ZIP); err != nil {
  232. ctx.Handle(500, "Download -> CreateArchive "+archivePath, err)
  233. return
  234. }
  235. }
  236. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(ctx.Repo.CommitId)+ext)
  237. }