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 32KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Copyright 2014 The Gogs Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package repo
  5. import (
  6. "bytes"
  7. gocontext "context"
  8. "encoding/base64"
  9. "fmt"
  10. "io"
  11. "net/http"
  12. "net/url"
  13. "path"
  14. "strings"
  15. "time"
  16. activities_model "code.gitea.io/gitea/models/activities"
  17. admin_model "code.gitea.io/gitea/models/admin"
  18. asymkey_model "code.gitea.io/gitea/models/asymkey"
  19. "code.gitea.io/gitea/models/db"
  20. git_model "code.gitea.io/gitea/models/git"
  21. repo_model "code.gitea.io/gitea/models/repo"
  22. unit_model "code.gitea.io/gitea/models/unit"
  23. user_model "code.gitea.io/gitea/models/user"
  24. "code.gitea.io/gitea/modules/actions"
  25. "code.gitea.io/gitea/modules/base"
  26. "code.gitea.io/gitea/modules/charset"
  27. "code.gitea.io/gitea/modules/container"
  28. "code.gitea.io/gitea/modules/context"
  29. "code.gitea.io/gitea/modules/git"
  30. "code.gitea.io/gitea/modules/highlight"
  31. "code.gitea.io/gitea/modules/lfs"
  32. "code.gitea.io/gitea/modules/log"
  33. "code.gitea.io/gitea/modules/markup"
  34. repo_module "code.gitea.io/gitea/modules/repository"
  35. "code.gitea.io/gitea/modules/setting"
  36. "code.gitea.io/gitea/modules/structs"
  37. "code.gitea.io/gitea/modules/typesniffer"
  38. "code.gitea.io/gitea/modules/util"
  39. "code.gitea.io/gitea/routers/web/feed"
  40. issue_service "code.gitea.io/gitea/services/issue"
  41. "github.com/nektos/act/pkg/model"
  42. )
  43. const (
  44. tplRepoEMPTY base.TplName = "repo/empty"
  45. tplRepoHome base.TplName = "repo/home"
  46. tplRepoViewList base.TplName = "repo/view_list"
  47. tplWatchers base.TplName = "repo/watchers"
  48. tplForks base.TplName = "repo/forks"
  49. tplMigrating base.TplName = "repo/migrate/migrating"
  50. )
  51. // locate a README for a tree in one of the supported paths.
  52. //
  53. // entries is passed to reduce calls to ListEntries(), so
  54. // this has precondition:
  55. //
  56. // entries == ctx.Repo.Commit.SubTree(ctx.Repo.TreePath).ListEntries()
  57. //
  58. // FIXME: There has to be a more efficient way of doing this
  59. func findReadmeFileInEntries(ctx *context.Context, entries []*git.TreeEntry, tryWellKnownDirs bool) (string, *git.TreeEntry, error) {
  60. // Create a list of extensions in priority order
  61. // 1. Markdown files - with and without localisation - e.g. README.en-us.md or README.md
  62. // 2. Txt files - e.g. README.txt
  63. // 3. No extension - e.g. README
  64. exts := append(localizedExtensions(".md", ctx.Locale.Language()), ".txt", "") // sorted by priority
  65. extCount := len(exts)
  66. readmeFiles := make([]*git.TreeEntry, extCount+1)
  67. docsEntries := make([]*git.TreeEntry, 3) // (one of docs/, .gitea/ or .github/)
  68. for _, entry := range entries {
  69. if tryWellKnownDirs && entry.IsDir() {
  70. // as a special case for the top-level repo introduction README,
  71. // fall back to subfolders, looking for e.g. docs/README.md, .gitea/README.zh-CN.txt, .github/README.txt, ...
  72. // (note that docsEntries is ignored unless we are at the root)
  73. lowerName := strings.ToLower(entry.Name())
  74. switch lowerName {
  75. case "docs":
  76. if entry.Name() == "docs" || docsEntries[0] == nil {
  77. docsEntries[0] = entry
  78. }
  79. case ".gitea":
  80. if entry.Name() == ".gitea" || docsEntries[1] == nil {
  81. docsEntries[1] = entry
  82. }
  83. case ".github":
  84. if entry.Name() == ".github" || docsEntries[2] == nil {
  85. docsEntries[2] = entry
  86. }
  87. }
  88. continue
  89. }
  90. if i, ok := util.IsReadmeFileExtension(entry.Name(), exts...); ok {
  91. log.Debug("Potential readme file: %s", entry.Name())
  92. if readmeFiles[i] == nil || base.NaturalSortLess(readmeFiles[i].Name(), entry.Blob().Name()) {
  93. if entry.IsLink() {
  94. target, err := entry.FollowLinks()
  95. if err != nil && !git.IsErrBadLink(err) {
  96. return "", nil, err
  97. } else if target != nil && (target.IsExecutable() || target.IsRegular()) {
  98. readmeFiles[i] = entry
  99. }
  100. } else {
  101. readmeFiles[i] = entry
  102. }
  103. }
  104. }
  105. }
  106. var readmeFile *git.TreeEntry
  107. for _, f := range readmeFiles {
  108. if f != nil {
  109. readmeFile = f
  110. break
  111. }
  112. }
  113. if ctx.Repo.TreePath == "" && readmeFile == nil {
  114. for _, subTreeEntry := range docsEntries {
  115. if subTreeEntry == nil {
  116. continue
  117. }
  118. subTree := subTreeEntry.Tree()
  119. if subTree == nil {
  120. // this should be impossible; if subTreeEntry exists so should this.
  121. continue
  122. }
  123. var err error
  124. childEntries, err := subTree.ListEntries()
  125. if err != nil {
  126. return "", nil, err
  127. }
  128. subfolder, readmeFile, err := findReadmeFileInEntries(ctx, childEntries, false)
  129. if err != nil && !git.IsErrNotExist(err) {
  130. return "", nil, err
  131. }
  132. if readmeFile != nil {
  133. return path.Join(subTreeEntry.Name(), subfolder), readmeFile, nil
  134. }
  135. }
  136. }
  137. return "", readmeFile, nil
  138. }
  139. func renderDirectory(ctx *context.Context, treeLink string) {
  140. entries := renderDirectoryFiles(ctx, 1*time.Second)
  141. if ctx.Written() {
  142. return
  143. }
  144. if ctx.Repo.TreePath != "" {
  145. ctx.Data["HideRepoInfo"] = true
  146. ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefName)
  147. }
  148. subfolder, readmeFile, err := findReadmeFileInEntries(ctx, entries, true)
  149. if err != nil {
  150. ctx.ServerError("findReadmeFileInEntries", err)
  151. return
  152. }
  153. renderReadmeFile(ctx, subfolder, readmeFile, treeLink)
  154. }
  155. // localizedExtensions prepends the provided language code with and without a
  156. // regional identifier to the provided extension.
  157. // Note: the language code will always be lower-cased, if a region is present it must be separated with a `-`
  158. // Note: ext should be prefixed with a `.`
  159. func localizedExtensions(ext, languageCode string) (localizedExts []string) {
  160. if len(languageCode) < 1 {
  161. return []string{ext}
  162. }
  163. lowerLangCode := "." + strings.ToLower(languageCode)
  164. if strings.Contains(lowerLangCode, "-") {
  165. underscoreLangCode := strings.ReplaceAll(lowerLangCode, "-", "_")
  166. indexOfDash := strings.Index(lowerLangCode, "-")
  167. // e.g. [.zh-cn.md, .zh_cn.md, .zh.md, _zh.md, .md]
  168. return []string{lowerLangCode + ext, underscoreLangCode + ext, lowerLangCode[:indexOfDash] + ext, "_" + lowerLangCode[1:indexOfDash] + ext, ext}
  169. }
  170. // e.g. [.en.md, .md]
  171. return []string{lowerLangCode + ext, ext}
  172. }
  173. type fileInfo struct {
  174. isTextFile bool
  175. isLFSFile bool
  176. fileSize int64
  177. lfsMeta *lfs.Pointer
  178. st typesniffer.SniffedType
  179. }
  180. func getFileReader(repoID int64, blob *git.Blob) ([]byte, io.ReadCloser, *fileInfo, error) {
  181. dataRc, err := blob.DataAsync()
  182. if err != nil {
  183. return nil, nil, nil, err
  184. }
  185. buf := make([]byte, 1024)
  186. n, _ := util.ReadAtMost(dataRc, buf)
  187. buf = buf[:n]
  188. st := typesniffer.DetectContentType(buf)
  189. isTextFile := st.IsText()
  190. // FIXME: what happens when README file is an image?
  191. if !isTextFile || !setting.LFS.StartServer {
  192. return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil
  193. }
  194. pointer, _ := lfs.ReadPointerFromBuffer(buf)
  195. if !pointer.IsValid() { // fallback to plain file
  196. return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil
  197. }
  198. meta, err := git_model.GetLFSMetaObjectByOid(db.DefaultContext, repoID, pointer.Oid)
  199. if err != nil && err != git_model.ErrLFSObjectNotExist { // fallback to plain file
  200. return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil
  201. }
  202. dataRc.Close()
  203. if err != nil {
  204. return nil, nil, nil, err
  205. }
  206. dataRc, err = lfs.ReadMetaObject(pointer)
  207. if err != nil {
  208. return nil, nil, nil, err
  209. }
  210. buf = make([]byte, 1024)
  211. n, err = util.ReadAtMost(dataRc, buf)
  212. if err != nil {
  213. dataRc.Close()
  214. return nil, nil, nil, err
  215. }
  216. buf = buf[:n]
  217. st = typesniffer.DetectContentType(buf)
  218. return buf, dataRc, &fileInfo{st.IsText(), true, meta.Size, &meta.Pointer, st}, nil
  219. }
  220. func renderReadmeFile(ctx *context.Context, subfolder string, readmeFile *git.TreeEntry, readmeTreelink string) {
  221. target := readmeFile
  222. if readmeFile != nil && readmeFile.IsLink() {
  223. target, _ = readmeFile.FollowLinks()
  224. }
  225. if target == nil {
  226. // if findReadmeFile() failed and/or gave us a broken symlink (which it shouldn't)
  227. // simply skip rendering the README
  228. return
  229. }
  230. ctx.Data["RawFileLink"] = ""
  231. ctx.Data["ReadmeInList"] = true
  232. ctx.Data["ReadmeExist"] = true
  233. ctx.Data["FileIsSymlink"] = readmeFile.IsLink()
  234. buf, dataRc, fInfo, err := getFileReader(ctx.Repo.Repository.ID, target.Blob())
  235. if err != nil {
  236. ctx.ServerError("getFileReader", err)
  237. return
  238. }
  239. defer dataRc.Close()
  240. ctx.Data["FileIsText"] = fInfo.isTextFile
  241. ctx.Data["FileName"] = path.Join(subfolder, readmeFile.Name())
  242. ctx.Data["IsLFSFile"] = fInfo.isLFSFile
  243. if fInfo.isLFSFile {
  244. filenameBase64 := base64.RawURLEncoding.EncodeToString([]byte(readmeFile.Name()))
  245. ctx.Data["RawFileLink"] = fmt.Sprintf("%s.git/info/lfs/objects/%s/%s", ctx.Repo.Repository.Link(), url.PathEscape(fInfo.lfsMeta.Oid), url.PathEscape(filenameBase64))
  246. }
  247. if !fInfo.isTextFile {
  248. return
  249. }
  250. if fInfo.fileSize >= setting.UI.MaxDisplayFileSize {
  251. // Pretend that this is a normal text file to display 'This file is too large to be shown'
  252. ctx.Data["IsFileTooLarge"] = true
  253. ctx.Data["IsTextFile"] = true
  254. ctx.Data["FileSize"] = fInfo.fileSize
  255. return
  256. }
  257. rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc))
  258. if markupType := markup.Type(readmeFile.Name()); markupType != "" {
  259. ctx.Data["IsMarkup"] = true
  260. ctx.Data["MarkupType"] = markupType
  261. ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, &markup.RenderContext{
  262. Ctx: ctx,
  263. RelativePath: path.Join(ctx.Repo.TreePath, readmeFile.Name()), // ctx.Repo.TreePath is the directory not the Readme so we must append the Readme filename (and path).
  264. URLPrefix: path.Join(readmeTreelink, subfolder),
  265. Metas: ctx.Repo.Repository.ComposeDocumentMetas(),
  266. GitRepo: ctx.Repo.GitRepo,
  267. }, rd)
  268. if err != nil {
  269. log.Error("Render failed for %s in %-v: %v Falling back to rendering source", readmeFile.Name(), ctx.Repo.Repository, err)
  270. buf := &bytes.Buffer{}
  271. ctx.Data["EscapeStatus"], _ = charset.EscapeControlStringReader(rd, buf, ctx.Locale)
  272. ctx.Data["FileContent"] = buf.String()
  273. }
  274. } else {
  275. ctx.Data["IsPlainText"] = true
  276. buf := &bytes.Buffer{}
  277. ctx.Data["EscapeStatus"], err = charset.EscapeControlStringReader(rd, buf, ctx.Locale)
  278. if err != nil {
  279. log.Error("Read failed: %v", err)
  280. }
  281. ctx.Data["FileContent"] = buf.String()
  282. }
  283. }
  284. func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink string) {
  285. ctx.Data["IsViewFile"] = true
  286. ctx.Data["HideRepoInfo"] = true
  287. blob := entry.Blob()
  288. buf, dataRc, fInfo, err := getFileReader(ctx.Repo.Repository.ID, blob)
  289. if err != nil {
  290. ctx.ServerError("getFileReader", err)
  291. return
  292. }
  293. defer dataRc.Close()
  294. ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefName)
  295. ctx.Data["FileIsSymlink"] = entry.IsLink()
  296. ctx.Data["FileName"] = blob.Name()
  297. ctx.Data["RawFileLink"] = rawLink + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
  298. if ctx.Repo.TreePath == ".editorconfig" {
  299. _, editorconfigWarning, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
  300. if editorconfigWarning != nil {
  301. ctx.Data["FileWarning"] = strings.TrimSpace(editorconfigWarning.Error())
  302. }
  303. if editorconfigErr != nil {
  304. ctx.Data["FileError"] = strings.TrimSpace(editorconfigErr.Error())
  305. }
  306. } else if issue_service.IsTemplateConfig(ctx.Repo.TreePath) {
  307. _, issueConfigErr := issue_service.GetTemplateConfig(ctx.Repo.GitRepo, ctx.Repo.TreePath, ctx.Repo.Commit)
  308. if issueConfigErr != nil {
  309. ctx.Data["FileError"] = strings.TrimSpace(issueConfigErr.Error())
  310. }
  311. } else if actions.IsWorkflow(ctx.Repo.TreePath) {
  312. content, err := actions.GetContentFromEntry(entry)
  313. if err != nil {
  314. log.Error("actions.GetContentFromEntry: %v", err)
  315. }
  316. _, workFlowErr := model.ReadWorkflow(bytes.NewReader(content))
  317. if workFlowErr != nil {
  318. ctx.Data["FileError"] = ctx.Locale.Tr("actions.runs.invalid_workflow_helper", workFlowErr.Error())
  319. }
  320. }
  321. isDisplayingSource := ctx.FormString("display") == "source"
  322. isDisplayingRendered := !isDisplayingSource
  323. if fInfo.isLFSFile {
  324. ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/media/" + ctx.Repo.BranchNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
  325. }
  326. isRepresentableAsText := fInfo.st.IsRepresentableAsText()
  327. if !isRepresentableAsText {
  328. // If we can't show plain text, always try to render.
  329. isDisplayingSource = false
  330. isDisplayingRendered = true
  331. }
  332. ctx.Data["IsLFSFile"] = fInfo.isLFSFile
  333. ctx.Data["FileSize"] = fInfo.fileSize
  334. ctx.Data["IsTextFile"] = fInfo.isTextFile
  335. ctx.Data["IsRepresentableAsText"] = isRepresentableAsText
  336. ctx.Data["IsDisplayingSource"] = isDisplayingSource
  337. ctx.Data["IsDisplayingRendered"] = isDisplayingRendered
  338. ctx.Data["IsExecutable"] = entry.IsExecutable()
  339. isTextSource := fInfo.isTextFile || isDisplayingSource
  340. ctx.Data["IsTextSource"] = isTextSource
  341. if isTextSource {
  342. ctx.Data["CanCopyContent"] = true
  343. }
  344. // Check LFS Lock
  345. lfsLock, err := git_model.GetTreePathLock(ctx, ctx.Repo.Repository.ID, ctx.Repo.TreePath)
  346. ctx.Data["LFSLock"] = lfsLock
  347. if err != nil {
  348. ctx.ServerError("GetTreePathLock", err)
  349. return
  350. }
  351. if lfsLock != nil {
  352. u, err := user_model.GetUserByID(ctx, lfsLock.OwnerID)
  353. if err != nil {
  354. ctx.ServerError("GetTreePathLock", err)
  355. return
  356. }
  357. ctx.Data["LFSLockOwner"] = u.Name
  358. ctx.Data["LFSLockOwnerHomeLink"] = u.HomeLink()
  359. ctx.Data["LFSLockHint"] = ctx.Tr("repo.editor.this_file_locked")
  360. }
  361. // Assume file is not editable first.
  362. if fInfo.isLFSFile {
  363. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_lfs_files")
  364. } else if !isRepresentableAsText {
  365. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_non_text_files")
  366. }
  367. switch {
  368. case isRepresentableAsText:
  369. if fInfo.st.IsSvgImage() {
  370. ctx.Data["IsImageFile"] = true
  371. ctx.Data["CanCopyContent"] = true
  372. ctx.Data["HasSourceRenderedToggle"] = true
  373. }
  374. if fInfo.fileSize >= setting.UI.MaxDisplayFileSize {
  375. ctx.Data["IsFileTooLarge"] = true
  376. break
  377. }
  378. rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc))
  379. shouldRenderSource := ctx.FormString("display") == "source"
  380. readmeExist := util.IsReadmeFileName(blob.Name())
  381. ctx.Data["ReadmeExist"] = readmeExist
  382. markupType := markup.Type(blob.Name())
  383. // If the markup is detected by custom markup renderer it should not be reset later on
  384. // to not pass it down to the render context.
  385. detected := false
  386. if markupType == "" {
  387. detected = true
  388. markupType = markup.DetectRendererType(blob.Name(), bytes.NewReader(buf))
  389. }
  390. if markupType != "" {
  391. ctx.Data["HasSourceRenderedToggle"] = true
  392. }
  393. if markupType != "" && !shouldRenderSource {
  394. ctx.Data["IsMarkup"] = true
  395. ctx.Data["MarkupType"] = markupType
  396. if !detected {
  397. markupType = ""
  398. }
  399. metas := ctx.Repo.Repository.ComposeDocumentMetas()
  400. metas["BranchNameSubURL"] = ctx.Repo.BranchNameSubURL()
  401. ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, &markup.RenderContext{
  402. Ctx: ctx,
  403. Type: markupType,
  404. RelativePath: ctx.Repo.TreePath,
  405. URLPrefix: path.Dir(treeLink),
  406. Metas: metas,
  407. GitRepo: ctx.Repo.GitRepo,
  408. }, rd)
  409. if err != nil {
  410. ctx.ServerError("Render", err)
  411. return
  412. }
  413. // to prevent iframe load third-party url
  414. ctx.Resp.Header().Add("Content-Security-Policy", "frame-src 'self'")
  415. } else {
  416. buf, _ := io.ReadAll(rd)
  417. // empty: 0 lines; "a": one line; "a\n": two lines; "a\nb": two lines;
  418. // the NumLines is only used for the display on the UI: "xxx lines"
  419. if len(buf) == 0 {
  420. ctx.Data["NumLines"] = 0
  421. } else {
  422. ctx.Data["NumLines"] = bytes.Count(buf, []byte{'\n'}) + 1
  423. }
  424. ctx.Data["NumLinesSet"] = true
  425. language := ""
  426. indexFilename, worktree, deleteTemporaryFile, err := ctx.Repo.GitRepo.ReadTreeToTemporaryIndex(ctx.Repo.CommitID)
  427. if err == nil {
  428. defer deleteTemporaryFile()
  429. filename2attribute2info, err := ctx.Repo.GitRepo.CheckAttribute(git.CheckAttributeOpts{
  430. CachedOnly: true,
  431. Attributes: []string{"linguist-language", "gitlab-language"},
  432. Filenames: []string{ctx.Repo.TreePath},
  433. IndexFile: indexFilename,
  434. WorkTree: worktree,
  435. })
  436. if err != nil {
  437. log.Error("Unable to load attributes for %-v:%s. Error: %v", ctx.Repo.Repository, ctx.Repo.TreePath, err)
  438. }
  439. language = filename2attribute2info[ctx.Repo.TreePath]["linguist-language"]
  440. if language == "" || language == "unspecified" {
  441. language = filename2attribute2info[ctx.Repo.TreePath]["gitlab-language"]
  442. }
  443. if language == "unspecified" {
  444. language = ""
  445. }
  446. }
  447. fileContent, lexerName, err := highlight.File(blob.Name(), language, buf)
  448. ctx.Data["LexerName"] = lexerName
  449. if err != nil {
  450. log.Error("highlight.File failed, fallback to plain text: %v", err)
  451. fileContent = highlight.PlainText(buf)
  452. }
  453. status := &charset.EscapeStatus{}
  454. statuses := make([]*charset.EscapeStatus, len(fileContent))
  455. for i, line := range fileContent {
  456. statuses[i], fileContent[i] = charset.EscapeControlHTML(line, ctx.Locale)
  457. status = status.Or(statuses[i])
  458. }
  459. ctx.Data["EscapeStatus"] = status
  460. ctx.Data["FileContent"] = fileContent
  461. ctx.Data["LineEscapeStatus"] = statuses
  462. }
  463. if !fInfo.isLFSFile {
  464. if ctx.Repo.CanEnableEditor(ctx.Doer) {
  465. if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID {
  466. ctx.Data["CanEditFile"] = false
  467. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.this_file_locked")
  468. } else {
  469. ctx.Data["CanEditFile"] = true
  470. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
  471. }
  472. } else if !ctx.Repo.IsViewBranch {
  473. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
  474. } else if !ctx.Repo.CanWriteToBranch(ctx.Doer, ctx.Repo.BranchName) {
  475. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit")
  476. }
  477. }
  478. case fInfo.st.IsPDF():
  479. ctx.Data["IsPDFFile"] = true
  480. case fInfo.st.IsVideo():
  481. ctx.Data["IsVideoFile"] = true
  482. case fInfo.st.IsAudio():
  483. ctx.Data["IsAudioFile"] = true
  484. case fInfo.st.IsImage() && (setting.UI.SVG.Enabled || !fInfo.st.IsSvgImage()):
  485. ctx.Data["IsImageFile"] = true
  486. ctx.Data["CanCopyContent"] = true
  487. default:
  488. if fInfo.fileSize >= setting.UI.MaxDisplayFileSize {
  489. ctx.Data["IsFileTooLarge"] = true
  490. break
  491. }
  492. if markupType := markup.Type(blob.Name()); markupType != "" {
  493. rd := io.MultiReader(bytes.NewReader(buf), dataRc)
  494. ctx.Data["IsMarkup"] = true
  495. ctx.Data["MarkupType"] = markupType
  496. ctx.Data["EscapeStatus"], ctx.Data["FileContent"], err = markupRender(ctx, &markup.RenderContext{
  497. Ctx: ctx,
  498. RelativePath: ctx.Repo.TreePath,
  499. URLPrefix: path.Dir(treeLink),
  500. Metas: ctx.Repo.Repository.ComposeDocumentMetas(),
  501. GitRepo: ctx.Repo.GitRepo,
  502. }, rd)
  503. if err != nil {
  504. ctx.ServerError("Render", err)
  505. return
  506. }
  507. }
  508. }
  509. if ctx.Repo.CanEnableEditor(ctx.Doer) {
  510. if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID {
  511. ctx.Data["CanDeleteFile"] = false
  512. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.this_file_locked")
  513. } else {
  514. ctx.Data["CanDeleteFile"] = true
  515. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.delete_this_file")
  516. }
  517. } else if !ctx.Repo.IsViewBranch {
  518. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
  519. } else if !ctx.Repo.CanWriteToBranch(ctx.Doer, ctx.Repo.BranchName) {
  520. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_have_write_access")
  521. }
  522. }
  523. func markupRender(ctx *context.Context, renderCtx *markup.RenderContext, input io.Reader) (escaped *charset.EscapeStatus, output string, err error) {
  524. markupRd, markupWr := io.Pipe()
  525. defer markupWr.Close()
  526. done := make(chan struct{})
  527. go func() {
  528. sb := &strings.Builder{}
  529. // We allow NBSP here this is rendered
  530. escaped, _ = charset.EscapeControlReader(markupRd, sb, ctx.Locale, charset.RuneNBSP)
  531. output = sb.String()
  532. close(done)
  533. }()
  534. err = markup.Render(renderCtx, input, markupWr)
  535. _ = markupWr.CloseWithError(err)
  536. <-done
  537. return escaped, output, err
  538. }
  539. func safeURL(address string) string {
  540. u, err := url.Parse(address)
  541. if err != nil {
  542. return address
  543. }
  544. u.User = nil
  545. return u.String()
  546. }
  547. func checkHomeCodeViewable(ctx *context.Context) {
  548. if len(ctx.Repo.Units) > 0 {
  549. if ctx.Repo.Repository.IsBeingCreated() {
  550. task, err := admin_model.GetMigratingTask(ctx.Repo.Repository.ID)
  551. if err != nil {
  552. if admin_model.IsErrTaskDoesNotExist(err) {
  553. ctx.Data["Repo"] = ctx.Repo
  554. ctx.Data["CloneAddr"] = ""
  555. ctx.Data["Failed"] = true
  556. ctx.HTML(http.StatusOK, tplMigrating)
  557. return
  558. }
  559. ctx.ServerError("models.GetMigratingTask", err)
  560. return
  561. }
  562. cfg, err := task.MigrateConfig()
  563. if err != nil {
  564. ctx.ServerError("task.MigrateConfig", err)
  565. return
  566. }
  567. ctx.Data["Repo"] = ctx.Repo
  568. ctx.Data["MigrateTask"] = task
  569. ctx.Data["CloneAddr"] = safeURL(cfg.CloneAddr)
  570. ctx.Data["Failed"] = task.Status == structs.TaskStatusFailed
  571. ctx.HTML(http.StatusOK, tplMigrating)
  572. return
  573. }
  574. if ctx.IsSigned {
  575. // Set repo notification-status read if unread
  576. if err := activities_model.SetRepoReadBy(ctx, ctx.Repo.Repository.ID, ctx.Doer.ID); err != nil {
  577. ctx.ServerError("ReadBy", err)
  578. return
  579. }
  580. }
  581. var firstUnit *unit_model.Unit
  582. for _, repoUnit := range ctx.Repo.Units {
  583. if repoUnit.Type == unit_model.TypeCode {
  584. return
  585. }
  586. unit, ok := unit_model.Units[repoUnit.Type]
  587. if ok && (firstUnit == nil || !firstUnit.IsLessThan(unit)) {
  588. firstUnit = &unit
  589. }
  590. }
  591. if firstUnit != nil {
  592. ctx.Redirect(fmt.Sprintf("%s%s", ctx.Repo.Repository.Link(), firstUnit.URI))
  593. return
  594. }
  595. }
  596. ctx.NotFound("Home", fmt.Errorf(ctx.Tr("units.error.no_unit_allowed_repo")))
  597. }
  598. func checkCitationFile(ctx *context.Context, entry *git.TreeEntry) {
  599. if entry.Name() != "" {
  600. return
  601. }
  602. tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
  603. if err != nil {
  604. ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err)
  605. return
  606. }
  607. allEntries, err := tree.ListEntries()
  608. if err != nil {
  609. ctx.ServerError("ListEntries", err)
  610. return
  611. }
  612. for _, entry := range allEntries {
  613. if entry.Name() == "CITATION.cff" || entry.Name() == "CITATION.bib" {
  614. ctx.Data["CitiationExist"] = true
  615. // Read Citation file contents
  616. blob := entry.Blob()
  617. dataRc, err := blob.DataAsync()
  618. if err != nil {
  619. ctx.ServerError("DataAsync", err)
  620. return
  621. }
  622. defer dataRc.Close()
  623. buf := make([]byte, 1024)
  624. n, err := util.ReadAtMost(dataRc, buf)
  625. if err != nil {
  626. ctx.ServerError("ReadAtMost", err)
  627. return
  628. }
  629. buf = buf[:n]
  630. ctx.PageData["citationFileContent"] = string(buf)
  631. break
  632. }
  633. }
  634. }
  635. // Home render repository home page
  636. func Home(ctx *context.Context) {
  637. if setting.Other.EnableFeed {
  638. isFeed, _, showFeedType := feed.GetFeedType(ctx.Params(":reponame"), ctx.Req)
  639. if isFeed {
  640. switch {
  641. case ctx.Link == fmt.Sprintf("%s.%s", ctx.Repo.RepoLink, showFeedType):
  642. feed.ShowRepoFeed(ctx, ctx.Repo.Repository, showFeedType)
  643. case ctx.Repo.TreePath == "":
  644. feed.ShowBranchFeed(ctx, ctx.Repo.Repository, showFeedType)
  645. case ctx.Repo.TreePath != "":
  646. feed.ShowFileFeed(ctx, ctx.Repo.Repository, showFeedType)
  647. }
  648. return
  649. }
  650. }
  651. checkHomeCodeViewable(ctx)
  652. if ctx.Written() {
  653. return
  654. }
  655. renderCode(ctx)
  656. }
  657. // LastCommit returns lastCommit data for the provided branch/tag/commit and directory (in url) and filenames in body
  658. func LastCommit(ctx *context.Context) {
  659. checkHomeCodeViewable(ctx)
  660. if ctx.Written() {
  661. return
  662. }
  663. renderDirectoryFiles(ctx, 0)
  664. if ctx.Written() {
  665. return
  666. }
  667. var treeNames []string
  668. paths := make([]string, 0, 5)
  669. if len(ctx.Repo.TreePath) > 0 {
  670. treeNames = strings.Split(ctx.Repo.TreePath, "/")
  671. for i := range treeNames {
  672. paths = append(paths, strings.Join(treeNames[:i+1], "/"))
  673. }
  674. ctx.Data["HasParentPath"] = true
  675. if len(paths)-2 >= 0 {
  676. ctx.Data["ParentPath"] = "/" + paths[len(paths)-2]
  677. }
  678. }
  679. branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
  680. ctx.Data["BranchLink"] = branchLink
  681. ctx.HTML(http.StatusOK, tplRepoViewList)
  682. }
  683. func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entries {
  684. tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
  685. if err != nil {
  686. ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err)
  687. return nil
  688. }
  689. ctx.Data["LastCommitLoaderURL"] = ctx.Repo.RepoLink + "/lastcommit/" + url.PathEscape(ctx.Repo.CommitID) + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
  690. // Get current entry user currently looking at.
  691. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
  692. if err != nil {
  693. ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
  694. return nil
  695. }
  696. if !entry.IsDir() {
  697. ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
  698. return nil
  699. }
  700. allEntries, err := tree.ListEntries()
  701. if err != nil {
  702. ctx.ServerError("ListEntries", err)
  703. return nil
  704. }
  705. allEntries.CustomSort(base.NaturalSortLess)
  706. commitInfoCtx := gocontext.Context(ctx)
  707. if timeout > 0 {
  708. var cancel gocontext.CancelFunc
  709. commitInfoCtx, cancel = gocontext.WithTimeout(ctx, timeout)
  710. defer cancel()
  711. }
  712. selected := make(container.Set[string])
  713. selected.AddMultiple(ctx.FormStrings("f[]")...)
  714. entries := allEntries
  715. if len(selected) > 0 {
  716. entries = make(git.Entries, 0, len(selected))
  717. for _, entry := range allEntries {
  718. if selected.Contains(entry.Name()) {
  719. entries = append(entries, entry)
  720. }
  721. }
  722. }
  723. var latestCommit *git.Commit
  724. ctx.Data["Files"], latestCommit, err = entries.GetCommitsInfo(commitInfoCtx, ctx.Repo.Commit, ctx.Repo.TreePath)
  725. if err != nil {
  726. ctx.ServerError("GetCommitsInfo", err)
  727. return nil
  728. }
  729. // Show latest commit info of repository in table header,
  730. // or of directory if not in root directory.
  731. ctx.Data["LatestCommit"] = latestCommit
  732. if latestCommit != nil {
  733. verification := asymkey_model.ParseCommitWithSignature(ctx, latestCommit)
  734. if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) {
  735. return repo_model.IsOwnerMemberCollaborator(ctx.Repo.Repository, user.ID)
  736. }, nil); err != nil {
  737. ctx.ServerError("CalculateTrustStatus", err)
  738. return nil
  739. }
  740. ctx.Data["LatestCommitVerification"] = verification
  741. ctx.Data["LatestCommitUser"] = user_model.ValidateCommitWithEmail(ctx, latestCommit)
  742. statuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, latestCommit.ID.String(), db.ListOptions{ListAll: true})
  743. if err != nil {
  744. log.Error("GetLatestCommitStatus: %v", err)
  745. }
  746. ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(statuses)
  747. ctx.Data["LatestCommitStatuses"] = statuses
  748. }
  749. branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
  750. treeLink := branchLink
  751. if len(ctx.Repo.TreePath) > 0 {
  752. treeLink += "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
  753. }
  754. ctx.Data["TreeLink"] = treeLink
  755. ctx.Data["SSHDomain"] = setting.SSH.Domain
  756. return allEntries
  757. }
  758. func renderLanguageStats(ctx *context.Context) {
  759. langs, err := repo_model.GetTopLanguageStats(ctx.Repo.Repository, 5)
  760. if err != nil {
  761. ctx.ServerError("Repo.GetTopLanguageStats", err)
  762. return
  763. }
  764. ctx.Data["LanguageStats"] = langs
  765. }
  766. func renderRepoTopics(ctx *context.Context) {
  767. topics, _, err := repo_model.FindTopics(&repo_model.FindTopicOptions{
  768. RepoID: ctx.Repo.Repository.ID,
  769. })
  770. if err != nil {
  771. ctx.ServerError("models.FindTopics", err)
  772. return
  773. }
  774. ctx.Data["Topics"] = topics
  775. }
  776. func renderCode(ctx *context.Context) {
  777. ctx.Data["PageIsViewCode"] = true
  778. ctx.Data["RepositoryUploadEnabled"] = setting.Repository.Upload.Enabled
  779. if ctx.Repo.Commit == nil || ctx.Repo.Repository.IsEmpty || ctx.Repo.Repository.IsBroken() {
  780. showEmpty := true
  781. var err error
  782. if ctx.Repo.GitRepo != nil {
  783. showEmpty, err = ctx.Repo.GitRepo.IsEmpty()
  784. if err != nil {
  785. log.Error("GitRepo.IsEmpty: %v", err)
  786. ctx.Repo.Repository.Status = repo_model.RepositoryBroken
  787. showEmpty = true
  788. ctx.Flash.Error(ctx.Tr("error.occurred"), true)
  789. }
  790. }
  791. if showEmpty {
  792. ctx.HTML(http.StatusOK, tplRepoEMPTY)
  793. return
  794. }
  795. // the repo is not really empty, so we should update the modal in database
  796. // such problem may be caused by:
  797. // 1) an error occurs during pushing/receiving. 2) the user replaces an empty git repo manually
  798. // and even more: the IsEmpty flag is deeply broken and should be removed with the UI changed to manage to cope with empty repos.
  799. // it's possible for a repository to be non-empty by that flag but still 500
  800. // because there are no branches - only tags -or the default branch is non-extant as it has been 0-pushed.
  801. ctx.Repo.Repository.IsEmpty = false
  802. if err = repo_model.UpdateRepositoryCols(ctx, ctx.Repo.Repository, "is_empty"); err != nil {
  803. ctx.ServerError("UpdateRepositoryCols", err)
  804. return
  805. }
  806. if err = repo_module.UpdateRepoSize(ctx, ctx.Repo.Repository); err != nil {
  807. ctx.ServerError("UpdateRepoSize", err)
  808. return
  809. }
  810. // the repo's IsEmpty has been updated, redirect to this page to make sure middlewares can get the correct values
  811. link := ctx.Link
  812. if ctx.Req.URL.RawQuery != "" {
  813. link += "?" + ctx.Req.URL.RawQuery
  814. }
  815. ctx.Redirect(link)
  816. return
  817. }
  818. title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
  819. if len(ctx.Repo.Repository.Description) > 0 {
  820. title += ": " + ctx.Repo.Repository.Description
  821. }
  822. ctx.Data["Title"] = title
  823. branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
  824. treeLink := branchLink
  825. rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL()
  826. if len(ctx.Repo.TreePath) > 0 {
  827. treeLink += "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
  828. }
  829. // Get Topics of this repo
  830. renderRepoTopics(ctx)
  831. if ctx.Written() {
  832. return
  833. }
  834. // Get current entry user currently looking at.
  835. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
  836. if err != nil {
  837. ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
  838. return
  839. }
  840. checkCitationFile(ctx, entry)
  841. if ctx.Written() {
  842. return
  843. }
  844. renderLanguageStats(ctx)
  845. if ctx.Written() {
  846. return
  847. }
  848. if entry.IsDir() {
  849. renderDirectory(ctx, treeLink)
  850. } else {
  851. renderFile(ctx, entry, treeLink, rawLink)
  852. }
  853. if ctx.Written() {
  854. return
  855. }
  856. var treeNames []string
  857. paths := make([]string, 0, 5)
  858. if len(ctx.Repo.TreePath) > 0 {
  859. treeNames = strings.Split(ctx.Repo.TreePath, "/")
  860. for i := range treeNames {
  861. paths = append(paths, strings.Join(treeNames[:i+1], "/"))
  862. }
  863. ctx.Data["HasParentPath"] = true
  864. if len(paths)-2 >= 0 {
  865. ctx.Data["ParentPath"] = "/" + paths[len(paths)-2]
  866. }
  867. }
  868. ctx.Data["Paths"] = paths
  869. ctx.Data["TreeLink"] = treeLink
  870. ctx.Data["TreeNames"] = treeNames
  871. ctx.Data["BranchLink"] = branchLink
  872. ctx.HTML(http.StatusOK, tplRepoHome)
  873. }
  874. // RenderUserCards render a page show users according the input template
  875. func RenderUserCards(ctx *context.Context, total int, getter func(opts db.ListOptions) ([]*user_model.User, error), tpl base.TplName) {
  876. page := ctx.FormInt("page")
  877. if page <= 0 {
  878. page = 1
  879. }
  880. pager := context.NewPagination(total, setting.ItemsPerPage, page, 5)
  881. ctx.Data["Page"] = pager
  882. items, err := getter(db.ListOptions{
  883. Page: pager.Paginater.Current(),
  884. PageSize: setting.ItemsPerPage,
  885. })
  886. if err != nil {
  887. ctx.ServerError("getter", err)
  888. return
  889. }
  890. ctx.Data["Cards"] = items
  891. ctx.HTML(http.StatusOK, tpl)
  892. }
  893. // Watchers render repository's watch users
  894. func Watchers(ctx *context.Context) {
  895. ctx.Data["Title"] = ctx.Tr("repo.watchers")
  896. ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers")
  897. ctx.Data["PageIsWatchers"] = true
  898. RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, func(opts db.ListOptions) ([]*user_model.User, error) {
  899. return repo_model.GetRepoWatchers(ctx.Repo.Repository.ID, opts)
  900. }, tplWatchers)
  901. }
  902. // Stars render repository's starred users
  903. func Stars(ctx *context.Context) {
  904. ctx.Data["Title"] = ctx.Tr("repo.stargazers")
  905. ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers")
  906. ctx.Data["PageIsStargazers"] = true
  907. RenderUserCards(ctx, ctx.Repo.Repository.NumStars, func(opts db.ListOptions) ([]*user_model.User, error) {
  908. return repo_model.GetStargazers(ctx.Repo.Repository, opts)
  909. }, tplWatchers)
  910. }
  911. // Forks render repository's forked users
  912. func Forks(ctx *context.Context) {
  913. ctx.Data["Title"] = ctx.Tr("repo.forks")
  914. page := ctx.FormInt("page")
  915. if page <= 0 {
  916. page = 1
  917. }
  918. pager := context.NewPagination(ctx.Repo.Repository.NumForks, setting.ItemsPerPage, page, 5)
  919. ctx.Data["Page"] = pager
  920. forks, err := repo_model.GetForks(ctx.Repo.Repository, db.ListOptions{
  921. Page: pager.Paginater.Current(),
  922. PageSize: setting.ItemsPerPage,
  923. })
  924. if err != nil {
  925. ctx.ServerError("GetForks", err)
  926. return
  927. }
  928. for _, fork := range forks {
  929. if err = fork.LoadOwner(ctx); err != nil {
  930. ctx.ServerError("LoadOwner", err)
  931. return
  932. }
  933. }
  934. ctx.Data["Forks"] = forks
  935. ctx.HTML(http.StatusOK, tplForks)
  936. }