Skip to content

fix(repo): make --limit cap total results - #135

Merged
andrew merged 3 commits into
git-pkgs:mainfrom
ychampion:fix-repo-limit-total
Jul 20, 2026
Merged

andrew merged 3 commits into
git-pkgs:mainfrom
ychampion:fix-repo-limit-total

Conversation

@ychampion

@ychampion ychampion commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Cap repo list --limit output after the repo service returns results.
  • Keep repo search --limit as both SearchRepoOpts.Limit and PerPage, then cap output after the search call.
  • Cover both paths with CLI tests that return more repos than the limit and assert the printed row count.

Why

Fixes #133. --limit should cap the total number of repositories printed. Search still uses the limit as the backend page size because the current search backends fetch one page.

Validation

  • go test ./internal/cli -run 'TestRepo(List|Search)LimitCapsTotalResults' -count=1
  • go test ./internal/cli -count=1
  • go test ./... -count=1
  • git diff --check

Copilot AI review requested due to automatic review settings July 8, 2026 11:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread internal/resolve/resolve.go Resolved
Comment thread internal/cli/repo_test.go Resolved

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up. As written this doesn't fix #133 and regresses repo search:

  • No backend reads ListRepoOpts.Limit or SearchRepoOpts.Limit (see github/github.go:105,402, gitlab/gitlab.go:95,388, gitea/gitea.go:102,365, bitbucket/bitbucket.go:210). FilterRepos in forge.go doesn't apply it either. So repo list --limit N still returns everything.
  • Search on every backend fetches a single page sized by PerPage. Removing PerPage: flagLimit means --limit 5 now returns 30 results instead of 5.

Please apply the two changes below so --limit actually caps output, and update the tests to prove it by returning more repos than the limit and checking the printed count. Stopping backend pagination early can be a follow-up.

Comment thread internal/cli/repo.go Resolved
Comment thread internal/cli/repo.go Resolved
Apply the review-requested output cap in the CLI after repo list/search calls so backends that do not honor Limit cannot over-print. Keep search PerPage tied to the user limit because search backends fetch a single page.

Constraint: Backends currently ignore ListRepoOpts.Limit and SearchRepoOpts.Limit.
Rejected: relying on backend pagination changes | larger follow-up than this review requested.
Confidence: high
Scope-risk: narrow
Directive: Keep CLI output caps until repo backends implement total Limit consistently.
Tested: go test ./internal/cli -run 'TestRepo(List|Search)LimitCapsTotalResults' -count=1
Tested: go test ./internal/cli -count=1
Tested: go test ./... -count=1
Tested: git diff --check

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the follow-up — both paths now cap correctly and the tests prove it.

@andrew
andrew merged commit 5e04c3d into git-pkgs:main Jul 20, 2026
4 checks passed
@andrew andrew mentioned this pull request Jul 20, 2026
andrew pushed a commit that referenced this pull request Jul 26, 2026
Backend Repos().List implementations (GitHub, GitLab, Gitea, Bitbucket)
previously ignored ListRepoOpts.Limit entirely and always fetched every
page of results before returning, even when the caller only asked for a
handful of repositories via `forge repo list --limit N`.

PR #135 worked around this by capping the CLI's printed output after the
fact, but the backends still made all the underlying API requests,
wasting rate-limit budget and latency for large orgs/users, exactly as
described in #134.

This closes the gap called out in #134's "Proposed changes":
- Add forge.RepoMatchesFilters, a shared archived/fork predicate reused
  by both the pagination loops and the existing FilterRepos post-filter.
- Add forge.CapRepos to truncate matched results to opts.Limit.
- In each backend's Repos().List, count filter-matching repos while
  paginating and stop requesting further pages once opts.Limit matching
  repos have been collected. Also shrink the requested page size to the
  limit when it's smaller, so a single small page is fetched instead of
  a full default page.
- Bitbucket's cursor-based pagination (follows a `next` URL) is handled
  the same way: stop following `next` once enough repos are collected.

No behavior change when Limit is 0 (unlimited); all backends still
paginate fully in that case, same as before.

Tested:
- go build ./...
- go vet ./...
- go test ./...  (all existing tests pass unchanged)
- Added TestGitHubListReposStopsPaginatingAtLimit,
  TestGitLabListReposStopsPaginatingAtLimit,
  TestGiteaListReposStopsPaginatingAtLimit,
  TestBitbucketListReposStopsPaginatingAtLimit; each asserts the
  backend issues exactly one HTTP request when Limit is satisfied by
  the first page, even though the mock server always advertises a next
  page.

Fixes #134
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix repo limit flag to cap total results

3 participants