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.

repo_blob_test.go 1.7KB

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 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "fmt"
  6. "io"
  7. "path/filepath"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestRepository_GetBlob_Found(t *testing.T) {
  12. repoPath := filepath.Join(testReposDir, "repo1_bare")
  13. r, err := openRepositoryWithDefaultContext(repoPath)
  14. assert.NoError(t, err)
  15. defer r.Close()
  16. testCases := []struct {
  17. OID string
  18. Data []byte
  19. }{
  20. {"e2129701f1a4d54dc44f03c93bca0a2aec7c5449", []byte("file1\n")},
  21. {"6c493ff740f9380390d5c9ddef4af18697ac9375", []byte("file2\n")},
  22. }
  23. for _, testCase := range testCases {
  24. blob, err := r.GetBlob(testCase.OID)
  25. assert.NoError(t, err)
  26. dataReader, err := blob.DataAsync()
  27. assert.NoError(t, err)
  28. data, err := io.ReadAll(dataReader)
  29. assert.NoError(t, dataReader.Close())
  30. assert.NoError(t, err)
  31. assert.Equal(t, testCase.Data, data)
  32. }
  33. }
  34. func TestRepository_GetBlob_NotExist(t *testing.T) {
  35. repoPath := filepath.Join(testReposDir, "repo1_bare")
  36. r, err := openRepositoryWithDefaultContext(repoPath)
  37. assert.NoError(t, err)
  38. defer r.Close()
  39. testCase := "0000000000000000000000000000000000000000"
  40. testError := ErrNotExist{testCase, ""}
  41. blob, err := r.GetBlob(testCase)
  42. assert.Nil(t, blob)
  43. assert.EqualError(t, err, testError.Error())
  44. }
  45. func TestRepository_GetBlob_NoId(t *testing.T) {
  46. repoPath := filepath.Join(testReposDir, "repo1_bare")
  47. r, err := openRepositoryWithDefaultContext(repoPath)
  48. assert.NoError(t, err)
  49. defer r.Close()
  50. testCase := ""
  51. testError := fmt.Errorf("Length must be 40: %s", testCase)
  52. blob, err := r.GetBlob(testCase)
  53. assert.Nil(t, blob)
  54. assert.EqualError(t, err, testError.Error())
  55. }