fix(git): Use HEAD instead of hardcoded main branch for GitHub archives#806
fix(git): Use HEAD instead of hardcoded main branch for GitHub archives#806
Conversation
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughDefault GitHub archive behavior changed to use HEAD.zip when no ref is provided. Filename derivation now defaults to repo-HEAD.zip. Corresponding tests updated to expect HEAD-first URLs and filenames; other ref types (branch, commit SHA, tag) remain unchanged. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes ✨ Finishing Touches🧪 Generate 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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
Status, Documentation and Community
|
There was a problem hiding this comment.
Pull Request Overview
This PR improves GitHub archive functionality by replacing hardcoded 'main' branch references with 'HEAD', allowing the system to automatically use a repository's actual default branch regardless of its name.
- Replace hardcoded 'main' with 'HEAD' in URL building and filename generation
- Update tests to reflect the new behavior using HEAD instead of main
- Improve compatibility with repositories using different default branch naming conventions
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/core/git/gitHubArchiveApi.ts | Updated buildGitHubArchiveUrl and getArchiveFilename to use HEAD instead of hardcoded main |
| tests/core/git/gitHubArchiveApi.test.ts | Updated unit tests to expect HEAD URLs and filenames instead of main |
| tests/core/git/gitHubArchive.test.ts | Updated integration test comments and expectations to reflect HEAD usage |
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
There was a problem hiding this comment.
Summary of Changes
Hello @yamadashy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request enhances the GitHub archive functionality by replacing hardcoded 'main' branch references with 'HEAD'. This change ensures that the system automatically uses the repository's default branch, whether it's 'main', 'master', or a custom name. The update improves compatibility with various repository configurations and future-proofs the codebase against changes in default branch naming conventions, simplifying the logic for retrieving repository archives.
Highlights
- Improved GitHub Archive URL Generation: The
buildGitHubArchiveUrlfunction now defaults toHEAD.zipinstead ofrefs/heads/main.zipwhen no specific reference is provided, making the archive URL dynamic to the repository's default branch. - Dynamic Archive Filename Generation: The
getArchiveFilenamefunction has been updated to useHEADas the default reference part for generating archive filenames, ensuring consistency with the new URL generation logic. - Enhanced Test Coverage: Corresponding unit and integration tests have been updated to validate the new behavior, including changes in expected URLs and filenames, and confirming the
HEADtomasterfallback mechanism. - Increased Compatibility: This change eliminates compatibility issues with GitHub repositories that do not use 'main' as their default branch, such as those still using 'master' or custom branch names, making the system more robust.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #806 +/- ##
=======================================
Coverage 87.41% 87.41%
=======================================
Files 113 113
Lines 6493 6493
Branches 1331 1331
=======================================
Hits 5676 5676
Misses 817 817 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request correctly replaces hardcoded 'main' branch references with 'HEAD' to improve compatibility with repositories using different default branch names. The changes in buildGitHubArchiveUrl and getArchiveFilename are consistent and well-reasoned. The accompanying test updates are thorough and correctly verify the new behavior, including the fallback mechanism. The implementation is clean and effectively addresses the problem described. Overall, this is a solid improvement.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (6)
src/core/git/gitHubArchiveApi.ts (3)
15-16: Switching default to HEAD is correct and future‑proof.Using
.../archive/HEAD.ziplets GitHub resolve the repo’s default branch server‑side. Nice simplification and works acrossmain,master, and custom defaults.Minor: Update the top-of-file comment block to also mention the HEAD form for completeness.
19-23: Tighten commit SHA detection to avoid misclassifying hex‑looking branch/tag names.
/^[0-9a-f]{4,40}$/irisks treating legit ref names likedeadbeef(branch/tag) as a commit and skipping branch/tag fallbacks. Using a more conservative lower bound (7) matches common abbreviated SHA practice and reduces false positives.Apply this diff:
- const isCommitSha = /^[0-9a-f]{4,40}$/i.test(ref); + const isCommitSha = /^[0-9a-f]{7,40}$/i.test(ref);Optionally, if you want to be ultra-safe for hex-looking refs even after this change, consider trying branch/tag fallbacks when a commit download returns 404.
25-28: Ensure refs with slashes build correct URLs (preserve/between path segments).For refs like
feature/xorrelease/v1.0,encodeURIComponent(ref)encodes/into%2F. While GitHub may handle this, relying on path decoding is brittle. Encode each segment and keep/as a delimiter.Apply this diff and reuse the helper in both branch and tag builders:
export const buildGitHubArchiveUrl = (repoInfo: GitHubRepoInfo): string => { const { owner, repo, ref } = repoInfo; const baseUrl = `https://github.com/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/archive`; if (!ref) { // Default to HEAD (repository's default branch) return `${baseUrl}/HEAD.zip`; } // Check if ref looks like a commit SHA (40 hex chars or shorter) - const isCommitSha = /^[0-9a-f]{4,40}$/i.test(ref); + const isCommitSha = /^[0-9a-f]{7,40}$/i.test(ref); if (isCommitSha) { return `${baseUrl}/${encodeURIComponent(ref)}.zip`; } // For branches and tags, we need to determine the type // Default to branch format, will fallback to tag if needed - return `${baseUrl}/refs/heads/${encodeURIComponent(ref)}.zip`; + const encodeRefForPath = (r: string) => r.split('/').map(encodeURIComponent).join('/'); + return `${baseUrl}/refs/heads/${encodeRefForPath(ref)}.zip`; };And in the tag URL builder:
export const buildGitHubTagArchiveUrl = (repoInfo: GitHubRepoInfo): string | null => { const { owner, repo, ref } = repoInfo; if (!ref || /^[0-9a-f]{7,40}$/i.test(ref)) { return null; // Not applicable for commits or no ref } - return `https://github.com/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/archive/refs/tags/${encodeURIComponent(ref)}.zip`; + const encodeRefForPath = (r: string) => r.split('/').map(encodeURIComponent).join('/'); + return `https://github.com/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/archive/refs/tags/${encodeRefForPath(ref)}.zip`; };Add unit tests for branch/tag containing
/(see test suggestions below).tests/core/git/gitHubArchiveApi.test.ts (1)
74-78: Filename test updated to HEAD — good. Add slash-in-ref test cases.Add tests to ensure branch/tag refs containing
/produce URLs with preserved path separators (and not%2F).Proposed additions:
@@ describe('buildGitHubArchiveUrl', () => { test('should build URL for specific branch', () => { @@ }); + + test('should build URL for branch with slash', () => { + const repoInfo = { owner: 'user', repo: 'repo', ref: 'feature/cool' }; + const url = buildGitHubArchiveUrl(repoInfo); + expect(url).toBe('https://github.com/user/repo/archive/refs/heads/feature/cool.zip'); + }); @@ test('should build URL for tag', () => { @@ }); + + test('should build URL for tag with slash', () => { + const repoInfo = { owner: 'user', repo: 'repo', ref: 'release/v1.0' }; + const url = buildGitHubTagArchiveUrl(repoInfo); + expect(url).toBe('https://github.com/user/repo/archive/refs/tags/release/v1.0.zip'); + });tests/core/git/gitHubArchive.test.ts (2)
209-214: Update stale comment to reflect HEAD 404, not main.The behavior mocked here is “404 for HEAD, then succeed for master”, but the comment still says “main”.
Apply this diff:
- // Mock 404 for main branch, success for master branch + // Mock 404 for HEAD (default), success for master branch
420-423: Consider adding a no-ref cleanup assertion to cover the HEAD path.We don’t currently assert that the temporary archive
repomix-HEAD.zipis removed when no ref is provided. Adding such an assertion will catch regressions in cleanup naming.Example (if your implementation always uses the initial filename regardless of fallback):
- // Should still attempt cleanup - expect(mockFs.unlink).toHaveBeenCalledWith(path.join(mockTargetDirectory, 'repomix-main.zip')); + // Should still attempt cleanup + expect(mockFs.unlink).toHaveBeenCalledWith(path.join(mockTargetDirectory, 'repomix-main.zip')); + // Optional: also cover HEAD-based downloads (no ref) + // expect(mockFs.unlink).toHaveBeenCalledWith(path.join(mockTargetDirectory, 'repomix-HEAD.zip'));If cleanup naming depends on the successful URL (e.g., master on fallback), adjust the expected filename accordingly.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (3)
src/core/git/gitHubArchiveApi.ts(2 hunks)tests/core/git/gitHubArchive.test.ts(1 hunks)tests/core/git/gitHubArchiveApi.test.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/core/git/gitHubArchiveApi.test.ts (1)
src/core/git/gitHubArchiveApi.ts (2)
buildGitHubArchiveUrl(10-28)getArchiveFilename(58-66)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: Build and run (windows-latest, 21.x)
- GitHub Check: Build and run (windows-latest, 22.x)
- GitHub Check: Test (windows-latest, 22.x)
- GitHub Check: Test (windows-latest, 24.x)
🔇 Additional comments (3)
src/core/git/gitHubArchiveApi.ts (1)
60-66: Cleanup invocation validated: HEAD.zip is used consistentlyCleanup logic invokes
deps.fs.unlink(tempArchivePath)(at src/core/git/gitHubArchive.ts:126), wheretempArchivePathis built fromgetArchiveFilename(repoInfo). SincegetArchiveFilenamedefaults to'HEAD'when norefis provided (and the tests verify it returnsmyrepo-HEAD.zip), the unlink call will always removemyrepo-HEAD.zip, even if the code falls back tomasterunder the hood. No orphaned artifacts will remain.tests/core/git/gitHubArchiveApi.test.ts (1)
14-18: Tests updated for HEAD default — good coverage.This validates the new default URL shape. Consider also verifying that HEAD default does not accidentally trigger tag fallback logic when no ref is provided (shouldn’t, given current code).
tests/core/git/gitHubArchive.test.ts (1)
245-248: HEAD-first fallback order assertion looks good.This matches the new default strategy and still validates the legacy
masterfallback.
Replace hardcoded 'main' branch with 'HEAD' to automatically use the repository's default branch, eliminating issues with repositories that use different default branch names like 'master' or custom branches. Changes: - buildGitHubArchiveUrl now uses HEAD.zip instead of refs/heads/main.zip - getArchiveFilename defaults to HEAD instead of main - Updated corresponding tests to reflect the new behavior
Update the test expectation to reflect the change from hardcoded 'main' branch to using HEAD for the repository's default branch.
9471f9b to
d5c5cd8
Compare
Deploying repomix with
|
| Latest commit: |
d5c5cd8
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://e254346d.repomix.pages.dev |
| Branch Preview URL: | https://feat-remote-head.repomix.pages.dev |
This PR replaces hardcoded 'main' branch references with 'HEAD' in GitHub archive functionality, making the system automatically use the repository's default branch regardless of whether it's 'main', 'master', or a custom branch name.
related:
Summary
buildGitHubArchiveUrlfunctiongetArchiveFilenameto default to 'HEAD' instead of 'main'Benefits
Test Plan
npm run test- All 743 tests passnpm run lint- No linting issuesbuildGitHubArchiveUrlandgetArchiveFilenamegitHubArchive.test.tsChecklist
npm run testnpm run lint