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.

738 lines
20 KiB

Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
7 years ago
Improve listing performance by using go-git (#6478) * Use go-git for tree reading and commit info lookup. Signed-off-by: Filip Navara <navara@emclient.com> * Use TreeEntry.IsRegular() instead of ObjectType that was removed. Signed-off-by: Filip Navara <navara@emclient.com> * Use the treePath to optimize commit info search. Signed-off-by: Filip Navara <navara@emclient.com> * Extract the latest commit at treePath along with the other commits. Signed-off-by: Filip Navara <navara@emclient.com> * Fix listing commit info for a directory that was created in one commit and never modified after. Signed-off-by: Filip Navara <navara@emclient.com> * Avoid nearly all external 'git' invocations when doing directory listing (.editorconfig code path is still hit). Signed-off-by: Filip Navara <navara@emclient.com> * Use go-git for reading blobs. Signed-off-by: Filip Navara <navara@emclient.com> * Make SHA1 type alias for plumbing.Hash in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Make Signature type alias for object.Signature in go-git. Signed-off-by: Filip Navara <navara@emclient.com> * Fix GetCommitsInfo for repository with only one commit. Signed-off-by: Filip Navara <navara@emclient.com> * Fix PGP signature verification. Signed-off-by: Filip Navara <navara@emclient.com> * Fix issues with walking commit graph across merges. Signed-off-by: Filip Navara <navara@emclient.com> * Fix typo in condition. Signed-off-by: Filip Navara <navara@emclient.com> * Speed up loading branch list by keeping the repository reference (and thus all the loaded packfile indexes). Signed-off-by: Filip Navara <navara@emclient.com> * Fix lising submodules. Signed-off-by: Filip Navara <navara@emclient.com> * Fix build Signed-off-by: Filip Navara <navara@emclient.com> * Add back commit cache because of name-rev Signed-off-by: Filip Navara <navara@emclient.com> * Fix tests Signed-off-by: Filip Navara <navara@emclient.com> * Fix code style * Fix spelling * Address PR feedback Signed-off-by: Filip Navara <navara@emclient.com> * Update vendor module list Signed-off-by: Filip Navara <navara@emclient.com> * Fix getting trees by commit id Signed-off-by: Filip Navara <navara@emclient.com> * Fix remaining unit test failures * Fix GetTreeBySHA * Avoid running `git name-rev` if not necessary Signed-off-by: Filip Navara <navara@emclient.com> * Move Branch code to git module * Clean up GPG signature verification and fix it for tagged commits * Address PR feedback (import formatting, copyright headers) * Make blob lookup by SHA working * Update tests to use public API * Allow getting content from any type of object through the blob interface * Change test to actually expect the object content that is in the GIT repository * Change one more test to actually expect the object content that is in the GIT repository * Add comments
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
9 years ago
Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
7 years ago
Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
7 years ago
Git LFS support v2 (#122) * Import github.com/git-lfs/lfs-test-server as lfs module base Imported commit is 3968aac269a77b73924649b9412ae03f7ccd3198 Removed: Dockerfile CONTRIBUTING.md mgmt* script/ vendor/ kvlogger.go .dockerignore .gitignore README.md * Remove config, add JWT support from github.com/mgit-at/lfs-test-server Imported commit f0cdcc5a01599c5a955dc1bbf683bb4acecdba83 * Add LFS settings * Add LFS meta object model * Add LFS routes and initialization * Import github.com/dgrijalva/jwt-go into vendor/ * Adapt LFS module: handlers, routing, meta store * Move LFS routes to /user/repo/info/lfs/* * Add request header checks to LFS BatchHandler / PostHandler * Implement LFS basic authentication * Rework JWT secret generation / load * Implement LFS SSH token authentication with JWT Specification: https://github.com/github/git-lfs/tree/master/docs/api * Integrate LFS settings into install process * Remove LFS objects when repository is deleted Only removes objects from content store when deleted repo is the only referencing repository * Make LFS module stateless Fixes bug where LFS would not work after installation without restarting Gitea * Change 500 'Internal Server Error' to 400 'Bad Request' * Change sql query to xorm call * Remove unneeded type from LFS module * Change internal imports to code.gitea.io/gitea/ * Add Gitea authors copyright * Change basic auth realm to "gitea-lfs" * Add unique indexes to LFS model * Use xorm count function in LFS check on repository delete * Return io.ReadCloser from content store and close after usage * Add LFS info to runWeb() * Export LFS content store base path * LFS file download from UI * Work around git-lfs client issue with unauthenticated requests Returning a dummy Authorization header for unauthenticated requests lets git-lfs client skip asking for auth credentials See: https://github.com/github/git-lfs/issues/1088 * Fix unauthenticated UI downloads from public repositories * Authentication check order, Finish LFS file view logic * Ignore LFS hooks if installed for current OS user Fixes Gitea UI actions for repositories tracking LFS files. Checks for minimum needed git version by parsing the semantic version string. * Hide LFS metafile diff from commit view, marking as binary * Show LFS notice if file in commit view is tracked * Add notbefore/nbf JWT claim * Correct lint suggestions - comments for structs and functions - Add comments to LFS model - Function comment for GetRandomBytesAsBase64 - LFS server function comments and lint variable suggestion * Move secret generation code out of conditional Ensures no LFS code may run with an empty secret * Do not hand out JWT tokens if LFS server support is disabled
7 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
8 years ago
8 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  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. "encoding/base64"
  9. "fmt"
  10. gotemplate "html/template"
  11. "io/ioutil"
  12. "net/url"
  13. "path"
  14. "strconv"
  15. "strings"
  16. "code.gitea.io/gitea/models"
  17. "code.gitea.io/gitea/modules/base"
  18. "code.gitea.io/gitea/modules/cache"
  19. "code.gitea.io/gitea/modules/charset"
  20. "code.gitea.io/gitea/modules/context"
  21. "code.gitea.io/gitea/modules/git"
  22. "code.gitea.io/gitea/modules/highlight"
  23. "code.gitea.io/gitea/modules/lfs"
  24. "code.gitea.io/gitea/modules/log"
  25. "code.gitea.io/gitea/modules/markup"
  26. "code.gitea.io/gitea/modules/setting"
  27. )
  28. const (
  29. tplRepoEMPTY base.TplName = "repo/empty"
  30. tplRepoHome base.TplName = "repo/home"
  31. tplWatchers base.TplName = "repo/watchers"
  32. tplForks base.TplName = "repo/forks"
  33. tplMigrating base.TplName = "repo/migrate/migrating"
  34. )
  35. type namedBlob struct {
  36. name string
  37. isSymlink bool
  38. blob *git.Blob
  39. }
  40. func linesBytesCount(s []byte) int {
  41. nl := []byte{'\n'}
  42. n := bytes.Count(s, nl)
  43. if len(s) > 0 && !bytes.HasSuffix(s, nl) {
  44. n++
  45. }
  46. return n
  47. }
  48. // FIXME: There has to be a more efficient way of doing this
  49. func getReadmeFileFromPath(commit *git.Commit, treePath string) (*namedBlob, error) {
  50. tree, err := commit.SubTree(treePath)
  51. if err != nil {
  52. return nil, err
  53. }
  54. entries, err := tree.ListEntries()
  55. if err != nil {
  56. return nil, err
  57. }
  58. var readmeFiles [4]*namedBlob
  59. var exts = []string{".md", ".txt", ""} // sorted by priority
  60. for _, entry := range entries {
  61. if entry.IsDir() {
  62. continue
  63. }
  64. for i, ext := range exts {
  65. if markup.IsReadmeFile(entry.Name(), ext) {
  66. if readmeFiles[i] == nil || base.NaturalSortLess(readmeFiles[i].name, entry.Blob().Name()) {
  67. name := entry.Name()
  68. isSymlink := entry.IsLink()
  69. target := entry
  70. if isSymlink {
  71. target, err = entry.FollowLinks()
  72. if err != nil && !git.IsErrBadLink(err) {
  73. return nil, err
  74. }
  75. }
  76. if target != nil && (target.IsExecutable() || target.IsRegular()) {
  77. readmeFiles[i] = &namedBlob{
  78. name,
  79. isSymlink,
  80. target.Blob(),
  81. }
  82. }
  83. }
  84. }
  85. }
  86. if markup.IsReadmeFile(entry.Name()) {
  87. if readmeFiles[3] == nil || base.NaturalSortLess(readmeFiles[3].name, entry.Blob().Name()) {
  88. name := entry.Name()
  89. isSymlink := entry.IsLink()
  90. if isSymlink {
  91. entry, err = entry.FollowLinks()
  92. if err != nil && !git.IsErrBadLink(err) {
  93. return nil, err
  94. }
  95. }
  96. if entry != nil && (entry.IsExecutable() || entry.IsRegular()) {
  97. readmeFiles[3] = &namedBlob{
  98. name,
  99. isSymlink,
  100. entry.Blob(),
  101. }
  102. }
  103. }
  104. }
  105. }
  106. var readmeFile *namedBlob
  107. for _, f := range readmeFiles {
  108. if f != nil {
  109. readmeFile = f
  110. break
  111. }
  112. }
  113. return readmeFile, nil
  114. }
  115. func renderDirectory(ctx *context.Context, treeLink string) {
  116. tree, err := ctx.Repo.Commit.SubTree(ctx.Repo.TreePath)
  117. if err != nil {
  118. ctx.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err)
  119. return
  120. }
  121. entries, err := tree.ListEntries()
  122. if err != nil {
  123. ctx.ServerError("ListEntries", err)
  124. return
  125. }
  126. entries.CustomSort(base.NaturalSortLess)
  127. var c git.LastCommitCache
  128. if setting.CacheService.LastCommit.Enabled && ctx.Repo.CommitsCount >= setting.CacheService.LastCommit.CommitsCount {
  129. c = cache.NewLastCommitCache(ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, int64(setting.CacheService.LastCommit.TTL.Seconds()))
  130. }
  131. var latestCommit *git.Commit
  132. ctx.Data["Files"], latestCommit, err = entries.GetCommitsInfo(ctx.Repo.Commit, ctx.Repo.TreePath, c)
  133. if err != nil {
  134. ctx.ServerError("GetCommitsInfo", err)
  135. return
  136. }
  137. // 3 for the extensions in exts[] in order
  138. // the last one is for a readme that doesn't
  139. // strictly match an extension
  140. var readmeFiles [4]*namedBlob
  141. var docsEntries [3]*git.TreeEntry
  142. var exts = []string{".md", ".txt", ""} // sorted by priority
  143. for _, entry := range entries {
  144. if entry.IsDir() {
  145. lowerName := strings.ToLower(entry.Name())
  146. switch lowerName {
  147. case "docs":
  148. if entry.Name() == "docs" || docsEntries[0] == nil {
  149. docsEntries[0] = entry
  150. }
  151. case ".gitea":
  152. if entry.Name() == ".gitea" || docsEntries[1] == nil {
  153. docsEntries[1] = entry
  154. }
  155. case ".github":
  156. if entry.Name() == ".github" || docsEntries[2] == nil {
  157. docsEntries[2] = entry
  158. }
  159. }
  160. continue
  161. }
  162. for i, ext := range exts {
  163. if markup.IsReadmeFile(entry.Name(), ext) {
  164. log.Debug("%s", entry.Name())
  165. name := entry.Name()
  166. isSymlink := entry.IsLink()
  167. target := entry
  168. if isSymlink {
  169. target, err = entry.FollowLinks()
  170. if err != nil && !git.IsErrBadLink(err) {
  171. ctx.ServerError("FollowLinks", err)
  172. return
  173. }
  174. }
  175. log.Debug("%t", target == nil)
  176. if target != nil && (target.IsExecutable() || target.IsRegular()) {
  177. readmeFiles[i] = &namedBlob{
  178. name,
  179. isSymlink,
  180. target.Blob(),
  181. }
  182. }
  183. }
  184. }
  185. if markup.IsReadmeFile(entry.Name()) {
  186. name := entry.Name()
  187. isSymlink := entry.IsLink()
  188. if isSymlink {
  189. entry, err = entry.FollowLinks()
  190. if err != nil && !git.IsErrBadLink(err) {
  191. ctx.ServerError("FollowLinks", err)
  192. return
  193. }
  194. }
  195. if entry != nil && (entry.IsExecutable() || entry.IsRegular()) {
  196. readmeFiles[3] = &namedBlob{
  197. name,
  198. isSymlink,
  199. entry.Blob(),
  200. }
  201. }
  202. }
  203. }
  204. var readmeFile *namedBlob
  205. readmeTreelink := treeLink
  206. for _, f := range readmeFiles {
  207. if f != nil {
  208. readmeFile = f
  209. break
  210. }
  211. }
  212. if ctx.Repo.TreePath == "" && readmeFile == nil {
  213. for _, entry := range docsEntries {
  214. if entry == nil {
  215. continue
  216. }
  217. readmeFile, err = getReadmeFileFromPath(ctx.Repo.Commit, entry.GetSubJumpablePathName())
  218. if err != nil {
  219. ctx.ServerError("getReadmeFileFromPath", err)
  220. return
  221. }
  222. if readmeFile != nil {
  223. readmeFile.name = entry.Name() + "/" + readmeFile.name
  224. readmeTreelink = treeLink + "/" + entry.GetSubJumpablePathName()
  225. break
  226. }
  227. }
  228. }
  229. if readmeFile != nil {
  230. ctx.Data["RawFileLink"] = ""
  231. ctx.Data["ReadmeInList"] = true
  232. ctx.Data["ReadmeExist"] = true
  233. ctx.Data["FileIsSymlink"] = readmeFile.isSymlink
  234. dataRc, err := readmeFile.blob.DataAsync()
  235. if err != nil {
  236. ctx.ServerError("Data", err)
  237. return
  238. }
  239. defer dataRc.Close()
  240. buf := make([]byte, 1024)
  241. n, _ := dataRc.Read(buf)
  242. buf = buf[:n]
  243. isTextFile := base.IsTextFile(buf)
  244. ctx.Data["FileIsText"] = isTextFile
  245. ctx.Data["FileName"] = readmeFile.name
  246. fileSize := int64(0)
  247. isLFSFile := false
  248. ctx.Data["IsLFSFile"] = false
  249. // FIXME: what happens when README file is an image?
  250. if isTextFile && setting.LFS.StartServer {
  251. meta := lfs.IsPointerFile(&buf)
  252. if meta != nil {
  253. meta, err = ctx.Repo.Repository.GetLFSMetaObjectByOid(meta.Oid)
  254. if err != nil && err != models.ErrLFSObjectNotExist {
  255. ctx.ServerError("GetLFSMetaObject", err)
  256. return
  257. }
  258. }
  259. if meta != nil {
  260. ctx.Data["IsLFSFile"] = true
  261. isLFSFile = true
  262. // OK read the lfs object
  263. var err error
  264. dataRc, err = lfs.ReadMetaObject(meta)
  265. if err != nil {
  266. ctx.ServerError("ReadMetaObject", err)
  267. return
  268. }
  269. defer dataRc.Close()
  270. buf = make([]byte, 1024)
  271. n, err = dataRc.Read(buf)
  272. if err != nil {
  273. ctx.ServerError("Data", err)
  274. return
  275. }
  276. buf = buf[:n]
  277. isTextFile = base.IsTextFile(buf)
  278. ctx.Data["IsTextFile"] = isTextFile
  279. fileSize = meta.Size
  280. ctx.Data["FileSize"] = meta.Size
  281. filenameBase64 := base64.RawURLEncoding.EncodeToString([]byte(readmeFile.name))
  282. ctx.Data["RawFileLink"] = fmt.Sprintf("%s%s.git/info/lfs/objects/%s/%s", setting.AppURL, ctx.Repo.Repository.FullName(), meta.Oid, filenameBase64)
  283. }
  284. }
  285. if !isLFSFile {
  286. fileSize = readmeFile.blob.Size()
  287. }
  288. if isTextFile {
  289. if fileSize >= setting.UI.MaxDisplayFileSize {
  290. // Pretend that this is a normal text file to display 'This file is too large to be shown'
  291. ctx.Data["IsFileTooLarge"] = true
  292. ctx.Data["IsTextFile"] = true
  293. ctx.Data["FileSize"] = fileSize
  294. } else {
  295. d, _ := ioutil.ReadAll(dataRc)
  296. buf = charset.ToUTF8WithFallback(append(buf, d...))
  297. if markupType := markup.Type(readmeFile.name); markupType != "" {
  298. ctx.Data["IsMarkup"] = true
  299. ctx.Data["MarkupType"] = string(markupType)
  300. ctx.Data["FileContent"] = string(markup.Render(readmeFile.name, buf, readmeTreelink, ctx.Repo.Repository.ComposeDocumentMetas()))
  301. } else {
  302. ctx.Data["IsRenderedHTML"] = true
  303. ctx.Data["FileContent"] = strings.Replace(
  304. gotemplate.HTMLEscapeString(string(buf)), "\n", `<br>`, -1,
  305. )
  306. }
  307. }
  308. }
  309. }
  310. // Show latest commit info of repository in table header,
  311. // or of directory if not in root directory.
  312. ctx.Data["LatestCommit"] = latestCommit
  313. verification := models.ParseCommitWithSignature(latestCommit)
  314. if err := models.CalculateTrustStatus(verification, ctx.Repo.Repository, nil); err != nil {
  315. ctx.ServerError("CalculateTrustStatus", err)
  316. return
  317. }
  318. ctx.Data["LatestCommitVerification"] = verification
  319. ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit)
  320. statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), 0)
  321. if err != nil {
  322. log.Error("GetLatestCommitStatus: %v", err)
  323. }
  324. ctx.Data["LatestCommitStatus"] = models.CalcCommitStatus(statuses)
  325. // Check permission to add or upload new file.
  326. if ctx.Repo.CanWrite(models.UnitTypeCode) && ctx.Repo.IsViewBranch {
  327. ctx.Data["CanAddFile"] = !ctx.Repo.Repository.IsArchived
  328. ctx.Data["CanUploadFile"] = setting.Repository.Upload.Enabled && !ctx.Repo.Repository.IsArchived
  329. }
  330. ctx.Data["SSHDomain"] = setting.SSH.Domain
  331. }
  332. func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink string) {
  333. ctx.Data["IsViewFile"] = true
  334. blob := entry.Blob()
  335. dataRc, err := blob.DataAsync()
  336. if err != nil {
  337. ctx.ServerError("DataAsync", err)
  338. return
  339. }
  340. defer dataRc.Close()
  341. ctx.Data["Title"] = ctx.Data["Title"].(string) + " - " + ctx.Repo.TreePath + " at " + ctx.Repo.BranchName
  342. fileSize := blob.Size()
  343. ctx.Data["FileIsSymlink"] = entry.IsLink()
  344. ctx.Data["FileSize"] = fileSize
  345. ctx.Data["FileName"] = blob.Name()
  346. ctx.Data["RawFileLink"] = rawLink + "/" + ctx.Repo.TreePath
  347. buf := make([]byte, 1024)
  348. n, _ := dataRc.Read(buf)
  349. buf = buf[:n]
  350. isTextFile := base.IsTextFile(buf)
  351. isLFSFile := false
  352. ctx.Data["IsTextFile"] = isTextFile
  353. //Check for LFS meta file
  354. if isTextFile && setting.LFS.StartServer {
  355. meta := lfs.IsPointerFile(&buf)
  356. if meta != nil {
  357. meta, err = ctx.Repo.Repository.GetLFSMetaObjectByOid(meta.Oid)
  358. if err != nil && err != models.ErrLFSObjectNotExist {
  359. ctx.ServerError("GetLFSMetaObject", err)
  360. return
  361. }
  362. }
  363. if meta != nil {
  364. ctx.Data["IsLFSFile"] = true
  365. isLFSFile = true
  366. // OK read the lfs object
  367. var err error
  368. dataRc, err = lfs.ReadMetaObject(meta)
  369. if err != nil {
  370. ctx.ServerError("ReadMetaObject", err)
  371. return
  372. }
  373. defer dataRc.Close()
  374. buf = make([]byte, 1024)
  375. n, err = dataRc.Read(buf)
  376. if err != nil {
  377. ctx.ServerError("Data", err)
  378. return
  379. }
  380. buf = buf[:n]
  381. isTextFile = base.IsTextFile(buf)
  382. ctx.Data["IsTextFile"] = isTextFile
  383. fileSize = meta.Size
  384. ctx.Data["FileSize"] = meta.Size
  385. filenameBase64 := base64.RawURLEncoding.EncodeToString([]byte(blob.Name()))
  386. ctx.Data["RawFileLink"] = fmt.Sprintf("%s%s.git/info/lfs/objects/%s/%s", setting.AppURL, ctx.Repo.Repository.FullName(), meta.Oid, filenameBase64)
  387. }
  388. }
  389. // Check LFS Lock
  390. lfsLock, err := ctx.Repo.Repository.GetTreePathLock(ctx.Repo.TreePath)
  391. ctx.Data["LFSLock"] = lfsLock
  392. if err != nil {
  393. ctx.ServerError("GetTreePathLock", err)
  394. return
  395. }
  396. if lfsLock != nil {
  397. ctx.Data["LFSLockOwner"] = lfsLock.Owner.DisplayName()
  398. ctx.Data["LFSLockHint"] = ctx.Tr("repo.editor.this_file_locked")
  399. }
  400. // Assume file is not editable first.
  401. if isLFSFile {
  402. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_lfs_files")
  403. } else if !isTextFile {
  404. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.cannot_edit_non_text_files")
  405. }
  406. switch {
  407. case isTextFile:
  408. if fileSize >= setting.UI.MaxDisplayFileSize {
  409. ctx.Data["IsFileTooLarge"] = true
  410. break
  411. }
  412. d, _ := ioutil.ReadAll(dataRc)
  413. buf = charset.ToUTF8WithFallback(append(buf, d...))
  414. readmeExist := markup.IsReadmeFile(blob.Name())
  415. ctx.Data["ReadmeExist"] = readmeExist
  416. if markupType := markup.Type(blob.Name()); markupType != "" {
  417. ctx.Data["IsMarkup"] = true
  418. ctx.Data["MarkupType"] = markupType
  419. ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeDocumentMetas()))
  420. } else if readmeExist {
  421. ctx.Data["IsRenderedHTML"] = true
  422. ctx.Data["FileContent"] = strings.Replace(
  423. gotemplate.HTMLEscapeString(string(buf)), "\n", `<br>`, -1,
  424. )
  425. } else {
  426. buf = charset.ToUTF8WithFallback(buf)
  427. lineNums := linesBytesCount(buf)
  428. ctx.Data["NumLines"] = strconv.Itoa(lineNums)
  429. ctx.Data["NumLinesSet"] = true
  430. ctx.Data["FileContent"] = highlight.File(lineNums, blob.Name(), buf)
  431. }
  432. if !isLFSFile {
  433. if ctx.Repo.CanEnableEditor() {
  434. if lfsLock != nil && lfsLock.OwnerID != ctx.User.ID {
  435. ctx.Data["CanEditFile"] = false
  436. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.this_file_locked")
  437. } else {
  438. ctx.Data["CanEditFile"] = true
  439. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file")
  440. }
  441. } else if !ctx.Repo.IsViewBranch {
  442. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
  443. } else if !ctx.Repo.CanWrite(models.UnitTypeCode) {
  444. ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit")
  445. }
  446. }
  447. case base.IsPDFFile(buf):
  448. ctx.Data["IsPDFFile"] = true
  449. case base.IsVideoFile(buf):
  450. ctx.Data["IsVideoFile"] = true
  451. case base.IsAudioFile(buf):
  452. ctx.Data["IsAudioFile"] = true
  453. case base.IsImageFile(buf):
  454. ctx.Data["IsImageFile"] = true
  455. default:
  456. if fileSize >= setting.UI.MaxDisplayFileSize {
  457. ctx.Data["IsFileTooLarge"] = true
  458. break
  459. }
  460. if markupType := markup.Type(blob.Name()); markupType != "" {
  461. d, _ := ioutil.ReadAll(dataRc)
  462. buf = append(buf, d...)
  463. ctx.Data["IsMarkup"] = true
  464. ctx.Data["MarkupType"] = markupType
  465. ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeDocumentMetas()))
  466. }
  467. }
  468. if ctx.Repo.CanEnableEditor() {
  469. if lfsLock != nil && lfsLock.OwnerID != ctx.User.ID {
  470. ctx.Data["CanDeleteFile"] = false
  471. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.this_file_locked")
  472. } else {
  473. ctx.Data["CanDeleteFile"] = true
  474. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.delete_this_file")
  475. }
  476. } else if !ctx.Repo.IsViewBranch {
  477. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch")
  478. } else if !ctx.Repo.CanWrite(models.UnitTypeCode) {
  479. ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_have_write_access")
  480. }
  481. }
  482. func safeURL(address string) string {
  483. u, err := url.Parse(address)
  484. if err != nil {
  485. return address
  486. }
  487. u.User = nil
  488. return u.String()
  489. }
  490. // Home render repository home page
  491. func Home(ctx *context.Context) {
  492. if len(ctx.Repo.Units) > 0 {
  493. if ctx.Repo.Repository.IsBeingCreated() {
  494. task, err := models.GetMigratingTask(ctx.Repo.Repository.ID)
  495. if err != nil {
  496. ctx.ServerError("models.GetMigratingTask", err)
  497. return
  498. }
  499. cfg, err := task.MigrateConfig()
  500. if err != nil {
  501. ctx.ServerError("task.MigrateConfig", err)
  502. return
  503. }
  504. ctx.Data["Repo"] = ctx.Repo
  505. ctx.Data["MigrateTask"] = task
  506. ctx.Data["CloneAddr"] = safeURL(cfg.CloneAddr)
  507. ctx.HTML(200, tplMigrating)
  508. return
  509. }
  510. var firstUnit *models.Unit
  511. for _, repoUnit := range ctx.Repo.Units {
  512. if repoUnit.Type == models.UnitTypeCode {
  513. renderCode(ctx)
  514. return
  515. }
  516. unit, ok := models.Units[repoUnit.Type]
  517. if ok && (firstUnit == nil || !firstUnit.IsLessThan(unit)) {
  518. firstUnit = &unit
  519. }
  520. }
  521. if firstUnit != nil {
  522. ctx.Redirect(fmt.Sprintf("%s/%s%s", setting.AppSubURL, ctx.Repo.Repository.FullName(), firstUnit.URI))
  523. return
  524. }
  525. }
  526. ctx.NotFound("Home", fmt.Errorf(ctx.Tr("units.error.no_unit_allowed_repo")))
  527. }
  528. func renderLanguageStats(ctx *context.Context) {
  529. langs, err := ctx.Repo.Repository.GetTopLanguageStats(5)
  530. if err != nil {
  531. ctx.ServerError("Repo.GetTopLanguageStats", err)
  532. return
  533. }
  534. ctx.Data["LanguageStats"] = langs
  535. }
  536. func renderRepoTopics(ctx *context.Context) {
  537. topics, err := models.FindTopics(&models.FindTopicOptions{
  538. RepoID: ctx.Repo.Repository.ID,
  539. })
  540. if err != nil {
  541. ctx.ServerError("models.FindTopics", err)
  542. return
  543. }
  544. ctx.Data["Topics"] = topics
  545. }
  546. func renderCode(ctx *context.Context) {
  547. ctx.Data["PageIsViewCode"] = true
  548. if ctx.Repo.Repository.IsEmpty {
  549. ctx.HTML(200, tplRepoEMPTY)
  550. return
  551. }
  552. title := ctx.Repo.Repository.Owner.Name + "/" + ctx.Repo.Repository.Name
  553. if len(ctx.Repo.Repository.Description) > 0 {
  554. title += ": " + ctx.Repo.Repository.Description
  555. }
  556. ctx.Data["Title"] = title
  557. branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL()
  558. treeLink := branchLink
  559. rawLink := ctx.Repo.RepoLink + "/raw/" + ctx.Repo.BranchNameSubURL()
  560. if len(ctx.Repo.TreePath) > 0 {
  561. treeLink += "/" + ctx.Repo.TreePath
  562. }
  563. // Get Topics of this repo
  564. renderRepoTopics(ctx)
  565. if ctx.Written() {
  566. return
  567. }
  568. // Get current entry user currently looking at.
  569. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
  570. if err != nil {
  571. ctx.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
  572. return
  573. }
  574. renderLanguageStats(ctx)
  575. if ctx.Written() {
  576. return
  577. }
  578. if entry.IsDir() {
  579. renderDirectory(ctx, treeLink)
  580. } else {
  581. renderFile(ctx, entry, treeLink, rawLink)
  582. }
  583. if ctx.Written() {
  584. return
  585. }
  586. var treeNames []string
  587. paths := make([]string, 0, 5)
  588. if len(ctx.Repo.TreePath) > 0 {
  589. treeNames = strings.Split(ctx.Repo.TreePath, "/")
  590. for i := range treeNames {
  591. paths = append(paths, strings.Join(treeNames[:i+1], "/"))
  592. }
  593. ctx.Data["HasParentPath"] = true
  594. if len(paths)-2 >= 0 {
  595. ctx.Data["ParentPath"] = "/" + paths[len(paths)-2]
  596. }
  597. }
  598. ctx.Data["Paths"] = paths
  599. ctx.Data["TreeLink"] = treeLink
  600. ctx.Data["TreeNames"] = treeNames
  601. ctx.Data["BranchLink"] = branchLink
  602. ctx.HTML(200, tplRepoHome)
  603. }
  604. // RenderUserCards render a page show users according the input templaet
  605. func RenderUserCards(ctx *context.Context, total int, getter func(opts models.ListOptions) ([]*models.User, error), tpl base.TplName) {
  606. page := ctx.QueryInt("page")
  607. if page <= 0 {
  608. page = 1
  609. }
  610. pager := context.NewPagination(total, models.ItemsPerPage, page, 5)
  611. ctx.Data["Page"] = pager
  612. items, err := getter(models.ListOptions{Page: pager.Paginater.Current()})
  613. if err != nil {
  614. ctx.ServerError("getter", err)
  615. return
  616. }
  617. ctx.Data["Cards"] = items
  618. ctx.HTML(200, tpl)
  619. }
  620. // Watchers render repository's watch users
  621. func Watchers(ctx *context.Context) {
  622. ctx.Data["Title"] = ctx.Tr("repo.watchers")
  623. ctx.Data["CardsTitle"] = ctx.Tr("repo.watchers")
  624. ctx.Data["PageIsWatchers"] = true
  625. RenderUserCards(ctx, ctx.Repo.Repository.NumWatches, ctx.Repo.Repository.GetWatchers, tplWatchers)
  626. }
  627. // Stars render repository's starred users
  628. func Stars(ctx *context.Context) {
  629. ctx.Data["Title"] = ctx.Tr("repo.stargazers")
  630. ctx.Data["CardsTitle"] = ctx.Tr("repo.stargazers")
  631. ctx.Data["PageIsStargazers"] = true
  632. RenderUserCards(ctx, ctx.Repo.Repository.NumStars, ctx.Repo.Repository.GetStargazers, tplWatchers)
  633. }
  634. // Forks render repository's forked users
  635. func Forks(ctx *context.Context) {
  636. ctx.Data["Title"] = ctx.Tr("repos.forks")
  637. forks, err := ctx.Repo.Repository.GetForks(models.ListOptions{})
  638. if err != nil {
  639. ctx.ServerError("GetForks", err)
  640. return
  641. }
  642. for _, fork := range forks {
  643. if err = fork.GetOwner(); err != nil {
  644. ctx.ServerError("GetOwner", err)
  645. return
  646. }
  647. }
  648. ctx.Data["Forks"] = forks
  649. ctx.HTML(200, tplForks)
  650. }