repo.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 Create(ctx *middleware.Context) {
  24. ctx.Data["Title"] = ctx.Tr("new_repo")
  25. // Give default value for template to render.
  26. ctx.Data["gitignore"] = "0"
  27. ctx.Data["license"] = "0"
  28. ctx.Data["Gitignores"] = models.Gitignores
  29. ctx.Data["Licenses"] = models.Licenses
  30. ctxUser := ctx.User
  31. if orgId := com.StrTo(ctx.Query("org")).MustInt64(); orgId > 0 {
  32. org, err := models.GetUserById(orgId)
  33. if err != nil && err != models.ErrUserNotExist {
  34. ctx.Handle(500, "GetUserById", err)
  35. return
  36. }
  37. ctxUser = org
  38. }
  39. ctx.Data["ContextUser"] = ctxUser
  40. if err := ctx.User.GetOrganizations(); err != nil {
  41. ctx.Handle(500, "GetOrganizations", err)
  42. return
  43. }
  44. ctx.Data["Orgs"] = ctx.User.Orgs
  45. ctx.HTML(200, CREATE)
  46. }
  47. func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) {
  48. ctx.Data["Title"] = ctx.Tr("new_repo")
  49. ctx.Data["Gitignores"] = models.Gitignores
  50. ctx.Data["Licenses"] = models.Licenses
  51. ctxUser := ctx.User
  52. // Not equal means current user is an organization.
  53. if form.Uid != ctx.User.Id {
  54. org, err := models.GetUserById(form.Uid)
  55. if err != nil && err != models.ErrUserNotExist {
  56. ctx.Handle(500, "GetUserById", err)
  57. return
  58. }
  59. ctxUser = org
  60. }
  61. ctx.Data["ContextUser"] = ctxUser
  62. if err := ctx.User.GetOrganizations(); err != nil {
  63. ctx.Handle(500, "GetOrganizations", err)
  64. return
  65. }
  66. ctx.Data["Orgs"] = ctx.User.Orgs
  67. if ctx.HasError() {
  68. ctx.HTML(200, CREATE)
  69. return
  70. }
  71. if ctxUser.IsOrganization() {
  72. // Check ownership of organization.
  73. if !ctxUser.IsOrgOwner(ctx.User.Id) {
  74. ctx.Error(403)
  75. return
  76. }
  77. }
  78. repo, err := models.CreateRepository(ctxUser, form.RepoName, form.Description,
  79. form.Gitignore, form.License, form.Private, false, form.InitReadme)
  80. if err == nil {
  81. log.Trace("Repository created: %s/%s", ctxUser.Name, form.RepoName)
  82. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName)
  83. return
  84. } else if err == models.ErrRepoAlreadyExist {
  85. ctx.Data["Err_RepoName"] = true
  86. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), CREATE, &form)
  87. return
  88. } else if err == models.ErrRepoNameIllegal {
  89. ctx.Data["Err_RepoName"] = true
  90. ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), CREATE, &form)
  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. ctx.Handle(500, "CreatePost", err)
  99. }
  100. func Migrate(ctx *middleware.Context) {
  101. ctx.Data["Title"] = ctx.Tr("new_migrate")
  102. ctxUser := ctx.User
  103. if orgId := com.StrTo(ctx.Query("org")).MustInt64(); orgId > 0 {
  104. org, err := models.GetUserById(orgId)
  105. if err != nil && err != models.ErrUserNotExist {
  106. ctx.Handle(500, "GetUserById", err)
  107. return
  108. }
  109. ctxUser = org
  110. }
  111. ctx.Data["ContextUser"] = ctxUser
  112. if err := ctx.User.GetOrganizations(); err != nil {
  113. ctx.Handle(500, "GetOrganizations", err)
  114. return
  115. }
  116. ctx.Data["Orgs"] = ctx.User.Orgs
  117. ctx.HTML(200, MIGRATE)
  118. }
  119. func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) {
  120. ctx.Data["Title"] = ctx.Tr("new_migrate")
  121. ctxUser := ctx.User
  122. // Not equal means current user is an organization.
  123. if form.Uid != ctx.User.Id {
  124. org, err := models.GetUserById(form.Uid)
  125. if err != nil {
  126. ctx.Handle(500, "GetUserById", err)
  127. return
  128. }
  129. ctxUser = org
  130. }
  131. ctx.Data["ContextUser"] = ctxUser
  132. if err := ctx.User.GetOrganizations(); err != nil {
  133. ctx.Handle(500, "GetOrganizations", err)
  134. return
  135. }
  136. ctx.Data["Orgs"] = ctx.User.Orgs
  137. if ctx.HasError() {
  138. ctx.HTML(200, MIGRATE)
  139. return
  140. }
  141. if ctxUser.IsOrganization() {
  142. // Check ownership of organization.
  143. if !ctxUser.IsOrgOwner(ctx.User.Id) {
  144. ctx.Error(403)
  145. return
  146. }
  147. }
  148. authStr := strings.Replace(fmt.Sprintf("://%s:%s",
  149. form.AuthUserName, form.AuthPasswd), "@", "%40", -1)
  150. url := strings.Replace(form.HttpsUrl, "://", authStr+"@", 1)
  151. repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private,
  152. form.Mirror, url)
  153. if err == nil {
  154. log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName)
  155. ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName)
  156. return
  157. } else if err == models.ErrRepoAlreadyExist {
  158. ctx.Data["Err_RepoName"] = true
  159. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), MIGRATE, &form)
  160. return
  161. } else if err == models.ErrRepoNameIllegal {
  162. ctx.Data["Err_RepoName"] = true
  163. ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), MIGRATE, &form)
  164. return
  165. }
  166. if repo != nil {
  167. if errDelete := models.DeleteRepository(ctxUser.Id, repo.Id, ctxUser.Name); errDelete != nil {
  168. log.Error(4, "DeleteRepository: %v", errDelete)
  169. }
  170. }
  171. if strings.Contains(err.Error(), "Authentication failed") {
  172. ctx.Data["Err_Auth"] = true
  173. ctx.RenderWithErr(ctx.Tr("form.auth_failed", err), MIGRATE, &form)
  174. return
  175. }
  176. ctx.Handle(500, "MigratePost", err)
  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 "fork":
  190. repo, err := models.ForkRepository(ctx.User, ctx.Repo.Repository)
  191. if err != nil {
  192. if err != models.ErrRepoAlreadyExist {
  193. log.Error(4, "Action(%s): %v", ctx.Params(":action"), err)
  194. ctx.JSON(200, map[string]interface{}{
  195. "ok": false,
  196. "err": err.Error(),
  197. })
  198. return
  199. }
  200. }
  201. ctx.Redirect(setting.AppSubUrl + "/" + repo.Owner.Name + "/" + repo.Name)
  202. return
  203. case "desc":
  204. if !ctx.Repo.IsOwner {
  205. ctx.Error(404)
  206. return
  207. }
  208. ctx.Repo.Repository.Description = ctx.Query("desc")
  209. ctx.Repo.Repository.Website = ctx.Query("site")
  210. err = models.UpdateRepository(ctx.Repo.Repository)
  211. }
  212. if err != nil {
  213. log.Error(4, "Action(%s): %v", ctx.Params(":action"), err)
  214. ctx.JSON(200, map[string]interface{}{
  215. "ok": false,
  216. "err": err.Error(),
  217. })
  218. return
  219. }
  220. ctx.Redirect(ctx.Repo.RepoLink)
  221. return
  222. ctx.JSON(200, map[string]interface{}{
  223. "ok": true,
  224. })
  225. }
  226. func Download(ctx *middleware.Context) {
  227. var (
  228. uri = ctx.Params("*")
  229. refName string
  230. ext string
  231. archivePath string
  232. archiveType git.ArchiveType
  233. )
  234. switch {
  235. case strings.HasSuffix(uri, ".zip"):
  236. ext = ".zip"
  237. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  238. archiveType = git.ZIP
  239. case strings.HasSuffix(uri, ".tar.gz"):
  240. ext = ".tar.gz"
  241. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz")
  242. archiveType = git.TARGZ
  243. default:
  244. ctx.Error(404)
  245. return
  246. }
  247. refName = strings.TrimSuffix(uri, ext)
  248. if !com.IsDir(archivePath) {
  249. if err := os.MkdirAll(archivePath, os.ModePerm); err != nil {
  250. ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err)
  251. return
  252. }
  253. }
  254. // Get corresponding commit.
  255. var (
  256. commit *git.Commit
  257. err error
  258. )
  259. gitRepo := ctx.Repo.GitRepo
  260. if gitRepo.IsBranchExist(refName) {
  261. commit, err = gitRepo.GetCommitOfBranch(refName)
  262. if err != nil {
  263. ctx.Handle(500, "Download", err)
  264. return
  265. }
  266. } else if gitRepo.IsTagExist(refName) {
  267. commit, err = gitRepo.GetCommitOfTag(refName)
  268. if err != nil {
  269. ctx.Handle(500, "Download", err)
  270. return
  271. }
  272. } else if len(refName) == 40 {
  273. commit, err = gitRepo.GetCommit(refName)
  274. if err != nil {
  275. ctx.Handle(404, "Download", nil)
  276. return
  277. }
  278. } else {
  279. ctx.Error(404)
  280. return
  281. }
  282. archivePath = path.Join(archivePath, base.ShortSha(commit.Id.String())+ext)
  283. if !com.IsFile(archivePath) {
  284. if err := commit.CreateArchive(archivePath, archiveType); err != nil {
  285. ctx.Handle(500, "Download -> CreateArchive "+archivePath, err)
  286. return
  287. }
  288. }
  289. ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+base.ShortSha(commit.Id.String())+ext)
  290. }