view.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package repo
  2. import (
  3. "bytes"
  4. "fmt"
  5. "gitote/gitote/models"
  6. "gitote/gitote/pkg/context"
  7. "gitote/gitote/pkg/markup"
  8. "gitote/gitote/pkg/setting"
  9. "gitote/gitote/pkg/template"
  10. "gitote/gitote/pkg/template/highlight"
  11. "gitote/gitote/pkg/tool"
  12. gotemplate "html/template"
  13. "io/ioutil"
  14. "path"
  15. "strings"
  16. raven "github.com/getsentry/raven-go"
  17. "gitlab.com/gitote/git-module"
  18. "gitlab.com/yoginth/paginater"
  19. log "gopkg.in/clog.v1"
  20. )
  21. const (
  22. BARE = "repo/bare"
  23. HOME = "repo/home"
  24. WATCHERS = "repo/watchers"
  25. FORKS = "repo/forks"
  26. )
  27. func renderDirectory(c *context.Context, treeLink string) {
  28. tree, err := c.Repo.Commit.SubTree(c.Repo.TreePath)
  29. if err != nil {
  30. c.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err)
  31. return
  32. }
  33. entries, err := tree.ListEntries()
  34. if err != nil {
  35. c.ServerError("ListEntries", err)
  36. return
  37. }
  38. entries.Sort()
  39. c.Data["Files"], err = entries.GetCommitsInfoWithCustomConcurrency(c.Repo.Commit, c.Repo.TreePath, setting.Repository.CommitsFetchConcurrency)
  40. if err != nil {
  41. c.ServerError("GetCommitsInfoWithCustomConcurrency", err)
  42. return
  43. }
  44. var readmeFile *git.Blob
  45. for _, entry := range entries {
  46. if entry.IsDir() || !markup.IsReadmeFile(entry.Name()) {
  47. continue
  48. }
  49. // TODO: collect all possible README files and show with priority.
  50. readmeFile = entry.Blob()
  51. break
  52. }
  53. if readmeFile != nil {
  54. c.Data["RawFileLink"] = ""
  55. c.Data["ReadmeInList"] = true
  56. c.Data["ReadmeExist"] = true
  57. dataRc, err := readmeFile.Data()
  58. if err != nil {
  59. c.ServerError("readmeFile.Data", err)
  60. return
  61. }
  62. buf := make([]byte, 1024)
  63. n, _ := dataRc.Read(buf)
  64. buf = buf[:n]
  65. isTextFile := tool.IsTextFile(buf)
  66. c.Data["IsTextFile"] = isTextFile
  67. c.Data["FileName"] = readmeFile.Name()
  68. if isTextFile {
  69. d, _ := ioutil.ReadAll(dataRc)
  70. buf = append(buf, d...)
  71. switch markup.Detect(readmeFile.Name()) {
  72. case markup.MARKDOWN:
  73. c.Data["IsMarkdown"] = true
  74. buf = markup.Markdown(buf, treeLink, c.Repo.Repository.ComposeMetas())
  75. case markup.ORG_MODE:
  76. c.Data["IsMarkdown"] = true
  77. buf = markup.OrgMode(buf, treeLink, c.Repo.Repository.ComposeMetas())
  78. case markup.IPYTHON_NOTEBOOK:
  79. c.Data["IsIPythonNotebook"] = true
  80. c.Data["RawFileLink"] = c.Repo.RepoLink + "/raw/" + path.Join(c.Repo.BranchName, c.Repo.TreePath, readmeFile.Name())
  81. default:
  82. buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
  83. }
  84. c.Data["FileContent"] = string(buf)
  85. }
  86. }
  87. // Show latest commit info of repository in table header,
  88. // or of directory if not in root directory.
  89. latestCommit := c.Repo.Commit
  90. if len(c.Repo.TreePath) > 0 {
  91. latestCommit, err = c.Repo.Commit.GetCommitByPath(c.Repo.TreePath)
  92. if err != nil {
  93. c.ServerError("GetCommitByPath", err)
  94. return
  95. }
  96. }
  97. c.Data["LatestCommit"] = latestCommit
  98. c.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit)
  99. if c.Repo.CanEnableEditor() {
  100. c.Data["CanAddFile"] = true
  101. c.Data["CanUploadFile"] = setting.Repository.Upload.Enabled
  102. }
  103. }
  104. func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink string) {
  105. c.Data["IsViewFile"] = true
  106. blob := entry.Blob()
  107. dataRc, err := blob.Data()
  108. if err != nil {
  109. c.Handle(500, "Data", err)
  110. return
  111. }
  112. c.Data["FileSize"] = blob.Size()
  113. c.Data["FileName"] = blob.Name()
  114. c.Data["HighlightClass"] = highlight.FileNameToHighlightClass(blob.Name())
  115. c.Data["RawFileLink"] = rawLink + "/" + c.Repo.TreePath
  116. buf := make([]byte, 1024)
  117. n, _ := dataRc.Read(buf)
  118. buf = buf[:n]
  119. isTextFile := tool.IsTextFile(buf)
  120. c.Data["IsTextFile"] = isTextFile
  121. // Assume file is not editable first.
  122. if !isTextFile {
  123. c.Data["EditFileTooltip"] = c.Tr("repo.editor.cannot_edit_non_text_files")
  124. }
  125. canEnableEditor := c.Repo.CanEnableEditor()
  126. switch {
  127. case isTextFile:
  128. if blob.Size() >= setting.UI.MaxDisplayFileSize {
  129. c.Data["IsFileTooLarge"] = true
  130. break
  131. }
  132. c.Data["ReadmeExist"] = markup.IsReadmeFile(blob.Name())
  133. d, _ := ioutil.ReadAll(dataRc)
  134. buf = append(buf, d...)
  135. switch markup.Detect(blob.Name()) {
  136. case markup.MARKDOWN:
  137. c.Data["IsMarkdown"] = true
  138. c.Data["FileContent"] = string(markup.Markdown(buf, path.Dir(treeLink), c.Repo.Repository.ComposeMetas()))
  139. case markup.ORG_MODE:
  140. c.Data["IsMarkdown"] = true
  141. c.Data["FileContent"] = string(markup.OrgMode(buf, path.Dir(treeLink), c.Repo.Repository.ComposeMetas()))
  142. case markup.IPYTHON_NOTEBOOK:
  143. c.Data["IsIPythonNotebook"] = true
  144. default:
  145. // Building code view blocks with line number on server side.
  146. var fileContent string
  147. if err, content := template.ToUTF8WithErr(buf); err != nil {
  148. if err != nil {
  149. raven.CaptureErrorAndWait(err, nil)
  150. log.Error(4, "ToUTF8WithErr: %s", err)
  151. }
  152. fileContent = string(buf)
  153. } else {
  154. fileContent = content
  155. }
  156. var output bytes.Buffer
  157. lines := strings.Split(fileContent, "\n")
  158. // Remove blank line at the end of file
  159. if len(lines) > 0 && len(lines[len(lines)-1]) == 0 {
  160. lines = lines[:len(lines)-1]
  161. }
  162. for index, line := range lines {
  163. output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(strings.TrimRight(line, "\r"))) + "\n")
  164. }
  165. c.Data["FileContent"] = gotemplate.HTML(output.String())
  166. output.Reset()
  167. for i := 0; i < len(lines); i++ {
  168. output.WriteString(fmt.Sprintf(`<span id="L%d">%d</span>`, i+1, i+1))
  169. }
  170. c.Data["LineNums"] = gotemplate.HTML(output.String())
  171. }
  172. if canEnableEditor {
  173. c.Data["CanEditFile"] = true
  174. c.Data["EditFileTooltip"] = c.Tr("repo.editor.edit_this_file")
  175. } else if !c.Repo.IsViewBranch {
  176. c.Data["EditFileTooltip"] = c.Tr("repo.editor.must_be_on_a_branch")
  177. } else if !c.Repo.IsWriter() {
  178. c.Data["EditFileTooltip"] = c.Tr("repo.editor.fork_before_edit")
  179. }
  180. case tool.IsPDFFile(buf):
  181. c.Data["IsPDFFile"] = true
  182. case tool.IsVideoFile(buf):
  183. c.Data["IsVideoFile"] = true
  184. case tool.IsImageFile(buf):
  185. c.Data["IsImageFile"] = true
  186. }
  187. if canEnableEditor {
  188. c.Data["CanDeleteFile"] = true
  189. c.Data["DeleteFileTooltip"] = c.Tr("repo.editor.delete_this_file")
  190. } else if !c.Repo.IsViewBranch {
  191. c.Data["DeleteFileTooltip"] = c.Tr("repo.editor.must_be_on_a_branch")
  192. } else if !c.Repo.IsWriter() {
  193. c.Data["DeleteFileTooltip"] = c.Tr("repo.editor.must_have_write_access")
  194. }
  195. }
  196. func setEditorconfigIfExists(c *context.Context) {
  197. ec, err := c.Repo.GetEditorconfig()
  198. if err != nil && !git.IsErrNotExist(err) {
  199. log.Trace("setEditorconfigIfExists.GetEditorconfig [%d]: %v", c.Repo.Repository.ID, err)
  200. return
  201. }
  202. c.Data["Editorconfig"] = ec
  203. }
  204. func Home(c *context.Context) {
  205. c.Data["PageIsViewFiles"] = true
  206. if c.Repo.Repository.IsBare {
  207. c.HTML(200, BARE)
  208. return
  209. }
  210. title := c.Repo.Repository.Owner.Name + "/" + c.Repo.Repository.Name
  211. if len(c.Repo.Repository.Description) > 0 {
  212. title += ": " + c.Repo.Repository.Description
  213. }
  214. c.Data["Title"] = title
  215. if c.Repo.BranchName != c.Repo.Repository.DefaultBranch {
  216. c.Data["Title"] = title + " @ " + c.Repo.BranchName
  217. }
  218. c.Data["RequireHighlightJS"] = true
  219. branchLink := c.Repo.RepoLink + "/src/" + c.Repo.BranchName
  220. treeLink := branchLink
  221. rawLink := c.Repo.RepoLink + "/raw/" + c.Repo.BranchName
  222. isRootDir := false
  223. if len(c.Repo.TreePath) > 0 {
  224. treeLink += "/" + c.Repo.TreePath
  225. } else {
  226. isRootDir = true
  227. // Only show Git stats panel when view root directory
  228. var err error
  229. c.Repo.CommitsCount, err = c.Repo.Commit.CommitsCount()
  230. if err != nil {
  231. c.Handle(500, "CommitsCount", err)
  232. return
  233. }
  234. c.Data["CommitsCount"] = c.Repo.CommitsCount
  235. }
  236. c.Data["PageIsRepoHome"] = isRootDir
  237. // Get current entry user currently looking at.
  238. entry, err := c.Repo.Commit.GetTreeEntryByPath(c.Repo.TreePath)
  239. if err != nil {
  240. c.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
  241. return
  242. }
  243. if entry.IsDir() {
  244. renderDirectory(c, treeLink)
  245. } else {
  246. renderFile(c, entry, treeLink, rawLink)
  247. }
  248. if c.Written() {
  249. return
  250. }
  251. setEditorconfigIfExists(c)
  252. if c.Written() {
  253. return
  254. }
  255. var treeNames []string
  256. paths := make([]string, 0, 5)
  257. if len(c.Repo.TreePath) > 0 {
  258. treeNames = strings.Split(c.Repo.TreePath, "/")
  259. for i := range treeNames {
  260. paths = append(paths, strings.Join(treeNames[:i+1], "/"))
  261. }
  262. c.Data["HasParentPath"] = true
  263. if len(paths)-2 >= 0 {
  264. c.Data["ParentPath"] = "/" + paths[len(paths)-2]
  265. }
  266. }
  267. c.Data["Paths"] = paths
  268. c.Data["TreeLink"] = treeLink
  269. c.Data["TreeNames"] = treeNames
  270. c.Data["BranchLink"] = branchLink
  271. c.HTML(200, HOME)
  272. }
  273. func RenderUserCards(c *context.Context, total int, getter func(page int) ([]*models.User, error), tpl string) {
  274. page := c.QueryInt("page")
  275. if page <= 0 {
  276. page = 1
  277. }
  278. pager := paginater.New(total, models.ItemsPerPage, page, 5)
  279. c.Data["Page"] = pager
  280. items, err := getter(pager.Current())
  281. if err != nil {
  282. c.Handle(500, "getter", err)
  283. return
  284. }
  285. c.Data["Cards"] = items
  286. c.HTML(200, tpl)
  287. }
  288. func Watchers(c *context.Context) {
  289. c.Data["Title"] = c.Tr("repo.watchers")
  290. c.Data["CardsTitle"] = c.Tr("repo.watchers")
  291. c.Data["PageIsWatchers"] = true
  292. RenderUserCards(c, c.Repo.Repository.NumWatches, c.Repo.Repository.GetWatchers, WATCHERS)
  293. }
  294. func Stars(c *context.Context) {
  295. c.Data["Title"] = c.Tr("repo.stargazers")
  296. c.Data["CardsTitle"] = c.Tr("repo.stargazers")
  297. c.Data["PageIsStargazers"] = true
  298. RenderUserCards(c, c.Repo.Repository.NumStars, c.Repo.Repository.GetStargazers, WATCHERS)
  299. }
  300. func Forks(c *context.Context) {
  301. c.Data["Title"] = c.Tr("repos.forks")
  302. forks, err := c.Repo.Repository.GetForks()
  303. if err != nil {
  304. c.Handle(500, "GetForks", err)
  305. return
  306. }
  307. for _, fork := range forks {
  308. if err = fork.GetOwner(); err != nil {
  309. c.Handle(500, "GetOwner", err)
  310. return
  311. }
  312. }
  313. c.Data["Forks"] = forks
  314. c.HTML(200, FORKS)
  315. }