diff --git a/models/git/branch_list.go b/models/git/branch_list.go index 8319e5ecd0..c6fc8ad4b1 100644 --- a/models/git/branch_list.go +++ b/models/git/branch_list.go @@ -120,6 +120,9 @@ func FindBranchNames(ctx context.Context, opts FindBranchOptions) ([]string, err } func FindBranchesByRepoAndBranchName(ctx context.Context, repoBranches map[int64]string) (map[int64]string, error) { + if len(repoBranches) == 0 { + return nil, nil + } cond := builder.NewCond() for repoID, branchName := range repoBranches { cond = cond.Or(builder.And(builder.Eq{"repo_id": repoID}, builder.Eq{"name": branchName})) diff --git a/models/git/branch_test.go b/models/git/branch_test.go index b8ea663e81..3aa578f44b 100644 --- a/models/git/branch_test.go +++ b/models/git/branch_test.go @@ -183,3 +183,12 @@ func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) { assert.NoError(t, err) assert.NotNil(t, deletedBranch) } + +func TestFindBranchesByRepoAndBranchName(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + // With no repos or branches given, we find no branches. + branches, err := git_model.FindBranchesByRepoAndBranchName(db.DefaultContext, map[int64]string{}) + assert.NoError(t, err) + assert.Len(t, branches, 0) +} diff --git a/release-notes/8.0.0/fix/3570.md b/release-notes/8.0.0/fix/3570.md new file mode 100644 index 0000000000..1e860eb958 --- /dev/null +++ b/release-notes/8.0.0/fix/3570.md @@ -0,0 +1 @@ +Fixed an issue that resulted in excessive and unnecessary database queries when a user with no repositories was viewing their dashboard.