2020-04-05 11:50:50 +05:30
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
2019-03-27 15:03:00 +05:30
package git
import (
2021-06-07 05:14:58 +05:30
"context"
2019-03-27 15:03:00 +05:30
"os"
"path/filepath"
"testing"
"time"
2020-08-12 01:35:34 +05:30
"code.gitea.io/gitea/modules/util"
2021-11-17 18:04:35 +05:30
2019-03-27 15:03:00 +05:30
"github.com/stretchr/testify/assert"
)
2022-01-20 23:16:10 +05:30
const (
2022-01-29 18:11:44 +05:30
testReposDir = "tests/repos/"
2022-01-20 23:16:10 +05:30
)
2019-03-27 15:03:00 +05:30
2022-01-29 18:11:44 +05:30
func cloneRepo ( url , name string ) ( string , error ) {
repoDir , err := os . MkdirTemp ( "" , name )
if err != nil {
return "" , err
2019-03-27 15:03:00 +05:30
}
2022-01-29 18:11:44 +05:30
if err := Clone ( DefaultContext , url , repoDir , CloneRepoOptions {
2019-03-27 15:03:00 +05:30
Mirror : false ,
Bare : false ,
Quiet : true ,
Timeout : 5 * time . Minute ,
2022-01-29 18:11:44 +05:30
} ) ; err != nil {
_ = util . RemoveAll ( repoDir )
return "" , err
}
return repoDir , nil
2019-03-27 15:03:00 +05:30
}
func testGetCommitsInfo ( t * testing . T , repo1 * Repository ) {
// these test case are specific to the repo1 test repo
testCases := [ ] struct {
2019-07-22 17:33:15 +05:30
CommitID string
Path string
ExpectedIDs map [ string ] string
ExpectedTreeCommit string
2019-03-27 15:03:00 +05:30
} {
{ "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2" , "" , map [ string ] string {
"file1.txt" : "95bb4d39648ee7e325106df01a621c530863a653" ,
"file2.txt" : "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2" ,
2019-07-22 17:33:15 +05:30
} , "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2" } ,
2019-03-27 15:03:00 +05:30
{ "2839944139e0de9737a044f78b0e4b40d989a9e3" , "" , map [ string ] string {
"file1.txt" : "2839944139e0de9737a044f78b0e4b40d989a9e3" ,
"branch1.txt" : "9c9aef8dd84e02bc7ec12641deb4c930a7c30185" ,
2019-07-22 17:33:15 +05:30
} , "2839944139e0de9737a044f78b0e4b40d989a9e3" } ,
2019-03-27 15:03:00 +05:30
{ "5c80b0245c1c6f8343fa418ec374b13b5d4ee658" , "branch2" , map [ string ] string {
"branch2.txt" : "5c80b0245c1c6f8343fa418ec374b13b5d4ee658" ,
2019-07-22 17:33:15 +05:30
} , "5c80b0245c1c6f8343fa418ec374b13b5d4ee658" } ,
{ "feaf4ba6bc635fec442f46ddd4512416ec43c2c2" , "" , map [ string ] string {
"file1.txt" : "95bb4d39648ee7e325106df01a621c530863a653" ,
"file2.txt" : "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2" ,
"foo" : "37991dec2c8e592043f47155ce4808d4580f9123" ,
} , "feaf4ba6bc635fec442f46ddd4512416ec43c2c2" } ,
2019-03-27 15:03:00 +05:30
}
for _ , testCase := range testCases {
commit , err := repo1 . GetCommit ( testCase . CommitID )
2022-01-29 18:11:44 +05:30
if err != nil {
assert . NoError ( t , err , "Unable to get commit: %s from testcase due to error: %v" , testCase . CommitID , err )
// no point trying to do anything else for this test.
continue
}
2020-12-17 19:30:47 +05:30
assert . NotNil ( t , commit )
assert . NotNil ( t , commit . Tree )
assert . NotNil ( t , commit . Tree . repo )
2019-03-27 15:03:00 +05:30
tree , err := commit . Tree . SubTree ( testCase . Path )
2022-01-29 18:11:44 +05:30
if err != nil {
assert . NoError ( t , err , "Unable to get subtree: %s of commit: %s from testcase due to error: %v" , testCase . Path , testCase . CommitID , err )
// no point trying to do anything else for this test.
continue
}
2020-12-17 19:30:47 +05:30
assert . NotNil ( t , tree , "tree is nil for testCase CommitID %s in Path %s" , testCase . CommitID , testCase . Path )
assert . NotNil ( t , tree . repo , "repo is nil for testCase CommitID %s in Path %s" , testCase . CommitID , testCase . Path )
2019-03-27 15:03:00 +05:30
entries , err := tree . ListEntries ( )
2022-01-29 18:11:44 +05:30
if err != nil {
assert . NoError ( t , err , "Unable to get entries of subtree: %s in commit: %s from testcase due to error: %v" , testCase . Path , testCase . CommitID , err )
// no point trying to do anything else for this test.
continue
}
// FIXME: Context.TODO() - if graceful has started we should use its Shutdown context otherwise use install signals in TestMain.
commitsInfo , treeCommit , err := entries . GetCommitsInfo ( context . TODO ( ) , commit , testCase . Path , nil )
assert . NoError ( t , err , "Unable to get commit information for entries of subtree: %s in commit: %s from testcase due to error: %v" , testCase . Path , testCase . CommitID , err )
2020-12-17 19:30:47 +05:30
if err != nil {
t . FailNow ( )
}
assert . Equal ( t , testCase . ExpectedTreeCommit , treeCommit . ID . String ( ) )
2019-03-27 15:03:00 +05:30
assert . Len ( t , commitsInfo , len ( testCase . ExpectedIDs ) )
for _ , commitInfo := range commitsInfo {
2020-12-17 19:30:47 +05:30
entry := commitInfo . Entry
commit := commitInfo . Commit
2019-03-27 15:03:00 +05:30
expectedID , ok := testCase . ExpectedIDs [ entry . Name ( ) ]
if ! assert . True ( t , ok ) {
continue
}
assert . Equal ( t , expectedID , commit . ID . String ( ) )
}
}
}
func TestEntries_GetCommitsInfo ( t * testing . T ) {
bareRepo1Path := filepath . Join ( testReposDir , "repo1_bare" )
bareRepo1 , err := OpenRepository ( bareRepo1Path )
assert . NoError ( t , err )
2019-11-13 12:31:19 +05:30
defer bareRepo1 . Close ( )
2019-03-27 15:03:00 +05:30
testGetCommitsInfo ( t , bareRepo1 )
2022-01-29 18:11:44 +05:30
clonedPath , err := cloneRepo ( bareRepo1Path , "repo1_TestEntries_GetCommitsInfo" )
if err != nil {
assert . NoError ( t , err )
}
2020-08-12 01:35:34 +05:30
defer util . RemoveAll ( clonedPath )
2019-03-27 15:03:00 +05:30
clonedRepo1 , err := OpenRepository ( clonedPath )
2022-01-29 18:11:44 +05:30
if err != nil {
assert . NoError ( t , err )
}
2019-11-13 12:31:19 +05:30
defer clonedRepo1 . Close ( )
2019-03-27 15:03:00 +05:30
testGetCommitsInfo ( t , clonedRepo1 )
}
func BenchmarkEntries_GetCommitsInfo ( b * testing . B ) {
2022-01-29 18:11:44 +05:30
type benchmarkType struct {
2019-03-27 15:03:00 +05:30
url string
name string
2022-01-29 18:11:44 +05:30
}
benchmarks := [ ] benchmarkType {
2019-03-27 15:03:00 +05:30
{ url : "https://github.com/go-gitea/gitea.git" , name : "gitea" } ,
{ url : "https://github.com/ethantkoenig/manyfiles.git" , name : "manyfiles" } ,
{ url : "https://github.com/moby/moby.git" , name : "moby" } ,
{ url : "https://github.com/golang/go.git" , name : "go" } ,
{ url : "https://github.com/torvalds/linux.git" , name : "linux" } ,
}
2022-01-29 18:11:44 +05:30
doBenchmark := func ( benchmark benchmarkType ) {
2019-03-27 15:03:00 +05:30
var commit * Commit
var entries Entries
2019-11-13 12:31:19 +05:30
var repo * Repository
2022-01-29 18:11:44 +05:30
repoPath , err := cloneRepo ( benchmark . url , benchmark . name )
if err != nil {
2019-03-27 15:03:00 +05:30
b . Fatal ( err )
2022-01-29 18:11:44 +05:30
}
defer util . RemoveAll ( repoPath )
if repo , err = OpenRepository ( repoPath ) ; err != nil {
2019-03-27 15:03:00 +05:30
b . Fatal ( err )
2022-01-29 18:11:44 +05:30
}
defer repo . Close ( )
if commit , err = repo . GetBranchCommit ( "master" ) ; err != nil {
2019-03-27 15:03:00 +05:30
b . Fatal ( err )
} else if entries , err = commit . Tree . ListEntries ( ) ; err != nil {
b . Fatal ( err )
}
entries . Sort ( )
b . ResetTimer ( )
b . Run ( benchmark . name , func ( b * testing . B ) {
for i := 0 ; i < b . N ; i ++ {
2021-06-07 05:14:58 +05:30
_ , _ , err := entries . GetCommitsInfo ( context . Background ( ) , commit , "" , nil )
2019-03-27 15:03:00 +05:30
if err != nil {
b . Fatal ( err )
}
}
} )
2022-01-29 18:11:44 +05:30
}
for _ , benchmark := range benchmarks {
doBenchmark ( benchmark )
2019-03-27 15:03:00 +05:30
}
}