Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static class AppVersion
| `SetPackageVersionFromBranch` | `true` | Automatically set NuGet package version from branch |
| `IncludeCommitIdMetadata` | `true` | Include commit ID as build metadata in versions |
| `GenerateVersionFile` | `false` | Generate `version.json` file during build |
| `SimpleBranchVersioning_Branch` | (auto-detected) | Override branch name (useful for CI) |

### Custom Class Name and Namespace

Expand All @@ -74,15 +75,20 @@ public static class AppVersion

### GitHub Actions

By default, GitHub Actions checks out PRs in detached HEAD state, which results in version `detached.<commit-id>`. To get the actual branch name, configure the checkout action:
By default, GitHub Actions checks out PRs in detached HEAD state, which results in version `detached.<commit-id>`. To get the actual branch name while still testing the merged result, use the `SimpleBranchVersioning_Branch` property:

```yaml
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }} # Checkout the actual branch, not the merge commit
- uses: actions/checkout@v4 # Default checkout tests the merge result

- name: Build
run: dotnet build -p:SimpleBranchVersioning_Branch=${{ github.head_ref }}
```

For push events on branches, the default checkout works correctly.
This approach:
- Tests the actual merge result (catches integration issues)
- Uses the correct branch name for versioning

For push events on branches, the default checkout works correctly without any override.

### Version File

Expand Down
16 changes: 13 additions & 3 deletions src/SimpleBranchVersioning/AppVersionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
provider.GlobalOptions.TryGetValue("build_property.IncludeCommitIdMetadata", out string? includeMetadataStr);
provider.GlobalOptions.TryGetValue("build_property.GenerateVersionFile", out string? generateVersionFileStr);
provider.GlobalOptions.TryGetValue("build_property.SetPackageVersionFromBranch", out string? setPackageVersionStr);
provider.GlobalOptions.TryGetValue("build_property.SimpleBranchVersioning_Branch", out string? overrideBranch);

// Parse IncludeCommitIdMetadata (default: true)
bool includeCommitIdMetadata = !string.Equals(includeMetadataStr, "false", StringComparison.OrdinalIgnoreCase);
Expand All @@ -90,7 +91,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
var (branch, commitId) = ParseGitInfo(gitInfo.HeadContent, gitInfo.GitDir)
?? ReadGitInfo(projectDir);

return new BuildProperties(rootNamespace, branch, commitId, includeCommitIdMetadata, generateVersionFile, setPackageVersionFromBranch);
return new BuildProperties(rootNamespace, branch, commitId, includeCommitIdMetadata, generateVersionFile, setPackageVersionFromBranch, overrideBranch);
});

// Look for AppVersionConfigAttribute in assembly attributes
Expand Down Expand Up @@ -120,7 +121,16 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
var ((buildProps, config), hasTopLevelStatements) = input;

string branch = string.IsNullOrEmpty(buildProps.Branch) ? "unknown" : buildProps.Branch!;
// Use override branch if provided, otherwise use detected branch
string branch;
if (!string.IsNullOrEmpty(buildProps.OverrideBranch))
{
branch = buildProps.OverrideBranch!;
}
else
{
branch = string.IsNullOrEmpty(buildProps.Branch) ? "unknown" : buildProps.Branch!;
}
string commitId = string.IsNullOrEmpty(buildProps.CommitId) ? "0000000" : buildProps.CommitId!;

// Calculate all version formats
Expand Down Expand Up @@ -404,6 +414,6 @@ private static (string? branch, string? commitId) ReadGitInfo(string? projectDir
}
#pragma warning restore RS1035

private sealed record BuildProperties(string? RootNamespace, string? Branch, string? CommitId, bool IncludeCommitIdMetadata, bool GenerateVersionFile, bool SetPackageVersionFromBranch);
private sealed record BuildProperties(string? RootNamespace, string? Branch, string? CommitId, bool IncludeCommitIdMetadata, bool GenerateVersionFile, bool SetPackageVersionFromBranch, string? OverrideBranch);
private sealed record AppVersionConfig(string? Namespace, string? ClassName);
}
2 changes: 2 additions & 0 deletions src/SimpleBranchVersioning/build/SimpleBranchVersioning.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<SetPackageVersionFromBranch Condition="'$(SetPackageVersionFromBranch)' == ''">true</SetPackageVersionFromBranch>
<!-- Default: include commit ID as build metadata in versions -->
<IncludeCommitIdMetadata Condition="'$(IncludeCommitIdMetadata)' == ''">true</IncludeCommitIdMetadata>
<!-- SimpleBranchVersioning_Branch: optional override for branch name (useful for CI detached HEAD) -->

<!-- Disable auto-generated assembly version attributes (source generator provides these) -->
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
Expand All @@ -18,6 +19,7 @@
<CompilerVisibleProperty Include="IncludeCommitIdMetadata" />
<CompilerVisibleProperty Include="GenerateVersionFile" />
<CompilerVisibleProperty Include="SetPackageVersionFromBranch" />
<CompilerVisibleProperty Include="SimpleBranchVersioning_Branch" />
</ItemGroup>

<!-- Find the .git directory by walking up from ProjectDir (using forward slashes for cross-platform) -->
Expand Down