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_branch_test.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestRepository_GetBranches(t *testing.T) {
  10. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  11. bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
  12. assert.NoError(t, err)
  13. defer bareRepo1.Close()
  14. branches, countAll, err := bareRepo1.GetBranchNames(0, 2)
  15. assert.NoError(t, err)
  16. assert.Len(t, branches, 2)
  17. assert.EqualValues(t, 3, countAll)
  18. assert.ElementsMatch(t, []string{"master", "branch2"}, branches)
  19. branches, countAll, err = bareRepo1.GetBranchNames(0, 0)
  20. assert.NoError(t, err)
  21. assert.Len(t, branches, 3)
  22. assert.EqualValues(t, 3, countAll)
  23. assert.ElementsMatch(t, []string{"master", "branch2", "branch1"}, branches)
  24. branches, countAll, err = bareRepo1.GetBranchNames(5, 1)
  25. assert.NoError(t, err)
  26. assert.Len(t, branches, 0)
  27. assert.EqualValues(t, 3, countAll)
  28. assert.ElementsMatch(t, []string{}, branches)
  29. }
  30. func BenchmarkRepository_GetBranches(b *testing.B) {
  31. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  32. bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
  33. if err != nil {
  34. b.Fatal(err)
  35. }
  36. defer bareRepo1.Close()
  37. for i := 0; i < b.N; i++ {
  38. _, _, err := bareRepo1.GetBranchNames(0, 0)
  39. if err != nil {
  40. b.Fatal(err)
  41. }
  42. }
  43. }
  44. func TestGetRefsBySha(t *testing.T) {
  45. bareRepo5Path := filepath.Join(testReposDir, "repo5_pulls")
  46. bareRepo5, err := OpenRepository(DefaultContext, bareRepo5Path)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. defer bareRepo5.Close()
  51. // do not exist
  52. branches, err := bareRepo5.GetRefsBySha("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", "")
  53. assert.NoError(t, err)
  54. assert.Len(t, branches, 0)
  55. // refs/pull/1/head
  56. branches, err = bareRepo5.GetRefsBySha("c83380d7056593c51a699d12b9c00627bd5743e9", PullPrefix)
  57. assert.NoError(t, err)
  58. assert.EqualValues(t, []string{"refs/pull/1/head"}, branches)
  59. branches, err = bareRepo5.GetRefsBySha("d8e0bbb45f200e67d9a784ce55bd90821af45ebd", BranchPrefix)
  60. assert.NoError(t, err)
  61. assert.EqualValues(t, []string{"refs/heads/master", "refs/heads/master-clone"}, branches)
  62. branches, err = bareRepo5.GetRefsBySha("58a4bcc53ac13e7ff76127e0fb518b5262bf09af", BranchPrefix)
  63. assert.NoError(t, err)
  64. assert.EqualValues(t, []string{"refs/heads/test-patch-1"}, branches)
  65. }
  66. func BenchmarkGetRefsBySha(b *testing.B) {
  67. bareRepo5Path := filepath.Join(testReposDir, "repo5_pulls")
  68. bareRepo5, err := OpenRepository(DefaultContext, bareRepo5Path)
  69. if err != nil {
  70. b.Fatal(err)
  71. }
  72. defer bareRepo5.Close()
  73. _, _ = bareRepo5.GetRefsBySha("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", "")
  74. _, _ = bareRepo5.GetRefsBySha("d8e0bbb45f200e67d9a784ce55bd90821af45ebd", "")
  75. _, _ = bareRepo5.GetRefsBySha("c83380d7056593c51a699d12b9c00627bd5743e9", "")
  76. _, _ = bareRepo5.GetRefsBySha("58a4bcc53ac13e7ff76127e0fb518b5262bf09af", "")
  77. }