Skip to content

Commit 739d84c

Browse files
committed
Merge remote-tracking branch 'giteaoffical/main'
* 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)
2 parents 21a5b19 + 1a4e2bf commit 739d84c

File tree

8 files changed

+192
-116
lines changed

8 files changed

+192
-116
lines changed

docs/content/doc/developers/hacking-on-gitea.en-us.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ One of these three distributions of Make will run on Windows:
7373
- 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.
7474
- In MingGW-w64, the binary is called `mingw32-make.exe` instead of `make.exe`. Add the `bin` folder to `PATH`.
7575
- In MSYS2, you can use `make` directly. See [MSYS2 Porting](https://www.msys2.org/wiki/Porting/).
76+
- 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`.
7677
- [Chocolatey package](https://chocolatey.org/packages/make). Run `choco install make`
7778

7879
**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
273274

274275
There are two types of test run by Gitea: Unit tests and Integration Tests.
275276

277+
### Unit Tests
278+
279+
Unit tests are covered by `*_test.go` in `go test` system.
280+
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).
281+
276282
```bash
277283
TAGS="bindata sqlite sqlite_unlock_notify" make test # Runs the unit tests
278284
```
279285

286+
### Integration Tests
287+
280288
Unit tests will not and cannot completely test Gitea alone. Therefore, we
281289
have written integration tests; however, these are database dependent.
282290

@@ -288,10 +296,12 @@ will run the integration tests in an SQLite environment. Integration tests
288296
require `git lfs` to be installed. Other database tests are available but
289297
may need adjustment to the local environment.
290298

291-
Look at
292-
[`integrations/README.md`](https://github.com/go-gitea/gitea/blob/main/integrations/README.md)
299+
Take a look at [`integrations/README.md`](https://github.com/go-gitea/gitea/blob/main/integrations/README.md)
293300
for more information and how to run a single test.
294301

302+
303+
### Testing for a PR
304+
295305
Our continuous integration will test the code passes its unit tests and that
296306
all supported databases will pass integration test in a Docker environment.
297307
Migration from several recent versions of Gitea will also be tested.

models/unittest/testdb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func CreateTestEngine(opts FixturesOptions) error {
152152
if err = db.SyncAllTables(); err != nil {
153153
return err
154154
}
155-
switch os.Getenv("GITEA_UNIT_TESTS_VERBOSE") {
155+
switch os.Getenv("GITEA_UNIT_TESTS_LOG_SQL") {
156156
case "true", "1":
157157
x.ShowSQL(true)
158158
}

routers/web/repo/migrate.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ func handleMigrateError(ctx *context.Context, owner *user_model.User, err error,
8181
case migrations.IsTwoFactorAuthError(err):
8282
ctx.RenderWithErr(ctx.Tr("form.2fa_auth_required"), tpl, form)
8383
case repo_model.IsErrReachLimitOfRepo(err):
84-
ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", owner.MaxCreationLimit()), tpl, form)
84+
var msg string
85+
maxCreationLimit := ctx.User.MaxCreationLimit()
86+
if maxCreationLimit == 1 {
87+
msg = ctx.Tr("repo.form.reach_limit_of_creation_1", maxCreationLimit)
88+
} else {
89+
msg = ctx.Tr("repo.form.reach_limit_of_creation_n", maxCreationLimit)
90+
}
91+
ctx.RenderWithErr(msg, tpl, form)
8592
case repo_model.IsErrRepoAlreadyExist(err):
8693
ctx.Data["Err_RepoName"] = true
8794
ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tpl, form)

routers/web/repo/repo.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,14 @@ func Create(ctx *context.Context) {
162162
func handleCreateError(ctx *context.Context, owner *user_model.User, err error, name string, tpl base.TplName, form interface{}) {
163163
switch {
164164
case repo_model.IsErrReachLimitOfRepo(err):
165-
ctx.RenderWithErr(ctx.Tr("repo.form.reach_limit_of_creation", owner.MaxCreationLimit()), tpl, form)
165+
var msg string
166+
maxCreationLimit := ctx.User.MaxCreationLimit()
167+
if maxCreationLimit == 1 {
168+
msg = ctx.Tr("repo.form.reach_limit_of_creation_1", maxCreationLimit)
169+
} else {
170+
msg = ctx.Tr("repo.form.reach_limit_of_creation_n", maxCreationLimit)
171+
}
172+
ctx.RenderWithErr(msg, tpl, form)
166173
case repo_model.IsErrRepoAlreadyExist(err):
167174
ctx.Data["Err_RepoName"] = true
168175
ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tpl, form)

routers/web/repo/setting.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,12 @@ func SettingsPost(ctx *context.Context) {
609609
}
610610

611611
if !ctx.Repo.Owner.CanCreateRepo() {
612-
ctx.Flash.Error(ctx.Tr("repo.form.reach_limit_of_creation", ctx.User.MaxCreationLimit()))
612+
maxCreationLimit := ctx.User.MaxCreationLimit()
613+
if maxCreationLimit == 1 {
614+
ctx.Flash.Error(ctx.Tr("repo.form.reach_limit_of_creation_1", maxCreationLimit))
615+
} else {
616+
ctx.Flash.Error(ctx.Tr("repo.form.reach_limit_of_creation_n", maxCreationLimit))
617+
}
613618
ctx.Redirect(repo.Link() + "/settings")
614619
return
615620
}

services/pull/patch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
235235
}()
236236

237237
numberOfConflicts := 0
238+
pr.ConflictedFiles = make([]string, 0, 5)
238239
conflict := false
239240

240241
for file := range unmerged {

services/repository/adopt.go

Lines changed: 70 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,57 @@ func DeleteUnadoptedRepository(doer, u *user_model.User, repoName string) error
217217
return util.RemoveAll(repoPath)
218218
}
219219

220+
type unadoptedRrepositories struct {
221+
repositories []string
222+
index int
223+
start int
224+
end int
225+
}
226+
227+
func (unadopted *unadoptedRrepositories) add(repository string) {
228+
if unadopted.index >= unadopted.start && unadopted.index < unadopted.end {
229+
unadopted.repositories = append(unadopted.repositories, repository)
230+
}
231+
unadopted.index++
232+
}
233+
234+
func checkUnadoptedRepositories(userName string, repoNamesToCheck []string, unadopted *unadoptedRrepositories) error {
235+
if len(repoNamesToCheck) == 0 {
236+
return nil
237+
}
238+
ctxUser, err := user_model.GetUserByName(userName)
239+
if err != nil {
240+
if user_model.IsErrUserNotExist(err) {
241+
log.Debug("Missing user: %s", userName)
242+
return nil
243+
}
244+
return err
245+
}
246+
repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{
247+
Actor: ctxUser,
248+
Private: true,
249+
ListOptions: db.ListOptions{
250+
Page: 1,
251+
PageSize: len(repoNamesToCheck),
252+
}, LowerNames: repoNamesToCheck})
253+
if err != nil {
254+
return err
255+
}
256+
if len(repos) == len(repoNamesToCheck) {
257+
return nil
258+
}
259+
repoNames := make(map[string]bool, len(repos))
260+
for _, repo := range repos {
261+
repoNames[repo.LowerName] = true
262+
}
263+
for _, repoName := range repoNamesToCheck {
264+
if _, ok := repoNames[repoName]; !ok {
265+
unadopted.add(filepath.Join(userName, repoName))
266+
}
267+
}
268+
return nil
269+
}
270+
220271
// ListUnadoptedRepositories lists all the unadopted repositories that match the provided query
221272
func ListUnadoptedRepositories(query string, opts *db.ListOptions) ([]string, int, error) {
222273
globUser, _ := glob.Compile("*")
@@ -236,15 +287,17 @@ func ListUnadoptedRepositories(query string, opts *db.ListOptions) ([]string, in
236287
}
237288
}
238289
}
239-
start := (opts.Page - 1) * opts.PageSize
240-
end := start + opts.PageSize
241-
242-
repoNamesToCheck := make([]string, 0, opts.PageSize)
290+
var repoNamesToCheck []string
243291

244-
repoNames := make([]string, 0, opts.PageSize)
245-
var ctxUser *user_model.User
292+
start := (opts.Page - 1) * opts.PageSize
293+
unadopted := &unadoptedRrepositories{
294+
repositories: make([]string, 0, opts.PageSize),
295+
start: start,
296+
end: start + opts.PageSize,
297+
index: 0,
298+
}
246299

247-
count := 0
300+
var userName string
248301

249302
// We're going to iterate by pagesize.
250303
root := filepath.Clean(setting.RepoRootPath)
@@ -258,51 +311,16 @@ func ListUnadoptedRepositories(query string, opts *db.ListOptions) ([]string, in
258311

259312
if !strings.ContainsRune(path[len(root)+1:], filepath.Separator) {
260313
// Got a new user
261-
262-
// Clean up old repoNamesToCheck
263-
if len(repoNamesToCheck) > 0 {
264-
repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{
265-
Actor: ctxUser,
266-
Private: true,
267-
ListOptions: db.ListOptions{
268-
Page: 1,
269-
PageSize: opts.PageSize,
270-
}, LowerNames: repoNamesToCheck})
271-
if err != nil {
272-
return err
273-
}
274-
for _, name := range repoNamesToCheck {
275-
found := false
276-
repoLoopCatchup:
277-
for i, repo := range repos {
278-
if repo.LowerName == name {
279-
found = true
280-
repos = append(repos[:i], repos[i+1:]...)
281-
break repoLoopCatchup
282-
}
283-
}
284-
if !found {
285-
if count >= start && count < end {
286-
repoNames = append(repoNames, fmt.Sprintf("%s/%s", ctxUser.Name, name))
287-
}
288-
count++
289-
}
290-
}
291-
repoNamesToCheck = repoNamesToCheck[:0]
314+
if err = checkUnadoptedRepositories(userName, repoNamesToCheck, unadopted); err != nil {
315+
return err
292316
}
317+
repoNamesToCheck = repoNamesToCheck[:0]
293318

294319
if !globUser.Match(info.Name()) {
295320
return filepath.SkipDir
296321
}
297322

298-
ctxUser, err = user_model.GetUserByName(info.Name())
299-
if err != nil {
300-
if user_model.IsErrUserNotExist(err) {
301-
log.Debug("Missing user: %s", info.Name())
302-
return filepath.SkipDir
303-
}
304-
return err
305-
}
323+
userName = info.Name()
306324
return nil
307325
}
308326

@@ -315,74 +333,16 @@ func ListUnadoptedRepositories(query string, opts *db.ListOptions) ([]string, in
315333
if repo_model.IsUsableRepoName(name) != nil || strings.ToLower(name) != name || !globRepo.Match(name) {
316334
return filepath.SkipDir
317335
}
318-
if count < end {
319-
repoNamesToCheck = append(repoNamesToCheck, name)
320-
if len(repoNamesToCheck) >= opts.PageSize {
321-
repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{
322-
Actor: ctxUser,
323-
Private: true,
324-
ListOptions: db.ListOptions{
325-
Page: 1,
326-
PageSize: opts.PageSize,
327-
}, LowerNames: repoNamesToCheck})
328-
if err != nil {
329-
return err
330-
}
331-
for _, name := range repoNamesToCheck {
332-
found := false
333-
repoLoop:
334-
for i, repo := range repos {
335-
if repo.LowerName == name {
336-
found = true
337-
repos = append(repos[:i], repos[i+1:]...)
338-
break repoLoop
339-
}
340-
}
341-
if !found {
342-
if count >= start && count < end {
343-
repoNames = append(repoNames, fmt.Sprintf("%s/%s", ctxUser.Name, name))
344-
}
345-
count++
346-
}
347-
}
348-
repoNamesToCheck = repoNamesToCheck[:0]
349-
}
350-
return filepath.SkipDir
351-
}
352-
count++
336+
337+
repoNamesToCheck = append(repoNamesToCheck, name)
353338
return filepath.SkipDir
354339
}); err != nil {
355340
return nil, 0, err
356341
}
357342

358-
if len(repoNamesToCheck) > 0 {
359-
repos, _, err := models.GetUserRepositories(&models.SearchRepoOptions{
360-
Actor: ctxUser,
361-
Private: true,
362-
ListOptions: db.ListOptions{
363-
Page: 1,
364-
PageSize: opts.PageSize,
365-
}, LowerNames: repoNamesToCheck})
366-
if err != nil {
367-
return nil, 0, err
368-
}
369-
for _, name := range repoNamesToCheck {
370-
found := false
371-
repoLoop:
372-
for i, repo := range repos {
373-
if repo.LowerName == name {
374-
found = true
375-
repos = append(repos[:i], repos[i+1:]...)
376-
break repoLoop
377-
}
378-
}
379-
if !found {
380-
if count >= start && count < end {
381-
repoNames = append(repoNames, fmt.Sprintf("%s/%s", ctxUser.Name, name))
382-
}
383-
count++
384-
}
385-
}
343+
if err := checkUnadoptedRepositories(userName, repoNamesToCheck, unadopted); err != nil {
344+
return nil, 0, err
386345
}
387-
return repoNames, count, nil
346+
347+
return unadopted.repositories, unadopted.index, nil
388348
}

0 commit comments

Comments
 (0)