2017-10-26 06:19:16 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
package git_test
|
2017-10-26 06:19:16 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-04-28 17:18:48 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 21:21:54 +05:30
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 20:06:47 +05:30
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-17 18:04:35 +05:30
|
|
|
|
2017-10-26 06:19:16 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddDeletedBranch(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 1})
|
2017-11-04 19:01:59 +05:30
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
assert.Error(t, git_model.AddDeletedBranch(repo.ID, firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID))
|
|
|
|
assert.NoError(t, git_model.AddDeletedBranch(repo.ID, "test", "5655464564554545466464656", int64(1)))
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
2017-11-04 19:01:59 +05:30
|
|
|
|
2017-10-26 06:19:16 +05:30
|
|
|
func TestGetDeletedBranches(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2017-10-26 06:19:16 +05:30
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
branches, err := git_model.GetDeletedBranches(repo.ID)
|
2017-10-26 06:19:16 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, branches, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDeletedBranch(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 07:52:25 +05:30
|
|
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 1})
|
2017-11-04 19:01:59 +05:30
|
|
|
|
2017-10-26 06:19:16 +05:30
|
|
|
assert.NotNil(t, getDeletedBranch(t, firstBranch))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeletedBranchLoadUser(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-11-04 19:01:59 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 1})
|
|
|
|
secondBranch := unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 2})
|
2017-11-04 19:01:59 +05:30
|
|
|
|
2017-10-26 06:19:16 +05:30
|
|
|
branch := getDeletedBranch(t, firstBranch)
|
|
|
|
assert.Nil(t, branch.DeletedBy)
|
|
|
|
branch.LoadUser()
|
|
|
|
assert.NotNil(t, branch.DeletedBy)
|
|
|
|
assert.Equal(t, "user1", branch.DeletedBy.Name)
|
|
|
|
|
|
|
|
branch = getDeletedBranch(t, secondBranch)
|
|
|
|
assert.Nil(t, branch.DeletedBy)
|
|
|
|
branch.LoadUser()
|
|
|
|
assert.NotNil(t, branch.DeletedBy)
|
|
|
|
assert.Equal(t, "Ghost", branch.DeletedBy.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveDeletedBranch(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2017-10-26 06:19:16 +05:30
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 1})
|
2017-11-04 19:01:59 +05:30
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
err := git_model.RemoveDeletedBranchByID(repo.ID, 1)
|
2017-10-26 06:19:16 +05:30
|
|
|
assert.NoError(t, err)
|
2021-11-16 14:23:21 +05:30
|
|
|
unittest.AssertNotExistsBean(t, firstBranch)
|
2022-06-12 21:21:54 +05:30
|
|
|
unittest.AssertExistsAndLoadBean(t, &git_model.DeletedBranch{ID: 2})
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
func getDeletedBranch(t *testing.T, branch *git_model.DeletedBranch) *git_model.DeletedBranch {
|
2022-08-16 07:52:25 +05:30
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2017-10-26 06:19:16 +05:30
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
deletedBranch, err := git_model.GetDeletedBranchByID(repo.ID, branch.ID)
|
2017-10-26 06:19:16 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, branch.ID, deletedBranch.ID)
|
|
|
|
assert.Equal(t, branch.Name, deletedBranch.Name)
|
|
|
|
assert.Equal(t, branch.Commit, deletedBranch.Commit)
|
|
|
|
assert.Equal(t, branch.DeletedByID, deletedBranch.DeletedByID)
|
|
|
|
|
|
|
|
return deletedBranch
|
|
|
|
}
|
2021-10-08 22:33:04 +05:30
|
|
|
|
|
|
|
func TestFindRenamedBranch(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-12 21:21:54 +05:30
|
|
|
branch, exist, err := git_model.FindRenamedBranch(1, "dev")
|
2021-10-08 22:33:04 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, true, exist)
|
|
|
|
assert.Equal(t, "master", branch.To)
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
_, exist, err = git_model.FindRenamedBranch(1, "unknow")
|
2021-10-08 22:33:04 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, false, exist)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameBranch(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-16 07:52:25 +05:30
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-10-08 22:33:04 +05:30
|
|
|
_isDefault := false
|
|
|
|
|
2022-11-13 01:48:50 +05:30
|
|
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
2022-04-28 17:18:48 +05:30
|
|
|
defer committer.Close()
|
|
|
|
assert.NoError(t, err)
|
2022-06-12 21:21:54 +05:30
|
|
|
assert.NoError(t, git_model.UpdateProtectBranch(ctx, repo1, &git_model.ProtectedBranch{
|
2021-10-08 22:33:04 +05:30
|
|
|
RepoID: repo1.ID,
|
|
|
|
BranchName: "master",
|
2022-06-12 21:21:54 +05:30
|
|
|
}, git_model.WhitelistOptions{}))
|
2022-04-28 17:18:48 +05:30
|
|
|
assert.NoError(t, committer.Commit())
|
2021-10-08 22:33:04 +05:30
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
assert.NoError(t, git_model.RenameBranch(repo1, "master", "main", func(isDefault bool) error {
|
2021-10-08 22:33:04 +05:30
|
|
|
_isDefault = isDefault
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert.Equal(t, true, _isDefault)
|
2022-08-16 07:52:25 +05:30
|
|
|
repo1 = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-10-08 22:33:04 +05:30
|
|
|
assert.Equal(t, "main", repo1.DefaultBranch)
|
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}) // merged
|
2021-10-08 22:33:04 +05:30
|
|
|
assert.Equal(t, "master", pull.BaseBranch)
|
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
pull = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}) // open
|
2021-10-08 22:33:04 +05:30
|
|
|
assert.Equal(t, "main", pull.BaseBranch)
|
|
|
|
|
2022-08-16 07:52:25 +05:30
|
|
|
renamedBranch := unittest.AssertExistsAndLoadBean(t, &git_model.RenamedBranch{ID: 2})
|
2021-10-08 22:33:04 +05:30
|
|
|
assert.Equal(t, "master", renamedBranch.From)
|
|
|
|
assert.Equal(t, "main", renamedBranch.To)
|
|
|
|
assert.Equal(t, int64(1), renamedBranch.RepoID)
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
unittest.AssertExistsAndLoadBean(t, &git_model.ProtectedBranch{
|
2021-10-08 22:33:04 +05:30
|
|
|
RepoID: repo1.ID,
|
|
|
|
BranchName: "main",
|
|
|
|
})
|
|
|
|
}
|
2021-11-08 21:15:37 +05:30
|
|
|
|
|
|
|
func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
|
2021-11-12 20:06:47 +05:30
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-08 21:15:37 +05:30
|
|
|
|
|
|
|
// 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 07:52:25 +05:30
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
2021-11-08 21:15:37 +05:30
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
deletedBranch, err := git_model.GetDeletedBranchByID(repo2.ID, 1)
|
2021-11-08 21:15:37 +05:30
|
|
|
|
|
|
|
// Expect no error, and the returned branch is nil.
|
|
|
|
assert.NoError(t, err)
|
|
|
|
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 07:52:25 +05:30
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-11-08 21:15:37 +05:30
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
deletedBranch, err = git_model.GetDeletedBranchByID(repo1.ID, 1)
|
2021-11-08 21:15:37 +05:30
|
|
|
|
|
|
|
// Expect no error, and the returned branch to be not nil.
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, deletedBranch)
|
|
|
|
}
|