view.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. "bytes"
  7. "fmt"
  8. gotemplate "html/template"
  9. "io/ioutil"
  10. "path"
  11. "strings"
  12. "github.com/Unknwon/paginater"
  13. "github.com/gogits/git-module"
  14. "github.com/gogits/gogs/models"
  15. "github.com/gogits/gogs/modules/base"
  16. "github.com/gogits/gogs/modules/context"
  17. "github.com/gogits/gogs/modules/log"
  18. "github.com/gogits/gogs/modules/markdown"
  19. "github.com/gogits/gogs/modules/setting"
  20. "github.com/gogits/gogs/modules/template"
  21. "github.com/gogits/gogs/modules/template/highlight"
  22. "strconv"
  23. )
  24. const (
  25. HOME base.TplName = "repo/home"
  26. WATCHERS base.TplName = "repo/watchers"
  27. FORKS base.TplName = "repo/forks"
  28. )
  29. func Home(ctx *context.Context) {
  30. title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
  31. if len(ctx.Repo.Repository.Description) > 0 {
  32. title += ": " + ctx.Repo.Repository.Description
  33. }
  34. ctx.Data["Title"] = title
  35. ctx.Data["PageIsViewCode"] = true
  36. ctx.Data["RequireHighlightJS"] = true
  37. ctx.Data["IsWriter"] = ctx.Repo.IsWriter()
  38. branchName := ctx.Repo.BranchName
  39. userName := ctx.Repo.Owner.Name
  40. repoName := ctx.Repo.Repository.Name
  41. repoLink := ctx.Repo.RepoLink
  42. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  43. treeLink := branchLink
  44. rawLink := ctx.Repo.RepoLink + "/raw/" + branchName
  45. editLink := ctx.Repo.RepoLink + "/_edit/" + branchName
  46. newFileLink := ctx.Repo.RepoLink + "/_new/" + branchName
  47. forkLink := setting.AppSubUrl + "/repo/fork/" + strconv.FormatInt(ctx.Repo.Repository.ID, 10)
  48. uploadFileLink := ctx.Repo.RepoLink + "/upload/" + branchName
  49. // Get tree path
  50. treename := ctx.Repo.TreeName
  51. if len(treename) > 0 {
  52. if treename[len(treename)-1] == '/' {
  53. ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1])
  54. return
  55. }
  56. treeLink += "/" + treename
  57. }
  58. treePath := treename
  59. if len(treePath) != 0 {
  60. treePath = treePath + "/"
  61. }
  62. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treename)
  63. if err != nil {
  64. if git.IsErrNotExist(err) {
  65. ctx.Handle(404, "GetTreeEntryByPath", err)
  66. } else {
  67. ctx.Handle(500, "GetTreeEntryByPath", err)
  68. }
  69. return
  70. }
  71. if !entry.IsDir() {
  72. blob := entry.Blob()
  73. dataRc, err := blob.Data()
  74. if err != nil {
  75. ctx.Handle(404, "blob.Data", err)
  76. return
  77. }
  78. ctx.Data["FileSize"] = blob.Size()
  79. ctx.Data["IsFile"] = true
  80. ctx.Data["FileName"] = blob.Name()
  81. ctx.Data["HighlightClass"] = highlight.FileNameToHighlightClass(blob.Name())
  82. ctx.Data["FileLink"] = rawLink + "/" + treename
  83. buf := make([]byte, 1024)
  84. n, _ := dataRc.Read(buf)
  85. if n > 0 {
  86. buf = buf[:n]
  87. }
  88. _, isTextFile := base.IsTextFile(buf)
  89. _, isImageFile := base.IsImageFile(buf)
  90. _, isPDFFile := base.IsPDFFile(buf)
  91. ctx.Data["IsFileText"] = isTextFile
  92. switch {
  93. case isPDFFile:
  94. ctx.Data["IsPDFFile"] = true
  95. ctx.Data["FileEditLinkTooltip"] = ctx.Tr("repo.cannot_edit_binary_files")
  96. case isImageFile:
  97. ctx.Data["IsImageFile"] = true
  98. ctx.Data["FileEditLinkTooltip"] = ctx.Tr("repo.cannot_edit_binary_files")
  99. case isTextFile:
  100. if blob.Size() >= setting.UI.MaxDisplayFileSize {
  101. ctx.Data["IsFileTooLarge"] = true
  102. } else {
  103. ctx.Data["IsFileTooLarge"] = false
  104. d, _ := ioutil.ReadAll(dataRc)
  105. buf = append(buf, d...)
  106. readmeExist := markdown.IsMarkdownFile(blob.Name()) || markdown.IsReadmeFile(blob.Name())
  107. isMarkdown := readmeExist || markdown.IsMarkdownFile(blob.Name())
  108. ctx.Data["ReadmeExist"] = readmeExist
  109. ctx.Data["IsMarkdown"] = isMarkdown
  110. if isMarkdown {
  111. ctx.Data["FileContent"] = string(markdown.Render(buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
  112. } else {
  113. // Building code view blocks with line number on server side.
  114. var filecontent string
  115. if err, content := template.ToUTF8WithErr(buf); err != nil {
  116. if err != nil {
  117. log.Error(4, "ToUTF8WithErr: %s", err)
  118. }
  119. filecontent = string(buf)
  120. } else {
  121. filecontent = content
  122. }
  123. var output bytes.Buffer
  124. lines := strings.Split(filecontent, "\n")
  125. for index, line := range lines {
  126. output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(line)) + "\n")
  127. }
  128. ctx.Data["FileContent"] = gotemplate.HTML(output.String())
  129. output.Reset()
  130. for i := 0; i < len(lines); i++ {
  131. output.WriteString(fmt.Sprintf(`<span id="L%d">%d</span>`, i+1, i+1))
  132. }
  133. ctx.Data["LineNums"] = gotemplate.HTML(output.String())
  134. }
  135. }
  136. if ctx.Repo.IsWriter() && ctx.Repo.IsViewBranch {
  137. ctx.Data["FileEditLink"] = editLink + "/" + treename
  138. ctx.Data["FileEditLinkTooltip"] = ctx.Tr("repo.edit_this_file")
  139. } else {
  140. if !ctx.Repo.IsViewBranch {
  141. ctx.Data["FileEditLinkTooltip"] = ctx.Tr("repo.must_be_on_branch")
  142. } else if !ctx.Repo.IsWriter() {
  143. ctx.Data["FileEditLink"] = forkLink
  144. ctx.Data["FileEditLinkTooltip"] = ctx.Tr("repo.fork_before_edit")
  145. }
  146. }
  147. default:
  148. ctx.Data["FileEditLinkTooltip"] = ctx.Tr("repo.cannot_edit_binary_files")
  149. }
  150. if ctx.Repo.IsWriter() && ctx.Repo.IsViewBranch {
  151. ctx.Data["FileDeleteLinkTooltip"] = ctx.Tr("repo.delete_this_file")
  152. } else {
  153. if !ctx.Repo.IsViewBranch {
  154. ctx.Data["FileDeleteLinkTooltip"] = ctx.Tr("repo.must_be_on_branch")
  155. } else if !ctx.Repo.IsWriter() {
  156. ctx.Data["FileDeleteLinkTooltip"] = ctx.Tr("repo.must_be_writer")
  157. }
  158. }
  159. } else {
  160. // Directory and file list.
  161. tree, err := ctx.Repo.Commit.SubTree(treename)
  162. if err != nil {
  163. ctx.Handle(404, "SubTree", err)
  164. return
  165. }
  166. entries, err := tree.ListEntries()
  167. if err != nil {
  168. ctx.Handle(500, "ListEntries", err)
  169. return
  170. }
  171. entries.Sort()
  172. ctx.Data["Files"], err = entries.GetCommitsInfo(ctx.Repo.Commit, treePath)
  173. if err != nil {
  174. ctx.Handle(500, "GetCommitsInfo", err)
  175. return
  176. }
  177. var readmeFile *git.Blob
  178. for _, f := range entries {
  179. if f.IsDir() || !markdown.IsReadmeFile(f.Name()) {
  180. continue
  181. } else {
  182. readmeFile = f.Blob()
  183. break
  184. }
  185. }
  186. if readmeFile != nil {
  187. ctx.Data["ReadmeInList"] = true
  188. ctx.Data["ReadmeExist"] = true
  189. if dataRc, err := readmeFile.Data(); err != nil {
  190. ctx.Handle(404, "repo.SinglereadmeFile.Data", err)
  191. return
  192. } else {
  193. buf := make([]byte, 1024)
  194. n, _ := dataRc.Read(buf)
  195. if n > 0 {
  196. buf = buf[:n]
  197. }
  198. ctx.Data["FileSize"] = readmeFile.Size()
  199. ctx.Data["FileLink"] = rawLink + "/" + treename
  200. _, isTextFile := base.IsTextFile(buf)
  201. ctx.Data["FileIsText"] = isTextFile
  202. ctx.Data["FileName"] = readmeFile.Name()
  203. if isTextFile {
  204. d, _ := ioutil.ReadAll(dataRc)
  205. buf = append(buf, d...)
  206. switch {
  207. case markdown.IsMarkdownFile(readmeFile.Name()):
  208. ctx.Data["IsMarkdown"] = true
  209. buf = markdown.Render(buf, treeLink, ctx.Repo.Repository.ComposeMetas())
  210. default:
  211. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  212. }
  213. ctx.Data["FileContent"] = string(buf)
  214. }
  215. }
  216. }
  217. lastCommit := ctx.Repo.Commit
  218. if len(treePath) > 0 {
  219. c, err := ctx.Repo.Commit.GetCommitByPath(treePath)
  220. if err != nil {
  221. ctx.Handle(500, "GetCommitByPath", err)
  222. return
  223. }
  224. lastCommit = c
  225. }
  226. ctx.Data["LastCommit"] = lastCommit
  227. ctx.Data["LastCommitUser"] = models.ValidateCommitWithEmail(lastCommit)
  228. if ctx.Repo.IsWriter() && ctx.Repo.IsViewBranch {
  229. ctx.Data["NewFileLink"] = newFileLink + "/" + treename
  230. if setting.Repository.Upload.Enabled {
  231. ctx.Data["UploadFileLink"] = uploadFileLink + "/" + treename
  232. }
  233. }
  234. }
  235. ctx.Data["Username"] = userName
  236. ctx.Data["Reponame"] = repoName
  237. ec, err := ctx.Repo.GetEditorconfig()
  238. if err != nil && !git.IsErrNotExist(err) {
  239. ctx.Handle(500, "ErrGettingEditorconfig", err)
  240. return
  241. }
  242. ctx.Data["Editorconfig"] = ec
  243. var treenames []string
  244. paths := make([]string, 0)
  245. if len(treename) > 0 {
  246. treenames = strings.Split(treename, "/")
  247. for i := range treenames {
  248. paths = append(paths, strings.Join(treenames[0:i+1], "/"))
  249. }
  250. ctx.Data["HasParentPath"] = true
  251. if len(paths)-2 >= 0 {
  252. ctx.Data["ParentPath"] = "/" + paths[len(paths)-2]
  253. }
  254. }
  255. ctx.Data["Paths"] = paths
  256. ctx.Data["TreeName"] = treename
  257. ctx.Data["Treenames"] = treenames
  258. ctx.Data["TreePath"] = treePath
  259. ctx.Data["BranchLink"] = branchLink
  260. ctx.HTML(200, HOME)
  261. }
  262. func RenderUserCards(ctx *context.Context, total int, getter func(page int) ([]*models.User, error), tpl base.TplName) {
  263. page := ctx.QueryInt("page")
  264. if page <= 0 {
  265. page = 1
  266. }
  267. pager := paginater.New(total, models.ItemsPerPage, page, 5)
  268. ctx.Data["Page"] = pager
  269. items, err := getter(pager.Current())
  270. if err != nil {
  271. ctx.Handle(500, "getter", err)
  272. return
  273. }
  274. ctx.Data["Cards"] = items
  275. ctx.HTML(200, tpl)
  276. }
  277. func Watchers(ctx *context.Context) {
  278. ctx.Data["Title"] = ctx.Tr("repo.watchers")
  279. ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers")
  280. ctx.Data["PageIsWatchers"] = true
  281. RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, ctx.Repo.Repository.GetWatchers, WATCHERS)
  282. }
  283. func Stars(ctx *context.Context) {
  284. ctx.Data["Title"] = ctx.Tr("repo.stargazers")
  285. ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers")
  286. ctx.Data["PageIsStargazers"] = true
  287. RenderUserCards(ctx, ctx.Repo.Repository.NumStars, ctx.Repo.Repository.GetStargazers, WATCHERS)
  288. }
  289. func Forks(ctx *context.Context) {
  290. ctx.Data["Title"] = ctx.Tr("repos.forks")
  291. forks, err := ctx.Repo.Repository.GetForks()
  292. if err != nil {
  293. ctx.Handle(500, "GetForks", err)
  294. return
  295. }
  296. for _, fork := range forks {
  297. if err = fork.GetOwner(); err != nil {
  298. ctx.Handle(500, "GetOwner", err)
  299. return
  300. }
  301. }
  302. ctx.Data["Forks"] = forks
  303. ctx.HTML(200, FORKS)
  304. }