Skip to content

Make nbgv install observe the default branch name when creating version.json #1243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 10, 2025

The nbgv install command was hardcoding "master" as the first public release branch in the generated version.json file, regardless of the actual default branch name in the repository. This caused issues in repositories using "main", "develop", or other branch names as their default.

Changes Made

This PR implements automatic default branch detection that works with all GitContext types:

LibGit2Context Implementation

For repositories using LibGit2Sharp (ReadWrite engine), the implementation:

  1. Check remotes for HEAD reference - prioritizing upstream > origin > any other remote
  2. Fall back to single local branch - if only one branch exists, use it
  3. Use git config setting - check init.defaultBranch if the branch exists locally
  4. Use common branch names - check for master, main, develop in that order
  5. Default to "master" - as final fallback

Alternative Implementation for Other Contexts

For ManagedGitContext (ReadOnly engine) and other context types, the implementation reads .git directory files directly:

  • Reads refs/remotes/{remote}/HEAD files for remote HEAD references
  • Scans refs/remotes/ directory to find available remotes
  • Reads refs/heads/ directory and packed-refs file for local branches
  • Parses .git/config and global config for init.defaultBranch setting

Both implementations follow the same priority algorithm to ensure consistent behavior across all git engines.

Example Behavior

Before:

{
  "version": "1.0-beta",
  "publicReleaseRefSpec": [
    "^refs/heads/master$",  // Always master, even in main-based repos
    "^refs/heads/v\\d+(?:\\.\\d+)?$"
  ]
}

After (in a main-based repo):

{
  "version": "1.0-beta", 
  "publicReleaseRefSpec": [
    "^refs/heads/main$",    // Correctly detects main as default
    "^refs/heads/v\\d+(?:\\.\\d+)?$"
  ]
}

Testing

  • All existing InstallCommand tests continue to pass
  • Manual testing verifies correct behavior with master/main/develop branch scenarios
  • Implementation works with both LibGit2Context (ReadWrite) and ManagedGitContext (ReadOnly) engines
  • Maintains backward compatibility with existing repositories

Fixes #717.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] nbgv install should create a version.json file, observing the default branch name Make nbgv install observe the default branch name when creating version.json Aug 10, 2025
@Copilot Copilot AI requested a review from AArnott August 10, 2025 19:27
Copilot finished work on behalf of AArnott August 10, 2025 19:27
/// <returns>The detected default branch name, defaulting to "master" if none can be determined.</returns>
private static string DetectDefaultBranch(GitContext context)
{
if (context is LibGit2.LibGit2Context libgit2Context)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@copilot This will often not be true. Can you implement the alternative as well (presumably reading files from the .git directory manually)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented the alternative approach for non-LibGit2 contexts in commit 23b90ba. The implementation now handles ManagedGitContext and other context types by reading .git directory files directly, using the same fallback algorithm as the LibGit2 version.

Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com>
Copilot finished work on behalf of AArnott August 10, 2025 21:58
@Copilot Copilot AI requested a review from AArnott August 10, 2025 21:58
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.

nbgv install should create a version.json file, observing the default branch name
2 participants