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.

repo.go 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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, error := models.ForkRepository(ctx.User, ctx.Repo.Repository)
  191. log.Info("Tried to fork a repo!")
  192. log.Info("Repo thing is " + repo.Name)
  193. if error != nil {
  194. ctx.Redirect(setting.AppSubUrl + "/" + ctx.User.Name + "/" + repo.Name)
  195. return
  196. }
  197. case "desc":
  198. if !ctx.Repo.IsOwner {
  199. ctx.Error(404)
  200. return
  201. }
  202. ctx.Repo.Repository.Description = ctx.Query("desc")
  203. ctx.Repo.Repository.Website = ctx.Query("site")
  204. err = models.UpdateRepository(ctx.Repo.Repository)
  205. }
  206. if err != nil {
  207. log.Error(4, "Action(%s): %v", ctx.Params(":action"), err)
  208. ctx.JSON(200, map[string]interface{}{
  209. "ok": false,
  210. "err": err.Error(),
  211. })
  212. return
  213. }
  214. ctx.Redirect(ctx.Repo.RepoLink)
  215. return
  216. ctx.JSON(200, map[string]interface{}{
  217. "ok": true,
  218. })
  219. }
  220. func Download(ctx *middleware.Context) {
  221. var (
  222. uri = ctx.Params("*")
  223. refName string
  224. ext string
  225. archivePath string
  226. )
  227. switch {
  228. case strings.HasSuffix(uri, ".zip"):
  229. ext = ".zip"
  230. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip")
  231. case strings.HasSuffix(uri, ".tar.gz"):
  232. ext = ".tar.gz"
  233. archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/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, ctx.Repo.CommitId+ext)
  274. if !com.IsFile(archivePath) {
  275. if err := commit.CreateArchive(archivePath, git.ZIP); 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. }