Skip to content

Commit

Permalink
Merge remote-tracking branch 'giteaoffical/main'
Browse files Browse the repository at this point in the history
* giteaoffical/main:
  tests: s/GITEA_UNIT_TESTS_VERBOSE/GITEA_UNIT_TESTS_LOG_SQL/ (go-gitea#18142)
  services/repository: fix ListUnadoptedRepositories incorrect total count (go-gitea#17865)
  Improve document for developers: Windows CGO, unit test option (go-gitea#18140)
  Reset the conflicted files list in testpatch (go-gitea#18139)
  Use correct translation key (go-gitea#18135)
  • Loading branch information
zjjhot committed Jan 1, 2022
2 parents 21a5b19 + 1a4e2bf commit 739d84c
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 116 deletions.
14 changes: 12 additions & 2 deletions docs/content/doc/developers/hacking-on-gitea.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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.

Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion models/unittest/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 8 additions & 1 deletion routers/web/repo/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion routers/web/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion routers/web/repo/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions services/pull/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
180 changes: 70 additions & 110 deletions services/repository/adopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("*")
Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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
}
Loading

0 comments on commit 739d84c

Please sign in to comment.