repo.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. // Not equal means current user is an organization.
  26. if uid == ctx.User.Id || uid == 0 {
  27. return ctx.User
  28. }
  29. org, err := models.GetUserByID(uid)
  30. if models.IsErrUserNotExist(err) {
  31. return ctx.User
  32. }
  33. if err != nil {
  34. ctx.Handle(500, "checkContextUser", fmt.Errorf("GetUserById(%d): %v", uid, err))
  35. return nil
  36. } else if !org.IsOrganization() {
  37. ctx.Error(403)
  38. return nil
  39. }
  40. return org
  41. }
  42. func Create(ctx *middleware.Context) {
  43. ctx.Data["Title"] = ctx.Tr("new_repo")
  44. // Give default value for template to render.
  45. ctx.Data["gitignore"] = "0"
  46. ctx.Data["license"] = "0"
  47. ctx.Data["Gitignores"] = models.Gitignores
  48. ctx.Data["Licenses"] = models.Licenses
  49. ctxUser := checkContextUser(ctx, ctx.QueryInt64("org"))
  50. if ctx.Written() {
  51. return
  52. }
  53. ctx.Data["ContextUser"] = ctxUser
  54. if err := ctx.User.GetOrganizations(); err != nil {
  55. ctx.Handle(500, "GetOrganizations", err)
  56. return
  57. }
  58. ctx.Data["Orgs"] = ctx.User.Orgs
  59. ctx.HTML(200, CREATE)
  60. }
  61. func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) {
  62. ctx.Data["Title"] = ctx.Tr("new_repo")
  63. ctx.Data["Gitignores"] = models.Gitignores
  64. ctx.Data["Licenses"] = models.Licenses
  65. ctxUser := checkContextUser(ctx, form.Uid)
  66. if ctx.Written() {
  67. return
  68. }
  69. ctx.Data["ContextUser"] = ctxUser
  70. if err := ctx.User.GetOrganizations(); err != nil {
  71. ctx.Handle(500, "GetOrganizations", err)
  72. return
  73. }
  74. ctx.Data["Orgs"] = ctx.User.Orgs
  75. if ctx.HasError() {
  76. ctx.HTML(200, CREATE)
  77. return
  78. }
  79. if ctxUser.IsOrganization() {
  80. // Check ownership of organization.
  81. if !ctxUser.IsOwnedBy(ctx.User.Id) {
  82. ctx.Error(403)
  83. return
  84. }
  85. }
  86. repo, err := models.CreateRepository(ctxUser, form.RepoName, form.Description,
  87. form.Gitignore, form.License, form.Private, false, form.AutoInit)
  88. if err == nil {
  89. log.Trace("Repository created: %s/%s", ctxUser.Name, repo.Name)
  90. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name)
  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. switch {
  99. case models.IsErrRepoAlreadyExist(err):
  100. ctx.Data["Err_RepoName"] = true
  101. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), CREATE, &form)
  102. case models.IsErrNameReserved(err):
  103. ctx.Data["Err_RepoName"] = true
  104. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), CREATE, &form)
  105. case models.IsErrNamePatternNotAllowed(err):
  106. ctx.Data["Err_RepoName"] = true
  107. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), CREATE, &form)
  108. default:
  109. ctx.Handle(500, "CreatePost", err)
  110. }
  111. }
  112. func Migrate(ctx *middleware.Context) {
  113. ctx.Data["Title"] = ctx.Tr("new_migrate")
  114. ctxUser := checkContextUser(ctx, ctx.QueryInt64("org"))
  115. if ctx.Written() {
  116. return
  117. }
  118. ctx.Data["ContextUser"] = ctxUser
  119. if err := ctx.User.GetOrganizations(); err != nil {
  120. ctx.Handle(500, "GetOrganizations", err)
  121. return
  122. }
  123. ctx.Data["Orgs"] = ctx.User.Orgs
  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 err := ctx.User.GetOrganizations(); err != nil {
  134. ctx.Handle(500, "GetOrganizations", err)
  135. return
  136. }
  137. ctx.Data["Orgs"] = ctx.User.Orgs
  138. if ctx.HasError() {
  139. ctx.HTML(200, MIGRATE)
  140. return
  141. }
  142. if ctxUser.IsOrganization() {
  143. // Check ownership of organization.
  144. if !ctxUser.IsOwnedBy(ctx.User.Id) {
  145. ctx.Error(403)
  146. return
  147. }
  148. }
  149. // Remote address can be HTTP/HTTPS/Git URL or local path.
  150. // Note: remember to change api/v1/repo.go: MigrateRepo
  151. // FIXME: merge these two functions with better error handling
  152. remoteAddr := form.CloneAddr
  153. if strings.HasPrefix(form.CloneAddr, "http://") ||
  154. strings.HasPrefix(form.CloneAddr, "https://") ||
  155. strings.HasPrefix(form.CloneAddr, "git://") {
  156. u, err := url.Parse(form.CloneAddr)
  157. if err != nil {
  158. ctx.Data["Err_CloneAddr"] = true
  159. ctx.RenderWithErr(ctx.Tr("form.url_error"), MIGRATE, &form)
  160. return
  161. }
  162. if len(form.AuthUsername) > 0 || len(form.AuthPassword) > 0 {
  163. u.User = url.UserPassword(form.AuthUsername, form.AuthPassword)
  164. }
  165. remoteAddr = u.String()
  166. } else if !com.IsDir(remoteAddr) {
  167. ctx.Data["Err_CloneAddr"] = true
  168. ctx.RenderWithErr(ctx.Tr("repo.migrate.invalid_local_path"), MIGRATE, &form)
  169. return
  170. }
  171. repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private, form.Mirror, remoteAddr)
  172. if err == nil {
  173. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  174. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName)
  175. return
  176. }
  177. if repo != nil {
  178. if errDelete := models.DeleteRepository(ctxUser.Id, repo.ID, ctxUser.Name); errDelete != nil {
  179. log.Error(4, "DeleteRepository: %v", errDelete)
  180. }
  181. }
  182. if strings.Contains(err.Error(), "Authentication failed") ||
  183. strings.Contains(err.Error(), " not found") {
  184. ctx.Data["Err_Auth"] = true
  185. ctx.RenderWithErr(ctx.Tr("form.auth_failed", strings.Replace(err.Error(), ":"+form.AuthPassword+"@", ":<password>@", 1)), MIGRATE, &form)
  186. return
  187. }
  188. switch {
  189. case models.IsErrRepoAlreadyExist(err):
  190. ctx.Data["Err_RepoName"] = true
  191. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), MIGRATE, &form)
  192. case models.IsErrNameReserved(err):
  193. ctx.Data["Err_RepoName"] = true
  194. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), MIGRATE, &form)
  195. case models.IsErrNamePatternNotAllowed(err):
  196. ctx.Data["Err_RepoName"] = true
  197. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), MIGRATE, &form)
  198. default:
  199. ctx.Handle(500, "MigratePost", err)
  200. }
  201. }
  202. func Action(ctx *middleware.Context) {
  203. var err error
  204. switch ctx.Params(":action") {
  205. case "watch":
  206. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.ID, true)
  207. case "unwatch":
  208. err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.ID, false)
  209. case "star":
  210. err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.ID, true)
  211. case "unstar":
  212. err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.ID, false)
  213. case "desc":
  214. if !ctx.Repo.IsOwner() {
  215. ctx.Error(404)
  216. return
  217. }
  218. ctx.Repo.Repository.Description = ctx.Query("desc")
  219. ctx.Repo.Repository.Website = ctx.Query("site")
  220. err = models.UpdateRepository(ctx.Repo.Repository, false)
  221. }
  222. if err != nil {
  223. log.Error(4, "Action(%s): %v", ctx.Params(":action"), err)
  224. ctx.JSON(200, map[string]interface{}{
  225. "ok": false,
  226. "err": err.Error(),
  227. })
  228. return
  229. }
  230. redirectTo := ctx.Query("redirect_to")
  231. if len(redirectTo) == 0 {
  232. redirectTo = ctx.Repo.RepoLink
  233. }
  234. ctx.Redirect(redirectTo)
  235. return
  236. ctx.JSON(200, map[string]interface{}{
  237. "ok": true,
  238. })
  239. }
  240. func Download(ctx *middleware.Context) {
  241. var (
  242. uri = ctx.Params("*")
  243. refName string
  244. ext string
  245. archivePath string
  246. archiveType git.ArchiveType
  247. )
  248. switch {
  249. case strings.HasSuffix(uri, ".zip"):
  250. ext = ".zip"
  251. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  252. archiveType = git.ZIP
  253. case strings.HasSuffix(uri, ".tar.gz"):
  254. ext = ".tar.gz"
  255. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
  256. archiveType = git.TARGZ
  257. default:
  258. ctx.Error(404)
  259. return
  260. }
  261. refName = strings.TrimSuffix(uri, ext)
  262. if !com.IsDir(archivePath) {
  263. if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
  264. ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err)
  265. return
  266. }
  267. }
  268. // Get corresponding commit.
  269. var (
  270. commit *git.Commit
  271. err error
  272. )
  273. gitRepo := ctx.Repo.GitRepo
  274. if gitRepo.IsBranchExist(refName) {
  275. commit, err = gitRepo.GetCommitOfBranch(refName)
  276. if err != nil {
  277. ctx.Handle(500, "Download", err)
  278. return
  279. }
  280. } else if gitRepo.IsTagExist(refName) {
  281. commit, err = gitRepo.GetCommitOfTag(refName)
  282. if err != nil {
  283. ctx.Handle(500, "Download", err)
  284. return
  285. }
  286. } else if len(refName) == 40 {
  287. commit, err = gitRepo.GetCommit(refName)
  288. if err != nil {
  289. ctx.Handle(404, "Download", nil)
  290. return
  291. }
  292. } else {
  293. ctx.Error(404)
  294. return
  295. }
  296. archivePath = path.Join(archivePath, base.ShortSha(commit.Id.String())+ext)
  297. if !com.IsFile(archivePath) {
  298. if err := commit.CreateArchive(archivePath, archiveType); err != nil {
  299. ctx.Handle(500, "Download -> CreateArchive "+archivePath, err)
  300. return
  301. }
  302. }
  303. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(commit.Id.String())+ext)
  304. }