github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/repo_branch_test.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea 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 6 package git 7 8 import ( 9 "path/filepath" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestRepository_GetBranches(t *testing.T) { 16 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 17 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 18 assert.NoError(t, err) 19 defer bareRepo1.Close() 20 21 branches, countAll, err := bareRepo1.GetBranchNames(0, 2) 22 23 assert.NoError(t, err) 24 assert.Len(t, branches, 2) 25 assert.EqualValues(t, 3, countAll) 26 assert.ElementsMatch(t, []string{"branch1", "branch2"}, branches) 27 28 branches, countAll, err = bareRepo1.GetBranchNames(0, 0) 29 30 assert.NoError(t, err) 31 assert.Len(t, branches, 3) 32 assert.EqualValues(t, 3, countAll) 33 assert.ElementsMatch(t, []string{"branch1", "branch2", "master"}, branches) 34 35 branches, countAll, err = bareRepo1.GetBranchNames(5, 1) 36 37 assert.NoError(t, err) 38 assert.Len(t, branches, 0) 39 assert.EqualValues(t, 3, countAll) 40 assert.ElementsMatch(t, []string{}, branches) 41 } 42 43 func BenchmarkRepository_GetBranches(b *testing.B) { 44 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 45 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 46 if err != nil { 47 b.Fatal(err) 48 } 49 defer bareRepo1.Close() 50 51 for i := 0; i < b.N; i++ { 52 _, _, err := bareRepo1.GetBranchNames(0, 0) 53 if err != nil { 54 b.Fatal(err) 55 } 56 } 57 } 58 59 func TestGetRefsBySha(t *testing.T) { 60 bareRepo5Path := filepath.Join(testReposDir, "repo5_pulls") 61 bareRepo5, err := OpenRepository(DefaultContext, bareRepo5Path) 62 if err != nil { 63 t.Fatal(err) 64 } 65 defer bareRepo5.Close() 66 67 // do not exist 68 branches, err := bareRepo5.GetRefsBySha("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", "") 69 assert.NoError(t, err) 70 assert.Len(t, branches, 0) 71 72 // refs/pull/1/head 73 branches, err = bareRepo5.GetRefsBySha("c83380d7056593c51a699d12b9c00627bd5743e9", PullPrefix) 74 assert.NoError(t, err) 75 assert.EqualValues(t, []string{"refs/pull/1/head"}, branches) 76 77 branches, err = bareRepo5.GetRefsBySha("d8e0bbb45f200e67d9a784ce55bd90821af45ebd", BranchPrefix) 78 assert.NoError(t, err) 79 assert.EqualValues(t, []string{"refs/heads/master", "refs/heads/master-clone"}, branches) 80 81 branches, err = bareRepo5.GetRefsBySha("58a4bcc53ac13e7ff76127e0fb518b5262bf09af", BranchPrefix) 82 assert.NoError(t, err) 83 assert.EqualValues(t, []string{"refs/heads/test-patch-1"}, branches) 84 } 85 86 func BenchmarkGetRefsBySha(b *testing.B) { 87 bareRepo5Path := filepath.Join(testReposDir, "repo5_pulls") 88 bareRepo5, err := OpenRepository(DefaultContext, bareRepo5Path) 89 if err != nil { 90 b.Fatal(err) 91 } 92 defer bareRepo5.Close() 93 94 _, _ = bareRepo5.GetRefsBySha("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", "") 95 _, _ = bareRepo5.GetRefsBySha("d8e0bbb45f200e67d9a784ce55bd90821af45ebd", "") 96 _, _ = bareRepo5.GetRefsBySha("c83380d7056593c51a699d12b9c00627bd5743e9", "") 97 _, _ = bareRepo5.GetRefsBySha("58a4bcc53ac13e7ff76127e0fb518b5262bf09af", "") 98 }