view.go 9.5 KB

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