github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/repo_commit_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_GetCommitBranches(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 // these test case are specific to the repo1_bare test repo 22 testCases := []struct { 23 CommitID string 24 ExpectedBranches []string 25 }{ 26 {"2839944139e0de9737a044f78b0e4b40d989a9e3", []string{"branch1"}}, 27 {"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", []string{"branch2"}}, 28 {"37991dec2c8e592043f47155ce4808d4580f9123", []string{"master"}}, 29 {"95bb4d39648ee7e325106df01a621c530863a653", []string{"branch1", "branch2"}}, 30 {"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", []string{"branch2", "master"}}, 31 {"master", []string{"master"}}, 32 } 33 for _, testCase := range testCases { 34 commit, err := bareRepo1.GetCommit(testCase.CommitID) 35 assert.NoError(t, err) 36 branches, err := bareRepo1.getBranches(commit, 2) 37 assert.NoError(t, err) 38 assert.Equal(t, testCase.ExpectedBranches, branches) 39 } 40 } 41 42 func TestGetTagCommitWithSignature(t *testing.T) { 43 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 44 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 45 assert.NoError(t, err) 46 defer bareRepo1.Close() 47 48 commit, err := bareRepo1.GetCommit("3ad28a9149a2864384548f3d17ed7f38014c9e8a") 49 assert.NoError(t, err) 50 assert.NotNil(t, commit) 51 assert.NotNil(t, commit.Signature) 52 // test that signature is not in message 53 assert.Equal(t, "tag", commit.CommitMessage) 54 } 55 56 func TestGetCommitWithBadCommitID(t *testing.T) { 57 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 58 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 59 assert.NoError(t, err) 60 defer bareRepo1.Close() 61 62 commit, err := bareRepo1.GetCommit("bad_branch") 63 assert.Nil(t, commit) 64 assert.Error(t, err) 65 assert.True(t, IsErrNotExist(err)) 66 } 67 68 func TestIsCommitInBranch(t *testing.T) { 69 bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") 70 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 71 assert.NoError(t, err) 72 defer bareRepo1.Close() 73 74 result, err := bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch1") 75 assert.NoError(t, err) 76 assert.True(t, result) 77 78 result, err = bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch2") 79 assert.NoError(t, err) 80 assert.False(t, result) 81 } 82 83 func TestRepository_CommitsBetweenIDs(t *testing.T) { 84 bareRepo1Path := filepath.Join(testReposDir, "repo4_commitsbetween") 85 bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path) 86 assert.NoError(t, err) 87 defer bareRepo1.Close() 88 89 cases := []struct { 90 OldID string 91 NewID string 92 ExpectedCommits int 93 }{ 94 {"fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", "78a445db1eac62fe15e624e1137965969addf344", 1}, // com1 -> com2 95 {"78a445db1eac62fe15e624e1137965969addf344", "fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", 0}, // reset HEAD~, com2 -> com1 96 {"78a445db1eac62fe15e624e1137965969addf344", "a78e5638b66ccfe7e1b4689d3d5684e42c97d7ca", 1}, // com2 -> com2_new 97 } 98 for i, c := range cases { 99 commits, err := bareRepo1.CommitsBetweenIDs(c.NewID, c.OldID) 100 assert.NoError(t, err) 101 assert.Equal(t, c.ExpectedCommits, len(commits), "case %d", i) 102 } 103 }