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.

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