2017-10-25 20:49:16 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-10-25 20:49:16 -04:00
|
|
|
|
2022-06-12 11:51:54 -04:00
|
|
|
package git_test
|
2017-10-25 20:49:16 -04:00
|
|
|
|
|
|
|
import (
|
2024-01-12 16:50:38 -05:00
|
|
|
"context"
|
2017-10-25 20:49:16 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-04-28 07:48:48 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 11:51:54 -04:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-06-13 05:37:59 -04:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2023-06-30 05:03:05 -04:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2024-02-22 21:18:33 -05:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2021-11-17 07:34:35 -05:00
|
|
|
|
2017-10-25 20:49:16 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-10-25 20:49:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddDeletedBranch(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2023-12-17 06:56:08 -05:00
|
|
|
assert.EqualValues(t, git.Sha1ObjectFormat.Name(), repo.ObjectFormatName)
|
2023-06-29 06:03:20 -04:00
|
|
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
|
2017-11-04 09:31:59 -04:00
|
|
|
|
2023-06-29 06:03:20 -04:00
|
|
|
assert.True(t, firstBranch.IsDeleted)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, git_model.AddDeletedBranch(db.DefaultContext, repo.ID, firstBranch.Name, firstBranch.DeletedByID))
|
|
|
|
require.NoError(t, git_model.AddDeletedBranch(db.DefaultContext, repo.ID, "branch2", int64(1)))
|
2023-06-29 06:03:20 -04:00
|
|
|
|
|
|
|
secondBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo.ID, Name: "branch2"})
|
|
|
|
assert.True(t, secondBranch.IsDeleted)
|
|
|
|
|
2023-06-30 05:03:05 -04:00
|
|
|
commit := &git.Commit{
|
2023-12-19 02:20:47 -05:00
|
|
|
ID: git.MustIDFromString(secondBranch.CommitID),
|
2023-06-30 05:03:05 -04:00
|
|
|
CommitMessage: secondBranch.CommitMessage,
|
|
|
|
Committer: &git.Signature{
|
|
|
|
When: secondBranch.CommitTime.AsLocalTime(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
Also sync DB branches on push if necessary (#28361)
Fix #28056
This PR will check whether the repo has zero branch when pushing a
branch. If that, it means this repository hasn't been synced.
The reason caused that is after user upgrade from v1.20 -> v1.21, he
just push branches without visit the repository user interface. Because
all repositories routers will check whether a branches sync is necessary
but push has not such check.
For every repository, it has two states, synced or not synced. If there
is zero branch for a repository, then it will be assumed as non-sync
state. Otherwise, it's synced state. So if we think it's synced, we just
need to update branch/insert new branch. Otherwise do a full sync. So
that, for every push, there will be almost no extra load added. It's
high performance than yours.
For the implementation, we in fact will try to update the branch first,
if updated success with affect records > 0, then all are done. Because
that means the branch has been in the database. If no record is
affected, that means the branch does not exist in database. So there are
two possibilities. One is this is a new branch, then we just need to
insert the record. Another is the branches haven't been synced, then we
need to sync all the branches into database.
2023-12-09 08:30:56 -05:00
|
|
|
_, err := git_model.UpdateBranch(db.DefaultContext, repo.ID, secondBranch.PusherID, secondBranch.Name, commit)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2017-10-25 20:49:16 -04:00
|
|
|
}
|
2017-11-04 09:31:59 -04:00
|
|
|
|
2017-10-25 20:49:16 -04:00
|
|
|
func TestGetDeletedBranches(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2017-10-25 20:49:16 -04:00
|
|
|
|
2023-12-11 03:56:48 -05:00
|
|
|
branches, err := db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{
|
|
|
|
ListOptions: db.ListOptionsAll,
|
2023-06-29 06:03:20 -04:00
|
|
|
RepoID: repo.ID,
|
2024-02-22 21:18:33 -05:00
|
|
|
IsDeletedBranch: optional.Some(true),
|
2023-06-29 06:03:20 -04:00
|
|
|
})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2017-10-25 20:49:16 -04:00
|
|
|
assert.Len(t, branches, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDeletedBranch(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-06-29 06:03:20 -04:00
|
|
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
|
2017-11-04 09:31:59 -04:00
|
|
|
|
2017-10-25 20:49:16 -04:00
|
|
|
assert.NotNil(t, getDeletedBranch(t, firstBranch))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeletedBranchLoadUser(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2017-11-04 09:31:59 -04:00
|
|
|
|
2023-06-29 06:03:20 -04:00
|
|
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
|
|
|
|
secondBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 2})
|
2017-11-04 09:31:59 -04:00
|
|
|
|
2017-10-25 20:49:16 -04:00
|
|
|
branch := getDeletedBranch(t, firstBranch)
|
|
|
|
assert.Nil(t, branch.DeletedBy)
|
2023-06-29 06:03:20 -04:00
|
|
|
branch.LoadDeletedBy(db.DefaultContext)
|
2017-10-25 20:49:16 -04: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 06:03:20 -04:00
|
|
|
branch.LoadDeletedBy(db.DefaultContext)
|
2017-10-25 20:49:16 -04:00
|
|
|
assert.NotNil(t, branch.DeletedBy)
|
|
|
|
assert.Equal(t, "Ghost", branch.DeletedBy.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveDeletedBranch(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2017-10-25 20:49:16 -04:00
|
|
|
|
2023-06-29 06:03:20 -04:00
|
|
|
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
|
2017-11-04 09:31:59 -04:00
|
|
|
|
2023-01-08 22:50:54 -05:00
|
|
|
err := git_model.RemoveDeletedBranchByID(db.DefaultContext, repo.ID, 1)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, firstBranch)
|
2023-06-29 06:03:20 -04:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 2})
|
2017-10-25 20:49:16 -04:00
|
|
|
}
|
|
|
|
|
2023-06-29 06:03:20 -04:00
|
|
|
func getDeletedBranch(t *testing.T, branch *git_model.Branch) *git_model.Branch {
|
2022-08-15 22:22:25 -04:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2017-10-25 20:49:16 -04:00
|
|
|
|
2023-01-08 22:50:54 -05:00
|
|
|
deletedBranch, err := git_model.GetDeletedBranchByID(db.DefaultContext, repo.ID, branch.ID)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2017-10-25 20:49:16 -04:00
|
|
|
assert.Equal(t, branch.ID, deletedBranch.ID)
|
|
|
|
assert.Equal(t, branch.Name, deletedBranch.Name)
|
2023-06-29 06:03:20 -04:00
|
|
|
assert.Equal(t, branch.CommitID, deletedBranch.CommitID)
|
2017-10-25 20:49:16 -04:00
|
|
|
assert.Equal(t, branch.DeletedByID, deletedBranch.DeletedByID)
|
|
|
|
|
|
|
|
return deletedBranch
|
|
|
|
}
|
2021-10-08 13:03:04 -04:00
|
|
|
|
|
|
|
func TestFindRenamedBranch(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2023-01-08 22:50:54 -05:00
|
|
|
branch, exist, err := git_model.FindRenamedBranch(db.DefaultContext, 1, "dev")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-04-22 17:56:27 -04:00
|
|
|
assert.True(t, exist)
|
2021-10-08 13:03:04 -04:00
|
|
|
assert.Equal(t, "master", branch.To)
|
|
|
|
|
2023-01-08 22:50:54 -05:00
|
|
|
_, exist, err = git_model.FindRenamedBranch(db.DefaultContext, 1, "unknow")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-04-22 17:56:27 -04:00
|
|
|
assert.False(t, exist)
|
2021-10-08 13:03:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameBranch(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-08-15 22:22:25 -04:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-10-08 13:03:04 -04:00
|
|
|
_isDefault := false
|
|
|
|
|
2022-11-12 15:18:50 -05:00
|
|
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
2022-04-28 07:48:48 -04:00
|
|
|
defer committer.Close()
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, git_model.UpdateProtectBranch(ctx, repo1, &git_model.ProtectedBranch{
|
2023-01-16 03:00:22 -05:00
|
|
|
RepoID: repo1.ID,
|
|
|
|
RuleName: "master",
|
2022-06-12 11:51:54 -04:00
|
|
|
}, git_model.WhitelistOptions{}))
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, committer.Commit())
|
2021-10-08 13:03:04 -04:00
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, git_model.RenameBranch(db.DefaultContext, repo1, "master", "main", func(ctx context.Context, isDefault bool) error {
|
2021-10-08 13:03:04 -04:00
|
|
|
_isDefault = isDefault
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
2023-04-22 17:56:27 -04:00
|
|
|
assert.True(t, _isDefault)
|
2022-08-15 22:22:25 -04:00
|
|
|
repo1 = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-10-08 13:03:04 -04:00
|
|
|
assert.Equal(t, "main", repo1.DefaultBranch)
|
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}) // merged
|
2021-10-08 13:03:04 -04:00
|
|
|
assert.Equal(t, "master", pull.BaseBranch)
|
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
pull = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}) // open
|
2021-10-08 13:03:04 -04:00
|
|
|
assert.Equal(t, "main", pull.BaseBranch)
|
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
renamedBranch := unittest.AssertExistsAndLoadBean(t, &git_model.RenamedBranch{ID: 2})
|
2021-10-08 13:03:04 -04:00
|
|
|
assert.Equal(t, "master", renamedBranch.From)
|
|
|
|
assert.Equal(t, "main", renamedBranch.To)
|
|
|
|
assert.Equal(t, int64(1), renamedBranch.RepoID)
|
|
|
|
|
2022-06-12 11:51:54 -04:00
|
|
|
unittest.AssertExistsAndLoadBean(t, &git_model.ProtectedBranch{
|
2023-01-16 03:00:22 -05:00
|
|
|
RepoID: repo1.ID,
|
|
|
|
RuleName: "main",
|
2021-10-08 13:03:04 -04:00
|
|
|
})
|
|
|
|
}
|
2021-11-08 10:45:37 -05:00
|
|
|
|
|
|
|
func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2021-11-08 10:45:37 -05: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-15 22:22:25 -04:00
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
2021-11-08 10:45:37 -05:00
|
|
|
|
2023-01-08 22:50:54 -05:00
|
|
|
deletedBranch, err := git_model.GetDeletedBranchByID(db.DefaultContext, repo2.ID, 1)
|
2021-11-08 10:45:37 -05:00
|
|
|
|
2023-06-29 06:03:20 -04:00
|
|
|
// Expect error, and the returned branch is nil.
|
2024-07-30 15:41:10 -04:00
|
|
|
require.Error(t, err)
|
2021-11-08 10:45:37 -05: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-15 22:22:25 -04:00
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-11-08 10:45:37 -05:00
|
|
|
|
2023-01-08 22:50:54 -05:00
|
|
|
deletedBranch, err = git_model.GetDeletedBranchByID(db.DefaultContext, repo1.ID, 1)
|
2021-11-08 10:45:37 -05:00
|
|
|
|
|
|
|
// Expect no error, and the returned branch to be not nil.
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-11-08 10:45:37 -05:00
|
|
|
assert.NotNil(t, deletedBranch)
|
|
|
|
}
|
Fix git_model.FindBranchesByRepoAndBranchName
When a logged in user with no repositories visits their dashboard, it will
display a search box that lists their own repositories.
This is served by the `repo.SearchRepos` handler, which in turn calls
`commitstatus_service.FindReposLastestCommitStatuses()` with an empty
repo list.
That, in turn, will call `git_model.FindBranchesByRepoAndBranchName()`,
with an empty map. With no map, `FindBranchesByRepoAndBranchName()` ends
up querying the entire `branch` table, because no conditions were set
up.
Armed with a gazillion repo & commit shas, we return to
`FindReposLastestCommitStatuses`, and promptly call
`git_model.GetLatestCommitStatusForPairs`, which constructs a monstrous
query with so many placeholders that the database tells us to go
somewhere else, and flips us off. At least on instances the size of
Codeberg. On smaller instances, it will eventually return, and throw
away all the data, and return an empty set, having performed all this
for naught.
We fix this by short-circuiting `FindBranchesByRepoAndBranchName`, and
returning fast if our inputs are empty.
A test case is included.
Fixes #3521.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-04-30 14:53:47 -04:00
|
|
|
|
|
|
|
func TestFindBranchesByRepoAndBranchName(t *testing.T) {
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
Fix git_model.FindBranchesByRepoAndBranchName
When a logged in user with no repositories visits their dashboard, it will
display a search box that lists their own repositories.
This is served by the `repo.SearchRepos` handler, which in turn calls
`commitstatus_service.FindReposLastestCommitStatuses()` with an empty
repo list.
That, in turn, will call `git_model.FindBranchesByRepoAndBranchName()`,
with an empty map. With no map, `FindBranchesByRepoAndBranchName()` ends
up querying the entire `branch` table, because no conditions were set
up.
Armed with a gazillion repo & commit shas, we return to
`FindReposLastestCommitStatuses`, and promptly call
`git_model.GetLatestCommitStatusForPairs`, which constructs a monstrous
query with so many placeholders that the database tells us to go
somewhere else, and flips us off. At least on instances the size of
Codeberg. On smaller instances, it will eventually return, and throw
away all the data, and return an empty set, having performed all this
for naught.
We fix this by short-circuiting `FindBranchesByRepoAndBranchName`, and
returning fast if our inputs are empty.
A test case is included.
Fixes #3521.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-04-30 14:53:47 -04:00
|
|
|
|
|
|
|
// With no repos or branches given, we find no branches.
|
|
|
|
branches, err := git_model.FindBranchesByRepoAndBranchName(db.DefaultContext, map[int64]string{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, branches)
|
Fix git_model.FindBranchesByRepoAndBranchName
When a logged in user with no repositories visits their dashboard, it will
display a search box that lists their own repositories.
This is served by the `repo.SearchRepos` handler, which in turn calls
`commitstatus_service.FindReposLastestCommitStatuses()` with an empty
repo list.
That, in turn, will call `git_model.FindBranchesByRepoAndBranchName()`,
with an empty map. With no map, `FindBranchesByRepoAndBranchName()` ends
up querying the entire `branch` table, because no conditions were set
up.
Armed with a gazillion repo & commit shas, we return to
`FindReposLastestCommitStatuses`, and promptly call
`git_model.GetLatestCommitStatusForPairs`, which constructs a monstrous
query with so many placeholders that the database tells us to go
somewhere else, and flips us off. At least on instances the size of
Codeberg. On smaller instances, it will eventually return, and throw
away all the data, and return an empty set, having performed all this
for naught.
We fix this by short-circuiting `FindBranchesByRepoAndBranchName`, and
returning fast if our inputs are empty.
A test case is included.
Fixes #3521.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
2024-04-30 14:53:47 -04:00
|
|
|
}
|