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

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package git
     5  
     6  import (
     7  	"context"
     8  	"path/filepath"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  const (
    16  	testReposDir = "tests/repos/"
    17  )
    18  
    19  func cloneRepo(tb testing.TB, url string) (string, error) {
    20  	repoDir := tb.TempDir()
    21  	if err := Clone(DefaultContext, url, repoDir, CloneRepoOptions{
    22  		Mirror:  false,
    23  		Bare:    false,
    24  		Quiet:   true,
    25  		Timeout: 5 * time.Minute,
    26  	}); err != nil {
    27  		return "", err
    28  	}
    29  	return repoDir, nil
    30  }
    31  
    32  func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
    33  	// these test case are specific to the repo1 test repo
    34  	testCases := []struct {
    35  		CommitID           string
    36  		Path               string
    37  		ExpectedIDs        map[string]string
    38  		ExpectedTreeCommit string
    39  	}{
    40  		{"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", "", map[string]string{
    41  			"file1.txt": "95bb4d39648ee7e325106df01a621c530863a653",
    42  			"file2.txt": "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
    43  		}, "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2"},
    44  		{"2839944139e0de9737a044f78b0e4b40d989a9e3", "", map[string]string{
    45  			"file1.txt":   "2839944139e0de9737a044f78b0e4b40d989a9e3",
    46  			"branch1.txt": "9c9aef8dd84e02bc7ec12641deb4c930a7c30185",
    47  		}, "2839944139e0de9737a044f78b0e4b40d989a9e3"},
    48  		{"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", "branch2", map[string]string{
    49  			"branch2.txt": "5c80b0245c1c6f8343fa418ec374b13b5d4ee658",
    50  		}, "5c80b0245c1c6f8343fa418ec374b13b5d4ee658"},
    51  		{"feaf4ba6bc635fec442f46ddd4512416ec43c2c2", "", map[string]string{
    52  			"file1.txt": "95bb4d39648ee7e325106df01a621c530863a653",
    53  			"file2.txt": "8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2",
    54  			"foo":       "37991dec2c8e592043f47155ce4808d4580f9123",
    55  		}, "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"},
    56  	}
    57  	for _, testCase := range testCases {
    58  		commit, err := repo1.GetCommit(testCase.CommitID)
    59  		if err != nil {
    60  			assert.NoError(t, err, "Unable to get commit: %s from testcase due to error: %v", testCase.CommitID, err)
    61  			// no point trying to do anything else for this test.
    62  			continue
    63  		}
    64  		assert.NotNil(t, commit)
    65  		assert.NotNil(t, commit.Tree)
    66  		assert.NotNil(t, commit.Tree.repo)
    67  
    68  		tree, err := commit.Tree.SubTree(testCase.Path)
    69  		if err != nil {
    70  			assert.NoError(t, err, "Unable to get subtree: %s of commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err)
    71  			// no point trying to do anything else for this test.
    72  			continue
    73  		}
    74  
    75  		assert.NotNil(t, tree, "tree is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path)
    76  		assert.NotNil(t, tree.repo, "repo is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path)
    77  
    78  		entries, err := tree.ListEntries()
    79  		if err != nil {
    80  			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)
    81  			// no point trying to do anything else for this test.
    82  			continue
    83  		}
    84  
    85  		// FIXME: Context.TODO() - if graceful has started we should use its Shutdown context otherwise use install signals in TestMain.
    86  		commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.TODO(), commit, testCase.Path)
    87  		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)
    88  		if err != nil {
    89  			t.FailNow()
    90  		}
    91  		assert.Equal(t, testCase.ExpectedTreeCommit, treeCommit.ID.String())
    92  		assert.Len(t, commitsInfo, len(testCase.ExpectedIDs))
    93  		for _, commitInfo := range commitsInfo {
    94  			entry := commitInfo.Entry
    95  			commit := commitInfo.Commit
    96  			expectedID, ok := testCase.ExpectedIDs[entry.Name()]
    97  			if !assert.True(t, ok) {
    98  				continue
    99  			}
   100  			assert.Equal(t, expectedID, commit.ID.String())
   101  		}
   102  	}
   103  }
   104  
   105  func TestEntries_GetCommitsInfo(t *testing.T) {
   106  	bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
   107  	bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
   108  	assert.NoError(t, err)
   109  	defer bareRepo1.Close()
   110  
   111  	testGetCommitsInfo(t, bareRepo1)
   112  
   113  	clonedPath, err := cloneRepo(t, bareRepo1Path)
   114  	if err != nil {
   115  		assert.NoError(t, err)
   116  	}
   117  	clonedRepo1, err := openRepositoryWithDefaultContext(clonedPath)
   118  	if err != nil {
   119  		assert.NoError(t, err)
   120  	}
   121  	defer clonedRepo1.Close()
   122  
   123  	testGetCommitsInfo(t, clonedRepo1)
   124  }
   125  
   126  func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
   127  	type benchmarkType struct {
   128  		url  string
   129  		name string
   130  	}
   131  
   132  	benchmarks := []benchmarkType{
   133  		{url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
   134  		{url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
   135  		{url: "https://github.com/moby/moby.git", name: "moby"},
   136  		{url: "https://github.com/golang/go.git", name: "go"},
   137  		{url: "https://github.com/torvalds/linux.git", name: "linux"},
   138  	}
   139  
   140  	doBenchmark := func(benchmark benchmarkType) {
   141  		var commit *Commit
   142  		var entries Entries
   143  		var repo *Repository
   144  		repoPath, err := cloneRepo(b, benchmark.url)
   145  		if err != nil {
   146  			b.Fatal(err)
   147  		}
   148  
   149  		if repo, err = openRepositoryWithDefaultContext(repoPath); err != nil {
   150  			b.Fatal(err)
   151  		}
   152  		defer repo.Close()
   153  
   154  		if commit, err = repo.GetBranchCommit("master"); err != nil {
   155  			b.Fatal(err)
   156  		} else if entries, err = commit.Tree.ListEntries(); err != nil {
   157  			b.Fatal(err)
   158  		}
   159  		entries.Sort()
   160  		b.ResetTimer()
   161  		b.Run(benchmark.name, func(b *testing.B) {
   162  			for i := 0; i < b.N; i++ {
   163  				_, _, err := entries.GetCommitsInfo(context.Background(), commit, "")
   164  				if err != nil {
   165  					b.Fatal(err)
   166  				}
   167  			}
   168  		})
   169  	}
   170  
   171  	for _, benchmark := range benchmarks {
   172  		doBenchmark(benchmark)
   173  	}
   174  }