single.go 4.8 KB

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