You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

view.go 9.6KB

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