diff --git a/docs/content/doc/developers/hacking-on-gitea.en-us.md b/docs/content/doc/developers/hacking-on-gitea.en-us.md index 5be158ae28b16..5f52856d1d2b9 100644 --- a/docs/content/doc/developers/hacking-on-gitea.en-us.md +++ b/docs/content/doc/developers/hacking-on-gitea.en-us.md @@ -73,6 +73,7 @@ One of these three distributions of Make will run on Windows: - MSYS2 is a collection of tools and libraries providing you with an easy-to-use environment for building, installing and running native Windows software, it includes MinGW-w64. - In MingGW-w64, the binary is called `mingw32-make.exe` instead of `make.exe`. Add the `bin` folder to `PATH`. - In MSYS2, you can use `make` directly. See [MSYS2 Porting](https://www.msys2.org/wiki/Porting/). + - To compile Gitea with CGO_ENABLED (eg: SQLite3), you might need to use [tdm-gcc](https://jmeubank.github.io/tdm-gcc/) instead of MSYS2 gcc, because MSYS2 gcc headers lack some Windows-only CRT functions like `_beginthread`. - [Chocolatey package](https://chocolatey.org/packages/make). Run `choco install make` **Note**: If you are attempting to build using make with Windows Command Prompt, you may run into issues. The above prompts (Git bash, or MinGW) are recommended, however if you only have command prompt (or potentially PowerShell) you can set environment variables using the [set](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/set_1) command, e.g. `set TAGS=bindata`. @@ -273,10 +274,17 @@ make test-sqlite-migration # with SQLite switched for the appropriate database There are two types of test run by Gitea: Unit tests and Integration Tests. +### Unit Tests + +Unit tests are covered by `*_test.go` in `go test` system. +You can set the environment variable `GITEA_UNIT_TESTS_LOG_SQL=1` to display all SQL statements when running the tests in verbose mode (i.e. when `GOTESTFLAGS=-v` is set). + ```bash TAGS="bindata sqlite sqlite_unlock_notify" make test # Runs the unit tests ``` +### Integration Tests + Unit tests will not and cannot completely test Gitea alone. Therefore, we have written integration tests; however, these are database dependent. @@ -288,10 +296,12 @@ will run the integration tests in an SQLite environment. Integration tests require `git lfs` to be installed. Other database tests are available but may need adjustment to the local environment. -Look at -[`integrations/README.md`](https://github.com/go-gitea/gitea/blob/main/integrations/README.md) +Take a look at [`integrations/README.md`](https://github.com/go-gitea/gitea/blob/main/integrations/README.md) for more information and how to run a single test. + +### Testing for a PR + Our continuous integration will test the code passes its unit tests and that all supported databases will pass integration test in a Docker environment. Migration from several recent versions of Gitea will also be tested. diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go index c798dbefb1042..6f3a36ed216f6 100644 --- a/models/unittest/testdb.go +++ b/models/unittest/testdb.go @@ -152,7 +152,7 @@ func CreateTestEngine(opts FixturesOptions) error { if err = db.SyncAllTables(); err != nil { return err } - switch os.Getenv("GITEA_UNIT_TESTS_VERBOSE") { + switch os.Getenv("GITEA_UNIT_TESTS_LOG_SQL") { case "true", "1": x.ShowSQL(true) } diff --git a/routers/web/repo/migrate.go b/routers/web/repo/migrate.go index 4284734102853..9b1265383908e 100644 --- a/routers/web/repo/migrate.go +++ b/routers/web/repo/migrate.go @@ -81,7 +81,14 @@ func handleMigrateError(ctx *context.Context, owner *user_model.User, err error, case migrations.IsTwoFactorAuthError(err): ctx.RenderWithErr(ctx.Tr("form.2fa_auth_required"), tpl, form) case repo_model.IsErrReachLimitOfRepo(err): - ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", owner.MaxCreationLimit()), tpl, form) + var msg string + maxCreationLimit := ctx.User.MaxCreationLimit() + if maxCreationLimit == 1 { + msg = ctx.Tr("repo.form.reach_limit_of_creation_1", maxCreationLimit) + } else { + msg = ctx.Tr("repo.form.reach_limit_of_creation_n", maxCreationLimit) + } + ctx.RenderWithErr(msg, tpl, form) case repo_model.IsErrRepoAlreadyExist(err): ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tpl, form) diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 9a36e6ee1db08..0d8c6d374f989 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -162,7 +162,14 @@ func Create(ctx *context.Context) { func handleCreateError(ctx *context.Context, owner *user_model.User, err error, name string, tpl base.TplName, form interface{}) { switch { case repo_model.IsErrReachLimitOfRepo(err): - ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", owner.MaxCreationLimit()), tpl, form) + var msg string + maxCreationLimit := ctx.User.MaxCreationLimit() + if maxCreationLimit == 1 { + msg = ctx.Tr("repo.form.reach_limit_of_creation_1", maxCreationLimit) + } else { + msg = ctx.Tr("repo.form.reach_limit_of_creation_n", maxCreationLimit) + } + ctx.RenderWithErr(msg, tpl, form) case repo_model.IsErrRepoAlreadyExist(err): ctx.Data["Err_RepoName"] = true ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tpl, form) diff --git a/routers/web/repo/setting.go b/routers/web/repo/setting.go index ff38f61d8b7b5..5457e651d38f6 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -609,7 +609,12 @@ func SettingsPost(ctx *context.Context) { } if !ctx.Repo.Owner.CanCreateRepo() { - ctx.Flash.Error(ctx.Tr("repo.form.reach_limit_of_creation", ctx.User.MaxCreationLimit())) + maxCreationLimit := ctx.User.MaxCreationLimit() + if maxCreationLimit == 1 { + ctx.Flash.Error(ctx.Tr("repo.form.reach_limit_of_creation_1", maxCreationLimit)) + } else { + ctx.Flash.Error(ctx.Tr("repo.form.reach_limit_of_creation_n", maxCreationLimit)) + } ctx.Redirect(repo.Link() + "/settings") return } diff --git a/services/pull/patch.go b/services/pull/patch.go index 4bc875f630455..0eba3f86ed5af 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -235,6 +235,7 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath }() numberOfConflicts := 0 + pr.ConflictedFiles = make([]string, 0, 5) conflict := false for file := range unmerged { diff --git a/services/repository/adopt.go b/services/repository/adopt.go index 2f87b0d7bd130..fc3fdc608fd93 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -217,6 +217,57 @@ func DeleteUnadoptedRepository(doer, u *user_model.User, repoName string) error return util.RemoveAll(repoPath) } +type unadoptedRrepositories struct { + repositories []string + index int + start int + end int +} + +func (unadopted *unadoptedRrepositories) add(repository string) { + if unadopted.index >= unadopted.start && unadopted.index < unadopted.end { + unadopted.repositories = append(unadopted.repositories, repository) + } + unadopted.index++ +} + +func checkUnadoptedRepositories(userName string, repoNamesToCheck []string, unadopted *unadoptedRrepositories) error { + if len(repoNamesToCheck) == 0 { + return nil + } + ctxUser, err := user_model.GetUserByName(userName) + if err != nil { + if user_model.IsErrUserNotExist(err) { + log.Debug("Missing user: %s", userName) + return nil + } + return err + } + repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{ + Actor: ctxUser, + Private: true, + ListOptions: db.ListOptions{ + Page: 1, + PageSize: len(repoNamesToCheck), + }, LowerNames: repoNamesToCheck}) + if err != nil { + return err + } + if len(repos) == len(repoNamesToCheck) { + return nil + } + repoNames := make(map[string]bool, len(repos)) + for _, repo := range repos { + repoNames[repo.LowerName] = true + } + for _, repoName := range repoNamesToCheck { + if _, ok := repoNames[repoName]; !ok { + unadopted.add(filepath.Join(userName, repoName)) + } + } + return nil +} + // ListUnadoptedRepositories lists all the unadopted repositories that match the provided query func ListUnadoptedRepositories(query string, opts *db.ListOptions) ([]string, int, error) { globUser, _ := glob.Compile("*") @@ -236,15 +287,17 @@ func ListUnadoptedRepositories(query string, opts *db.ListOptions) ([]string, in } } } - start := (opts.Page - 1) * opts.PageSize - end := start + opts.PageSize - - repoNamesToCheck := make([]string, 0, opts.PageSize) + var repoNamesToCheck []string - repoNames := make([]string, 0, opts.PageSize) - var ctxUser *user_model.User + start := (opts.Page - 1) * opts.PageSize + unadopted := &unadoptedRrepositories{ + repositories: make([]string, 0, opts.PageSize), + start: start, + end: start + opts.PageSize, + index: 0, + } - count := 0 + var userName string // We're going to iterate by pagesize. root := filepath.Clean(setting.RepoRootPath) @@ -258,51 +311,16 @@ func ListUnadoptedRepositories(query string, opts *db.ListOptions) ([]string, in if !strings.ContainsRune(path[len(root)+1:], filepath.Separator) { // Got a new user - - // Clean up old repoNamesToCheck - if len(repoNamesToCheck) > 0 { - repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{ - Actor: ctxUser, - Private: true, - ListOptions: db.ListOptions{ - Page: 1, - PageSize: opts.PageSize, - }, LowerNames: repoNamesToCheck}) - if err != nil { - return err - } - for _, name := range repoNamesToCheck { - found := false - repoLoopCatchup: - for i, repo := range repos { - if repo.LowerName == name { - found = true - repos = append(repos[:i], repos[i+1:]...) - break repoLoopCatchup - } - } - if !found { - if count >= start && count < end { - repoNames = append(repoNames, fmt.Sprintf("%s/%s", ctxUser.Name, name)) - } - count++ - } - } - repoNamesToCheck = repoNamesToCheck[:0] + if err = checkUnadoptedRepositories(userName, repoNamesToCheck, unadopted); err != nil { + return err } + repoNamesToCheck = repoNamesToCheck[:0] if !globUser.Match(info.Name()) { return filepath.SkipDir } - ctxUser, err = user_model.GetUserByName(info.Name()) - if err != nil { - if user_model.IsErrUserNotExist(err) { - log.Debug("Missing user: %s", info.Name()) - return filepath.SkipDir - } - return err - } + userName = info.Name() return nil } @@ -315,74 +333,16 @@ func ListUnadoptedRepositories(query string, opts *db.ListOptions) ([]string, in if repo_model.IsUsableRepoName(name) != nil || strings.ToLower(name) != name || !globRepo.Match(name) { return filepath.SkipDir } - if count < end { - repoNamesToCheck = append(repoNamesToCheck, name) - if len(repoNamesToCheck) >= opts.PageSize { - repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{ - Actor: ctxUser, - Private: true, - ListOptions: db.ListOptions{ - Page: 1, - PageSize: opts.PageSize, - }, LowerNames: repoNamesToCheck}) - if err != nil { - return err - } - for _, name := range repoNamesToCheck { - found := false - repoLoop: - for i, repo := range repos { - if repo.LowerName == name { - found = true - repos = append(repos[:i], repos[i+1:]...) - break repoLoop - } - } - if !found { - if count >= start && count < end { - repoNames = append(repoNames, fmt.Sprintf("%s/%s", ctxUser.Name, name)) - } - count++ - } - } - repoNamesToCheck = repoNamesToCheck[:0] - } - return filepath.SkipDir - } - count++ + + repoNamesToCheck = append(repoNamesToCheck, name) return filepath.SkipDir }); err != nil { return nil, 0, err } - if len(repoNamesToCheck) > 0 { - repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{ - Actor: ctxUser, - Private: true, - ListOptions: db.ListOptions{ - Page: 1, - PageSize: opts.PageSize, - }, LowerNames: repoNamesToCheck}) - if err != nil { - return nil, 0, err - } - for _, name := range repoNamesToCheck { - found := false - repoLoop: - for i, repo := range repos { - if repo.LowerName == name { - found = true - repos = append(repos[:i], repos[i+1:]...) - break repoLoop - } - } - if !found { - if count >= start && count < end { - repoNames = append(repoNames, fmt.Sprintf("%s/%s", ctxUser.Name, name)) - } - count++ - } - } + if err := checkUnadoptedRepositories(userName, repoNamesToCheck, unadopted); err != nil { + return nil, 0, err } - return repoNames, count, nil + + return unadopted.repositories, unadopted.index, nil } diff --git a/services/repository/adopt_test.go b/services/repository/adopt_test.go new file mode 100644 index 0000000000000..2053151106fd2 --- /dev/null +++ b/services/repository/adopt_test.go @@ -0,0 +1,86 @@ +// Copyright 2021 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. + +package repository + +import ( + "os" + "path" + "testing" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/modules/setting" + + "github.com/stretchr/testify/assert" +) + +func TestCheckUnadoptedRepositories_Add(t *testing.T) { + start := 10 + end := 20 + unadopted := &unadoptedRrepositories{ + start: start, + end: end, + index: 0, + } + + total := 30 + for i := 0; i < total; i++ { + unadopted.add("something") + } + + assert.Equal(t, total, unadopted.index) + assert.Equal(t, end-start, len(unadopted.repositories)) +} + +func TestCheckUnadoptedRepositories(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + // + // Non existent user + // + unadopted := &unadoptedRrepositories{start: 0, end: 100} + err := checkUnadoptedRepositories("notauser", []string{"repo"}, unadopted) + assert.NoError(t, err) + assert.Equal(t, 0, len(unadopted.repositories)) + // + // Unadopted repository is returned + // Existing (adopted) repository is not returned + // + userName := "user2" + repoName := "repo2" + unadoptedRepoName := "unadopted" + unadopted = &unadoptedRrepositories{start: 0, end: 100} + err = checkUnadoptedRepositories(userName, []string{repoName, unadoptedRepoName}, unadopted) + assert.NoError(t, err) + assert.Equal(t, []string{path.Join(userName, unadoptedRepoName)}, unadopted.repositories) + // + // Existing (adopted) repository is not returned + // + unadopted = &unadoptedRrepositories{start: 0, end: 100} + err = checkUnadoptedRepositories(userName, []string{repoName}, unadopted) + assert.NoError(t, err) + assert.Equal(t, 0, len(unadopted.repositories)) + assert.Equal(t, 0, unadopted.index) +} + +func TestListUnadoptedRepositories_ListOptions(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + username := "user2" + unadoptedList := []string{path.Join(username, "unadopted1"), path.Join(username, "unadopted2")} + for _, unadopted := range unadoptedList { + _ = os.Mkdir(path.Join(setting.RepoRootPath, unadopted+".git"), 0755) + } + + opts := db.ListOptions{Page: 1, PageSize: 1} + repoNames, count, err := ListUnadoptedRepositories("", &opts) + assert.NoError(t, err) + assert.Equal(t, 2, count) + assert.Equal(t, unadoptedList[0], repoNames[0]) + + opts = db.ListOptions{Page: 2, PageSize: 1} + repoNames, count, err = ListUnadoptedRepositories("", &opts) + assert.NoError(t, err) + assert.Equal(t, 2, count) + assert.Equal(t, unadoptedList[1], repoNames[0]) +}