Skip to content

fix(worker): prefer newest refs when applying the 64 revision cap#1122

Open
GitBalake wants to merge 3 commits intosourcebot-dev:mainfrom
GitBalake:fix/sort-refs-before-cap
Open

fix(worker): prefer newest refs when applying the 64 revision cap#1122
GitBalake wants to merge 3 commits intosourcebot-dev:mainfrom
GitBalake:fix/sort-refs-before-cap

Conversation

@GitBalake
Copy link
Copy Markdown

@GitBalake GitBalake commented Apr 15, 2026

Part of #1016

Summary

Sourcebot currently caps explicit branch/tag indexing at 64 revisions. Before this change, matching refs were effectively pruned in ref-name order, which could cause older tags to be kept while newer tags were dropped.

This change applies sorting before truncation so the cap keeps the newest matching refs by default:

  • branches are ordered by committer date
  • tags are ordered by creator date
  • the default branch remains pinned first

Verification

  • Added backend tests covering newest-first branch and tag selection
  • Added regression coverage for default-branch pinning and de-duplication before truncation
  • Passed all backend testing
  • Ran backend build
  • Verified live indexing on a real repo with more than 64 matching refs and confirmed Sourcebot stored newest-first indexed revisions

Summary by CodeRabbit

  • Bug Fixes

    • Revision selection now prefers the newest matching branches and tags and correctly limits to 64 revisions, with de-duplication and truncation applied consistently.
  • Tests

    • Added tests that verify branch and tag ordering by timestamp and ensure revisions are selected in the expected newest-first order.
  • Documentation

    • Updated changelog to document the revision-selection fix.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8667c5a8-8894-49d8-b062-a24df5bb3eec

📥 Commits

Reviewing files that changed from the base of the PR and between 4fc2710 and a5741d1.

📒 Files selected for processing (1)
  • CHANGELOG.md

Walkthrough

Lists Git branches and tags sorted by newest date before truncation: getTags uses creatordate and getBranches uses committerdate. Tests added to validate ordering and to ensure the 64-revision cap preserves newest refs and de-duplicates the default branch.

Changes

Cohort / File(s) Summary
Documentation
CHANGELOG.md
Added Fixed entry documenting that revision selection under the 64-revision cap prefers newest matching branches and tags.
Core Git Logic
packages/backend/src/git.ts
Replaced prior git.tags()/git.branch() usage with git for-each-ref via new internal helpers getSortedRefs and parseRefNames; returns newline-trimmed short ref names sorted by -creatordate (tags) and -committerdate (branches).
Git Reference Tests
packages/backend/src/git.test.ts
New Vitest suite creating isolated temp repos to assert getTags orders by creatordate and getBranches orders by committerdate (newest first); deterministic git config and cleanup added.
Repository Index Manager Tests
packages/backend/src/repoIndexManager.test.ts
Updated mocks to include getTags/getBranches; added tests asserting revisions passed to indexGitRepository are de-duplicated, start with default branch, and are truncated to 64 newest refs for both tags and branches scenarios.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related issues

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the main change: sorting refs by date before applying the 64-revision cap to prefer newest refs instead of pruning by ref-name order.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@CHANGELOG.md`:
- Line 12: The Unreleased changelog bullet "Fixed revision selection so the
64-revision cap prefers the newest matching branches and tags instead of pruning
by ref-name order." is missing the required PR link suffix; edit CHANGELOG.md
and append the PR reference in the required format for this entry, e.g. add "
[`#1122`](https://github.com/sourcebot-dev/sourcebot/pull/1122)" to the end of
that line so it follows the project's changelog guideline.

In `@packages/backend/src/git.test.ts`:
- Around line 69-131: Update the tests for getTags and getBranches to first
assert the expected refs exist (use assertions on getTags/getBranches like
expect(tags).toContain("z-newest") and expect(tags).toContain("a-oldest") /
expect(branches).toContain("zzz-newest") and
expect(branches).toContain("aaa-oldest")) before comparing order, then compare
ordering (either check indices as before or assert the first element equals the
newest ref) so a missing ref (indexOf == -1) cannot make the test pass; update
the assertions in the tests referencing getTags and getBranches accordingly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 881525c5-fc19-433c-86e7-25a497bc3a9d

📥 Commits

Reviewing files that changed from the base of the PR and between ed05fc4 and c967f27.

📒 Files selected for processing (4)
  • CHANGELOG.md
  • packages/backend/src/git.test.ts
  • packages/backend/src/git.ts
  • packages/backend/src/repoIndexManager.test.ts

Comment thread CHANGELOG.md Outdated
Comment thread packages/backend/src/git.test.ts
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.

1 participant