You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_commit_test.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestRepository_GetCommitBranches(t *testing.T) {
  10. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  11. bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
  12. assert.NoError(t, err)
  13. defer bareRepo1.Close()
  14. // these test case are specific to the repo1_bare test repo
  15. testCases := []struct {
  16. CommitID string
  17. ExpectedBranches []string
  18. }{
  19. {"2839944139e0de9737a044f78b0e4b40d989a9e3", []string{"branch1"}},
  20. {"5c80b0245c1c6f8343fa418ec374b13b5d4ee658", []string{"branch2"}},
  21. {"37991dec2c8e592043f47155ce4808d4580f9123", []string{"master"}},
  22. {"95bb4d39648ee7e325106df01a621c530863a653", []string{"branch1", "branch2"}},
  23. {"8d92fc957a4d7cfd98bc375f0b7bb189a0d6c9f2", []string{"branch2", "master"}},
  24. {"master", []string{"master"}},
  25. }
  26. for _, testCase := range testCases {
  27. commit, err := bareRepo1.GetCommit(testCase.CommitID)
  28. assert.NoError(t, err)
  29. branches, err := bareRepo1.getBranches(commit, 2)
  30. assert.NoError(t, err)
  31. assert.Equal(t, testCase.ExpectedBranches, branches)
  32. }
  33. }
  34. func TestGetTagCommitWithSignature(t *testing.T) {
  35. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  36. bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
  37. assert.NoError(t, err)
  38. defer bareRepo1.Close()
  39. // both the tag and the commit are signed here, this validates only the commit signature
  40. commit, err := bareRepo1.GetCommit("28b55526e7100924d864dd89e35c1ea62e7a5a32")
  41. assert.NoError(t, err)
  42. assert.NotNil(t, commit)
  43. assert.NotNil(t, commit.Signature)
  44. // test that signature is not in message
  45. assert.Equal(t, "signed-commit\n", commit.CommitMessage)
  46. }
  47. func TestGetCommitWithBadCommitID(t *testing.T) {
  48. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  49. bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
  50. assert.NoError(t, err)
  51. defer bareRepo1.Close()
  52. commit, err := bareRepo1.GetCommit("bad_branch")
  53. assert.Nil(t, commit)
  54. assert.Error(t, err)
  55. assert.True(t, IsErrNotExist(err))
  56. }
  57. func TestIsCommitInBranch(t *testing.T) {
  58. bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
  59. bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
  60. assert.NoError(t, err)
  61. defer bareRepo1.Close()
  62. result, err := bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch1")
  63. assert.NoError(t, err)
  64. assert.True(t, result)
  65. result, err = bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch2")
  66. assert.NoError(t, err)
  67. assert.False(t, result)
  68. }
  69. func TestRepository_CommitsBetweenIDs(t *testing.T) {
  70. bareRepo1Path := filepath.Join(testReposDir, "repo4_commitsbetween")
  71. bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
  72. assert.NoError(t, err)
  73. defer bareRepo1.Close()
  74. cases := []struct {
  75. OldID string
  76. NewID string
  77. ExpectedCommits int
  78. }{
  79. {"fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", "78a445db1eac62fe15e624e1137965969addf344", 1}, // com1 -> com2
  80. {"78a445db1eac62fe15e624e1137965969addf344", "fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", 0}, // reset HEAD~, com2 -> com1
  81. {"78a445db1eac62fe15e624e1137965969addf344", "a78e5638b66ccfe7e1b4689d3d5684e42c97d7ca", 1}, // com2 -> com2_new
  82. }
  83. for i, c := range cases {
  84. commits, err := bareRepo1.CommitsBetweenIDs(c.NewID, c.OldID)
  85. assert.NoError(t, err)
  86. assert.Len(t, commits, c.ExpectedCommits, "case %d", i)
  87. }
  88. }