Skip to content

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

Open
GitBalake wants to merge 2 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 2 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 prioritizes the newest matching branches and tags when limiting to 64 revisions.
  • Tests

    • Added tests validating correct ordering of branches and tags by timestamp.
  • Documentation

    • Updated changelog with the above 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: b0e10a7d-cce9-4f25-be3d-2d00788e0c30

📥 Commits

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

📒 Files selected for processing (2)
  • CHANGELOG.md
  • packages/backend/src/git.test.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/backend/src/git.test.ts

Walkthrough

Sorts Git refs before truncation: getTags and getBranches now list refs ordered by creatordate/committerdate (newest first). Tests added/updated to verify ordering and that revision selection honors newest refs under the 64-revision cap.

Changes

Cohort / File(s) Summary
Documentation
CHANGELOG.md
Added a Fixed entry documenting that revision selection under the 64-revision cap now prefers newest matching branches and tags.
Core Git Logic
packages/backend/src/git.ts
Rewrote getTags and getBranches to use git for-each-ref with -creatordate/-committerdate sorting via new internal helpers getSortedRefs and parseRefNames; returns trimmed short ref names.
Git Reference Tests
packages/backend/src/git.test.ts
New Vitest suite creating isolated repos to assert getTags orders by creatordate and getBranches orders by committerdate (newest first). Includes cleanup of temp repos.
Repository Index Manager Tests
packages/backend/src/repoIndexManager.test.ts
Updated mocks for getTags/getBranches; added tests verifying revisions list truncation/deduplication when many tags or branches are present, ensuring newest refs are kept and default branch ordering is preserved.

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 PR title clearly and specifically describes the main change: modifying ref selection to prefer newest matching branches and tags when applying the 64-revision cap.
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