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.5 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
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
9 years ago
10 years ago
10 years ago
9 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 checkContextUser(ctx *middleware.Context, uid int64) *models.User {
  24. orgs, err := models.GetOwnedOrgsByUserIDDesc(ctx.User.Id, "updated")
  25. if err != nil {
  26. ctx.Handle(500, "GetOwnedOrgsByUserIDDesc", err)
  27. return nil
  28. }
  29. ctx.Data["Orgs"] = orgs
  30. // Not equal means current user is an organization.
  31. if uid == ctx.User.Id || uid == 0 {
  32. return ctx.User
  33. }
  34. org, err := models.GetUserByID(uid)
  35. if models.IsErrUserNotExist(err) {
  36. return ctx.User
  37. }
  38. if err != nil {
  39. ctx.Handle(500, "GetUserByID", fmt.Errorf("[%d]: %v", uid, err))
  40. return nil
  41. }
  42. // Check ownership of organization.
  43. if !org.IsOrganization() || !org.IsOwnedBy(ctx.User.Id) {
  44. ctx.Error(403)
  45. return nil
  46. }
  47. return org
  48. }
  49. func Create(ctx *middleware.Context) {
  50. ctx.Data["Title"] = ctx.Tr("new_repo")
  51. // Give default value for template to render.
  52. ctx.Data["Gitignores"] = models.Gitignores
  53. ctx.Data["Licenses"] = models.Licenses
  54. ctx.Data["Readmes"] = models.Readmes
  55. ctx.Data["readme"] = "Default"
  56. ctx.Data["private"] = ctx.User.LastRepoVisibility
  57. ctx.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
  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 || setting.Repository.ForcePrivate,
  101. AutoInit: form.AutoInit,
  102. })
  103. if err == nil {
  104. log.Trace("Repository created[%d]: %s/%s", repo.ID, 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. ctx.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
  119. ctxUser := checkContextUser(ctx, ctx.QueryInt64("org"))
  120. if ctx.Written() {
  121. return
  122. }
  123. ctx.Data["ContextUser"] = ctxUser
  124. ctx.HTML(200, MIGRATE)
  125. }
  126. func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) {
  127. ctx.Data["Title"] = ctx.Tr("new_migrate")
  128. ctxUser := checkContextUser(ctx, form.Uid)
  129. if ctx.Written() {
  130. return
  131. }
  132. ctx.Data["ContextUser"] = ctxUser
  133. if ctx.HasError() {
  134. ctx.HTML(200, MIGRATE)
  135. return
  136. }
  137. remoteAddr, err := form.ParseRemoteAddr(ctx.User)
  138. if err != nil {
  139. if models.IsErrInvalidCloneAddr(err) {
  140. ctx.Data["Err_CloneAddr"] = true
  141. addrErr := err.(models.ErrInvalidCloneAddr)
  142. switch {
  143. case addrErr.IsURLError:
  144. ctx.RenderWithErr(ctx.Tr("form.url_error"), MIGRATE, &form)
  145. case addrErr.IsPermissionDenied:
  146. ctx.RenderWithErr(ctx.Tr("repo.migrate.permission_denied"), MIGRATE, &form)
  147. case addrErr.IsInvalidPath:
  148. ctx.RenderWithErr(ctx.Tr("repo.migrate.invalid_local_path"), MIGRATE, &form)
  149. default:
  150. ctx.Handle(500, "Unknown error", err)
  151. }
  152. } else {
  153. ctx.Handle(500, "ParseRemoteAddr", err)
  154. }
  155. return
  156. }
  157. repo, err := models.MigrateRepository(ctxUser, models.MigrateRepoOptions{
  158. Name: form.RepoName,
  159. Description: form.Description,
  160. IsPrivate: form.Private || setting.Repository.ForcePrivate,
  161. IsMirror: form.Mirror,
  162. RemoteAddr: remoteAddr,
  163. })
  164. if err == nil {
  165. log.Trace("Repository migrated[%d]: %s/%s", repo.ID, ctxUser.Name, form.RepoName)
  166. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName)
  167. return
  168. }
  169. if repo != nil {
  170. if errDelete := models.DeleteRepository(ctxUser.Id, repo.ID); errDelete != nil {
  171. log.Error(4, "DeleteRepository: %v", errDelete)
  172. }
  173. }
  174. if strings.Contains(err.Error(), "Authentication failed") ||
  175. strings.Contains(err.Error(), " not found") ||
  176. strings.Contains(err.Error(), "could not read Username") {
  177. ctx.Data["Err_Auth"] = true
  178. ctx.RenderWithErr(ctx.Tr("form.auth_failed", strings.Replace(err.Error(), ":"+form.AuthPassword+"@", ":<password>@", 1)), MIGRATE, &form)
  179. return
  180. }
  181. handleCreateError(ctx, err, "MigratePost", MIGRATE, &form)
  182. }
  183. func Action(ctx *middleware.Context) {
  184. var err error
  185. switch ctx.Params(":action") {
  186. case "watch":
  187. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.ID, true)
  188. case "unwatch":
  189. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.ID, false)
  190. case "star":
  191. err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.ID, true)
  192. case "unstar":
  193. err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.ID, false)
  194. case "desc":
  195. if !ctx.Repo.IsOwner() {
  196. ctx.Error(404)
  197. return
  198. }
  199. ctx.Repo.Repository.Description = ctx.Query("desc")
  200. ctx.Repo.Repository.Website = ctx.Query("site")
  201. err = models.UpdateRepository(ctx.Repo.Repository, false)
  202. }
  203. if err != nil {
  204. log.Error(4, "Action(%s): %v", ctx.Params(":action"), err)
  205. ctx.JSON(200, map[string]interface{}{
  206. "ok": false,
  207. "err": err.Error(),
  208. })
  209. return
  210. }
  211. redirectTo := ctx.Query("redirect_to")
  212. if len(redirectTo) == 0 {
  213. redirectTo = ctx.Repo.RepoLink
  214. }
  215. ctx.Redirect(redirectTo)
  216. }
  217. func Download(ctx *middleware.Context) {
  218. var (
  219. uri = ctx.Params("*")
  220. refName string
  221. ext string
  222. archivePath string
  223. archiveType git.ArchiveType
  224. )
  225. switch {
  226. case strings.HasSuffix(uri, ".zip"):
  227. ext = ".zip"
  228. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  229. archiveType = git.ZIP
  230. case strings.HasSuffix(uri, ".tar.gz"):
  231. ext = ".tar.gz"
  232. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
  233. archiveType = git.TARGZ
  234. default:
  235. ctx.Error(404)
  236. return
  237. }
  238. refName = strings.TrimSuffix(uri, ext)
  239. if !com.IsDir(archivePath) {
  240. if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
  241. ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err)
  242. return
  243. }
  244. }
  245. // Get corresponding commit.
  246. var (
  247. commit *git.Commit
  248. err error
  249. )
  250. gitRepo := ctx.Repo.GitRepo
  251. if gitRepo.IsBranchExist(refName) {
  252. commit, err = gitRepo.GetCommitOfBranch(refName)
  253. if err != nil {
  254. ctx.Handle(500, "Download", err)
  255. return
  256. }
  257. } else if gitRepo.IsTagExist(refName) {
  258. commit, err = gitRepo.GetCommitOfTag(refName)
  259. if err != nil {
  260. ctx.Handle(500, "Download", err)
  261. return
  262. }
  263. } else if len(refName) == 40 {
  264. commit, err = gitRepo.GetCommit(refName)
  265. if err != nil {
  266. ctx.Handle(404, "Download", nil)
  267. return
  268. }
  269. } else {
  270. ctx.Error(404)
  271. return
  272. }
  273. archivePath = path.Join(archivePath, base.ShortSha(commit.ID.String())+ext)
  274. if !com.IsFile(archivePath) {
  275. if err := commit.CreateArchive(archivePath, archiveType); err != nil {
  276. ctx.Handle(500, "Download -> CreateArchive "+archivePath, err)
  277. return
  278. }
  279. }
  280. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(commit.ID.String())+ext)
  281. }