You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
7.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. "github.com/gogits/gogs/modules/setting"
  18. )
  19. const (
  20. CREATE base.TplName = "repo/create"
  21. MIGRATE base.TplName = "repo/migrate"
  22. )
  23. func Create(ctx *middleware.Context) {
  24. ctx.Data["Title"] = ctx.Tr("new_repo")
  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["Gitignores"] = models.Gitignores
  50. ctx.Data["Licenses"] = models.Licenses
  51. ctxUser := ctx.User
  52. // Not equal means current user is an organization.
  53. if form.Uid != ctx.User.Id {
  54. org, err := models.GetUserById(form.Uid)
  55. if err != nil && err != models.ErrUserNotExist {
  56. ctx.Handle(500, "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, "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. if ctxUser.IsOrganization() {
  72. // Check ownership of organization.
  73. if !ctxUser.IsOrgOwner(ctx.User.Id) {
  74. ctx.Error(403)
  75. return
  76. }
  77. }
  78. repo, err := models.CreateRepository(ctxUser, form.RepoName, form.Description,
  79. form.Gitignore, form.License, form.Private, false, form.InitReadme)
  80. if err == nil {
  81. log.Trace("Repository created: %s/%s", ctxUser.Name, form.RepoName)
  82. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName)
  83. return
  84. } else if err == models.ErrRepoAlreadyExist {
  85. ctx.Data["Err_RepoName"] = true
  86. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), CREATE, &form)
  87. return
  88. } else if err == models.ErrRepoNameIllegal {
  89. ctx.Data["Err_RepoName"] = true
  90. ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), CREATE, &form)
  91. return
  92. }
  93. if repo != nil {
  94. if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil {
  95. log.Error(4, "DeleteRepository: %v", errDelete)
  96. }
  97. }
  98. ctx.Handle(500, "CreatePost", err)
  99. }
  100. func Migrate(ctx *middleware.Context) {
  101. ctx.Data["Title"] = ctx.Tr("new_migrate")
  102. ctxUser := ctx.User
  103. if orgId := com.StrTo(ctx.Query("org")).MustInt64(); orgId > 0 {
  104. org, err := models.GetUserById(orgId)
  105. if err != nil && err != models.ErrUserNotExist {
  106. ctx.Handle(500, "GetUserById", err)
  107. return
  108. }
  109. ctxUser = org
  110. }
  111. ctx.Data["ContextUser"] = ctxUser
  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. ctx.HTML(200, MIGRATE)
  118. }
  119. func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) {
  120. ctx.Data["Title"] = ctx.Tr("new_migrate")
  121. ctxUser := ctx.User
  122. // Not equal means current user is an organization.
  123. if form.Uid != ctx.User.Id {
  124. org, err := models.GetUserById(form.Uid)
  125. if err != nil {
  126. ctx.Handle(500, "GetUserById", err)
  127. return
  128. }
  129. ctxUser = org
  130. }
  131. ctx.Data["ContextUser"] = ctxUser
  132. if err := ctx.User.GetOrganizations(); err != nil {
  133. ctx.Handle(500, "GetOrganizations", err)
  134. return
  135. }
  136. ctx.Data["Orgs"] = ctx.User.Orgs
  137. if ctx.HasError() {
  138. ctx.HTML(200, MIGRATE)
  139. return
  140. }
  141. if ctxUser.IsOrganization() {
  142. // Check ownership of organization.
  143. if !ctxUser.IsOrgOwner(ctx.User.Id) {
  144. ctx.Error(403)
  145. return
  146. }
  147. }
  148. authStr := strings.Replace(fmt.Sprintf("://%s:%s",
  149. form.AuthUserName, form.AuthPasswd), "@", "%40", -1)
  150. url := strings.Replace(form.HttpsUrl, "://", authStr+"@", 1)
  151. repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private,
  152. form.Mirror, url)
  153. if err == nil {
  154. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  155. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName)
  156. return
  157. } else if err == models.ErrRepoAlreadyExist {
  158. ctx.Data["Err_RepoName"] = true
  159. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), MIGRATE, &form)
  160. return
  161. } else if err == models.ErrRepoNameIllegal {
  162. ctx.Data["Err_RepoName"] = true
  163. ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), MIGRATE, &form)
  164. return
  165. }
  166. if repo != nil {
  167. if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil {
  168. log.Error(4, "DeleteRepository: %v", errDelete)
  169. }
  170. }
  171. if strings.Contains(err.Error(), "Authentication failed") {
  172. ctx.Data["Err_Auth"] = true
  173. ctx.RenderWithErr(ctx.Tr("form.auth_failed", err), MIGRATE, &form)
  174. return
  175. }
  176. ctx.Handle(500, "MigratePost", err)
  177. }
  178. func Action(ctx *middleware.Context) {
  179. var err error
  180. switch ctx.Params(":action") {
  181. case "watch":
  182. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, true)
  183. case "unwatch":
  184. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.Id, false)
  185. case "star":
  186. err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.Id, true)
  187. case "unstar":
  188. err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.Id, false)
  189. case "desc":
  190. if !ctx.Repo.IsOwner {
  191. ctx.Error(404)
  192. return
  193. }
  194. ctx.Repo.Repository.Description = ctx.Query("desc")
  195. ctx.Repo.Repository.Website = ctx.Query("site")
  196. err = models.UpdateRepository(ctx.Repo.Repository)
  197. }
  198. if err != nil {
  199. log.Error(4, "Action(%s): %v", ctx.Params(":action"), err)
  200. ctx.JSON(200, map[string]interface{}{
  201. "ok": false,
  202. "err": err.Error(),
  203. })
  204. return
  205. }
  206. ctx.Redirect(ctx.Repo.RepoLink)
  207. return
  208. ctx.JSON(200, map[string]interface{}{
  209. "ok": true,
  210. })
  211. }
  212. func Download(ctx *middleware.Context) {
  213. var (
  214. uri = ctx.Params("*")
  215. refName string
  216. ext string
  217. archivePath string
  218. )
  219. switch {
  220. case strings.HasSuffix(uri, ".zip"):
  221. ext = ".zip"
  222. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  223. case strings.HasSuffix(uri, ".tar.gz"):
  224. ext = ".tar.gz"
  225. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
  226. default:
  227. ctx.Error(404)
  228. return
  229. }
  230. refName = strings.TrimSuffix(uri, ext)
  231. if !com.IsDir(archivePath) {
  232. if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
  233. ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err)
  234. return
  235. }
  236. }
  237. // Get corresponding commit.
  238. var (
  239. commit *git.Commit
  240. err error
  241. )
  242. gitRepo := ctx.Repo.GitRepo
  243. if gitRepo.IsBranchExist(refName) {
  244. commit, err = gitRepo.GetCommitOfBranch(refName)
  245. if err != nil {
  246. ctx.Handle(500, "Download", err)
  247. return
  248. }
  249. } else if gitRepo.IsTagExist(refName) {
  250. commit, err = gitRepo.GetCommitOfTag(refName)
  251. if err != nil {
  252. ctx.Handle(500, "Download", err)
  253. return
  254. }
  255. } else if len(refName) == 40 {
  256. commit, err = gitRepo.GetCommit(refName)
  257. if err != nil {
  258. ctx.Handle(404, "Download", nil)
  259. return
  260. }
  261. } else {
  262. ctx.Error(404)
  263. return
  264. }
  265. archivePath = path.Join(archivePath, ctx.Repo.CommitId+ext)
  266. if !com.IsFile(archivePath) {
  267. if err := commit.CreateArchive(archivePath, git.ZIP); err != nil {
  268. ctx.Handle(500, "Download -> CreateArchive "+archivePath, err)
  269. return
  270. }
  271. }
  272. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(commit.Id.String())+ext)
  273. }