Repository: go-gitea/gitea
File: services/packages/arch/repository.go (Go)

Code before:
	// Delete the package indices if there are no packages
	if len(pfs) == 0 {
		pf, err := packages_model.GetFileForVersionByName(ctx, repoVersion.ID, IndexArchiveFilename, fmt.Sprintf("%s|%s", repository, architecture))
		if err != nil && !errors.Is(err, util.ErrNotExist) {
			return err
		} else if pf == nil {
			return nil
		}

		return packages_service.DeletePackageFile(ctx, pf)
	}

	vpfs := make(map[int64]*entryOptions)
	for _, pf := range pfs {
		current := &entryOptions{
			File: pf,
		}
		current.Version, err = packages_model.GetVersionByID(ctx, pf.VersionID)
		if err != nil {
			return err
		}

		if old, ok := vpfs[current.Version.PackageID]; ok {
			if compareVersions(old.Version.Version, current.Version.Version) == -1 {
				vpfs[current.Version.PackageID] = current
			}
		} else {
			vpfs[current.Version.PackageID] = current
		}
	}

	indexContent, _ := packages_module.NewHashedBuffer()
	defer indexContent.Close()

	gw := gzip.NewWriter(indexContent)
	tw := tar.NewWriter(gw)

	cache := make(map[int64]*packages_model.Package)

	for _, opts := range vpfs {
		if err := json.Unmarshal([]byte(opts.Version.MetadataJSON), &opts.VersionMetadata); err != nil {
			return err
		}
		opts.Package = cache[opts.Version.PackageID]
		if opts.Package == nil {
			opts.Package, err = packages_model.GetPackageByID(ctx, opts.Version.PackageID)
			if err != nil {
				return err
			}
			cache[opts.Package.ID] = opts.Package
		}

Review comment (suggestion):
```suggestion
		// here we compare the versions but not using SearchLatestVersions because we shouldn't allow "downgrading" to a older version by "latest" one.
		// https://wiki.archlinux.org/title/Downgrading_packages : randomly downgrading can mess up dependencies:
		// If a downgrade involves a soname change, all dependencies may need downgrading or rebuilding too.
		if old, ok := vpfs[current.Version.PackageID]; ok {
```

Code after:
	// Delete the package indices if there are no packages
	if len(pfs) == 0 {
		pf, err := packages_model.GetFileForVersionByName(ctx, repoVersion.ID, IndexArchiveFilename, fmt.Sprintf("%s|%s", repository, architecture))
		if err != nil && !errors.Is(err, util.ErrNotExist) {
			return err
		} else if pf == nil {
			return nil
		}

		return packages_service.DeletePackageFile(ctx, pf)
	}

	vpfs := make(map[int64]*entryOptions)
	for _, pf := range pfs {
		current := &entryOptions{
			File: pf,
		}
		current.Version, err = packages_model.GetVersionByID(ctx, pf.VersionID)
		if err != nil {
			return err
		}

		// here we compare the versions but not using SearchLatestVersions because we shouldn't allow "downgrading" to a older version by "latest" one.
		// https://wiki.archlinux.org/title/Downgrading_packages : randomly downgrading can mess up dependencies:
		// If a downgrade involves a soname change, all dependencies may need downgrading or rebuilding too.
		if old, ok := vpfs[current.Version.PackageID]; ok {
			if compareVersions(old.Version.Version, current.Version.Version) == -1 {
				vpfs[current.Version.PackageID] = current
			}
		} else {
			vpfs[current.Version.PackageID] = current
		}
	}

	indexContent, _ := packages_module.NewHashedBuffer()
	defer indexContent.Close()

	gw := gzip.NewWriter(indexContent)
	tw := tar.NewWriter(gw)

	cache := make(map[int64]*packages_model.Package)

	for _, opts := range vpfs {
		if err := json.Unmarshal([]byte(opts.Version.MetadataJSON), &opts.VersionMetadata); err != nil {
			return err
		}
		opts.Package = cache[opts.Version.PackageID]
		if opts.Package == nil {
			opts.Package, err = packages_model.GetPackageByID(ctx, opts.Version.PackageID)
			if err != nil {
				return err