single.go 4.9 KB

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