2019-03-27 10:33:00 +01:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-03-27 10:33:00 +01:00
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 21:41:10 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-03-27 10:33:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepository_GetCommitBranches(t *testing.T) {
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
2022-03-29 21:13:41 +02:00
|
|
|
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-11-13 08:01:19 +01:00
|
|
|
defer bareRepo1.Close()
|
2019-03-27 10:33:00 +01:00
|
|
|
|
|
|
|
// these test case are specific to the repo1_bare test repo
|
|
|
|
testCases := []struct {
|
|
|
|
CommitID string
|
|
|
|
ExpectedBranches []string
|
|
|
|
}{
|
|
|
|
{"2839944139e0de9737a044f78b0e4b40d989a9e3", []string{"branch1"}},
|
|
|
|
{"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", []string{"branch2"}},
|
|
|
|
{"37991dec2c8e592043f47155ce4808d4580f9123", []string{"master"}},
|
|
|
|
{"95bb4d39648ee7e325106df01a621c530863a653", []string{"branch1", "branch2"}},
|
|
|
|
{"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", []string{"branch2", "master"}},
|
|
|
|
{"master", []string{"master"}},
|
|
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
commit, err := bareRepo1.GetCommit(testCase.CommitID)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-03-27 10:33:00 +01:00
|
|
|
branches, err := bareRepo1.getBranches(commit, 2)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-03-27 10:33:00 +01:00
|
|
|
assert.Equal(t, testCase.ExpectedBranches, branches)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTagCommitWithSignature(t *testing.T) {
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
2022-03-29 21:13:41 +02:00
|
|
|
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-11-13 08:01:19 +01:00
|
|
|
defer bareRepo1.Close()
|
2019-03-27 10:33:00 +01:00
|
|
|
|
2023-03-02 06:32:21 +01:00
|
|
|
// both the tag and the commit are signed here, this validates only the commit signature
|
|
|
|
commit, err := bareRepo1.GetCommit("28b55526e7100924d864dd89e35c1ea62e7a5a32")
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-03-27 10:33:00 +01:00
|
|
|
assert.NotNil(t, commit)
|
|
|
|
assert.NotNil(t, commit.Signature)
|
|
|
|
// test that signature is not in message
|
2023-03-02 06:32:21 +01:00
|
|
|
assert.Equal(t, "signed-commit\n", commit.CommitMessage)
|
2019-03-27 10:33:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCommitWithBadCommitID(t *testing.T) {
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
2022-03-29 21:13:41 +02:00
|
|
|
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2019-11-13 08:01:19 +01:00
|
|
|
defer bareRepo1.Close()
|
2019-07-23 21:28:43 +02:00
|
|
|
|
2019-03-27 10:33:00 +01:00
|
|
|
commit, err := bareRepo1.GetCommit("bad_branch")
|
|
|
|
assert.Nil(t, commit)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.Error(t, err)
|
2019-08-05 22:39:39 +02:00
|
|
|
assert.True(t, IsErrNotExist(err))
|
2019-03-27 10:33:00 +01:00
|
|
|
}
|
2021-03-04 04:41:23 +01:00
|
|
|
|
|
|
|
func TestIsCommitInBranch(t *testing.T) {
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
2022-03-29 21:13:41 +02:00
|
|
|
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2021-03-04 04:41:23 +01:00
|
|
|
defer bareRepo1.Close()
|
|
|
|
|
|
|
|
result, err := bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch1")
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-07 07:27:09 +02:00
|
|
|
assert.True(t, result)
|
2021-03-04 04:41:23 +01:00
|
|
|
|
|
|
|
result, err = bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch2")
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-07 07:27:09 +02:00
|
|
|
assert.False(t, result)
|
2021-03-04 04:41:23 +01:00
|
|
|
}
|
2021-06-30 19:49:06 +02:00
|
|
|
|
|
|
|
func TestRepository_CommitsBetweenIDs(t *testing.T) {
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo4_commitsbetween")
|
2022-03-29 21:13:41 +02:00
|
|
|
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2021-06-30 19:49:06 +02:00
|
|
|
defer bareRepo1.Close()
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
OldID string
|
|
|
|
NewID string
|
|
|
|
ExpectedCommits int
|
|
|
|
}{
|
2022-01-20 18:46:10 +01:00
|
|
|
{"fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", "78a445db1eac62fe15e624e1137965969addf344", 1}, // com1 -> com2
|
|
|
|
{"78a445db1eac62fe15e624e1137965969addf344", "fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", 0}, // reset HEAD~, com2 -> com1
|
|
|
|
{"78a445db1eac62fe15e624e1137965969addf344", "a78e5638b66ccfe7e1b4689d3d5684e42c97d7ca", 1}, // com2 -> com2_new
|
2021-06-30 19:49:06 +02:00
|
|
|
}
|
|
|
|
for i, c := range cases {
|
|
|
|
commits, err := bareRepo1.CommitsBetweenIDs(c.NewID, c.OldID)
|
2024-07-30 21:41:10 +02:00
|
|
|
require.NoError(t, err)
|
2023-04-22 23:56:27 +02:00
|
|
|
assert.Len(t, commits, c.ExpectedCommits, "case %d", i)
|
2021-06-30 19:49:06 +02:00
|
|
|
}
|
|
|
|
}
|
2024-10-21 15:48:22 +02:00
|
|
|
|
|
|
|
func TestGetTagCommit(t *testing.T) {
|
|
|
|
t.Setenv("GIT_COMMITTER_DATE", "2006-01-01 13:37")
|
|
|
|
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
|
|
|
|
|
|
|
|
clonedPath, err := cloneRepo(t, bareRepo1Path)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
bareRepo1, err := openRepositoryWithDefaultContext(clonedPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer bareRepo1.Close()
|
|
|
|
|
|
|
|
lTagCommitID := "6fbd69e9823458e6c4a2fc5c0f6bc022b2f2acd1"
|
|
|
|
lTagName := "lightweightTag"
|
|
|
|
bareRepo1.CreateTag(lTagName, lTagCommitID)
|
|
|
|
|
|
|
|
aTagCommitID := "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0"
|
|
|
|
aTagName := "annotatedTag"
|
|
|
|
aTagMessage := "my annotated message"
|
|
|
|
bareRepo1.CreateAnnotatedTag(aTagName, aTagMessage, aTagCommitID)
|
|
|
|
|
|
|
|
aTagID, err := bareRepo1.GetTagCommitID(aTagName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotEqualValues(t, aTagCommitID, aTagID)
|
|
|
|
|
|
|
|
lTagID, err := bareRepo1.GetTagCommitID(lTagName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, lTagCommitID, lTagID)
|
|
|
|
|
|
|
|
aTag, err := bareRepo1.GetTagCommit(aTagName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, aTagCommitID, aTag.ID.String())
|
|
|
|
|
|
|
|
lTag, err := bareRepo1.GetTagCommit(lTagName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, lTagCommitID, lTag.ID.String())
|
|
|
|
}
|