aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-04-19 21:40:42 +0800
committerGitHub <noreply@github.com>2023-04-19 21:40:42 +0800
commite422342eebc18034ef586ec58f1e2fff0340091d (patch)
tree307264b46c1683915429083d54e9634ee4f2fc4d /models
parent01214c8ada993bf5f54a4149979d140443d69410 (diff)
downloadgitea-e422342eebc18034ef586ec58f1e2fff0340091d.tar.gz
gitea-e422342eebc18034ef586ec58f1e2fff0340091d.zip
Allow adding new files to an empty repo (#24164)
![image](https://user-images.githubusercontent.com/2114189/232561612-2bfcfd0a-fc04-47ba-965f-5d0bcea46c54.png)
Diffstat (limited to 'models')
-rw-r--r--models/db/iterate_test.go11
-rw-r--r--models/db/list_test.go11
-rw-r--r--models/dbfs/dbfs_test.go2
-rw-r--r--models/fixtures/repo_unit.yml6
-rw-r--r--models/fixtures/repository.yml1
-rw-r--r--models/fixtures/user.yml2
-rw-r--r--models/repo/repo.go8
-rw-r--r--models/unittest/fixtures.go20
-rw-r--r--models/user/user_test.go8
9 files changed, 44 insertions, 25 deletions
diff --git a/models/db/iterate_test.go b/models/db/iterate_test.go
index f9f1213721..5362f34075 100644
--- a/models/db/iterate_test.go
+++ b/models/db/iterate_test.go
@@ -19,13 +19,16 @@ func TestIterate(t *testing.T) {
xe := unittest.GetXORMEngine()
assert.NoError(t, xe.Sync(&repo_model.RepoUnit{}))
- var repoCnt int
- err := db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repo *repo_model.RepoUnit) error {
- repoCnt++
+ cnt, err := db.GetEngine(db.DefaultContext).Count(&repo_model.RepoUnit{})
+ assert.NoError(t, err)
+
+ var repoUnitCnt int
+ err = db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repo *repo_model.RepoUnit) error {
+ repoUnitCnt++
return nil
})
assert.NoError(t, err)
- assert.EqualValues(t, 89, repoCnt)
+ assert.EqualValues(t, cnt, repoUnitCnt)
err = db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repoUnit *repo_model.RepoUnit) error {
reopUnit2 := repo_model.RepoUnit{ID: repoUnit.ID}
diff --git a/models/db/list_test.go b/models/db/list_test.go
index 195450b1e7..6b9bebd64b 100644
--- a/models/db/list_test.go
+++ b/models/db/list_test.go
@@ -31,15 +31,20 @@ func TestFind(t *testing.T) {
xe := unittest.GetXORMEngine()
assert.NoError(t, xe.Sync(&repo_model.RepoUnit{}))
+ var repoUnitCount int
+ _, err := db.GetEngine(db.DefaultContext).SQL("SELECT COUNT(*) FROM repo_unit").Get(&repoUnitCount)
+ assert.NoError(t, err)
+ assert.NotEmpty(t, repoUnitCount)
+
opts := mockListOptions{}
var repoUnits []repo_model.RepoUnit
- err := db.Find(db.DefaultContext, &opts, &repoUnits)
+ err = db.Find(db.DefaultContext, &opts, &repoUnits)
assert.NoError(t, err)
- assert.EqualValues(t, 89, len(repoUnits))
+ assert.EqualValues(t, repoUnitCount, len(repoUnits))
cnt, err := db.Count(db.DefaultContext, &opts, new(repo_model.RepoUnit))
assert.NoError(t, err)
- assert.EqualValues(t, 89, cnt)
+ assert.EqualValues(t, repoUnitCount, cnt)
repoUnits = make([]repo_model.RepoUnit, 0, 10)
newCnt, err := db.FindAndCount(db.DefaultContext, &opts, &repoUnits)
diff --git a/models/dbfs/dbfs_test.go b/models/dbfs/dbfs_test.go
index 30aa6463c5..300758c623 100644
--- a/models/dbfs/dbfs_test.go
+++ b/models/dbfs/dbfs_test.go
@@ -12,8 +12,6 @@ import (
"code.gitea.io/gitea/models/db"
"github.com/stretchr/testify/assert"
-
- _ "github.com/mattn/go-sqlite3"
)
func changeDefaultFileBlockSize(n int64) (restore func()) {
diff --git a/models/fixtures/repo_unit.yml b/models/fixtures/repo_unit.yml
index 184be2d861..5bb974a7d7 100644
--- a/models/fixtures/repo_unit.yml
+++ b/models/fixtures/repo_unit.yml
@@ -601,3 +601,9 @@
repo_id: 57
type: 5
created_unix: 946684810
+
+-
+ id: 90
+ repo_id: 52
+ type: 1
+ created_unix: 946684810
diff --git a/models/fixtures/repository.yml b/models/fixtures/repository.yml
index 496d1a3e7a..ef7730780f 100644
--- a/models/fixtures/repository.yml
+++ b/models/fixtures/repository.yml
@@ -1560,6 +1560,7 @@
owner_name: user30
lower_name: empty
name: empty
+ default_branch: master
num_watches: 0
num_stars: 0
num_forks: 0
diff --git a/models/fixtures/user.yml b/models/fixtures/user.yml
index fce4a4bda0..eba33a7c36 100644
--- a/models/fixtures/user.yml
+++ b/models/fixtures/user.yml
@@ -1091,7 +1091,7 @@
max_repo_creation: -1
is_active: true
is_admin: false
- is_restricted: true
+ is_restricted: false
allow_git_hook: false
allow_import_local: false
allow_create_organization: true
diff --git a/models/repo/repo.go b/models/repo/repo.go
index 3653dae015..266cbc288c 100644
--- a/models/repo/repo.go
+++ b/models/repo/repo.go
@@ -225,6 +225,12 @@ func (repo *Repository) IsBroken() bool {
return repo.Status == RepositoryBroken
}
+// MarkAsBrokenEmpty marks the repo as broken and empty
+func (repo *Repository) MarkAsBrokenEmpty() {
+ repo.Status = RepositoryBroken
+ repo.IsEmpty = true
+}
+
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (repo *Repository) AfterLoad() {
repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues
@@ -729,7 +735,7 @@ func IsRepositoryExist(ctx context.Context, u *user_model.User, repoName string)
return false, err
}
isDir, err := util.IsDir(RepoPath(u.Name, repoName))
- return has && isDir, err
+ return has || isDir, err
}
// GetTemplateRepo populates repo.TemplateRepo for a generated repository and
diff --git a/models/unittest/fixtures.go b/models/unittest/fixtures.go
index 545452a159..f7ee766731 100644
--- a/models/unittest/fixtures.go
+++ b/models/unittest/fixtures.go
@@ -17,7 +17,7 @@ import (
"xorm.io/xorm/schemas"
)
-var fixtures *testfixtures.Loader
+var fixturesLoader *testfixtures.Loader
// GetXORMEngine gets the XORM engine
func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) {
@@ -30,11 +30,11 @@ func GetXORMEngine(engine ...*xorm.Engine) (x *xorm.Engine) {
// InitFixtures initialize test fixtures for a test database
func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
e := GetXORMEngine(engine...)
- var testfiles func(*testfixtures.Loader) error
+ var fixtureOptionFiles func(*testfixtures.Loader) error
if opts.Dir != "" {
- testfiles = testfixtures.Directory(opts.Dir)
+ fixtureOptionFiles = testfixtures.Directory(opts.Dir)
} else {
- testfiles = testfixtures.Files(opts.Files...)
+ fixtureOptionFiles = testfixtures.Files(opts.Files...)
}
dialect := "unknown"
switch e.Dialect().URI().DBType {
@@ -54,14 +54,14 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
testfixtures.Database(e.DB().DB),
testfixtures.Dialect(dialect),
testfixtures.DangerousSkipTestDatabaseCheck(),
- testfiles,
+ fixtureOptionFiles,
}
if e.Dialect().URI().DBType == schemas.POSTGRES {
loaderOptions = append(loaderOptions, testfixtures.SkipResetSequences())
}
- fixtures, err = testfixtures.New(loaderOptions...)
+ fixturesLoader, err = testfixtures.New(loaderOptions...)
if err != nil {
return err
}
@@ -78,11 +78,9 @@ func InitFixtures(opts FixturesOptions, engine ...*xorm.Engine) (err error) {
func LoadFixtures(engine ...*xorm.Engine) error {
e := GetXORMEngine(engine...)
var err error
- // Database transaction conflicts could occur and result in ROLLBACK
- // As a simple workaround, we just retry 20 times.
- for i := 0; i < 20; i++ {
- err = fixtures.Load()
- if err == nil {
+ // (doubt) database transaction conflicts could occur and result in ROLLBACK? just try for a few times.
+ for i := 0; i < 5; i++ {
+ if err = fixturesLoader.Load(); err == nil {
break
}
time.Sleep(200 * time.Millisecond)
diff --git a/models/user/user_test.go b/models/user/user_test.go
index 8e78fee6b3..c2314d5c03 100644
--- a/models/user/user_test.go
+++ b/models/user/user_test.go
@@ -5,6 +5,7 @@ package user_test
import (
"context"
+ "fmt"
"math/rand"
"strings"
"testing"
@@ -64,9 +65,10 @@ func TestSearchUsers(t *testing.T) {
testSuccess := func(opts *user_model.SearchUserOptions, expectedUserOrOrgIDs []int64) {
users, _, err := user_model.SearchUsers(opts)
assert.NoError(t, err)
- if assert.Len(t, users, len(expectedUserOrOrgIDs), opts) {
+ cassText := fmt.Sprintf("ids: %v, opts: %v", expectedUserOrOrgIDs, opts)
+ if assert.Len(t, users, len(expectedUserOrOrgIDs), "case: %s", cassText) {
for i, expectedID := range expectedUserOrOrgIDs {
- assert.EqualValues(t, expectedID, users[i].ID)
+ assert.EqualValues(t, expectedID, users[i].ID, "case: %s", cassText)
}
}
}
@@ -118,7 +120,7 @@ func TestSearchUsers(t *testing.T) {
[]int64{1})
testUserSuccess(&user_model.SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsRestricted: util.OptionalBoolTrue},
- []int64{29, 30})
+ []int64{29})
testUserSuccess(&user_model.SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsProhibitLogin: util.OptionalBoolTrue},
[]int64{30})