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 10 kB

11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. "code.gitea.io/git"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/auth"
  14. "code.gitea.io/gitea/modules/base"
  15. "code.gitea.io/gitea/modules/context"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/util"
  19. )
  20. const (
  21. tplCreate base.TplName = "repo/create"
  22. tplMigrate base.TplName = "repo/migrate"
  23. )
  24. // MustBeNotBare render when a repo is a bare git dir
  25. func MustBeNotBare(ctx *context.Context) {
  26. if ctx.Repo.Repository.IsBare {
  27. ctx.Handle(404, "MustBeNotBare", nil)
  28. }
  29. }
  30. // MustBeEditable check that repo can be edited
  31. func MustBeEditable(ctx *context.Context) {
  32. if !ctx.Repo.Repository.CanEnableEditor() || ctx.Repo.IsViewCommit {
  33. ctx.Handle(404, "", nil)
  34. return
  35. }
  36. }
  37. // MustBeAbleToUpload check that repo can be uploaded to
  38. func MustBeAbleToUpload(ctx *context.Context) {
  39. if !setting.Repository.Upload.Enabled {
  40. ctx.Handle(404, "", nil)
  41. }
  42. }
  43. func checkContextUser(ctx *context.Context, uid int64) *models.User {
  44. orgs, err := models.GetOwnedOrgsByUserIDDesc(ctx.User.ID, "updated_unix")
  45. if err != nil {
  46. ctx.Handle(500, "GetOwnedOrgsByUserIDDesc", err)
  47. return nil
  48. }
  49. ctx.Data["Orgs"] = orgs
  50. // Not equal means current user is an organization.
  51. if uid == ctx.User.ID || uid == 0 {
  52. return ctx.User
  53. }
  54. org, err := models.GetUserByID(uid)
  55. if models.IsErrUserNotExist(err) {
  56. return ctx.User
  57. }
  58. if err != nil {
  59. ctx.Handle(500, "GetUserByID", fmt.Errorf("[%d]: %v", uid, err))
  60. return nil
  61. }
  62. // Check ownership of organization.
  63. if !org.IsOrganization() || !(ctx.User.IsAdmin || org.IsOwnedBy(ctx.User.ID)) {
  64. ctx.Error(403)
  65. return nil
  66. }
  67. return org
  68. }
  69. // Create render creating repository page
  70. func Create(ctx *context.Context) {
  71. if !ctx.User.CanCreateRepo() {
  72. ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", ctx.User.MaxCreationLimit()), tplCreate, nil)
  73. }
  74. ctx.Data["Title"] = ctx.Tr("new_repo")
  75. // Give default value for template to render.
  76. ctx.Data["Gitignores"] = models.Gitignores
  77. ctx.Data["Licenses"] = models.Licenses
  78. ctx.Data["Readmes"] = models.Readmes
  79. ctx.Data["readme"] = "Default"
  80. ctx.Data["private"] = ctx.User.LastRepoVisibility
  81. ctx.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
  82. ctxUser := checkContextUser(ctx, ctx.QueryInt64("org"))
  83. if ctx.Written() {
  84. return
  85. }
  86. ctx.Data["ContextUser"] = ctxUser
  87. ctx.HTML(200, tplCreate)
  88. }
  89. func handleCreateError(ctx *context.Context, owner *models.User, err error, name string, tpl base.TplName, form interface{}) {
  90. switch {
  91. case models.IsErrReachLimitOfRepo(err):
  92. ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", owner.MaxCreationLimit()), tpl, form)
  93. case models.IsErrRepoAlreadyExist(err):
  94. ctx.Data["Err_RepoName"] = true
  95. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tpl, form)
  96. case models.IsErrNameReserved(err):
  97. ctx.Data["Err_RepoName"] = true
  98. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tpl, form)
  99. case models.IsErrNamePatternNotAllowed(err):
  100. ctx.Data["Err_RepoName"] = true
  101. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tpl, form)
  102. default:
  103. ctx.Handle(500, name, err)
  104. }
  105. }
  106. // CreatePost response for creating repository
  107. func CreatePost(ctx *context.Context, form auth.CreateRepoForm) {
  108. ctx.Data["Title"] = ctx.Tr("new_repo")
  109. ctx.Data["Gitignores"] = models.Gitignores
  110. ctx.Data["Licenses"] = models.Licenses
  111. ctx.Data["Readmes"] = models.Readmes
  112. ctxUser := checkContextUser(ctx, form.UID)
  113. if ctx.Written() {
  114. return
  115. }
  116. ctx.Data["ContextUser"] = ctxUser
  117. if ctx.HasError() {
  118. ctx.HTML(200, tplCreate)
  119. return
  120. }
  121. repo, err := models.CreateRepository(ctx.User, ctxUser, models.CreateRepoOptions{
  122. Name: form.RepoName,
  123. Description: form.Description,
  124. Gitignores: form.Gitignores,
  125. License: form.License,
  126. Readme: form.Readme,
  127. IsPrivate: form.Private || setting.Repository.ForcePrivate,
  128. AutoInit: form.AutoInit,
  129. })
  130. if err == nil {
  131. log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)
  132. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + repo.Name)
  133. return
  134. }
  135. if repo != nil {
  136. if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil {
  137. log.Error(4, "DeleteRepository: %v", errDelete)
  138. }
  139. }
  140. handleCreateError(ctx, ctxUser, err, "CreatePost", tplCreate, &form)
  141. }
  142. // Migrate render migration of repository page
  143. func Migrate(ctx *context.Context) {
  144. ctx.Data["Title"] = ctx.Tr("new_migrate")
  145. ctx.Data["private"] = ctx.User.LastRepoVisibility
  146. ctx.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
  147. ctx.Data["mirror"] = ctx.Query("mirror") == "1"
  148. ctx.Data["LFSActive"] = setting.LFS.StartServer
  149. ctxUser := checkContextUser(ctx, ctx.QueryInt64("org"))
  150. if ctx.Written() {
  151. return
  152. }
  153. ctx.Data["ContextUser"] = ctxUser
  154. ctx.HTML(200, tplMigrate)
  155. }
  156. // MigratePost response for migrating from external git repository
  157. func MigratePost(ctx *context.Context, form auth.MigrateRepoForm) {
  158. ctx.Data["Title"] = ctx.Tr("new_migrate")
  159. ctxUser := checkContextUser(ctx, form.UID)
  160. if ctx.Written() {
  161. return
  162. }
  163. ctx.Data["ContextUser"] = ctxUser
  164. if ctx.HasError() {
  165. ctx.HTML(200, tplMigrate)
  166. return
  167. }
  168. remoteAddr, err := form.ParseRemoteAddr(ctx.User)
  169. if err != nil {
  170. if models.IsErrInvalidCloneAddr(err) {
  171. ctx.Data["Err_CloneAddr"] = true
  172. addrErr := err.(models.ErrInvalidCloneAddr)
  173. switch {
  174. case addrErr.IsURLError:
  175. ctx.RenderWithErr(ctx.Tr("form.url_error"), tplMigrate, &form)
  176. case addrErr.IsPermissionDenied:
  177. ctx.RenderWithErr(ctx.Tr("repo.migrate.permission_denied"), tplMigrate, &form)
  178. case addrErr.IsInvalidPath:
  179. ctx.RenderWithErr(ctx.Tr("repo.migrate.invalid_local_path"), tplMigrate, &form)
  180. default:
  181. ctx.Handle(500, "Unknown error", err)
  182. }
  183. } else {
  184. ctx.Handle(500, "ParseRemoteAddr", err)
  185. }
  186. return
  187. }
  188. repo, err := models.MigrateRepository(ctx.User, ctxUser, models.MigrateRepoOptions{
  189. Name: form.RepoName,
  190. Description: form.Description,
  191. IsPrivate: form.Private || setting.Repository.ForcePrivate,
  192. IsMirror: form.Mirror,
  193. RemoteAddr: remoteAddr,
  194. })
  195. if err == nil {
  196. log.Trace("Repository migrated [%d]: %s/%s", repo.ID, ctxUser.Name, form.RepoName)
  197. ctx.Redirect(setting.AppSubURL + "/" + ctxUser.Name + "/" + form.RepoName)
  198. return
  199. }
  200. // remoteAddr may contain credentials, so we sanitize it
  201. err = util.URLSanitizedError(err, remoteAddr)
  202. if repo != nil {
  203. if errDelete := models.DeleteRepository(ctx.User, ctxUser.ID, repo.ID); errDelete != nil {
  204. log.Error(4, "DeleteRepository: %v", errDelete)
  205. }
  206. }
  207. if strings.Contains(err.Error(), "Authentication failed") ||
  208. strings.Contains(err.Error(), "could not read Username") {
  209. ctx.Data["Err_Auth"] = true
  210. ctx.RenderWithErr(ctx.Tr("form.auth_failed", err.Error()), tplMigrate, &form)
  211. return
  212. } else if strings.Contains(err.Error(), "fatal:") {
  213. ctx.Data["Err_CloneAddr"] = true
  214. ctx.RenderWithErr(ctx.Tr("repo.migrate.failed", err.Error()), tplMigrate, &form)
  215. return
  216. }
  217. handleCreateError(ctx, ctxUser, err, "MigratePost", tplMigrate, &form)
  218. }
  219. // Action response for actions to a repository
  220. func Action(ctx *context.Context) {
  221. var err error
  222. switch ctx.Params(":action") {
  223. case "watch":
  224. err = models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  225. case "unwatch":
  226. err = models.WatchRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  227. case "star":
  228. err = models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, true)
  229. case "unstar":
  230. err = models.StarRepo(ctx.User.ID, ctx.Repo.Repository.ID, false)
  231. case "desc": // FIXME: this is not used
  232. if !ctx.Repo.IsOwner() {
  233. ctx.Error(404)
  234. return
  235. }
  236. ctx.Repo.Repository.Description = ctx.Query("desc")
  237. ctx.Repo.Repository.Website = ctx.Query("site")
  238. err = models.UpdateRepository(ctx.Repo.Repository, false)
  239. }
  240. if err != nil {
  241. ctx.Handle(500, fmt.Sprintf("Action (%s)", ctx.Params(":action")), err)
  242. return
  243. }
  244. redirectTo := ctx.Query("redirect_to")
  245. if len(redirectTo) == 0 {
  246. redirectTo = ctx.Repo.RepoLink
  247. }
  248. ctx.Redirect(redirectTo)
  249. }
  250. // Download download an archive of a repository
  251. func Download(ctx *context.Context) {
  252. var (
  253. uri = ctx.Params("*")
  254. refName string
  255. ext string
  256. archivePath string
  257. archiveType git.ArchiveType
  258. )
  259. switch {
  260. case strings.HasSuffix(uri, ".zip"):
  261. ext = ".zip"
  262. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  263. archiveType = git.ZIP
  264. case strings.HasSuffix(uri, ".tar.gz"):
  265. ext = ".tar.gz"
  266. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
  267. archiveType = git.TARGZ
  268. default:
  269. log.Trace("Unknown format: %s", uri)
  270. ctx.Error(404)
  271. return
  272. }
  273. refName = strings.TrimSuffix(uri, ext)
  274. if !com.IsDir(archivePath) {
  275. if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
  276. ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err)
  277. return
  278. }
  279. }
  280. // Get corresponding commit.
  281. var (
  282. commit *git.Commit
  283. err error
  284. )
  285. gitRepo := ctx.Repo.GitRepo
  286. if gitRepo.IsBranchExist(refName) {
  287. commit, err = gitRepo.GetBranchCommit(refName)
  288. if err != nil {
  289. ctx.Handle(500, "GetBranchCommit", err)
  290. return
  291. }
  292. } else if gitRepo.IsTagExist(refName) {
  293. commit, err = gitRepo.GetTagCommit(refName)
  294. if err != nil {
  295. ctx.Handle(500, "GetTagCommit", err)
  296. return
  297. }
  298. } else if len(refName) >= 4 && len(refName) <= 40 {
  299. commit, err = gitRepo.GetCommit(refName)
  300. if err != nil {
  301. ctx.Handle(404, "GetCommit", nil)
  302. return
  303. }
  304. } else {
  305. ctx.Handle(404, "Download", nil)
  306. return
  307. }
  308. archivePath = path.Join(archivePath, base.ShortSha(commit.ID.String())+ext)
  309. if !com.IsFile(archivePath) {
  310. if err := commit.CreateArchive(archivePath, archiveType); err != nil {
  311. ctx.Handle(500, "Download -> CreateArchive "+archivePath, err)
  312. return
  313. }
  314. }
  315. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+refName+ext)
  316. }