2017-10-26 03:49:16 +03:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-10-26 03:49:16 +03:00
|
|
|
|
2022-06-12 18:51:54 +03:00
|
|
|
package git_test
|
2017-10-26 03:49:16 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-04-28 14:48:48 +03:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 18:51:54 +03:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-06-13 12:37:59 +03:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-10 04:27:50 +03:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 17:36:47 +03:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2023-06-29 13:03:20 +03:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-11-17 15:34:35 +03:00
|
|
|
|
2017-10-26 03:49:16 +03:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddDeletedBranch(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 05:22:25 +03:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2023-06-29 13:03:20 +03:00
|
|
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
|
2017-11-04 16:31:59 +03:00
|
|
|
|
2023-06-29 13:03:20 +03:00
|
|
|
assert.True(t, firstBranch.IsDeleted)
|
|
|
|
assert.NoError(t, git_model.AddDeletedBranch(db.DefaultContext, repo.ID, firstBranch.Name, firstBranch.DeletedByID))
|
|
|
|
assert.NoError(t, git_model.AddDeletedBranch(db.DefaultContext, repo.ID, "branch2", int64(1)))
|
|
|
|
|
|
|
|
secondBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo.ID, Name: "branch2"})
|
|
|
|
assert.True(t, secondBranch.IsDeleted)
|
|
|
|
|
|
|
|
err := git_model.UpdateBranch(db.DefaultContext, repo.ID, secondBranch.Name, secondBranch.CommitID, secondBranch.CommitMessage, secondBranch.PusherID, secondBranch.CommitTime.AsLocalTime())
|
|
|
|
assert.NoError(t, err)
|
2017-10-26 03:49:16 +03:00
|
|
|
}
|
2017-11-04 16:31:59 +03:00
|
|
|
|
2017-10-26 03:49:16 +03:00
|
|
|
func TestGetDeletedBranches(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 05:22:25 +03:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2017-10-26 03:49:16 +03:00
|
|
|
|
2023-06-29 13:03:20 +03:00
|
|
|
branches, err := git_model.FindBranches(db.DefaultContext, git_model.FindBranchOptions{
|
|
|
|
ListOptions: db.ListOptions{
|
|
|
|
ListAll: true,
|
|
|
|
},
|
|
|
|
RepoID: repo.ID,
|
|
|
|
IsDeletedBranch: util.OptionalBoolTrue,
|
|
|
|
})
|
2017-10-26 03:49:16 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, branches, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDeletedBranch(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2023-06-29 13:03:20 +03:00
|
|
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
|
2017-11-04 16:31:59 +03:00
|
|
|
|
2017-10-26 03:49:16 +03:00
|
|
|
assert.NotNil(t, getDeletedBranch(t, firstBranch))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeletedBranchLoadUser(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-11-04 16:31:59 +03:00
|
|
|
|
2023-06-29 13:03:20 +03:00
|
|
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
|
|
|
|
secondBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 2})
|
2017-11-04 16:31:59 +03:00
|
|
|
|
2017-10-26 03:49:16 +03:00
|
|
|
branch := getDeletedBranch(t, firstBranch)
|
|
|
|
assert.Nil(t, branch.DeletedBy)
|
2023-06-29 13:03:20 +03:00
|
|
|
branch.LoadDeletedBy(db.DefaultContext)
|
2017-10-26 03:49:16 +03:00
|
|
|
assert.NotNil(t, branch.DeletedBy)
|
|
|
|
assert.Equal(t, "user1", branch.DeletedBy.Name)
|
|
|
|
|
|
|
|
branch = getDeletedBranch(t, secondBranch)
|
|
|
|
assert.Nil(t, branch.DeletedBy)
|
2023-06-29 13:03:20 +03:00
|
|
|
branch.LoadDeletedBy(db.DefaultContext)
|
2017-10-26 03:49:16 +03:00
|
|
|
assert.NotNil(t, branch.DeletedBy)
|
|
|
|
assert.Equal(t, "Ghost", branch.DeletedBy.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveDeletedBranch(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 05:22:25 +03:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2017-10-26 03:49:16 +03:00
|
|
|
|
2023-06-29 13:03:20 +03:00
|
|
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
|
2017-11-04 16:31:59 +03:00
|
|
|
|
2023-01-09 06:50:54 +03:00
|
|
|
err := git_model.RemoveDeletedBranchByID(db.DefaultContext, repo.ID, 1)
|
2017-10-26 03:49:16 +03:00
|
|
|
assert.NoError(t, err)
|
2021-11-16 11:53:21 +03:00
|
|
|
unittest.AssertNotExistsBean(t, firstBranch)
|
2023-06-29 13:03:20 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 2})
|
2017-10-26 03:49:16 +03:00
|
|
|
}
|
|
|
|
|
2023-06-29 13:03:20 +03:00
|
|
|
func getDeletedBranch(t *testing.T, branch *git_model.Branch) *git_model.Branch {
|
2022-08-16 05:22:25 +03:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2017-10-26 03:49:16 +03:00
|
|
|
|
2023-01-09 06:50:54 +03:00
|
|
|
deletedBranch, err := git_model.GetDeletedBranchByID(db.DefaultContext, repo.ID, branch.ID)
|
2017-10-26 03:49:16 +03:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, branch.ID, deletedBranch.ID)
|
|
|
|
assert.Equal(t, branch.Name, deletedBranch.Name)
|
2023-06-29 13:03:20 +03:00
|
|
|
assert.Equal(t, branch.CommitID, deletedBranch.CommitID)
|
2017-10-26 03:49:16 +03:00
|
|
|
assert.Equal(t, branch.DeletedByID, deletedBranch.DeletedByID)
|
|
|
|
|
|
|
|
return deletedBranch
|
|
|
|
}
|
2021-10-08 20:03:04 +03:00
|
|
|
|
|
|
|
func TestFindRenamedBranch(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2023-01-09 06:50:54 +03:00
|
|
|
branch, exist, err := git_model.FindRenamedBranch(db.DefaultContext, 1, "dev")
|
2021-10-08 20:03:04 +03:00
|
|
|
assert.NoError(t, err)
|
2023-04-23 00:56:27 +03:00
|
|
|
assert.True(t, exist)
|
2021-10-08 20:03:04 +03:00
|
|
|
assert.Equal(t, "master", branch.To)
|
|
|
|
|
2023-01-09 06:50:54 +03:00
|
|
|
_, exist, err = git_model.FindRenamedBranch(db.DefaultContext, 1, "unknow")
|
2021-10-08 20:03:04 +03:00
|
|
|
assert.NoError(t, err)
|
2023-04-23 00:56:27 +03:00
|
|
|
assert.False(t, exist)
|
2021-10-08 20:03:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameBranch(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 05:22:25 +03:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-10-08 20:03:04 +03:00
|
|
|
_isDefault := false
|
|
|
|
|
2022-11-12 23:18:50 +03:00
|
|
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
2022-04-28 14:48:48 +03:00
|
|
|
defer committer.Close()
|
|
|
|
assert.NoError(t, err)
|
2022-06-12 18:51:54 +03:00
|
|
|
assert.NoError(t, git_model.UpdateProtectBranch(ctx, repo1, &git_model.ProtectedBranch{
|
2023-01-16 11:00:22 +03:00
|
|
|
RepoID: repo1.ID,
|
|
|
|
RuleName: "master",
|
2022-06-12 18:51:54 +03:00
|
|
|
}, git_model.WhitelistOptions{}))
|
2022-04-28 14:48:48 +03:00
|
|
|
assert.NoError(t, committer.Commit())
|
2021-10-08 20:03:04 +03:00
|
|
|
|
2023-01-09 06:50:54 +03:00
|
|
|
assert.NoError(t, git_model.RenameBranch(db.DefaultContext, repo1, "master", "main", func(isDefault bool) error {
|
2021-10-08 20:03:04 +03:00
|
|
|
_isDefault = isDefault
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
2023-04-23 00:56:27 +03:00
|
|
|
assert.True(t, _isDefault)
|
2022-08-16 05:22:25 +03:00
|
|
|
repo1 = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-10-08 20:03:04 +03:00
|
|
|
assert.Equal(t, "main", repo1.DefaultBranch)
|
|
|
|
|
2022-08-16 05:22:25 +03:00
|
|
|
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}) // merged
|
2021-10-08 20:03:04 +03:00
|
|
|
assert.Equal(t, "master", pull.BaseBranch)
|
|
|
|
|
2022-08-16 05:22:25 +03:00
|
|
|
pull = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}) // open
|
2021-10-08 20:03:04 +03:00
|
|
|
assert.Equal(t, "main", pull.BaseBranch)
|
|
|
|
|
2022-08-16 05:22:25 +03:00
|
|
|
renamedBranch := unittest.AssertExistsAndLoadBean(t, &git_model.RenamedBranch{ID: 2})
|
2021-10-08 20:03:04 +03:00
|
|
|
assert.Equal(t, "master", renamedBranch.From)
|
|
|
|
assert.Equal(t, "main", renamedBranch.To)
|
|
|
|
assert.Equal(t, int64(1), renamedBranch.RepoID)
|
|
|
|
|
2022-06-12 18:51:54 +03:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &git_model.ProtectedBranch{
|
2023-01-16 11:00:22 +03:00
|
|
|
RepoID: repo1.ID,
|
|
|
|
RuleName: "main",
|
2021-10-08 20:03:04 +03:00
|
|
|
})
|
|
|
|
}
|
2021-11-08 18:45:37 +03:00
|
|
|
|
|
|
|
func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
|
2021-11-12 17:36:47 +03:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-08 18:45:37 +03:00
|
|
|
|
|
|
|
// Get deletedBranch with ID of 1 on repo with ID 2.
|
|
|
|
// This should return a nil branch as this deleted branch
|
|
|
|
// is actually on repo with ID 1.
|
2022-08-16 05:22:25 +03:00
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
2021-11-08 18:45:37 +03:00
|
|
|
|
2023-01-09 06:50:54 +03:00
|
|
|
deletedBranch, err := git_model.GetDeletedBranchByID(db.DefaultContext, repo2.ID, 1)
|
2021-11-08 18:45:37 +03:00
|
|
|
|
2023-06-29 13:03:20 +03:00
|
|
|
// Expect error, and the returned branch is nil.
|
|
|
|
assert.Error(t, err)
|
2021-11-08 18:45:37 +03:00
|
|
|
assert.Nil(t, deletedBranch)
|
|
|
|
|
|
|
|
// Now get the deletedBranch with ID of 1 on repo with ID 1.
|
|
|
|
// This should return the deletedBranch.
|
2022-08-16 05:22:25 +03:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-11-08 18:45:37 +03:00
|
|
|
|
2023-01-09 06:50:54 +03:00
|
|
|
deletedBranch, err = git_model.GetDeletedBranchByID(db.DefaultContext, repo1.ID, 1)
|
2021-11-08 18:45:37 +03:00
|
|
|
|
|
|
|
// Expect no error, and the returned branch to be not nil.
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, deletedBranch)
|
|
|
|
}
|