single.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.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.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.Error(404)
  47. return
  48. } else if len(brs) == 0 {
  49. ctx.Data["IsBareRepo"] = true
  50. ctx.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.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. ctx.Data["HasParentPath"] = true
  73. if len(Paths)-2 >= 0 {
  74. ctx.Data["ParentPath"] = "/" + Paths[len(Paths)-2]
  75. }
  76. }
  77. // Get latest commit according username and repo name
  78. commit, err := models.GetCommit(params["username"], params["reponame"],
  79. params["branchname"], params["commitid"])
  80. if err != nil {
  81. log.Error("repo.Single(GetCommit): %v", err)
  82. ctx.Error(404)
  83. return
  84. }
  85. ctx.Data["LastCommit"] = commit
  86. var readmeFile *models.RepoFile
  87. for _, f := range files {
  88. if !f.IsFile() || len(f.Name) < 6 {
  89. continue
  90. } else if strings.ToLower(f.Name[:6]) == "readme" {
  91. readmeFile = f
  92. break
  93. }
  94. }
  95. if readmeFile != nil {
  96. ctx.Data["ReadmeExist"] = true
  97. // if file large than 1M not show it
  98. if readmeFile.Size > 1024*1024 || readmeFile.Filemode != git.FileModeBlob {
  99. ctx.Data["FileIsLarge"] = true
  100. } else if blob, err := readmeFile.LookupBlob(); err != nil {
  101. ctx.Data["ReadmeExist"] = false
  102. } else {
  103. // current repo branch link
  104. urlPrefix := "http://" + base.Domain + "/" + ctx.Repo.Owner.LowerName + "/" +
  105. ctx.Repo.Repository.Name + "/blob/" + params["branchname"]
  106. ctx.Data["ReadmeContent"] = string(base.RenderMarkdown(blob.Contents(), urlPrefix))
  107. }
  108. }
  109. ctx.Data["Paths"] = Paths
  110. ctx.Data["Treenames"] = treenames
  111. ctx.Data["IsRepoToolbarSource"] = true
  112. ctx.Data["Files"] = files
  113. ctx.HTML(200, "repo/single", ctx.Data)
  114. }
  115. func Setting(ctx *middleware.Context, params martini.Params) {
  116. if !ctx.Repo.IsOwner {
  117. ctx.Error(404)
  118. return
  119. }
  120. // Branches.
  121. brs, err := models.GetBranches(params["username"], params["reponame"])
  122. if err != nil {
  123. log.Error("repo.Setting(GetBranches): %v", err)
  124. ctx.Error(404)
  125. return
  126. } else if len(brs) == 0 {
  127. ctx.Data["IsBareRepo"] = true
  128. ctx.HTML(200, "repo/setting", ctx.Data)
  129. return
  130. }
  131. var title string
  132. if t, ok := ctx.Data["Title"].(string); ok {
  133. title = t
  134. }
  135. ctx.Data["Title"] = title + " - settings"
  136. ctx.Data["IsRepoToolbarSetting"] = true
  137. ctx.HTML(200, "repo/setting", ctx.Data)
  138. }
  139. func Commits(ctx *middleware.Context, params martini.Params) {
  140. brs, err := models.GetBranches(params["username"], params["reponame"])
  141. if err != nil {
  142. ctx.Handle(200, "repo.Commits", err)
  143. return
  144. } else if len(brs) == 0 {
  145. ctx.Error(404)
  146. return
  147. }
  148. ctx.Data["IsRepoToolbarCommits"] = true
  149. commits, err := models.GetCommits(params["username"],
  150. params["reponame"], params["branchname"])
  151. if err != nil {
  152. ctx.Error(404)
  153. return
  154. }
  155. ctx.Data["Commits"] = commits
  156. ctx.HTML(200, "repo/commits", ctx.Data)
  157. }
  158. func Issues(ctx *middleware.Context) {
  159. ctx.Data["IsRepoToolbarIssues"] = true
  160. ctx.HTML(200, "repo/issues", ctx.Data)
  161. }
  162. func Pulls(ctx *middleware.Context) {
  163. ctx.Data["IsRepoToolbarPulls"] = true
  164. ctx.HTML(200, "repo/pulls", ctx.Data)
  165. }