single.go 4.6 KB

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