Skip to content

fix(git): Use HEAD instead of hardcoded main branch for GitHub archives#806

Merged
yamadashy merged 2 commits intomainfrom
feat/remote-head
Aug 25, 2025
Merged

fix(git): Use HEAD instead of hardcoded main branch for GitHub archives#806
yamadashy merged 2 commits intomainfrom
feat/remote-head

Conversation

@yamadashy
Copy link
Copy Markdown
Owner

@yamadashy yamadashy commented Aug 24, 2025

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

  • Replace hardcoded 'main' branch with 'HEAD' in buildGitHubArchiveUrl function
  • Update getArchiveFilename to default to 'HEAD' instead of 'main'
  • Update corresponding tests to reflect the new behavior
  • Eliminates compatibility issues with repositories using different default branch names

Benefits

  • Better compatibility: Works with repositories using 'master', 'main', or custom default branches
  • Future-proof: No need to update code when GitHub or repositories change default branch naming conventions
  • Simplified logic: Relies on GitHub's native HEAD resolution instead of guessing branch names

Test Plan

  • Run npm run test - All 743 tests pass
  • Run npm run lint - No linting issues
  • Updated unit tests for buildGitHubArchiveUrl and getArchiveFilename
  • Updated integration tests in gitHubArchive.test.ts
  • Verified fallback behavior still works (HEAD → master fallback)

Checklist

  • Run npm run test
  • Run npm run lint

Copilot AI review requested due to automatic review settings August 24, 2025 15:54
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Aug 24, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

Default 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

Cohort / File(s) Summary
Core GitHub Archive API
src/core/git/gitHubArchiveApi.ts
Default archive URL switched from refs/heads/main.zip to HEAD.zip; getArchiveFilename default changed from *-main.zip to *-HEAD.zip.
Tests
tests/core/git/gitHubArchive.test.ts, tests/core/git/gitHubArchiveApi.test.ts
Updated expectations and descriptions to use HEAD.zip and *-HEAD.zip for default branch behavior; other test cases unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/remote-head

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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
Copy link
Copy Markdown
Contributor

claude bot commented Aug 24, 2025

Claude encountered an error —— View job


I'll analyze this and get back to you.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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 buildGitHubArchiveUrl function now defaults to HEAD.zip instead of refs/heads/main.zip when no specific reference is provided, making the archive URL dynamic to the repository's default branch.
  • Dynamic Archive Filename Generation: The getArchiveFilename function has been updated to use HEAD as 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 HEAD to master fallback 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

  1. 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
Copy link
Copy Markdown

codecov bot commented Aug 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.41%. Comparing base (28f6d26) to head (d5c5cd8).
⚠️ Report is 3 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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.

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: 0

🧹 Nitpick comments (6)
src/core/git/gitHubArchiveApi.ts (3)

15-16: Switching default to HEAD is correct and future‑proof.

Using .../archive/HEAD.zip lets GitHub resolve the repo’s default branch server‑side. Nice simplification and works across main, 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}$/i risks treating legit ref names like deadbeef (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/x or release/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.zip is 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.

📥 Commits

Reviewing files that changed from the base of the PR and between 4d166b5 and 9471f9b.

📒 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 consistently

Cleanup logic invokes deps.fs.unlink(tempArchivePath) (at src/core/git/gitHubArchive.ts:126), where tempArchivePath is built from getArchiveFilename(repoInfo). Since getArchiveFilename defaults to 'HEAD' when no ref is provided (and the tests verify it returns myrepo-HEAD.zip), the unlink call will always remove myrepo-HEAD.zip, even if the code falls back to master under 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 master fallback.

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.
@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying repomix with  Cloudflare Pages  Cloudflare Pages

Latest commit: d5c5cd8
Status: ✅  Deploy successful!
Preview URL: https://e254346d.repomix.pages.dev
Branch Preview URL: https://feat-remote-head.repomix.pages.dev

View logs

@yamadashy yamadashy merged commit 1e3d0f8 into main Aug 25, 2025
57 checks passed
@yamadashy yamadashy deleted the feat/remote-head branch August 25, 2025 16:24
@yamadashy yamadashy changed the title feat(git): Use HEAD instead of hardcoded main branch for GitHub archives fix(git): Use HEAD instead of hardcoded main branch for GitHub archives Aug 25, 2025
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.

2 participants