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.

73 lines
1.9 KiB

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
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repofiles
  5. import (
  6. "net/url"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. api "code.gitea.io/sdk/gitea"
  10. )
  11. // GetFileContents gets the meta data on a file's contents
  12. func GetFileContents(repo *models.Repository, treePath, ref string) (*api.FileContentResponse, error) {
  13. if ref == "" {
  14. ref = repo.DefaultBranch
  15. }
  16. // Check that the path given in opts.treePath is valid (not a git path)
  17. treePath = CleanUploadFileName(treePath)
  18. if treePath == "" {
  19. return nil, models.ErrFilenameInvalid{
  20. Path: treePath,
  21. }
  22. }
  23. gitRepo, err := git.OpenRepository(repo.RepoPath())
  24. if err != nil {
  25. return nil, err
  26. }
  27. // Get the commit object for the ref
  28. commit, err := gitRepo.GetCommit(ref)
  29. if err != nil {
  30. return nil, err
  31. }
  32. entry, err := commit.GetTreeEntryByPath(treePath)
  33. if err != nil {
  34. return nil, err
  35. }
  36. urlRef := ref
  37. if _, err := gitRepo.GetBranchCommit(ref); err == nil {
  38. urlRef = "branch/" + ref
  39. }
  40. selfURL, _ := url.Parse(repo.APIURL() + "/contents/" + treePath)
  41. gitURL, _ := url.Parse(repo.APIURL() + "/git/blobs/" + entry.ID.String())
  42. downloadURL, _ := url.Parse(repo.HTMLURL() + "/raw/" + urlRef + "/" + treePath)
  43. htmlURL, _ := url.Parse(repo.HTMLURL() + "/blob/" + ref + "/" + treePath)
  44. fileContent := &api.FileContentResponse{
  45. Name: entry.Name(),
  46. Path: treePath,
  47. SHA: entry.ID.String(),
  48. Size: entry.Size(),
  49. URL: selfURL.String(),
  50. HTMLURL: htmlURL.String(),
  51. GitURL: gitURL.String(),
  52. DownloadURL: downloadURL.String(),
  53. Type: entry.Type(),
  54. Links: &api.FileLinksResponse{
  55. Self: selfURL.String(),
  56. GitURL: gitURL.String(),
  57. HTMLURL: htmlURL.String(),
  58. },
  59. }
  60. return fileContent, nil
  61. }