code.gitea.io/gitea@v1.19.3/modules/git/repo_branch_test.go (about)

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