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.

branch_test.go 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git_test
  4. import (
  5. "testing"
  6. "code.gitea.io/gitea/models/db"
  7. git_model "code.gitea.io/gitea/models/git"
  8. issues_model "code.gitea.io/gitea/models/issues"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. "code.gitea.io/gitea/models/unittest"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/util"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestAddDeletedBranch(t *testing.T) {
  16. assert.NoError(t, unittest.PrepareTestDatabase())
  17. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  18. firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
  19. assert.True(t, firstBranch.IsDeleted)
  20. assert.NoError(t, git_model.AddDeletedBranch(db.DefaultContext, repo.ID, firstBranch.Name, firstBranch.DeletedByID))
  21. assert.NoError(t, git_model.AddDeletedBranch(db.DefaultContext, repo.ID, "branch2", int64(1)))
  22. secondBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo.ID, Name: "branch2"})
  23. assert.True(t, secondBranch.IsDeleted)
  24. commit := &git.Commit{
  25. ID: git.MustIDFromString(secondBranch.CommitID),
  26. CommitMessage: secondBranch.CommitMessage,
  27. Committer: &git.Signature{
  28. When: secondBranch.CommitTime.AsLocalTime(),
  29. },
  30. }
  31. _, err := git_model.UpdateBranch(db.DefaultContext, repo.ID, secondBranch.PusherID, secondBranch.Name, commit)
  32. assert.NoError(t, err)
  33. }
  34. func TestGetDeletedBranches(t *testing.T) {
  35. assert.NoError(t, unittest.PrepareTestDatabase())
  36. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  37. branches, err := git_model.FindBranches(db.DefaultContext, git_model.FindBranchOptions{
  38. ListOptions: db.ListOptions{
  39. ListAll: true,
  40. },
  41. RepoID: repo.ID,
  42. IsDeletedBranch: util.OptionalBoolTrue,
  43. })
  44. assert.NoError(t, err)
  45. assert.Len(t, branches, 2)
  46. }
  47. func TestGetDeletedBranch(t *testing.T) {
  48. assert.NoError(t, unittest.PrepareTestDatabase())
  49. firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
  50. assert.NotNil(t, getDeletedBranch(t, firstBranch))
  51. }
  52. func TestDeletedBranchLoadUser(t *testing.T) {
  53. assert.NoError(t, unittest.PrepareTestDatabase())
  54. firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
  55. secondBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 2})
  56. branch := getDeletedBranch(t, firstBranch)
  57. assert.Nil(t, branch.DeletedBy)
  58. branch.LoadDeletedBy(db.DefaultContext)
  59. assert.NotNil(t, branch.DeletedBy)
  60. assert.Equal(t, "user1", branch.DeletedBy.Name)
  61. branch = getDeletedBranch(t, secondBranch)
  62. assert.Nil(t, branch.DeletedBy)
  63. branch.LoadDeletedBy(db.DefaultContext)
  64. assert.NotNil(t, branch.DeletedBy)
  65. assert.Equal(t, "Ghost", branch.DeletedBy.Name)
  66. }
  67. func TestRemoveDeletedBranch(t *testing.T) {
  68. assert.NoError(t, unittest.PrepareTestDatabase())
  69. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  70. firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
  71. err := git_model.RemoveDeletedBranchByID(db.DefaultContext, repo.ID, 1)
  72. assert.NoError(t, err)
  73. unittest.AssertNotExistsBean(t, firstBranch)
  74. unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 2})
  75. }
  76. func getDeletedBranch(t *testing.T, branch *git_model.Branch) *git_model.Branch {
  77. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  78. deletedBranch, err := git_model.GetDeletedBranchByID(db.DefaultContext, repo.ID, branch.ID)
  79. assert.NoError(t, err)
  80. assert.Equal(t, branch.ID, deletedBranch.ID)
  81. assert.Equal(t, branch.Name, deletedBranch.Name)
  82. assert.Equal(t, branch.CommitID, deletedBranch.CommitID)
  83. assert.Equal(t, branch.DeletedByID, deletedBranch.DeletedByID)
  84. return deletedBranch
  85. }
  86. func TestFindRenamedBranch(t *testing.T) {
  87. assert.NoError(t, unittest.PrepareTestDatabase())
  88. branch, exist, err := git_model.FindRenamedBranch(db.DefaultContext, 1, "dev")
  89. assert.NoError(t, err)
  90. assert.True(t, exist)
  91. assert.Equal(t, "master", branch.To)
  92. _, exist, err = git_model.FindRenamedBranch(db.DefaultContext, 1, "unknow")
  93. assert.NoError(t, err)
  94. assert.False(t, exist)
  95. }
  96. func TestRenameBranch(t *testing.T) {
  97. assert.NoError(t, unittest.PrepareTestDatabase())
  98. repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  99. _isDefault := false
  100. ctx, committer, err := db.TxContext(db.DefaultContext)
  101. defer committer.Close()
  102. assert.NoError(t, err)
  103. assert.NoError(t, git_model.UpdateProtectBranch(ctx, repo1, &git_model.ProtectedBranch{
  104. RepoID: repo1.ID,
  105. RuleName: "master",
  106. }, git_model.WhitelistOptions{}))
  107. assert.NoError(t, committer.Commit())
  108. assert.NoError(t, git_model.RenameBranch(db.DefaultContext, repo1, "master", "main", func(isDefault bool) error {
  109. _isDefault = isDefault
  110. return nil
  111. }))
  112. assert.True(t, _isDefault)
  113. repo1 = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  114. assert.Equal(t, "main", repo1.DefaultBranch)
  115. pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}) // merged
  116. assert.Equal(t, "master", pull.BaseBranch)
  117. pull = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}) // open
  118. assert.Equal(t, "main", pull.BaseBranch)
  119. renamedBranch := unittest.AssertExistsAndLoadBean(t, &git_model.RenamedBranch{ID: 2})
  120. assert.Equal(t, "master", renamedBranch.From)
  121. assert.Equal(t, "main", renamedBranch.To)
  122. assert.Equal(t, int64(1), renamedBranch.RepoID)
  123. unittest.AssertExistsAndLoadBean(t, &git_model.ProtectedBranch{
  124. RepoID: repo1.ID,
  125. RuleName: "main",
  126. })
  127. }
  128. func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
  129. assert.NoError(t, unittest.PrepareTestDatabase())
  130. // Get deletedBranch with ID of 1 on repo with ID 2.
  131. // This should return a nil branch as this deleted branch
  132. // is actually on repo with ID 1.
  133. repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
  134. deletedBranch, err := git_model.GetDeletedBranchByID(db.DefaultContext, repo2.ID, 1)
  135. // Expect error, and the returned branch is nil.
  136. assert.Error(t, err)
  137. assert.Nil(t, deletedBranch)
  138. // Now get the deletedBranch with ID of 1 on repo with ID 1.
  139. // This should return the deletedBranch.
  140. repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  141. deletedBranch, err = git_model.GetDeletedBranchByID(db.DefaultContext, repo1.ID, 1)
  142. // Expect no error, and the returned branch to be not nil.
  143. assert.NoError(t, err)
  144. assert.NotNil(t, deletedBranch)
  145. }