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.

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