Skip to content

Commit 61650e5

Browse files
authored
Merge branch 'master' into dpkg-compliant-version-compare
2 parents b464e7f + 4e457aa commit 61650e5

File tree

17 files changed

+336
-21
lines changed

17 files changed

+336
-21
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
- name: Install and initialize swagger
3636
run: |
3737
go install github.com/swaggo/swag/cmd/swag@latest
38-
swag init -q --markdownFiles docs
38+
swag init -q --propertyStrategy pascalcase --markdownFiles docs
3939
shell: sh
4040

4141
- name: golangci-lint

AUTHORS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,5 @@ List of contributors, in chronological order:
7575
* Agustin Henze (https://github.com/agustinhenze)
7676
* Tobias Assarsson (https://github.com/daedaluz)
7777
* Yaksh Bariya (https://github.com/thunder-coding)
78+
* Juan Calderon-Perez (https://github.com/gaby)
79+
* Ato Araki (https://github.com/atotto)

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ azurite-stop:
7777

7878
swagger: swagger-install
7979
# Generate swagger docs
80-
@PATH=$(BINPATH)/:$(PATH) swag init --parseDependency --parseInternal --markdownFiles docs --generalInfo docs/swagger.conf
80+
@PATH=$(BINPATH)/:$(PATH) swag init --propertyStrategy pascalcase --parseDependency --parseInternal --markdownFiles docs --generalInfo docs/swagger.conf
8181

8282
etcd-install:
8383
# Install etcd
@@ -131,7 +131,7 @@ serve: prepare swagger-install ## Run development server (auto recompiling)
131131
test -f $(BINPATH)/air || go install github.com/air-verse/[email protected]
132132
cp debian/aptly.conf ~/.aptly.conf
133133
sed -i /enable_swagger_endpoint/s/false/true/ ~/.aptly.conf
134-
PATH=$(BINPATH):$$PATH air -build.pre_cmd 'swag init -q --markdownFiles docs --generalInfo docs/swagger.conf' -build.exclude_dir docs,system,debian,pgp/keyrings,pgp/test-bins,completion.d,man,deb/testdata,console,_man,systemd,obj-x86_64-linux-gnu -- api serve -listen 0.0.0.0:3142
134+
PATH=$(BINPATH):$$PATH air -build.pre_cmd 'swag init -q --propertyStrategy pascalcase --markdownFiles docs --generalInfo docs/swagger.conf' -build.exclude_dir docs,system,debian,pgp/keyrings,pgp/test-bins,completion.d,man,deb/testdata,console,_man,systemd,obj-x86_64-linux-gnu -- api serve -listen 0.0.0.0:3142
135135

136136
dpkg: prepare swagger ## Build debian packages
137137
@test -n "$(DEBARCH)" || (echo "please define DEBARCH"; exit 1)

api/mirror.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ type mirrorUpdateParams struct {
343343
ForceUpdate bool ` json:"ForceUpdate"`
344344
// Set "true" to skip downloading already downloaded packages
345345
SkipExistingPackages bool ` json:"SkipExistingPackages"`
346+
// Set "true" to download only the latest version per package/architecture
347+
LatestOnly bool ` json:"LatestOnly"`
346348
}
347349

348350
// @Summary Update Mirror
@@ -434,7 +436,7 @@ func apiMirrorsUpdate(c *gin.Context) {
434436
}
435437

436438
queue, downloadSize, err := remote.BuildDownloadQueue(context.PackagePool(), collectionFactory.PackageCollection(),
437-
collectionFactory.ChecksumCollection(nil), b.SkipExistingPackages)
439+
collectionFactory.ChecksumCollection(nil), b.SkipExistingPackages, b.LatestOnly)
438440
if err != nil {
439441
return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, fmt.Errorf("unable to update: %s", err)
440442
}

cmd/mirror_update.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ func aptlyMirrorUpdate(cmd *commander.Command, args []string) error {
8787
)
8888

8989
skipExistingPackages := context.Flags().Lookup("skip-existing-packages").Value.Get().(bool)
90+
latestOnly := context.Flags().Lookup("latest").Value.Get().(bool)
9091

9192
context.Progress().Printf("Building download queue...\n")
9293
queue, downloadSize, err = repo.BuildDownloadQueue(context.PackagePool(), collectionFactory.PackageCollection(),
93-
collectionFactory.ChecksumCollection(nil), skipExistingPackages)
94+
collectionFactory.ChecksumCollection(nil), skipExistingPackages, latestOnly)
9495

9596
if err != nil {
9697
return fmt.Errorf("unable to update: %s", err)
@@ -292,6 +293,7 @@ Example:
292293
cmd.Flag.Bool("ignore-checksums", false, "ignore checksum mismatches while downloading package files and metadata")
293294
cmd.Flag.Bool("ignore-signatures", false, "disable verification of Release file signatures")
294295
cmd.Flag.Bool("skip-existing-packages", false, "do not check file existence for packages listed in the internal database of the mirror")
296+
cmd.Flag.Bool("latest", false, "download only latest version of each package (per architecture)")
295297
cmd.Flag.Int64("download-limit", 0, "limit download speed (kbytes/sec)")
296298
cmd.Flag.String("downloader", "default", "downloader to use (e.g. grab)")
297299
cmd.Flag.Int("max-tries", 1, "max download tries till process fails with download error")

completion.d/_aptly

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ local keyring="*-keyring=[gpg keyring to use when verifying Release file (could
211211
$keyring \
212212
"-max-tries=[max download tries till process fails with download error]:number: " \
213213
"-skip-existing-packages=[do not check file existence for packages listed in the internal database of the mirror]:$bool" \
214+
"-latest=[download only latest version of each package (per architecture)]:$bool" \
214215
"(-)2:mirror name:$mirrors"
215216
;;
216217
rename)

completion.d/aptly

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ _aptly()
263263
"update")
264264
if [[ $numargs -eq 0 ]]; then
265265
if [[ "$cur" == -* ]]; then
266-
COMPREPLY=($(compgen -W "-force -download-limit= -downloader= -ignore-checksums -ignore-signatures -keyring= -skip-existing-packages" -- ${cur}))
266+
COMPREPLY=($(compgen -W "-force -download-limit= -downloader= -ignore-checksums -ignore-signatures -keyring= -skip-existing-packages -latest" -- ${cur}))
267267
else
268268
COMPREPLY=($(compgen -W "$(__aptly_mirror_list)" -- ${cur}))
269269
fi

deb/list.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,39 @@ func (l *PackageList) ForEach(handler func(*Package) error) error {
172172
return err
173173
}
174174

175+
// FilterLatest creates a copy of the package list containing only the
176+
// latest version for each package name/architecture pair.
177+
func (l *PackageList) FilterLatest() (*PackageList, error) {
178+
if l == nil {
179+
return nil, fmt.Errorf("package list is nil")
180+
}
181+
182+
filtered := make(map[string]*Package, l.Len())
183+
184+
err := l.ForEach(func(p *Package) error {
185+
key := p.Architecture + "|" + p.Name
186+
187+
if existing, found := filtered[key]; !found || CompareVersions(p.Version, existing.Version) > 0 {
188+
filtered[key] = p
189+
}
190+
191+
return nil
192+
})
193+
if err != nil {
194+
return nil, err
195+
}
196+
197+
result := NewPackageListWithDuplicates(l.duplicatesAllowed, len(filtered))
198+
199+
for _, pkg := range filtered {
200+
if err = result.Add(pkg); err != nil {
201+
return nil, err
202+
}
203+
}
204+
205+
return result, nil
206+
}
207+
175208
// ForEachIndexed calls handler for each package in list in indexed order
176209
func (l *PackageList) ForEachIndexed(handler func(*Package) error) error {
177210
if !l.indexed {

deb/list_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,52 @@ func (s *PackageListSuite) TestArchitectures(c *C) {
503503
sort.Strings(archs)
504504
c.Check(archs, DeepEquals, []string{"amd64", "arm", "i386", "s390"})
505505
}
506+
507+
func (s *PackageListSuite) TestFilterLatest(c *C) {
508+
list := NewPackageList()
509+
510+
older := packageStanza.Copy()
511+
older["Version"] = "1.0"
512+
olderPkg := NewPackageFromControlFile(older)
513+
_ = list.Add(olderPkg)
514+
515+
newer := packageStanza.Copy()
516+
newer["Version"] = "2.0"
517+
newerPkg := NewPackageFromControlFile(newer)
518+
_ = list.Add(newerPkg)
519+
520+
shared := packageStanza.Copy()
521+
shared["Architecture"] = ArchitectureAll
522+
shared["Version"] = "3.0"
523+
shared["Package"] = "shared"
524+
sharedPkg := NewPackageFromControlFile(shared)
525+
_ = list.Add(sharedPkg)
526+
527+
filtered, err := list.FilterLatest()
528+
c.Assert(err, IsNil)
529+
c.Assert(filtered.Len(), Equals, 2)
530+
c.Check(filtered.Has(newerPkg), Equals, true)
531+
c.Check(filtered.Has(sharedPkg), Equals, true)
532+
}
533+
534+
func (s *PackageListSuite) TestFilterLatestPreservesDuplicatesFlag(c *C) {
535+
list := NewPackageListWithDuplicates(true, 2)
536+
537+
_ = list.Add(NewPackageFromControlFile(packageStanza.Copy()))
538+
539+
another := packageStanza.Copy()
540+
another["Version"] = "7.41-1"
541+
_ = list.Add(NewPackageFromControlFile(another))
542+
543+
filtered, err := list.FilterLatest()
544+
c.Assert(err, IsNil)
545+
c.Assert(filtered.duplicatesAllowed, Equals, true)
546+
}
547+
548+
func (s *PackageListSuite) TestFilterLatestNil(c *C) {
549+
var list *PackageList
550+
551+
filtered, err := list.FilterLatest()
552+
c.Assert(err, ErrorMatches, "package list is nil")
553+
c.Assert(filtered, IsNil)
554+
}

deb/remote.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,19 @@ func (repo *RemoteRepo) ApplyFilter(dependencyOptions int, filterQuery PackageQu
612612
}
613613

614614
// BuildDownloadQueue builds queue, discards current PackageList
615-
func (repo *RemoteRepo) BuildDownloadQueue(packagePool aptly.PackagePool, packageCollection *PackageCollection, checksumStorage aptly.ChecksumStorage, skipExistingPackages bool) (queue []PackageDownloadTask, downloadSize int64, err error) {
615+
func (repo *RemoteRepo) BuildDownloadQueue(packagePool aptly.PackagePool, packageCollection *PackageCollection, checksumStorage aptly.ChecksumStorage, skipExistingPackages, latestOnly bool) (queue []PackageDownloadTask, downloadSize int64, err error) {
616+
if repo.packageList == nil {
617+
err = fmt.Errorf("package list is empty, please (re)download package indexes")
618+
return
619+
}
620+
621+
if latestOnly {
622+
repo.packageList, err = repo.packageList.FilterLatest()
623+
if err != nil {
624+
return
625+
}
626+
}
627+
616628
queue = make([]PackageDownloadTask, 0, repo.packageList.Len())
617629
seen := make(map[string]int, repo.packageList.Len())
618630

0 commit comments

Comments
 (0)