code.gitea.io/gitea@v1.22.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 "github.com/stretchr/testify/require" 12 ) 13 14 func TestRepository_GetBranches(t *testing.T) { 15 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 16 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 17 assert.NoError(t, err) 18 defer bareRepo1.Close() 19 20 branches, countAll, err := bareRepo1.GetBranchNames(0, 2) 21 22 assert.NoError(t, err) 23 assert.Len(t, branches, 2) 24 assert.EqualValues(t, 3, countAll) 25 assert.ElementsMatch(t, []string{"master", "branch2"}, branches) 26 27 branches, countAll, err = bareRepo1.GetBranchNames(0, 0) 28 29 assert.NoError(t, err) 30 assert.Len(t, branches, 3) 31 assert.EqualValues(t, 3, countAll) 32 assert.ElementsMatch(t, []string{"master", "branch2", "branch1"}, branches) 33 34 branches, countAll, err = bareRepo1.GetBranchNames(5, 1) 35 36 assert.NoError(t, err) 37 assert.Len(t, branches, 0) 38 assert.EqualValues(t, 3, countAll) 39 assert.ElementsMatch(t, []string{}, branches) 40 } 41 42 func BenchmarkRepository_GetBranches(b *testing.B) { 43 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 44 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 45 if err != nil { 46 b.Fatal(err) 47 } 48 defer bareRepo1.Close() 49 50 for i := 0; i < b.N; i++ { 51 _, _, err := bareRepo1.GetBranchNames(0, 0) 52 if err != nil { 53 b.Fatal(err) 54 } 55 } 56 } 57 58 func TestGetRefsBySha(t *testing.T) { 59 bareRepo5Path := filepath.Join(testReposDir, "repo5_pulls") 60 bareRepo5, err := OpenRepository(DefaultContext, bareRepo5Path) 61 if err != nil { 62 t.Fatal(err) 63 } 64 defer bareRepo5.Close() 65 66 // do not exist 67 branches, err := bareRepo5.GetRefsBySha("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", "") 68 assert.NoError(t, err) 69 assert.Len(t, branches, 0) 70 71 // refs/pull/1/head 72 branches, err = bareRepo5.GetRefsBySha("c83380d7056593c51a699d12b9c00627bd5743e9", PullPrefix) 73 assert.NoError(t, err) 74 assert.EqualValues(t, []string{"refs/pull/1/head"}, branches) 75 76 branches, err = bareRepo5.GetRefsBySha("d8e0bbb45f200e67d9a784ce55bd90821af45ebd", BranchPrefix) 77 assert.NoError(t, err) 78 assert.EqualValues(t, []string{"refs/heads/master", "refs/heads/master-clone"}, branches) 79 80 branches, err = bareRepo5.GetRefsBySha("58a4bcc53ac13e7ff76127e0fb518b5262bf09af", BranchPrefix) 81 assert.NoError(t, err) 82 assert.EqualValues(t, []string{"refs/heads/test-patch-1"}, branches) 83 } 84 85 func BenchmarkGetRefsBySha(b *testing.B) { 86 bareRepo5Path := filepath.Join(testReposDir, "repo5_pulls") 87 bareRepo5, err := OpenRepository(DefaultContext, bareRepo5Path) 88 if err != nil { 89 b.Fatal(err) 90 } 91 defer bareRepo5.Close() 92 93 _, _ = bareRepo5.GetRefsBySha("8006ff9adbf0cb94da7dad9e537e53817f9fa5c0", "") 94 _, _ = bareRepo5.GetRefsBySha("d8e0bbb45f200e67d9a784ce55bd90821af45ebd", "") 95 _, _ = bareRepo5.GetRefsBySha("c83380d7056593c51a699d12b9c00627bd5743e9", "") 96 _, _ = bareRepo5.GetRefsBySha("58a4bcc53ac13e7ff76127e0fb518b5262bf09af", "") 97 } 98 99 func TestRepository_IsObjectExist(t *testing.T) { 100 repo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo1_bare")) 101 require.NoError(t, err) 102 defer repo.Close() 103 104 // FIXME: Inconsistent behavior between gogit and nogogit editions 105 // See the comment of IsObjectExist in gogit edition for more details. 106 supportShortHash := !isGogit 107 108 tests := []struct { 109 name string 110 arg string 111 want bool 112 }{ 113 { 114 name: "empty", 115 arg: "", 116 want: false, 117 }, 118 { 119 name: "branch", 120 arg: "master", 121 want: false, 122 }, 123 { 124 name: "commit hash", 125 arg: "ce064814f4a0d337b333e646ece456cd39fab612", 126 want: true, 127 }, 128 { 129 name: "short commit hash", 130 arg: "ce06481", 131 want: supportShortHash, 132 }, 133 { 134 name: "blob hash", 135 arg: "153f451b9ee7fa1da317ab17a127e9fd9d384310", 136 want: true, 137 }, 138 { 139 name: "short blob hash", 140 arg: "153f451", 141 want: supportShortHash, 142 }, 143 } 144 for _, tt := range tests { 145 t.Run(tt.name, func(t *testing.T) { 146 assert.Equal(t, tt.want, repo.IsObjectExist(tt.arg)) 147 }) 148 } 149 } 150 151 func TestRepository_IsReferenceExist(t *testing.T) { 152 repo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo1_bare")) 153 require.NoError(t, err) 154 defer repo.Close() 155 156 // FIXME: Inconsistent behavior between gogit and nogogit editions 157 // See the comment of IsReferenceExist in gogit edition for more details. 158 supportBlobHash := !isGogit 159 160 tests := []struct { 161 name string 162 arg string 163 want bool 164 }{ 165 { 166 name: "empty", 167 arg: "", 168 want: false, 169 }, 170 { 171 name: "branch", 172 arg: "master", 173 want: true, 174 }, 175 { 176 name: "commit hash", 177 arg: "ce064814f4a0d337b333e646ece456cd39fab612", 178 want: true, 179 }, 180 { 181 name: "short commit hash", 182 arg: "ce06481", 183 want: true, 184 }, 185 { 186 name: "blob hash", 187 arg: "153f451b9ee7fa1da317ab17a127e9fd9d384310", 188 want: supportBlobHash, 189 }, 190 { 191 name: "short blob hash", 192 arg: "153f451", 193 want: supportBlobHash, 194 }, 195 } 196 for _, tt := range tests { 197 t.Run(tt.name, func(t *testing.T) { 198 assert.Equal(t, tt.want, repo.IsReferenceExist(tt.arg)) 199 }) 200 } 201 }