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.

single.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. "strings"
  7. "github.com/codegangsta/martini"
  8. "github.com/gogits/git"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/base"
  11. "github.com/gogits/gogs/modules/log"
  12. "github.com/gogits/gogs/modules/middleware"
  13. )
  14. func Branches(ctx *middleware.Context, params martini.Params) {
  15. if !ctx.Repo.IsValid {
  16. return
  17. }
  18. brs, err := models.GetBranches(params["username"], params["reponame"])
  19. if err != nil {
  20. ctx.Handle(200, "repo.Branches", err)
  21. return
  22. } else if len(brs) == 0 {
  23. ctx.Render.Error(404)
  24. return
  25. }
  26. ctx.Data["Username"] = params["username"]
  27. ctx.Data["Reponame"] = params["reponame"]
  28. ctx.Data["Branchname"] = brs[0]
  29. ctx.Data["Branches"] = brs
  30. ctx.Data["IsRepoToolbarBranches"] = true
  31. ctx.Render.HTML(200, "repo/branches", ctx.Data)
  32. }
  33. func Single(ctx *middleware.Context, params martini.Params) {
  34. if !ctx.Repo.IsValid {
  35. return
  36. }
  37. if params["branchname"] == "" {
  38. params["branchname"] = "master"
  39. }
  40. // Get tree path
  41. treename := params["_1"]
  42. // Branches.
  43. brs, err := models.GetBranches(params["username"], params["reponame"])
  44. if err != nil {
  45. log.Error("repo.Single(GetBranches): %v", err)
  46. ctx.Render.Error(404)
  47. return
  48. } else if len(brs) == 0 {
  49. ctx.Data["IsBareRepo"] = true
  50. ctx.Render.HTML(200, "repo/single", ctx.Data)
  51. return
  52. }
  53. ctx.Data["Branches"] = brs
  54. // Directory and file list.
  55. files, err := models.GetReposFiles(params["username"], params["reponame"],
  56. params["branchname"], params["commitid"], treename)
  57. if err != nil {
  58. log.Error("repo.Single(GetReposFiles): %v", err)
  59. ctx.Render.Error(404)
  60. return
  61. }
  62. ctx.Data["Username"] = params["username"]
  63. ctx.Data["Reponame"] = params["reponame"]
  64. ctx.Data["Branchname"] = params["branchname"]
  65. var treenames []string
  66. Paths := make([]string, 0)
  67. if len(treename) > 0 {
  68. treenames = strings.Split(treename, "/")
  69. for i, _ := range treenames {
  70. Paths = append(Paths, strings.Join(treenames[0:i+1], "/"))
  71. }
  72. }
  73. // Get latest commit according username and repo name
  74. commit, err := models.GetCommit(params["username"], params["reponame"],
  75. params["branchname"], params["commitid"])
  76. if err != nil {
  77. log.Error("repo.Single(GetCommit): %v", err)
  78. ctx.Render.Error(404)
  79. return
  80. }
  81. ctx.Data["CurrentCommit"] = commit
  82. var readmeFile *models.RepoFile
  83. for _, f := range files {
  84. if !f.IsFile() || len(f.Name) < 6 {
  85. continue
  86. } else if strings.ToLower(f.Name[:6]) == "readme" {
  87. readmeFile = f
  88. break
  89. }
  90. }
  91. if readmeFile != nil {
  92. ctx.Data["ReadmeExist"] = true
  93. // if file large than 1M not show it
  94. if readmeFile.Size > 1024*1024 || readmeFile.Filemode != git.FileModeBlob {
  95. ctx.Data["FileIsLarge"] = true
  96. } else if blob, err := readmeFile.LookupBlob(); err != nil {
  97. ctx.Data["ReadmeExist"] = false
  98. } else {
  99. // current repo branch link
  100. urlPrefix := "http://" + base.Domain + "/" + ctx.Repo.Owner.LowerName + "/" +
  101. ctx.Repo.Repository.Name + "/blob/" + params["branchname"]
  102. ctx.Data["ReadmeContent"] = string(base.RenderMarkdown(blob.Contents(), urlPrefix))
  103. }
  104. }
  105. ctx.Data["Paths"] = Paths
  106. ctx.Data["Treenames"] = treenames
  107. ctx.Data["IsRepoToolbarSource"] = true
  108. ctx.Data["Files"] = files
  109. ctx.Render.HTML(200, "repo/single", ctx.Data)
  110. }
  111. func Setting(ctx *middleware.Context, params martini.Params) {
  112. if !ctx.Repo.IsOwner {
  113. ctx.Render.Error(404)
  114. return
  115. }
  116. // Branches.
  117. brs, err := models.GetBranches(params["username"], params["reponame"])
  118. if err != nil {
  119. log.Error("repo.Setting(GetBranches): %v", err)
  120. ctx.Render.Error(404)
  121. return
  122. } else if len(brs) == 0 {
  123. ctx.Data["IsBareRepo"] = true
  124. ctx.Render.HTML(200, "repo/setting", ctx.Data)
  125. return
  126. }
  127. var title string
  128. if t, ok := ctx.Data["Title"].(string); ok {
  129. title = t
  130. }
  131. ctx.Data["Title"] = title + " - settings"
  132. ctx.Data["IsRepoToolbarSetting"] = true
  133. ctx.Render.HTML(200, "repo/setting", ctx.Data)
  134. }
  135. func Commits(ctx *middleware.Context, params martini.Params) {
  136. brs, err := models.GetBranches(params["username"], params["reponame"])
  137. if err != nil {
  138. ctx.Handle(200, "repo.Commits", err)
  139. return
  140. } else if len(brs) == 0 {
  141. ctx.Render.Error(404)
  142. return
  143. }
  144. ctx.Data["IsRepoToolbarCommits"] = true
  145. commits, err := models.GetCommits(params["username"],
  146. params["reponame"], params["branchname"])
  147. if err != nil {
  148. ctx.Render.Error(404)
  149. return
  150. }
  151. ctx.Data["Commits"] = commits
  152. ctx.Render.HTML(200, "repo/commits", ctx.Data)
  153. }
  154. func Issues(ctx *middleware.Context) string {
  155. return "This is issues page"
  156. }
  157. func Pulls(ctx *middleware.Context) string {
  158. return "This is pulls page"
  159. }