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.

tree_entry_test.go 3.5KB

Use native git variants by default with go-git variants as build tag (#13673) * Move last commit cache back into modules/git Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from the interface for last commit cache Signed-off-by: Andrew Thornton <art27@cantab.net> * move cacheref to last_commit_cache Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove go-git from routers/private/hook Signed-off-by: Andrew Thornton <art27@cantab.net> * Move FindLFSFiles to pipeline Signed-off-by: Andrew Thornton <art27@cantab.net> * Make no-go-git variants Signed-off-by: Andrew Thornton <art27@cantab.net> * Submodule RefID Signed-off-by: Andrew Thornton <art27@cantab.net> * fix issue with GetCommitsInfo Signed-off-by: Andrew Thornton <art27@cantab.net> * fix GetLastCommitForPaths Signed-off-by: Andrew Thornton <art27@cantab.net> * Improve efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * More efficiency Signed-off-by: Andrew Thornton <art27@cantab.net> * even faster Signed-off-by: Andrew Thornton <art27@cantab.net> * Reduce duplication * As per @lunny Signed-off-by: Andrew Thornton <art27@cantab.net> * attempt to fix drone Signed-off-by: Andrew Thornton <art27@cantab.net> * fix test-tags Signed-off-by: Andrew Thornton <art27@cantab.net> * default to use no-go-git variants and add gogit build tag Signed-off-by: Andrew Thornton <art27@cantab.net> * placate lint Signed-off-by: Andrew Thornton <art27@cantab.net> * as per @6543 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
3 lat temu
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 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2017 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. //go:build gogit
  5. // +build gogit
  6. package git
  7. import (
  8. "testing"
  9. "github.com/go-git/go-git/v5/plumbing/filemode"
  10. "github.com/go-git/go-git/v5/plumbing/object"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func getTestEntries() Entries {
  14. return Entries{
  15. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v1.0", Mode: filemode.Dir}},
  16. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.0", Mode: filemode.Dir}},
  17. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.1", Mode: filemode.Dir}},
  18. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.12", Mode: filemode.Dir}},
  19. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.2", Mode: filemode.Dir}},
  20. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v12.0", Mode: filemode.Dir}},
  21. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "abc", Mode: filemode.Regular}},
  22. &TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "bcd", Mode: filemode.Regular}},
  23. }
  24. }
  25. func TestEntriesSort(t *testing.T) {
  26. entries := getTestEntries()
  27. entries.Sort()
  28. assert.Equal(t, "v1.0", entries[0].Name())
  29. assert.Equal(t, "v12.0", entries[1].Name())
  30. assert.Equal(t, "v2.0", entries[2].Name())
  31. assert.Equal(t, "v2.1", entries[3].Name())
  32. assert.Equal(t, "v2.12", entries[4].Name())
  33. assert.Equal(t, "v2.2", entries[5].Name())
  34. assert.Equal(t, "abc", entries[6].Name())
  35. assert.Equal(t, "bcd", entries[7].Name())
  36. }
  37. func TestEntriesCustomSort(t *testing.T) {
  38. entries := getTestEntries()
  39. entries.CustomSort(func(s1, s2 string) bool {
  40. return s1 > s2
  41. })
  42. assert.Equal(t, "v2.2", entries[0].Name())
  43. assert.Equal(t, "v2.12", entries[1].Name())
  44. assert.Equal(t, "v2.1", entries[2].Name())
  45. assert.Equal(t, "v2.0", entries[3].Name())
  46. assert.Equal(t, "v12.0", entries[4].Name())
  47. assert.Equal(t, "v1.0", entries[5].Name())
  48. assert.Equal(t, "bcd", entries[6].Name())
  49. assert.Equal(t, "abc", entries[7].Name())
  50. }
  51. func TestFollowLink(t *testing.T) {
  52. r, err := OpenRepository("tests/repos/repo1_bare")
  53. assert.NoError(t, err)
  54. defer r.Close()
  55. commit, err := r.GetCommit("37991dec2c8e592043f47155ce4808d4580f9123")
  56. assert.NoError(t, err)
  57. // get the symlink
  58. lnk, err := commit.Tree.GetTreeEntryByPath("foo/bar/link_to_hello")
  59. assert.NoError(t, err)
  60. assert.True(t, lnk.IsLink())
  61. // should be able to dereference to target
  62. target, err := lnk.FollowLink()
  63. assert.NoError(t, err)
  64. assert.Equal(t, "hello", target.Name())
  65. assert.False(t, target.IsLink())
  66. assert.Equal(t, "b14df6442ea5a1b382985a6549b85d435376c351", target.ID.String())
  67. // should error when called on normal file
  68. target, err = commit.Tree.GetTreeEntryByPath("file1.txt")
  69. assert.NoError(t, err)
  70. _, err = target.FollowLink()
  71. assert.EqualError(t, err, "file1.txt: not a symlink")
  72. // should error for broken links
  73. target, err = commit.Tree.GetTreeEntryByPath("foo/broken_link")
  74. assert.NoError(t, err)
  75. assert.True(t, target.IsLink())
  76. _, err = target.FollowLink()
  77. assert.EqualError(t, err, "broken_link: broken link")
  78. // should error for external links
  79. target, err = commit.Tree.GetTreeEntryByPath("foo/outside_repo")
  80. assert.NoError(t, err)
  81. assert.True(t, target.IsLink())
  82. _, err = target.FollowLink()
  83. assert.EqualError(t, err, "outside_repo: points outside of repo")
  84. // testing fix for short link bug
  85. target, err = commit.Tree.GetTreeEntryByPath("foo/link_short")
  86. assert.NoError(t, err)
  87. _, err = target.FollowLink()
  88. assert.EqualError(t, err, "link_short: broken link")
  89. }