diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2023-05-03 22:58:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-03 16:58:43 -0400 |
commit | 723598b803919bfc074fee05f830421a99881c3b (patch) | |
tree | f220028e75c4815f1286ea24809ef72ca9408b6b /services/packages | |
parent | 48e3e38ee040c9cfec6ea2adea2da49a7f21f2c7 (diff) | |
download | gitea-723598b803919bfc074fee05f830421a99881c3b.tar.gz gitea-723598b803919bfc074fee05f830421a99881c3b.zip |
Implement Cargo HTTP index (#24452)
This implements the HTTP index
[RFC](https://rust-lang.github.io/rfcs/2789-sparse-index.html) for Cargo
registries.
Currently this is a preview feature and you need to use the nightly of
`cargo`:
`cargo +nightly -Z sparse-registry update`
See https://github.com/rust-lang/cargo/issues/9069 for more information.
---------
Co-authored-by: Giteabot <teabot@gitea.io>
Diffstat (limited to 'services/packages')
-rw-r--r-- | services/packages/cargo/index.go | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/services/packages/cargo/index.go b/services/packages/cargo/index.go index e58a472816..1c2d17b504 100644 --- a/services/packages/cargo/index.go +++ b/services/packages/cargo/index.go @@ -137,21 +137,21 @@ type IndexVersionEntry struct { Links string `json:"links,omitempty"` } -func addOrUpdatePackageIndex(ctx context.Context, t *files_service.TemporaryUploadRepository, p *packages_model.Package) error { +func BuildPackageIndex(ctx context.Context, p *packages_model.Package) (*bytes.Buffer, error) { pvs, _, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{ PackageID: p.ID, Sort: packages_model.SortVersionAsc, }) if err != nil { - return fmt.Errorf("SearchVersions[%s]: %w", p.Name, err) + return nil, fmt.Errorf("SearchVersions[%s]: %w", p.Name, err) } if len(pvs) == 0 { - return nil + return nil, nil } pds, err := packages_model.GetPackageDescriptors(ctx, pvs) if err != nil { - return fmt.Errorf("GetPackageDescriptors[%s]: %w", p.Name, err) + return nil, fmt.Errorf("GetPackageDescriptors[%s]: %w", p.Name, err) } var b bytes.Buffer @@ -179,14 +179,26 @@ func addOrUpdatePackageIndex(ctx context.Context, t *files_service.TemporaryUplo Links: metadata.Links, }) if err != nil { - return err + return nil, err } b.Write(entry) b.WriteString("\n") } - return writeObjectToIndex(t, BuildPackagePath(pds[0].Package.LowerName), &b) + return &b, nil +} + +func addOrUpdatePackageIndex(ctx context.Context, t *files_service.TemporaryUploadRepository, p *packages_model.Package) error { + b, err := BuildPackageIndex(ctx, p) + if err != nil { + return err + } + if b == nil { + return nil + } + + return writeObjectToIndex(t, BuildPackagePath(p.LowerName), b) } func getOrCreateIndexRepository(ctx context.Context, doer, owner *user_model.User) (*repo_model.Repository, error) { @@ -212,6 +224,13 @@ type Config struct { APIURL string `json:"api"` } +func BuildConfig(owner *user_model.User) *Config { + return &Config{ + DownloadURL: setting.AppURL + "api/packages/" + owner.Name + "/cargo/api/v1/crates", + APIURL: setting.AppURL + "api/packages/" + owner.Name + "/cargo", + } +} + func createOrUpdateConfigFile(ctx context.Context, repo *repo_model.Repository, doer, owner *user_model.User) error { return alterRepositoryContent( ctx, @@ -220,10 +239,7 @@ func createOrUpdateConfigFile(ctx context.Context, repo *repo_model.Repository, "Initialize Cargo Config", func(t *files_service.TemporaryUploadRepository) error { var b bytes.Buffer - err := json.NewEncoder(&b).Encode(Config{ - DownloadURL: setting.AppURL + "api/packages/" + owner.Name + "/cargo/api/v1/crates", - APIURL: setting.AppURL + "api/packages/" + owner.Name + "/cargo", - }) + err := json.NewEncoder(&b).Encode(BuildConfig(owner)) if err != nil { return err } |