Skip to content

Commit 2454ab6

Browse files
committed
Merge branch 'main' into lunny/catfile_batch_refactor
2 parents 334eb9e + a2ae7c6 commit 2454ab6

File tree

18 files changed

+194
-103
lines changed

18 files changed

+194
-103
lines changed

cmd/admin_auth_smtp.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ func smtpCLIFlags() []cli.Flag {
3939
&cli.BoolFlag{
4040
Name: "force-smtps",
4141
Usage: "SMTPS is always used on port 465. Set this to force SMTPS on other ports.",
42-
Value: true,
4342
},
4443
&cli.BoolFlag{
4544
Name: "skip-verify",
4645
Usage: "Skip TLS verify.",
47-
Value: true,
4846
},
4947
&cli.StringFlag{
5048
Name: "helo-hostname",
@@ -54,7 +52,6 @@ func smtpCLIFlags() []cli.Flag {
5452
&cli.BoolFlag{
5553
Name: "disable-helo",
5654
Usage: "Disable SMTP helo.",
57-
Value: true,
5855
},
5956
&cli.StringFlag{
6057
Name: "allowed-domains",
@@ -64,7 +61,6 @@ func smtpCLIFlags() []cli.Flag {
6461
&cli.BoolFlag{
6562
Name: "skip-local-2fa",
6663
Usage: "Skip 2FA to log on.",
67-
Value: true,
6864
},
6965
&cli.BoolFlag{
7066
Name: "active",

cmd/admin_auth_smtp_test.go

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ func TestAddSMTP(t *testing.T) {
6060
Auth: "PLAIN",
6161
Host: "localhost",
6262
Port: 25,
63-
// ForceSMTPS: true,
64-
// SkipVerify: true,
6563
},
66-
TwoFactorPolicy: "skip",
64+
TwoFactorPolicy: "",
6765
},
6866
},
6967
{
@@ -73,12 +71,12 @@ func TestAddSMTP(t *testing.T) {
7371
"--host", "localhost",
7472
"--port", "25",
7573
"--auth-type", "LOGIN",
76-
"--force-smtps=false",
77-
"--skip-verify=false",
74+
"--force-smtps",
75+
"--skip-verify",
7876
"--helo-hostname", "example.com",
79-
"--disable-helo=false",
77+
"--disable-helo=true",
8078
"--allowed-domains", "example.com,example.org",
81-
"--skip-local-2fa=false",
79+
"--skip-local-2fa",
8280
"--active=false",
8381
},
8482
source: &auth_model.Source{
@@ -89,13 +87,13 @@ func TestAddSMTP(t *testing.T) {
8987
Auth: "LOGIN",
9088
Host: "localhost",
9189
Port: 25,
92-
ForceSMTPS: false,
93-
SkipVerify: false,
90+
ForceSMTPS: true,
91+
SkipVerify: true,
9492
HeloHostname: "example.com",
95-
DisableHelo: false,
93+
DisableHelo: true,
9694
AllowedDomains: "example.com,example.org",
9795
},
98-
TwoFactorPolicy: "",
96+
TwoFactorPolicy: "skip",
9997
},
10098
},
10199
}
@@ -157,13 +155,10 @@ func TestUpdateSMTP(t *testing.T) {
157155
Name: "old name",
158156
IsActive: true,
159157
Cfg: &smtp.Source{
160-
Auth: "PLAIN",
161-
Host: "old host",
162-
Port: 26,
163-
ForceSMTPS: true,
164-
SkipVerify: true,
158+
Auth: "PLAIN",
159+
Host: "old host",
160+
Port: 26,
165161
},
166-
TwoFactorPolicy: "",
167162
},
168163
args: []string{
169164
"--id", "1",
@@ -177,13 +172,10 @@ func TestUpdateSMTP(t *testing.T) {
177172
Name: "test",
178173
IsActive: true,
179174
Cfg: &smtp.Source{
180-
Auth: "PLAIN",
181-
Host: "localhost",
182-
Port: 25,
183-
ForceSMTPS: true,
184-
SkipVerify: true,
175+
Auth: "PLAIN",
176+
Host: "localhost",
177+
Port: 25,
185178
},
186-
TwoFactorPolicy: "skip",
187179
},
188180
},
189181
{
@@ -197,10 +189,7 @@ func TestUpdateSMTP(t *testing.T) {
197189
Auth: "PLAIN",
198190
Host: "old host",
199191
Port: 26,
200-
ForceSMTPS: true,
201-
SkipVerify: true,
202192
HeloHostname: "old.example.com",
203-
DisableHelo: false,
204193
AllowedDomains: "old.example.com",
205194
},
206195
TwoFactorPolicy: "",
@@ -211,12 +200,12 @@ func TestUpdateSMTP(t *testing.T) {
211200
"--host", "localhost",
212201
"--port", "25",
213202
"--auth-type", "LOGIN",
214-
"--force-smtps=false",
215-
"--skip-verify=false",
203+
"--force-smtps",
204+
"--skip-verify",
216205
"--helo-hostname", "example.com",
217-
"--disable-helo=true",
206+
"--disable-helo",
218207
"--allowed-domains", "example.com,example.org",
219-
"--skip-local-2fa=true",
208+
"--skip-local-2fa",
220209
"--active=false",
221210
},
222211
authSource: &auth_model.Source{
@@ -228,8 +217,8 @@ func TestUpdateSMTP(t *testing.T) {
228217
Auth: "LOGIN",
229218
Host: "localhost",
230219
Port: 25,
231-
ForceSMTPS: false,
232-
SkipVerify: false,
220+
ForceSMTPS: true,
221+
SkipVerify: true,
233222
HeloHostname: "example.com",
234223
DisableHelo: true,
235224
AllowedDomains: "example.com,example.org",
@@ -252,11 +241,8 @@ func TestUpdateSMTP(t *testing.T) {
252241
Name: "test",
253242
IsActive: true,
254243
Cfg: &smtp.Source{
255-
Auth: "PLAIN",
256-
SkipVerify: true,
257-
ForceSMTPS: true,
244+
Auth: "PLAIN",
258245
},
259-
TwoFactorPolicy: "skip",
260246
}, nil
261247
},
262248

cmd/cmd.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,13 @@ func PrepareConsoleLoggerLevel(defaultLevel log.Level) func(context.Context, *cl
132132
return ctx, nil
133133
}
134134
}
135+
136+
func isValidDefaultSubCommand(cmd *cli.Command) (string, bool) {
137+
// Dirty patch for urfave/cli's strange design.
138+
// "./gitea bad-cmd" should not start the web server.
139+
rootArgs := cmd.Root().Args().Slice()
140+
if len(rootArgs) != 0 && rootArgs[0] != cmd.Name {
141+
return rootArgs[0], false
142+
}
143+
return "", true
144+
}

cmd/cmd_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package cmd
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/urfave/cli/v3"
12+
)
13+
14+
func TestDefaultCommand(t *testing.T) {
15+
test := func(t *testing.T, args []string, expectedRetName string, expectedRetValid bool) {
16+
called := false
17+
cmd := &cli.Command{
18+
DefaultCommand: "test",
19+
Commands: []*cli.Command{
20+
{
21+
Name: "test",
22+
Action: func(ctx context.Context, command *cli.Command) error {
23+
retName, retValid := isValidDefaultSubCommand(command)
24+
assert.Equal(t, expectedRetName, retName)
25+
assert.Equal(t, expectedRetValid, retValid)
26+
called = true
27+
return nil
28+
},
29+
},
30+
},
31+
}
32+
assert.NoError(t, cmd.Run(t.Context(), args))
33+
assert.True(t, called)
34+
}
35+
test(t, []string{"./gitea"}, "", true)
36+
test(t, []string{"./gitea", "test"}, "", true)
37+
test(t, []string{"./gitea", "other"}, "other", false)
38+
}

cmd/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ func NewMainApp(appVer AppVersion) *cli.Command {
152152
CmdDocs,
153153
}
154154

155+
// TODO: we should eventually drop the default command,
156+
// but not sure whether it would break Windows users who used to double-click the EXE to run.
155157
app.DefaultCommand = CmdWeb.Name
156158

157159
app.Flags = append(app.Flags, cli.VersionFlag)

cmd/manager_logging.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ var (
119119
Name: "rotate",
120120
Aliases: []string{"r"},
121121
Usage: "Rotate logs",
122-
Value: true,
123122
},
124123
&cli.Int64Flag{
125124
Name: "max-size",
@@ -130,7 +129,6 @@ var (
130129
Name: "daily",
131130
Aliases: []string{"d"},
132131
Usage: "Rotate logs daily",
133-
Value: true,
134132
},
135133
&cli.IntFlag{
136134
Name: "max-days",
@@ -141,7 +139,6 @@ var (
141139
Name: "compress",
142140
Aliases: []string{"z"},
143141
Usage: "Compress rotated logs",
144-
Value: true,
145142
},
146143
&cli.IntFlag{
147144
Name: "compression-level",

cmd/web.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ func runWeb(_ context.Context, cmd *cli.Command) error {
251251
}
252252
}()
253253

254+
if subCmdName, valid := isValidDefaultSubCommand(cmd); !valid {
255+
return fmt.Errorf("unknown command: %s", subCmdName)
256+
}
257+
254258
managerCtx, cancel := context.WithCancel(context.Background())
255259
graceful.InitManager(managerCtx)
256260
defer cancel()

models/packages/package_property.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ func DeletePropertyByID(ctx context.Context, propertyID int64) error {
9292
return err
9393
}
9494

95-
// DeletePropertyByName deletes properties by name
96-
func DeletePropertyByName(ctx context.Context, refType PropertyType, refID int64, name string) error {
95+
// DeletePropertiesByName deletes properties by name
96+
func DeletePropertiesByName(ctx context.Context, refType PropertyType, refID int64, name string) error {
9797
_, err := db.GetEngine(ctx).Where("ref_type = ? AND ref_id = ? AND name = ?", refType, refID, name).Delete(&PackageProperty{})
9898
return err
9999
}

modules/packages/content_store.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ func NewContentStore() *ContentStore {
2828
return contentStore
2929
}
3030

31-
// Get gets a package blob
32-
func (s *ContentStore) Get(key BlobHash256Key) (storage.Object, error) {
31+
func (s *ContentStore) OpenBlob(key BlobHash256Key) (storage.Object, error) {
3332
return s.store.Open(KeyToRelativePath(key))
3433
}
3534

routers/api/packages/container/manifest.go

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"code.gitea.io/gitea/modules/util"
2424
notify_service "code.gitea.io/gitea/services/notify"
2525
packages_service "code.gitea.io/gitea/services/packages"
26+
container_service "code.gitea.io/gitea/services/packages/container"
2627

2728
"github.com/opencontainers/go-digest"
2829
oci "github.com/opencontainers/image-spec/specs-go/v1"
@@ -84,12 +85,11 @@ func processOciImageManifest(ctx context.Context, mci *manifestCreationInfo, buf
8485
manifestDigest := ""
8586

8687
err := func() error {
87-
var manifest oci.Manifest
88-
if err := json.NewDecoder(buf).Decode(&manifest); err != nil {
88+
manifest, configDescriptor, metadata, err := container_service.ParseManifestMetadata(ctx, buf, mci.Owner.ID, mci.Image)
89+
if err != nil {
8990
return err
9091
}
91-
92-
if _, err := buf.Seek(0, io.SeekStart); err != nil {
92+
if _, err = buf.Seek(0, io.SeekStart); err != nil {
9393
return err
9494
}
9595

@@ -99,28 +99,7 @@ func processOciImageManifest(ctx context.Context, mci *manifestCreationInfo, buf
9999
}
100100
defer committer.Close()
101101

102-
configDescriptor, err := container_model.GetContainerBlob(ctx, &container_model.BlobSearchOptions{
103-
OwnerID: mci.Owner.ID,
104-
Image: mci.Image,
105-
Digest: string(manifest.Config.Digest),
106-
})
107-
if err != nil {
108-
return err
109-
}
110-
111-
configReader, err := packages_module.NewContentStore().Get(packages_module.BlobHash256Key(configDescriptor.Blob.HashSHA256))
112-
if err != nil {
113-
return err
114-
}
115-
defer configReader.Close()
116-
117-
metadata, err := container_module.ParseImageConfig(manifest.Config.MediaType, configReader)
118-
if err != nil {
119-
return err
120-
}
121-
122102
blobReferences := make([]*blobReference, 0, 1+len(manifest.Layers))
123-
124103
blobReferences = append(blobReferences, &blobReference{
125104
Digest: manifest.Config.Digest,
126105
MediaType: manifest.Config.MediaType,
@@ -388,19 +367,16 @@ func createPackageAndVersion(ctx context.Context, mci *manifestCreationInfo, met
388367
return nil, err
389368
}
390369
} else {
391-
props, err := packages_model.GetPropertiesByName(ctx, packages_model.PropertyTypeVersion, pv.ID, container_module.PropertyManifestTagged)
392-
if err != nil {
370+
if err = packages_model.DeletePropertiesByName(ctx, packages_model.PropertyTypeVersion, pv.ID, container_module.PropertyManifestTagged); err != nil {
393371
return nil, err
394372
}
395-
for _, prop := range props {
396-
if err = packages_model.DeletePropertyByID(ctx, prop.ID); err != nil {
397-
return nil, err
398-
}
399-
}
400373
}
401374

375+
if err = packages_model.DeletePropertiesByName(ctx, packages_model.PropertyTypeVersion, pv.ID, container_module.PropertyManifestReference); err != nil {
376+
return nil, err
377+
}
402378
for _, manifest := range metadata.Manifests {
403-
if err = packages_model.InsertOrUpdateProperty(ctx, packages_model.PropertyTypeVersion, pv.ID, container_module.PropertyManifestReference, manifest.Digest); err != nil {
379+
if _, err = packages_model.InsertProperty(ctx, packages_model.PropertyTypeVersion, pv.ID, container_module.PropertyManifestReference, manifest.Digest); err != nil {
404380
return nil, err
405381
}
406382
}

0 commit comments

Comments
 (0)