Skip to content

Commit 44937ea

Browse files
committed
Merge branch 'main' of https://github.com/go-gitea/gitea into oauth-client-type
2 parents eb87013 + 083ac16 commit 44937ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2383
-1905
lines changed

.drone.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ steps:
571571

572572
# TODO: We should probably build all dependencies into a test image
573573
- name: test-e2e
574-
image: mcr.microsoft.com/playwright:v1.24.0-focal
574+
image: mcr.microsoft.com/playwright:v1.27.0-focal
575575
commands:
576576
- curl -sLO https://go.dev/dl/go1.19.linux-amd64.tar.gz && tar -C /usr/local -xzf go1.19.linux-amd64.tar.gz
577577
- groupadd --gid 1001 gitea && useradd -m --gid 1001 --uid 1001 gitea

.eslintrc.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ rules:
185185
linebreak-style: [2, unix]
186186
lines-around-comment: [0]
187187
lines-between-class-members: [0]
188+
logical-assignment-operators: [0]
188189
max-classes-per-file: [0]
189190
max-depth: [0]
190191
max-len: [0]
@@ -245,7 +246,7 @@ rules:
245246
no-floating-decimal: [0]
246247
no-func-assign: [2]
247248
no-global-assign: [2]
248-
no-implicit-coercion: [0]
249+
no-implicit-coercion: [2]
249250
no-implicit-globals: [0]
250251
no-implied-eval: [2]
251252
no-import-assign: [2]
@@ -322,7 +323,7 @@ rules:
322323
no-unused-private-class-members: [2]
323324
no-unused-vars: [2, {args: all, argsIgnorePattern: ^_, varsIgnorePattern: ^_, caughtErrorsIgnorePattern: ^_, destructuredArrayIgnorePattern: ^_, ignoreRestSiblings: false}]
324325
no-use-before-define: [2, {functions: false, classes: true, variables: true, allowNamedExports: true}]
325-
no-useless-backreference: [0]
326+
no-useless-backreference: [2]
326327
no-useless-call: [2]
327328
no-useless-catch: [2]
328329
no-useless-computed-key: [2]
@@ -353,7 +354,7 @@ rules:
353354
prefer-named-capture-group: [0]
354355
prefer-numeric-literals: [2]
355356
prefer-object-has-own: [0]
356-
prefer-object-spread: [0]
357+
prefer-object-spread: [2]
357358
prefer-promise-reject-errors: [2, {allowEmptyReject: false}]
358359
prefer-regex-literals: [2]
359360
prefer-rest-params: [2]
@@ -455,6 +456,7 @@ rules:
455456
unicorn/no-static-only-class: [2]
456457
unicorn/no-thenable: [2]
457458
unicorn/no-this-assignment: [2]
459+
unicorn/no-unnecessary-await: [2]
458460
unicorn/no-unreadable-array-destructuring: [0]
459461
unicorn/no-unreadable-iife: [2]
460462
unicorn/no-unsafe-regex: [0]
@@ -519,6 +521,7 @@ rules:
519521
unicorn/require-number-to-fixed-digits-argument: [2]
520522
unicorn/require-post-message-target-origin: [0]
521523
unicorn/string-content: [0]
524+
unicorn/switch-case-braces: [0]
522525
unicorn/template-indent: [2]
523526
unicorn/text-encoding-identifier-case: [0]
524527
unicorn/throw-new-error: [2]

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ lint: lint-frontend lint-backend
341341

342342
.PHONY: lint-frontend
343343
lint-frontend: node_modules
344-
npx eslint --color --max-warnings=0 --ext js,vue web_src/js build *.config.js docs/assets/js tests/e2e/*.test.e2e.js tests/e2e/utils_e2e.js
344+
npx eslint --color --max-warnings=0 --ext js,vue web_src/js build *.config.js docs/assets/js tests/e2e
345345
npx stylelint --color --max-warnings=0 web_src/less
346346
npx spectral lint -q -F hint $(SWAGGER_SPEC)
347347
npx markdownlint docs *.md

assets/go-licenses.json

Lines changed: 10 additions & 0 deletions
Large diffs are not rendered by default.

docs/content/doc/developers/guidelines-backend.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,18 @@ Some actions should allow for rollback when database record insertion/update/del
6767
So services must be allowed to create a database transaction. Here is some example,
6868

6969
```go
70-
// servcies/repository/repo.go
71-
func CreateXXXX() error {\
72-
ctx, committer, err := db.TxContext()
73-
if err != nil {
74-
return err
75-
}
76-
defer committer.Close()
77-
78-
// do something, if return err, it will rollback automatically when `committer.Close()` is invoked.
79-
if err := issues.UpdateIssue(ctx, repoID); err != nil {
80-
// ...
81-
}
82-
83-
// ......
84-
85-
return committer.Commit()
70+
// services/repository/repository.go
71+
func CreateXXXX() error {
72+
return db.WithTx(func(ctx context.Context) error {
73+
e := db.GetEngine(ctx)
74+
// do something, if err is returned, it will rollback automatically
75+
if err := issues.UpdateIssue(ctx, repoID); err != nil {
76+
// ...
77+
return err
78+
}
79+
// ...
80+
return nil
81+
})
8682
}
8783
```
8884

@@ -94,14 +90,14 @@ If the function will be used in the transaction, just let `context.Context` as t
9490
func UpdateIssue(ctx context.Context, repoID int64) error {
9591
e := db.GetEngine(ctx)
9692

97-
// ......
93+
// ...
9894
}
9995
```
10096

10197
### Package Name
10298

10399
For the top level package, use a plural as package name, i.e. `services`, `models`, for sub packages, use singular,
104-
i.e. `servcies/user`, `models/repository`.
100+
i.e. `services/user`, `models/repository`.
105101

106102
### Import Alias
107103

jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// to run tests with ES6 module, node must run with "--experimental-vm-modules", or see Makefile's "test-frontend" for reference
12
export default {
23
rootDir: 'web_src',
34
setupFilesAfterEnv: ['jest-extended/all'],
@@ -7,6 +8,8 @@ export default {
78
transform: {
89
'\\.svg$': '<rootDir>/js/testUtils/jestRawLoader.js',
910
},
11+
setupFiles: [
12+
'./js/testUtils/jestSetup.js', // prepare global variables used by our code (eg: window.config)
13+
],
1014
verbose: false,
1115
};
12-

models/auth/oauth2.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ func updateOAuth2Application(ctx context.Context, app *OAuth2Application) error
236236

237237
func deleteOAuth2Application(ctx context.Context, id, userid int64) error {
238238
sess := db.GetEngine(ctx)
239-
if deleted, err := sess.Delete(&OAuth2Application{ID: id, UID: userid}); err != nil {
239+
// the userid could be 0 if the app is instance-wide
240+
if deleted, err := sess.Where(builder.Eq{"id": id, "uid": userid}).Delete(&OAuth2Application{}); err != nil {
240241
return err
241242
} else if deleted == 0 {
242243
return ErrOAuthApplicationNotFound{ID: id}
@@ -487,7 +488,7 @@ func GetOAuth2GrantsByUserID(ctx context.Context, uid int64) ([]*OAuth2Grant, er
487488

488489
// RevokeOAuth2Grant deletes the grant with grantID and userID
489490
func RevokeOAuth2Grant(ctx context.Context, grantID, userID int64) error {
490-
_, err := db.DeleteByBean(ctx, &OAuth2Grant{ID: grantID, UserID: userID})
491+
_, err := db.GetEngine(ctx).Where(builder.Eq{"id": grantID, "user_id": userID}).Delete(&OAuth2Grant{})
491492
return err
492493
}
493494

modules/git/parse_nogogit.go

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,70 +22,72 @@ func ParseTreeEntries(data []byte) ([]*TreeEntry, error) {
2222
return parseTreeEntries(data, nil)
2323
}
2424

25+
var sepSpace = []byte{' '}
26+
2527
func parseTreeEntries(data []byte, ptree *Tree) ([]*TreeEntry, error) {
26-
entries := make([]*TreeEntry, 0, 10)
28+
var err error
29+
entries := make([]*TreeEntry, 0, bytes.Count(data, []byte{'\n'})+1)
2730
for pos := 0; pos < len(data); {
28-
// expect line to be of the form "<mode> <type> <sha> <space-padded-size>\t<filename>"
31+
// expect line to be of the form:
32+
// <mode> <type> <sha> <space-padded-size>\t<filename>
33+
// <mode> <type> <sha>\t<filename>
34+
posEnd := bytes.IndexByte(data[pos:], '\n')
35+
if posEnd == -1 {
36+
posEnd = len(data)
37+
} else {
38+
posEnd += pos
39+
}
40+
line := data[pos:posEnd]
41+
posTab := bytes.IndexByte(line, '\t')
42+
if posTab == -1 {
43+
return nil, fmt.Errorf("invalid ls-tree output (no tab): %q", line)
44+
}
45+
2946
entry := new(TreeEntry)
3047
entry.ptree = ptree
31-
if pos+6 > len(data) {
32-
return nil, fmt.Errorf("Invalid ls-tree output: %s", string(data))
48+
49+
entryAttrs := line[:posTab]
50+
entryName := line[posTab+1:]
51+
52+
entryMode, entryAttrs, _ := bytes.Cut(entryAttrs, sepSpace)
53+
_ /* entryType */, entryAttrs, _ = bytes.Cut(entryAttrs, sepSpace) // the type is not used, the mode is enough to determine the type
54+
entryObjectID, entryAttrs, _ := bytes.Cut(entryAttrs, sepSpace)
55+
if len(entryAttrs) > 0 {
56+
entrySize := entryAttrs // the last field is the space-padded-size
57+
entry.size, _ = strconv.ParseInt(strings.TrimSpace(string(entrySize)), 10, 64)
58+
entry.sized = true
3359
}
34-
switch string(data[pos : pos+6]) {
60+
61+
switch string(entryMode) {
3562
case "100644":
3663
entry.entryMode = EntryModeBlob
37-
pos += 12 // skip over "100644 blob "
3864
case "100755":
3965
entry.entryMode = EntryModeExec
40-
pos += 12 // skip over "100755 blob "
4166
case "120000":
4267
entry.entryMode = EntryModeSymlink
43-
pos += 12 // skip over "120000 blob "
4468
case "160000":
4569
entry.entryMode = EntryModeCommit
46-
pos += 14 // skip over "160000 object "
4770
case "040000", "040755": // git uses 040000 for tree object, but some users may get 040755 for unknown reasons
4871
entry.entryMode = EntryModeTree
49-
pos += 12 // skip over "040000 tree "
5072
default:
51-
return nil, fmt.Errorf("unknown type: %v", string(data[pos:pos+6]))
73+
return nil, fmt.Errorf("unknown type: %v", string(entryMode))
5274
}
5375

54-
if pos+40 > len(data) {
55-
return nil, fmt.Errorf("Invalid ls-tree output: %s", string(data))
56-
}
57-
id, err := NewIDFromString(string(data[pos : pos+40]))
76+
entry.ID, err = NewIDFromString(string(entryObjectID))
5877
if err != nil {
59-
return nil, fmt.Errorf("Invalid ls-tree output: %v", err)
60-
}
61-
entry.ID = id
62-
pos += 41 // skip over sha and trailing space
63-
64-
end := pos + bytes.IndexByte(data[pos:], '\t')
65-
if end < pos {
66-
return nil, fmt.Errorf("Invalid ls-tree -l output: %s", string(data))
67-
}
68-
entry.size, _ = strconv.ParseInt(strings.TrimSpace(string(data[pos:end])), 10, 64)
69-
entry.sized = true
70-
71-
pos = end + 1
72-
73-
end = pos + bytes.IndexByte(data[pos:], '\n')
74-
if end < pos {
75-
return nil, fmt.Errorf("Invalid ls-tree output: %s", string(data))
78+
return nil, fmt.Errorf("invalid ls-tree output (invalid object id): %q, err: %w", line, err)
7679
}
7780

78-
// In case entry name is surrounded by double quotes(it happens only in git-shell).
79-
if data[pos] == '"' {
80-
entry.name, err = strconv.Unquote(string(data[pos:end]))
81+
if len(entryName) > 0 && entryName[0] == '"' {
82+
entry.name, err = strconv.Unquote(string(entryName))
8183
if err != nil {
82-
return nil, fmt.Errorf("Invalid ls-tree output: %v", err)
84+
return nil, fmt.Errorf("invalid ls-tree output (invalid name): %q, err: %w", line, err)
8385
}
8486
} else {
85-
entry.name = string(data[pos:end])
87+
entry.name = string(entryName)
8688
}
8789

88-
pos = end + 1
90+
pos = posEnd + 1
8991
entries = append(entries, entry)
9092
}
9193
return entries, nil

modules/git/parse_nogogit_test.go

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/stretchr/testify/assert"
1313
)
1414

15-
func TestParseTreeEntries(t *testing.T) {
15+
func TestParseTreeEntriesLong(t *testing.T) {
1616
testCases := []struct {
1717
Input string
1818
Expected []*TreeEntry
@@ -59,11 +59,47 @@ func TestParseTreeEntries(t *testing.T) {
5959
assert.NoError(t, err)
6060
assert.Len(t, entries, len(testCase.Expected))
6161
for i, entry := range entries {
62-
assert.EqualValues(t, testCase.Expected[i].ID, entry.ID)
63-
assert.EqualValues(t, testCase.Expected[i].name, entry.name)
64-
assert.EqualValues(t, testCase.Expected[i].entryMode, entry.entryMode)
65-
assert.EqualValues(t, testCase.Expected[i].sized, entry.sized)
66-
assert.EqualValues(t, testCase.Expected[i].size, entry.size)
62+
assert.EqualValues(t, testCase.Expected[i], entry)
6763
}
6864
}
6965
}
66+
67+
func TestParseTreeEntriesShort(t *testing.T) {
68+
testCases := []struct {
69+
Input string
70+
Expected []*TreeEntry
71+
}{
72+
{
73+
Input: `100644 blob ea0d83c9081af9500ac9f804101b3fd0a5c293af README.md
74+
040000 tree 84b90550547016f73c5dd3f50dea662389e67b6d assets
75+
`,
76+
Expected: []*TreeEntry{
77+
{
78+
ID: MustIDFromString("ea0d83c9081af9500ac9f804101b3fd0a5c293af"),
79+
name: "README.md",
80+
entryMode: EntryModeBlob,
81+
},
82+
{
83+
ID: MustIDFromString("84b90550547016f73c5dd3f50dea662389e67b6d"),
84+
name: "assets",
85+
entryMode: EntryModeTree,
86+
},
87+
},
88+
},
89+
}
90+
for _, testCase := range testCases {
91+
entries, err := ParseTreeEntries([]byte(testCase.Input))
92+
assert.NoError(t, err)
93+
assert.Len(t, entries, len(testCase.Expected))
94+
for i, entry := range entries {
95+
assert.EqualValues(t, testCase.Expected[i], entry)
96+
}
97+
}
98+
}
99+
100+
func TestParseTreeEntriesInvalid(t *testing.T) {
101+
// there was a panic: "runtime error: slice bounds out of range" when the input was invalid: #20315
102+
entries, err := ParseTreeEntries([]byte("100644 blob ea0d83c9081af9500ac9f804101b3fd0a5c293af"))
103+
assert.Error(t, err)
104+
assert.Len(t, entries, 0)
105+
}

modules/git/repo_language_stats_nogogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
5757

5858
tree := commit.Tree
5959

60-
entries, err := tree.ListEntriesRecursive()
60+
entries, err := tree.ListEntriesRecursiveWithSize()
6161
if err != nil {
6262
return nil, err
6363
}

0 commit comments

Comments
 (0)