-
-
Notifications
You must be signed in to change notification settings - Fork 177
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
Copilot
wants to merge
3
commits into
main
Choose a base branch
from
copilot/fix-717
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+590
−15
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
test/Nerdbank.GitVersioning.Tests/InstallCommandTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| // Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System.IO; | ||
| using LibGit2Sharp; | ||
| using Nerdbank.GitVersioning; | ||
| using Nerdbank.GitVersioning.LibGit2; | ||
| using Newtonsoft.Json.Linq; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| public class InstallCommandTests : RepoTestBase | ||
| { | ||
| public InstallCommandTests(ITestOutputHelper logger) | ||
| : base(logger) | ||
| { | ||
| this.InitializeSourceControl(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Install_CreatesVersionJsonWithMasterBranch_WhenOnlyMasterBranchExists() | ||
| { | ||
| // Arrange: Repo is already initialized with master branch | ||
|
|
||
| // Act: Install version.json using default branch detection | ||
| var versionOptions = new VersionOptions | ||
| { | ||
| Version = SemanticVersion.Parse("1.0-beta"), | ||
| PublicReleaseRefSpec = this.DetectPublicReleaseRefSpecForTesting(), | ||
| }; | ||
|
|
||
| string versionFile = this.Context!.VersionFile.SetVersion(this.RepoPath, versionOptions); | ||
|
|
||
| // Assert: Verify the version.json contains the correct branch | ||
| string jsonContent = File.ReadAllText(versionFile); | ||
| JObject versionJson = JObject.Parse(jsonContent); | ||
| JArray? publicReleaseRefSpec = versionJson["publicReleaseRefSpec"] as JArray; | ||
|
|
||
| Assert.NotNull(publicReleaseRefSpec); | ||
| Assert.Equal("^refs/heads/master$", publicReleaseRefSpec![0]!.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Install_CreatesVersionJsonWithMainBranch_WhenOnlyMainBranchExists() | ||
| { | ||
| // Arrange: Rename the default branch to main | ||
| if (this.LibGit2Repository is object) | ||
| { | ||
| // First, make sure we have a commit, then rename | ||
| this.LibGit2Repository.Commit("Initial commit", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true }); | ||
| this.LibGit2Repository.Refs.Rename("refs/heads/master", "refs/heads/main"); | ||
| this.LibGit2Repository.Refs.UpdateTarget("HEAD", "refs/heads/main"); | ||
| } | ||
|
|
||
| // Act: Install version.json using default branch detection | ||
| var versionOptions = new VersionOptions | ||
| { | ||
| Version = SemanticVersion.Parse("1.0-beta"), | ||
| PublicReleaseRefSpec = this.DetectPublicReleaseRefSpecForTesting(), | ||
| }; | ||
|
|
||
| string versionFile = this.Context!.VersionFile.SetVersion(this.RepoPath, versionOptions); | ||
|
|
||
| // Assert: Verify the version.json contains the correct branch | ||
| string jsonContent = File.ReadAllText(versionFile); | ||
| JObject versionJson = JObject.Parse(jsonContent); | ||
| JArray? publicReleaseRefSpec = versionJson["publicReleaseRefSpec"] as JArray; | ||
|
|
||
| Assert.NotNull(publicReleaseRefSpec); | ||
| Assert.Equal("^refs/heads/main$", publicReleaseRefSpec![0]!.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Install_CreatesVersionJsonWithDevelopBranch_WhenOnlyDevelopBranchExists() | ||
| { | ||
| // Arrange: Rename the default branch to develop | ||
| if (this.LibGit2Repository is object) | ||
| { | ||
| // First, make sure we have a commit, then rename | ||
| this.LibGit2Repository.Commit("Initial commit", this.Signer, this.Signer, new CommitOptions { AllowEmptyCommit = true }); | ||
| this.LibGit2Repository.Refs.Rename("refs/heads/master", "refs/heads/develop"); | ||
| this.LibGit2Repository.Refs.UpdateTarget("HEAD", "refs/heads/develop"); | ||
| } | ||
|
|
||
| // Act: Install version.json using default branch detection | ||
| var versionOptions = new VersionOptions | ||
| { | ||
| Version = SemanticVersion.Parse("1.0-beta"), | ||
| PublicReleaseRefSpec = this.DetectPublicReleaseRefSpecForTesting(), | ||
| }; | ||
|
|
||
| string versionFile = this.Context!.VersionFile.SetVersion(this.RepoPath, versionOptions); | ||
|
|
||
| // Assert: Verify the version.json contains the correct branch | ||
| string jsonContent = File.ReadAllText(versionFile); | ||
| JObject versionJson = JObject.Parse(jsonContent); | ||
| JArray? publicReleaseRefSpec = versionJson["publicReleaseRefSpec"] as JArray; | ||
|
|
||
| Assert.NotNull(publicReleaseRefSpec); | ||
| Assert.Equal("^refs/heads/develop$", publicReleaseRefSpec![0]!.ToString()); | ||
| } | ||
|
|
||
| protected override GitContext CreateGitContext(string path, string? committish = null) | ||
| { | ||
| return GitContext.Create(path, committish, engine: GitContext.Engine.ReadWrite); | ||
| } | ||
|
|
||
| private string[] DetectPublicReleaseRefSpecForTesting() | ||
| { | ||
| // This method replicates the logic from DetectDefaultBranch for testing | ||
| string defaultBranch = "master"; // Default fallback | ||
|
|
||
| if (this.Context is LibGit2Context libgit2Context) | ||
| { | ||
| LibGit2Sharp.Repository repository = libgit2Context.Repository; | ||
|
|
||
| // For testing, we'll use the simple logic of checking local branches | ||
| LibGit2Sharp.Branch[] localBranches = repository.Branches.Where(b => !b.IsRemote).ToArray(); | ||
| if (localBranches.Length == 1) | ||
| { | ||
| defaultBranch = localBranches[0].FriendlyName; | ||
| } | ||
| else | ||
| { | ||
| // Use the first local branch that exists from: master, main, develop | ||
| string[] commonBranchNames = { "master", "main", "develop" }; | ||
| foreach (string branchName in commonBranchNames) | ||
| { | ||
| if (localBranches.Any(b => b.FriendlyName == branchName)) | ||
| { | ||
| defaultBranch = branchName; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return new string[] | ||
| { | ||
| $"^refs/heads/{defaultBranch}$", | ||
| @"^refs/heads/v\d+(?:\.\d+)?$", | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
.gitdirectory manually)?There was a problem hiding this comment.
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.