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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Copyright 2014 The Gogs Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "bytes"
  8. gocontext "context"
  9. "encoding/base64"
  10. "fmt"
  11. gotemplate "html/template"
  12. "io"
  13. "net/http"
  14. "net/url"
  15. "path"
  16. "strconv"
  17. "strings"
  18. "time"
  19. "code.gitea.io/gitea/models"
  20. asymkey_model "code.gitea.io/gitea/models/asymkey"
  21. "code.gitea.io/gitea/models/db"
  22. git_model "code.gitea.io/gitea/models/git"
  23. repo_model "code.gitea.io/gitea/models/repo"
  24. unit_model "code.gitea.io/gitea/models/unit"
  25. user_model "code.gitea.io/gitea/models/user"
  26. "code.gitea.io/gitea/modules/base"
  27. "code.gitea.io/gitea/modules/charset"
  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. )
  41. const (
  42. tplRepoEMPTY base.TplName = "repo/empty"
  43. tplRepoHome base.TplName = "repo/home"
  44. tplRepoViewList base.TplName = "repo/view_list"
  45. tplWatchers base.TplName = "repo/watchers"
  46. tplForks base.TplName = "repo/forks"
  47. tplMigrating base.TplName = "repo/migrate/migrating"
  48. )
  49. type namedBlob struct {
  50. name string
  51. isSymlink bool
  52. blob *git.Blob
  53. }
  54. func linesBytesCount(s []byte) int {
  55. nl := []byte{'\n'}
  56. n := bytes.Count(s, nl)
  57. if len(s) > 0 && !bytes.HasSuffix(s, nl) {
  58. n++
  59. }
  60. return n
  61. }
  62. // FIXME: There has to be a more efficient way of doing this
  63. func getReadmeFileFromPath(commit *git.Commit, treePath string) (*namedBlob, error) {
  64. tree, err := commit.SubTree(treePath)
  65. if err != nil {
  66. return nil, err
  67. }
  68. entries, err := tree.ListEntries()
  69. if err != nil {
  70. return nil, err
  71. }
  72. var readmeFiles [4]*namedBlob
  73. exts := []string{".md", ".txt", ""} // sorted by priority
  74. for _, entry := range entries {
  75. if entry.IsDir() {
  76. continue
  77. }
  78. for i, ext := range exts {
  79. if markup.IsReadmeFile(entry.Name(), ext) {
  80. if readmeFiles[i] == nil || base.NaturalSortLess(readmeFiles[i].name, entry.Blob().Name()) {
  81. name := entry.Name()
  82. isSymlink := entry.IsLink()
  83. target := entry
  84. if isSymlink {
  85. target, err = entry.FollowLinks()
  86. if err != nil && !git.IsErrBadLink(err) {
  87. return nil, err
  88. }
  89. }
  90. if target != nil && (target.IsExecutable() || target.IsRegular()) {
  91. readmeFiles[i] = &namedBlob{
  92. name,
  93. isSymlink,
  94. target.Blob(),
  95. }
  96. }
  97. }
  98. }
  99. }
  100. if markup.IsReadmeFile(entry.Name()) {
  101. if readmeFiles[3] == nil || base.NaturalSortLess(readmeFiles[3].name, entry.Blob().Name()) {
  102. name := entry.Name()
  103. isSymlink := entry.IsLink()
  104. if isSymlink {
  105. entry, err = entry.FollowLinks()
  106. if err != nil && !git.IsErrBadLink(err) {
  107. return nil, err
  108. }
  109. }
  110. if entry != nil && (entry.IsExecutable() || entry.IsRegular()) {
  111. readmeFiles[3] = &namedBlob{
  112. name,
  113. isSymlink,
  114. entry.Blob(),
  115. }
  116. }
  117. }
  118. }
  119. }
  120. var readmeFile *namedBlob
  121. for _, f := range readmeFiles {
  122. if f != nil {
  123. readmeFile = f
  124. break
  125. }
  126. }
  127. return readmeFile, nil
  128. }
  129. func renderDirectory(ctx *context.Context, treeLink string) {
  130. entries := renderDirectoryFiles(ctx, 1*time.Second)
  131. if ctx.Written() {
  132. return
  133. }
  134. if ctx.Repo.TreePath != "" {
  135. ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefName)
  136. }
  137. // Check permission to add or upload new file.
  138. if ctx.Repo.CanWrite(unit_model.TypeCode) && ctx.Repo.IsViewBranch {
  139. ctx.Data["CanAddFile"] = !ctx.Repo.Repository.IsArchived
  140. ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled && !ctx.Repo.Repository.IsArchived
  141. }
  142. readmeFile, readmeTreelink := findReadmeFile(ctx, entries, treeLink)
  143. if ctx.Written() || readmeFile == nil {
  144. return
  145. }
  146. renderReadmeFile(ctx, readmeFile, readmeTreelink)
  147. }
  148. func findReadmeFile(ctx *context.Context, entries git.Entries, treeLink string) (*namedBlob, string) {
  149. // 3 for the extensions in exts[] in order
  150. // the last one is for a readme that doesn't
  151. // strictly match an extension
  152. var readmeFiles [4]*namedBlob
  153. var docsEntries [3]*git.TreeEntry
  154. exts := []string{".md", ".txt", ""} // sorted by priority
  155. for _, entry := range entries {
  156. if entry.IsDir() {
  157. lowerName := strings.ToLower(entry.Name())
  158. switch lowerName {
  159. case "docs":
  160. if entry.Name() == "docs" || docsEntries[0] == nil {
  161. docsEntries[0] = entry
  162. }
  163. case ".gitea":
  164. if entry.Name() == ".gitea" || docsEntries[1] == nil {
  165. docsEntries[1] = entry
  166. }
  167. case ".github":
  168. if entry.Name() == ".github" || docsEntries[2] == nil {
  169. docsEntries[2] = entry
  170. }
  171. }
  172. continue
  173. }
  174. for i, ext := range exts {
  175. if markup.IsReadmeFile(entry.Name(), ext) {
  176. log.Debug("%s", entry.Name())
  177. name := entry.Name()
  178. isSymlink := entry.IsLink()
  179. target := entry
  180. if isSymlink {
  181. var err error
  182. target, err = entry.FollowLinks()
  183. if err != nil && !git.IsErrBadLink(err) {
  184. ctx.ServerError("FollowLinks", err)
  185. return nil, ""
  186. }
  187. }
  188. log.Debug("%t", target == nil)
  189. if target != nil && (target.IsExecutable() || target.IsRegular()) {
  190. readmeFiles[i] = &namedBlob{
  191. name,
  192. isSymlink,
  193. target.Blob(),
  194. }
  195. }
  196. }
  197. }
  198. if markup.IsReadmeFile(entry.Name()) {
  199. name := entry.Name()
  200. isSymlink := entry.IsLink()
  201. if isSymlink {
  202. var err error
  203. entry, err = entry.FollowLinks()
  204. if err != nil && !git.IsErrBadLink(err) {
  205. ctx.ServerError("FollowLinks", err)
  206. return nil, ""
  207. }
  208. }
  209. if entry != nil && (entry.IsExecutable() || entry.IsRegular()) {
  210. readmeFiles[3] = &namedBlob{
  211. name,
  212. isSymlink,
  213. entry.Blob(),
  214. }
  215. }
  216. }
  217. }
  218. var readmeFile *namedBlob
  219. readmeTreelink := treeLink
  220. for _, f := range readmeFiles {
  221. if f != nil {
  222. readmeFile = f
  223. break
  224. }
  225. }
  226. if ctx.Repo.TreePath == "" && readmeFile == nil {
  227. for _, entry := range docsEntries {
  228. if entry == nil {
  229. continue
  230. }
  231. var err error
  232. readmeFile, err = getReadmeFileFromPath(ctx.Repo.Commit, entry.GetSubJumpablePathName())
  233. if err != nil {
  234. ctx.ServerError("getReadmeFileFromPath", err)
  235. return nil, ""
  236. }
  237. if readmeFile != nil {
  238. readmeFile.name = entry.Name() + "/" + readmeFile.name
  239. readmeTreelink = treeLink + "/" + util.PathEscapeSegments(entry.GetSubJumpablePathName())
  240. break
  241. }
  242. }
  243. }
  244. return readmeFile, readmeTreelink
  245. }
  246. func renderReadmeFile(ctx *context.Context, readmeFile *namedBlob, readmeTreelink string) {
  247. ctx.Data["RawFileLink"] = ""
  248. ctx.Data["ReadmeInList"] = true
  249. ctx.Data["ReadmeExist"] = true
  250. ctx.Data["FileIsSymlink"] = readmeFile.isSymlink
  251. dataRc, err := readmeFile.blob.DataAsync()
  252. if err != nil {
  253. ctx.ServerError("Data", err)
  254. return
  255. }
  256. defer dataRc.Close()
  257. buf := make([]byte, 1024)
  258. n, _ := util.ReadAtMost(dataRc, buf)
  259. buf = buf[:n]
  260. st := typesniffer.DetectContentType(buf)
  261. isTextFile := st.IsText()
  262. ctx.Data["FileIsText"] = isTextFile
  263. ctx.Data["FileName"] = readmeFile.name
  264. fileSize := int64(0)
  265. isLFSFile := false
  266. ctx.Data["IsLFSFile"] = false
  267. // FIXME: what happens when README file is an image?
  268. if isTextFile && setting.LFS.StartServer {
  269. pointer, _ := lfs.ReadPointerFromBuffer(buf)
  270. if pointer.IsValid() {
  271. meta, err := git_model.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid)
  272. if err != nil && err != git_model.ErrLFSObjectNotExist {
  273. ctx.ServerError("GetLFSMetaObject", err)
  274. return
  275. }
  276. if meta != nil {
  277. ctx.Data["IsLFSFile"] = true
  278. isLFSFile = true
  279. // OK read the lfs object
  280. var err error
  281. dataRc, err = lfs.ReadMetaObject(pointer)
  282. if err != nil {
  283. ctx.ServerError("ReadMetaObject", err)
  284. return
  285. }
  286. defer dataRc.Close()
  287. buf = make([]byte, 1024)
  288. n, err = util.ReadAtMost(dataRc, buf)
  289. if err != nil {
  290. ctx.ServerError("Data", err)
  291. return
  292. }
  293. buf = buf[:n]
  294. st = typesniffer.DetectContentType(buf)
  295. isTextFile = st.IsText()
  296. ctx.Data["IsTextFile"] = isTextFile
  297. fileSize = meta.Size
  298. ctx.Data["FileSize"] = meta.Size
  299. filenameBase64 := base64.RawURLEncoding.EncodeToString([]byte(readmeFile.name))
  300. ctx.Data["RawFileLink"] = fmt.Sprintf("%s.git/info/lfs/objects/%s/%s", ctx.Repo.Repository.HTMLURL(), url.PathEscape(meta.Oid), url.PathEscape(filenameBase64))
  301. }
  302. }
  303. }
  304. if !isTextFile {
  305. return
  306. }
  307. if !isLFSFile {
  308. fileSize = readmeFile.blob.Size()
  309. }
  310. if fileSize >= setting.UI.MaxDisplayFileSize {
  311. // Pretend that this is a normal text file to display 'This file is too large to be shown'
  312. ctx.Data["IsFileTooLarge"] = true
  313. ctx.Data["IsTextFile"] = true
  314. ctx.Data["FileSize"] = fileSize
  315. return
  316. }
  317. rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc))
  318. if markupType := markup.Type(readmeFile.name); markupType != "" {
  319. ctx.Data["IsMarkup"] = true
  320. ctx.Data["MarkupType"] = markupType
  321. var result strings.Builder
  322. err := markup.Render(&markup.RenderContext{
  323. Ctx: ctx,
  324. 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).
  325. URLPrefix: readmeTreelink,
  326. Metas: ctx.Repo.Repository.ComposeDocumentMetas(),
  327. GitRepo: ctx.Repo.GitRepo,
  328. }, rd, &result)
  329. if err != nil {
  330. log.Error("Render failed: %v then fallback", err)
  331. buf := &bytes.Buffer{}
  332. ctx.Data["EscapeStatus"], _ = charset.EscapeControlReader(rd, buf)
  333. ctx.Data["FileContent"] = strings.ReplaceAll(
  334. gotemplate.HTMLEscapeString(buf.String()), "\n", `<br>`,
  335. )
  336. } else {
  337. ctx.Data["EscapeStatus"], ctx.Data["FileContent"] = charset.EscapeControlString(result.String())
  338. }
  339. } else {
  340. ctx.Data["IsRenderedHTML"] = true
  341. buf := &bytes.Buffer{}
  342. ctx.Data["EscapeStatus"], err = charset.EscapeControlReader(rd, buf)
  343. if err != nil {
  344. log.Error("Read failed: %v", err)
  345. }
  346. ctx.Data["FileContent"] = strings.ReplaceAll(
  347. gotemplate.HTMLEscapeString(buf.String()), "\n", `<br>`,
  348. )
  349. }
  350. }
  351. func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink string) {
  352. ctx.Data["IsViewFile"] = true
  353. blob := entry.Blob()
  354. dataRc, err := blob.DataAsync()
  355. if err != nil {
  356. ctx.ServerError("DataAsync", err)
  357. return
  358. }
  359. defer dataRc.Close()
  360. ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefName)
  361. fileSize := blob.Size()
  362. ctx.Data["FileIsSymlink"] = entry.IsLink()
  363. ctx.Data["FileName"] = blob.Name()
  364. ctx.Data["RawFileLink"] = rawLink + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
  365. buf := make([]byte, 1024)
  366. n, _ := util.ReadAtMost(dataRc, buf)
  367. buf = buf[:n]
  368. st := typesniffer.DetectContentType(buf)
  369. isTextFile := st.IsText()
  370. isLFSFile := false
  371. isDisplayingSource := ctx.FormString("display") == "source"
  372. isDisplayingRendered := !isDisplayingSource
  373. // Check for LFS meta file
  374. if isTextFile && setting.LFS.StartServer {
  375. pointer, _ := lfs.ReadPointerFromBuffer(buf)
  376. if pointer.IsValid() {
  377. meta, err := git_model.GetLFSMetaObjectByOid(ctx.Repo.Repository.ID, pointer.Oid)
  378. if err != nil && err != git_model.ErrLFSObjectNotExist {
  379. ctx.ServerError("GetLFSMetaObject", err)
  380. return
  381. }
  382. if meta != nil {
  383. isLFSFile = true
  384. // OK read the lfs object
  385. var err error
  386. dataRc, err = lfs.ReadMetaObject(pointer)
  387. if err != nil {
  388. ctx.ServerError("ReadMetaObject", err)
  389. return
  390. }
  391. defer dataRc.Close()
  392. buf = make([]byte, 1024)
  393. n, err = util.ReadAtMost(dataRc, buf)
  394. if err != nil {
  395. ctx.ServerError("Data", err)
  396. return
  397. }
  398. buf = buf[:n]
  399. st = typesniffer.DetectContentType(buf)
  400. isTextFile = st.IsText()
  401. fileSize = meta.Size
  402. ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/media/" + ctx.Repo.BranchNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
  403. }
  404. }
  405. }
  406. isRepresentableAsText := st.IsRepresentableAsText()
  407. if !isRepresentableAsText {
  408. // If we can't show plain text, always try to render.
  409. isDisplayingSource = false
  410. isDisplayingRendered = true
  411. }
  412. ctx.Data["IsLFSFile"] = isLFSFile
  413. ctx.Data["FileSize"] = fileSize
  414. ctx.Data["IsTextFile"] = isTextFile
  415. ctx.Data["IsRepresentableAsText"] = isRepresentableAsText
  416. ctx.Data["IsDisplayingSource"] = isDisplayingSource
  417. ctx.Data["IsDisplayingRendered"] = isDisplayingRendered
  418. ctx.Data["IsTextSource"] = isTextFile || isDisplayingSource
  419. // Check LFS Lock
  420. lfsLock, err := git_model.GetTreePathLock(ctx.Repo.Repository.ID, ctx.Repo.TreePath)
  421. ctx.Data["LFSLock"] = lfsLock
  422. if err != nil {
  423. ctx.ServerError("GetTreePathLock", err)
  424. return
  425. }
  426. if lfsLock != nil {
  427. u, err := user_model.GetUserByID(lfsLock.OwnerID)
  428. if err != nil {
  429. ctx.ServerError("GetTreePathLock", err)
  430. return
  431. }
  432. ctx.Data["LFSLockOwner"] = u.DisplayName()
  433. ctx.Data["LFSLockOwnerHomeLink"] = u.HomeLink()
  434. ctx.Data["LFSLockHint"] = ctx.Tr("repo.editor.this_file_locked")
  435. }
  436. // Assume file is not editable first.
  437. if isLFSFile {
  438. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_lfs_files")
  439. } else if !isRepresentableAsText {
  440. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_non_text_files")
  441. }
  442. switch {
  443. case isRepresentableAsText:
  444. if st.IsSvgImage() {
  445. ctx.Data["IsImageFile"] = true
  446. ctx.Data["HasSourceRenderedToggle"] = true
  447. }
  448. if fileSize >= setting.UI.MaxDisplayFileSize {
  449. ctx.Data["IsFileTooLarge"] = true
  450. break
  451. }
  452. rd := charset.ToUTF8WithFallbackReader(io.MultiReader(bytes.NewReader(buf), dataRc))
  453. shouldRenderSource := ctx.FormString("display") == "source"
  454. readmeExist := markup.IsReadmeFile(blob.Name())
  455. ctx.Data["ReadmeExist"] = readmeExist
  456. markupType := markup.Type(blob.Name())
  457. // If the markup is detected by custom markup renderer it should not be reset later on
  458. // to not pass it down to the render context.
  459. detected := false
  460. if markupType == "" {
  461. detected = true
  462. markupType = markup.DetectRendererType(blob.Name(), bytes.NewReader(buf))
  463. }
  464. if markupType != "" {
  465. ctx.Data["HasSourceRenderedToggle"] = true
  466. }
  467. if markupType != "" && !shouldRenderSource {
  468. ctx.Data["IsMarkup"] = true
  469. ctx.Data["MarkupType"] = markupType
  470. var result strings.Builder
  471. if !detected {
  472. markupType = ""
  473. }
  474. metas := ctx.Repo.Repository.ComposeDocumentMetas()
  475. metas["BranchNameSubURL"] = ctx.Repo.BranchNameSubURL()
  476. err := markup.Render(&markup.RenderContext{
  477. Ctx: ctx,
  478. Type: markupType,
  479. RelativePath: ctx.Repo.TreePath,
  480. URLPrefix: path.Dir(treeLink),
  481. Metas: metas,
  482. GitRepo: ctx.Repo.GitRepo,
  483. }, rd, &result)
  484. if err != nil {
  485. ctx.ServerError("Render", err)
  486. return
  487. }
  488. // to prevent iframe load third-party url
  489. ctx.Resp.Header().Add("Content-Security-Policy", "frame-src 'self'")
  490. ctx.Data["EscapeStatus"], ctx.Data["FileContent"] = charset.EscapeControlString(result.String())
  491. } else if readmeExist && !shouldRenderSource {
  492. buf := &bytes.Buffer{}
  493. ctx.Data["IsRenderedHTML"] = true
  494. ctx.Data["EscapeStatus"], _ = charset.EscapeControlReader(rd, buf)
  495. ctx.Data["FileContent"] = strings.ReplaceAll(
  496. gotemplate.HTMLEscapeString(buf.String()), "\n", `<br>`,
  497. )
  498. } else {
  499. buf, _ := io.ReadAll(rd)
  500. lineNums := linesBytesCount(buf)
  501. ctx.Data["NumLines"] = strconv.Itoa(lineNums)
  502. ctx.Data["NumLinesSet"] = true
  503. language := ""
  504. indexFilename, worktree, deleteTemporaryFile, err := ctx.Repo.GitRepo.ReadTreeToTemporaryIndex(ctx.Repo.CommitID)
  505. if err == nil {
  506. defer deleteTemporaryFile()
  507. filename2attribute2info, err := ctx.Repo.GitRepo.CheckAttribute(git.CheckAttributeOpts{
  508. CachedOnly: true,
  509. Attributes: []string{"linguist-language", "gitlab-language"},
  510. Filenames: []string{ctx.Repo.TreePath},
  511. IndexFile: indexFilename,
  512. WorkTree: worktree,
  513. })
  514. if err != nil {
  515. log.Error("Unable to load attributes for %-v:%s. Error: %v", ctx.Repo.Repository, ctx.Repo.TreePath, err)
  516. }
  517. language = filename2attribute2info[ctx.Repo.TreePath]["linguist-language"]
  518. if language == "" || language == "unspecified" {
  519. language = filename2attribute2info[ctx.Repo.TreePath]["gitlab-language"]
  520. }
  521. if language == "unspecified" {
  522. language = ""
  523. }
  524. }
  525. fileContent := highlight.File(lineNums, blob.Name(), language, buf)
  526. status, _ := charset.EscapeControlReader(bytes.NewReader(buf), io.Discard)
  527. ctx.Data["EscapeStatus"] = status
  528. statuses := make([]charset.EscapeStatus, len(fileContent))
  529. for i, line := range fileContent {
  530. statuses[i], fileContent[i] = charset.EscapeControlString(line)
  531. }
  532. ctx.Data["FileContent"] = fileContent
  533. ctx.Data["LineEscapeStatus"] = statuses
  534. }
  535. if !isLFSFile {
  536. if ctx.Repo.CanEnableEditor(ctx.Doer) {
  537. if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID {
  538. ctx.Data["CanEditFile"] = false
  539. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.this_file_locked")
  540. } else {
  541. ctx.Data["CanEditFile"] = true
  542. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
  543. }
  544. } else if !ctx.Repo.IsViewBranch {
  545. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
  546. } else if !ctx.Repo.CanWriteToBranch(ctx.Doer, ctx.Repo.BranchName) {
  547. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit")
  548. }
  549. }
  550. case st.IsPDF():
  551. ctx.Data["IsPDFFile"] = true
  552. case st.IsVideo():
  553. ctx.Data["IsVideoFile"] = true
  554. case st.IsAudio():
  555. ctx.Data["IsAudioFile"] = true
  556. case st.IsImage() && (setting.UI.SVG.Enabled || !st.IsSvgImage()):
  557. ctx.Data["IsImageFile"] = true
  558. default:
  559. if fileSize >= setting.UI.MaxDisplayFileSize {
  560. ctx.Data["IsFileTooLarge"] = true
  561. break
  562. }
  563. if markupType := markup.Type(blob.Name()); markupType != "" {
  564. rd := io.MultiReader(bytes.NewReader(buf), dataRc)
  565. ctx.Data["IsMarkup"] = true
  566. ctx.Data["MarkupType"] = markupType
  567. var result strings.Builder
  568. err := markup.Render(&markup.RenderContext{
  569. Ctx: ctx,
  570. RelativePath: ctx.Repo.TreePath,
  571. URLPrefix: path.Dir(treeLink),
  572. Metas: ctx.Repo.Repository.ComposeDocumentMetas(),
  573. GitRepo: ctx.Repo.GitRepo,
  574. }, rd, &result)
  575. if err != nil {
  576. ctx.ServerError("Render", err)
  577. return
  578. }
  579. ctx.Data["EscapeStatus"], ctx.Data["FileContent"] = charset.EscapeControlString(result.String())
  580. }
  581. }
  582. if ctx.Repo.CanEnableEditor(ctx.Doer) {
  583. if lfsLock != nil && lfsLock.OwnerID != ctx.Doer.ID {
  584. ctx.Data["CanDeleteFile"] = false
  585. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.this_file_locked")
  586. } else {
  587. ctx.Data["CanDeleteFile"] = true
  588. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.delete_this_file")
  589. }
  590. } else if !ctx.Repo.IsViewBranch {
  591. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
  592. } else if !ctx.Repo.CanWriteToBranch(ctx.Doer, ctx.Repo.BranchName) {
  593. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_have_write_access")
  594. }
  595. }
  596. func safeURL(address string) string {
  597. u, err := url.Parse(address)
  598. if err != nil {
  599. return address
  600. }
  601. u.User = nil
  602. return u.String()
  603. }
  604. func checkHomeCodeViewable(ctx *context.Context) {
  605. if len(ctx.Repo.Units) > 0 {
  606. if ctx.Repo.Repository.IsBeingCreated() {
  607. task, err := models.GetMigratingTask(ctx.Repo.Repository.ID)
  608. if err != nil {
  609. if models.IsErrTaskDoesNotExist(err) {
  610. ctx.Data["Repo"] = ctx.Repo
  611. ctx.Data["CloneAddr"] = ""
  612. ctx.Data["Failed"] = true
  613. ctx.HTML(http.StatusOK, tplMigrating)
  614. return
  615. }
  616. ctx.ServerError("models.GetMigratingTask", err)
  617. return
  618. }
  619. cfg, err := task.MigrateConfig()
  620. if err != nil {
  621. ctx.ServerError("task.MigrateConfig", err)
  622. return
  623. }
  624. ctx.Data["Repo"] = ctx.Repo
  625. ctx.Data["MigrateTask"] = task
  626. ctx.Data["CloneAddr"] = safeURL(cfg.CloneAddr)
  627. ctx.Data["Failed"] = task.Status == structs.TaskStatusFailed
  628. ctx.HTML(http.StatusOK, tplMigrating)
  629. return
  630. }
  631. if ctx.IsSigned {
  632. // Set repo notification-status read if unread
  633. if err := models.SetRepoReadBy(ctx, ctx.Repo.Repository.ID, ctx.Doer.ID); err != nil {
  634. ctx.ServerError("ReadBy", err)
  635. return
  636. }
  637. }
  638. var firstUnit *unit_model.Unit
  639. for _, repoUnit := range ctx.Repo.Units {
  640. if repoUnit.Type == unit_model.TypeCode {
  641. return
  642. }
  643. unit, ok := unit_model.Units[repoUnit.Type]
  644. if ok && (firstUnit == nil || !firstUnit.IsLessThan(unit)) {
  645. firstUnit = &unit
  646. }
  647. }
  648. if firstUnit != nil {
  649. ctx.Redirect(fmt.Sprintf("%s%s", ctx.Repo.Repository.Link(), firstUnit.URI))
  650. return
  651. }
  652. }
  653. ctx.NotFound("Home", fmt.Errorf(ctx.Tr("units.error.no_unit_allowed_repo")))
  654. }
  655. // Home render repository home page
  656. func Home(ctx *context.Context) {
  657. isFeed, _, showFeedType := feed.GetFeedType(ctx.Params(":reponame"), ctx.Req)
  658. if isFeed {
  659. feed.ShowRepoFeed(ctx, ctx.Repo.Repository, showFeedType)
  660. return
  661. }
  662. ctx.Data["FeedURL"] = ctx.Repo.Repository.HTMLURL()
  663. checkHomeCodeViewable(ctx)
  664. if ctx.Written() {
  665. return
  666. }
  667. renderCode(ctx)
  668. }
  669. // LastCommit returns lastCommit data for the provided branch/tag/commit and directory (in url) and filenames in body
  670. func LastCommit(ctx *context.Context) {
  671. checkHomeCodeViewable(ctx)
  672. if ctx.Written() {
  673. return
  674. }
  675. renderDirectoryFiles(ctx, 0)
  676. if ctx.Written() {
  677. return
  678. }
  679. var treeNames []string
  680. paths := make([]string, 0, 5)
  681. if len(ctx.Repo.TreePath) > 0 {
  682. treeNames = strings.Split(ctx.Repo.TreePath, "/")
  683. for i := range treeNames {
  684. paths = append(paths, strings.Join(treeNames[:i+1], "/"))
  685. }
  686. ctx.Data["HasParentPath"] = true
  687. if len(paths)-2 >= 0 {
  688. ctx.Data["ParentPath"] = "/" + paths[len(paths)-2]
  689. }
  690. }
  691. branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
  692. ctx.Data["BranchLink"] = branchLink
  693. ctx.HTML(http.StatusOK, tplRepoViewList)
  694. }
  695. func renderDirectoryFiles(ctx *context.Context, timeout time.Duration) git.Entries {
  696. tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
  697. if err != nil {
  698. ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err)
  699. return nil
  700. }
  701. ctx.Data["LastCommitLoaderURL"] = ctx.Repo.RepoLink + "/lastcommit/" + url.PathEscape(ctx.Repo.CommitID) + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
  702. // Get current entry user currently looking at.
  703. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
  704. if err != nil {
  705. ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
  706. return nil
  707. }
  708. if !entry.IsDir() {
  709. ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
  710. return nil
  711. }
  712. allEntries, err := tree.ListEntries()
  713. if err != nil {
  714. ctx.ServerError("ListEntries", err)
  715. return nil
  716. }
  717. allEntries.CustomSort(base.NaturalSortLess)
  718. commitInfoCtx := gocontext.Context(ctx)
  719. if timeout > 0 {
  720. var cancel gocontext.CancelFunc
  721. commitInfoCtx, cancel = gocontext.WithTimeout(ctx, timeout)
  722. defer cancel()
  723. }
  724. selected := map[string]bool{}
  725. for _, pth := range ctx.FormStrings("f[]") {
  726. selected[pth] = true
  727. }
  728. entries := allEntries
  729. if len(selected) > 0 {
  730. entries = make(git.Entries, 0, len(selected))
  731. for _, entry := range allEntries {
  732. if selected[entry.Name()] {
  733. entries = append(entries, entry)
  734. }
  735. }
  736. }
  737. var latestCommit *git.Commit
  738. ctx.Data["Files"], latestCommit, err = entries.GetCommitsInfo(commitInfoCtx, ctx.Repo.Commit, ctx.Repo.TreePath)
  739. if err != nil {
  740. ctx.ServerError("GetCommitsInfo", err)
  741. return nil
  742. }
  743. // Show latest commit info of repository in table header,
  744. // or of directory if not in root directory.
  745. ctx.Data["LatestCommit"] = latestCommit
  746. if latestCommit != nil {
  747. verification := asymkey_model.ParseCommitWithSignature(latestCommit)
  748. if err := asymkey_model.CalculateTrustStatus(verification, ctx.Repo.Repository.GetTrustModel(), func(user *user_model.User) (bool, error) {
  749. return repo_model.IsOwnerMemberCollaborator(ctx.Repo.Repository, user.ID)
  750. }, nil); err != nil {
  751. ctx.ServerError("CalculateTrustStatus", err)
  752. return nil
  753. }
  754. ctx.Data["LatestCommitVerification"] = verification
  755. ctx.Data["LatestCommitUser"] = user_model.ValidateCommitWithEmail(latestCommit)
  756. statuses, _, err := git_model.GetLatestCommitStatus(ctx, ctx.Repo.Repository.ID, latestCommit.ID.String(), db.ListOptions{})
  757. if err != nil {
  758. log.Error("GetLatestCommitStatus: %v", err)
  759. }
  760. ctx.Data["LatestCommitStatus"] = git_model.CalcCommitStatus(statuses)
  761. ctx.Data["LatestCommitStatuses"] = statuses
  762. }
  763. branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
  764. treeLink := branchLink
  765. if len(ctx.Repo.TreePath) > 0 {
  766. treeLink += "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
  767. }
  768. ctx.Data["TreeLink"] = treeLink
  769. ctx.Data["SSHDomain"] = setting.SSH.Domain
  770. return allEntries
  771. }
  772. func renderLanguageStats(ctx *context.Context) {
  773. langs, err := repo_model.GetTopLanguageStats(ctx.Repo.Repository, 5)
  774. if err != nil {
  775. ctx.ServerError("Repo.GetTopLanguageStats", err)
  776. return
  777. }
  778. ctx.Data["LanguageStats"] = langs
  779. }
  780. func renderRepoTopics(ctx *context.Context) {
  781. topics, _, err := repo_model.FindTopics(&repo_model.FindTopicOptions{
  782. RepoID: ctx.Repo.Repository.ID,
  783. })
  784. if err != nil {
  785. ctx.ServerError("models.FindTopics", err)
  786. return
  787. }
  788. ctx.Data["Topics"] = topics
  789. }
  790. func renderCode(ctx *context.Context) {
  791. ctx.Data["PageIsViewCode"] = true
  792. if ctx.Repo.Repository.IsEmpty {
  793. reallyEmpty := true
  794. var err error
  795. if ctx.Repo.GitRepo != nil {
  796. reallyEmpty, err = ctx.Repo.GitRepo.IsEmpty()
  797. if err != nil {
  798. ctx.ServerError("GitRepo.IsEmpty", err)
  799. return
  800. }
  801. }
  802. if reallyEmpty {
  803. ctx.HTML(http.StatusOK, tplRepoEMPTY)
  804. return
  805. }
  806. // the repo is not really empty, so we should update the modal in database
  807. // such problem may be caused by:
  808. // 1) an error occurs during pushing/receiving. 2) the user replaces an empty git repo manually
  809. // and even more: the IsEmpty flag is deeply broken and should be removed with the UI changed to manage to cope with empty repos.
  810. // it's possible for a repository to be non-empty by that flag but still 500
  811. // because there are no branches - only tags -or the default branch is non-extant as it has been 0-pushed.
  812. ctx.Repo.Repository.IsEmpty = false
  813. if err = repo_model.UpdateRepositoryCols(ctx, ctx.Repo.Repository, "is_empty"); err != nil {
  814. ctx.ServerError("UpdateRepositoryCols", err)
  815. return
  816. }
  817. if err = repo_module.UpdateRepoSize(ctx, ctx.Repo.Repository); err != nil {
  818. ctx.ServerError("UpdateRepoSize", err)
  819. return
  820. }
  821. }
  822. title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
  823. if len(ctx.Repo.Repository.Description) > 0 {
  824. title += ": " + ctx.Repo.Repository.Description
  825. }
  826. ctx.Data["Title"] = title
  827. branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
  828. treeLink := branchLink
  829. rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL()
  830. if len(ctx.Repo.TreePath) > 0 {
  831. treeLink += "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
  832. }
  833. // Get Topics of this repo
  834. renderRepoTopics(ctx)
  835. if ctx.Written() {
  836. return
  837. }
  838. // Get current entry user currently looking at.
  839. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
  840. if err != nil {
  841. ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
  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("repos.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.GetOwner(ctx); err != nil {
  930. ctx.ServerError("GetOwner", err)
  931. return
  932. }
  933. }
  934. ctx.Data["Forks"] = forks
  935. ctx.HTML(http.StatusOK, tplForks)
  936. }