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.

324 lines
8.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 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. "net/url"
  8. "os"
  9. "path"
  10. "strings"
  11. "github.com/Unknwon/com"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/auth"
  14. "github.com/gogits/gogs/modules/base"
  15. "github.com/gogits/gogs/modules/git"
  16. "github.com/gogits/gogs/modules/log"
  17. "github.com/gogits/gogs/modules/middleware"
  18. "github.com/gogits/gogs/modules/setting"
  19. )
  20. const (
  21. CREATE base.TplName = "repo/create"
  22. MIGRATE base.TplName = "repo/migrate"
  23. )
  24. func checkContextUser(ctx *middleware.Context, uid int64) *models.User {
  25. orgs, err := models.GetOwnedOrgsByUserIDDesc(ctx.User.Id, "updated")
  26. if err != nil {
  27. ctx.Handle(500, "GetOwnedOrgsByUserIDDesc", err)
  28. return nil
  29. }
  30. ctx.Data["Orgs"] = orgs
  31. // Not equal means current user is an organization.
  32. if uid == ctx.User.Id || uid == 0 {
  33. return ctx.User
  34. }
  35. org, err := models.GetUserByID(uid)
  36. if models.IsErrUserNotExist(err) {
  37. return ctx.User
  38. }
  39. if err != nil {
  40. ctx.Handle(500, "checkContextUser", fmt.Errorf("GetUserById(%d): %v", uid, err))
  41. return nil
  42. }
  43. // Check ownership of organization.
  44. if !org.IsOrganization() || !org.IsOwnedBy(ctx.User.Id) {
  45. ctx.Error(403)
  46. return nil
  47. }
  48. return org
  49. }
  50. func Create(ctx *middleware.Context) {
  51. ctx.Data["Title"] = ctx.Tr("new_repo")
  52. // Give default value for template to render.
  53. ctx.Data["Gitignores"] = models.Gitignores
  54. ctx.Data["Licenses"] = models.Licenses
  55. ctx.Data["Readmes"] = models.Readmes
  56. ctx.Data["readme"] = "Default"
  57. ctx.Data["private"] = ctx.User.LastRepoVisibility
  58. ctxUser := checkContextUser(ctx, ctx.QueryInt64("org"))
  59. if ctx.Written() {
  60. return
  61. }
  62. ctx.Data["ContextUser"] = ctxUser
  63. ctx.HTML(200, CREATE)
  64. }
  65. func handleCreateError(ctx *middleware.Context, err error, name string, tpl base.TplName, form interface{}) {
  66. switch {
  67. case models.IsErrRepoAlreadyExist(err):
  68. ctx.Data["Err_RepoName"] = true
  69. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tpl, form)
  70. case models.IsErrNameReserved(err):
  71. ctx.Data["Err_RepoName"] = true
  72. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tpl, form)
  73. case models.IsErrNamePatternNotAllowed(err):
  74. ctx.Data["Err_RepoName"] = true
  75. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form)
  76. default:
  77. ctx.Handle(500, name, err)
  78. }
  79. }
  80. func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) {
  81. ctx.Data["Title"] = ctx.Tr("new_repo")
  82. ctx.Data["Gitignores"] = models.Gitignores
  83. ctx.Data["Licenses"] = models.Licenses
  84. ctx.Data["Readmes"] = models.Readmes
  85. ctxUser := checkContextUser(ctx, form.Uid)
  86. if ctx.Written() {
  87. return
  88. }
  89. ctx.Data["ContextUser"] = ctxUser
  90. if ctx.HasError() {
  91. ctx.HTML(200, CREATE)
  92. return
  93. }
  94. repo, err := models.CreateRepository(ctxUser, models.CreateRepoOptions{
  95. Name: form.RepoName,
  96. Description: form.Description,
  97. Gitignores: form.Gitignores,
  98. License: form.License,
  99. Readme: form.Readme,
  100. IsPrivate: form.Private,
  101. AutoInit: form.AutoInit,
  102. })
  103. if err == nil {
  104. log.Trace("Repository created: %s/%s", ctxUser.Name, repo.Name)
  105. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  106. return
  107. }
  108. if repo != nil {
  109. if errDelete := models.DeleteRepository(ctxUser.Id, repo.ID); errDelete != nil {
  110. log.Error(4, "DeleteRepository: %v", errDelete)
  111. }
  112. }
  113. handleCreateError(ctx, err, "CreatePost", CREATE, &form)
  114. }
  115. func Migrate(ctx *middleware.Context) {
  116. ctx.Data["Title"] = ctx.Tr("new_migrate")
  117. ctx.Data["private"] = ctx.User.LastRepoVisibility
  118. ctxUser := checkContextUser(ctx, ctx.QueryInt64("org"))
  119. if ctx.Written() {
  120. return
  121. }
  122. ctx.Data["ContextUser"] = ctxUser
  123. ctx.HTML(200, MIGRATE)
  124. }
  125. func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) {
  126. ctx.Data["Title"] = ctx.Tr("new_migrate")
  127. ctxUser := checkContextUser(ctx, form.Uid)
  128. if ctx.Written() {
  129. return
  130. }
  131. ctx.Data["ContextUser"] = ctxUser
  132. if ctx.HasError() {
  133. ctx.HTML(200, MIGRATE)
  134. return
  135. }
  136. // Remote address can be HTTP/HTTPS/Git URL or local path.
  137. // Note: remember to change api/v1/repo.go: MigrateRepo
  138. // FIXME: merge these two functions with better error handling
  139. remoteAddr := form.CloneAddr
  140. if strings.HasPrefix(form.CloneAddr, "http://") ||
  141. strings.HasPrefix(form.CloneAddr, "https://") ||
  142. strings.HasPrefix(form.CloneAddr, "git://") {
  143. u, err := url.Parse(form.CloneAddr)
  144. if err != nil {
  145. ctx.Data["Err_CloneAddr"] = true
  146. ctx.RenderWithErr(ctx.Tr("form.url_error"), MIGRATE, &form)
  147. return
  148. }
  149. if len(form.AuthUsername) > 0 || len(form.AuthPassword) > 0 {
  150. u.User = url.UserPassword(form.AuthUsername, form.AuthPassword)
  151. }
  152. remoteAddr = u.String()
  153. } else if !com.IsDir(remoteAddr) {
  154. ctx.Data["Err_CloneAddr"] = true
  155. ctx.RenderWithErr(ctx.Tr("repo.migrate.invalid_local_path"), MIGRATE, &form)
  156. return
  157. }
  158. repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private, form.Mirror, remoteAddr)
  159. if err == nil {
  160. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  161. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName)
  162. return
  163. }
  164. if repo != nil {
  165. if errDelete := models.DeleteRepository(ctxUser.Id, repo.ID); errDelete != nil {
  166. log.Error(4, "DeleteRepository: %v", errDelete)
  167. }
  168. }
  169. if strings.Contains(err.Error(), "Authentication failed") ||
  170. strings.Contains(err.Error(), " not found") ||
  171. strings.Contains(err.Error(), "could not read Username") {
  172. ctx.Data["Err_Auth"] = true
  173. ctx.RenderWithErr(ctx.Tr("form.auth_failed", strings.Replace(err.Error(), ":"+form.AuthPassword+"@", ":<password>@", 1)), MIGRATE, &form)
  174. return
  175. }
  176. handleCreateError(ctx, err, "MigratePost", MIGRATE, &form)
  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, false)
  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. redirectTo := ctx.Query("redirect_to")
  207. if len(redirectTo) == 0 {
  208. redirectTo = ctx.Repo.RepoLink
  209. }
  210. ctx.Redirect(redirectTo)
  211. return
  212. ctx.JSON(200, map[string]interface{}{
  213. "ok": true,
  214. })
  215. }
  216. func Download(ctx *middleware.Context) {
  217. var (
  218. uri = ctx.Params("*")
  219. refName string
  220. ext string
  221. archivePath string
  222. archiveType git.ArchiveType
  223. )
  224. switch {
  225. case strings.HasSuffix(uri, ".zip"):
  226. ext = ".zip"
  227. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  228. archiveType = git.ZIP
  229. case strings.HasSuffix(uri, ".tar.gz"):
  230. ext = ".tar.gz"
  231. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
  232. archiveType = git.TARGZ
  233. default:
  234. ctx.Error(404)
  235. return
  236. }
  237. refName = strings.TrimSuffix(uri, ext)
  238. if !com.IsDir(archivePath) {
  239. if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
  240. ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err)
  241. return
  242. }
  243. }
  244. // Get corresponding commit.
  245. var (
  246. commit *git.Commit
  247. err error
  248. )
  249. gitRepo := ctx.Repo.GitRepo
  250. if gitRepo.IsBranchExist(refName) {
  251. commit, err = gitRepo.GetCommitOfBranch(refName)
  252. if err != nil {
  253. ctx.Handle(500, "Download", err)
  254. return
  255. }
  256. } else if gitRepo.IsTagExist(refName) {
  257. commit, err = gitRepo.GetCommitOfTag(refName)
  258. if err != nil {
  259. ctx.Handle(500, "Download", err)
  260. return
  261. }
  262. } else if len(refName) == 40 {
  263. commit, err = gitRepo.GetCommit(refName)
  264. if err != nil {
  265. ctx.Handle(404, "Download", nil)
  266. return
  267. }
  268. } else {
  269. ctx.Error(404)
  270. return
  271. }
  272. archivePath = path.Join(archivePath, base.ShortSha(commit.Id.String())+ext)
  273. if !com.IsFile(archivePath) {
  274. if err := commit.CreateArchive(archivePath, archiveType); err != nil {
  275. ctx.Handle(500, "Download -> CreateArchive "+archivePath, err)
  276. return
  277. }
  278. }
  279. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(commit.Id.String())+ext)
  280. }