diff --git a/buildSrc/src/main/kotlin/datadog/gradle/plugin/version/TracerVersionPlugin.kt b/buildSrc/src/main/kotlin/datadog/gradle/plugin/version/TracerVersionPlugin.kt index 5255cbf5328..4cd8669c389 100644 --- a/buildSrc/src/main/kotlin/datadog/gradle/plugin/version/TracerVersionPlugin.kt +++ b/buildSrc/src/main/kotlin/datadog/gradle/plugin/version/TracerVersionPlugin.kt @@ -48,9 +48,18 @@ class TracerVersionPlugin @Inject constructor( // Not a git repository extension.defaultVersion.get() } else { + val currentBranchProvider = gitCurrentBranchProvider(repoWorkingDirectory) providerFactory.zip( - gitDescribeProvider(extension, repoWorkingDirectory), - gitCurrentBranchProvider(repoWorkingDirectory) + currentBranchProvider.flatMap { currentBranch -> + // Use --first-parent only on release branches so they stay anchored to their own + // version line; feature branches pick up newer tags merged in from main. + gitDescribeProvider( + extension, + repoWorkingDirectory, + firstParent = currentBranch.startsWith("release/v") + ) + }, + currentBranchProvider ) { describeString, currentBranch -> toTracerVersion(describeString, extension) { when { @@ -87,7 +96,8 @@ class TracerVersionPlugin @Inject constructor( private fun gitDescribeProvider( extension: TracerVersionExtension, - repoWorkingDirectory: File + repoWorkingDirectory: File, + firstParent: Boolean, ) = providerFactory.of(GitCommandValueSource::class.java) { parameters { val tagPrefix = extension.tagVersionPrefix.get() @@ -96,10 +106,13 @@ class TracerVersionPlugin @Inject constructor( "describe", "--abbrev=8", "--tags", - "--first-parent", "--match=$tagPrefix[0-9].[0-9]*.[0-9]*", ) + if (firstParent) { + gitCommand.add("--first-parent") + } + if (extension.detectDirty.get()) { gitCommand.add("--dirty") } diff --git a/buildSrc/src/test/kotlin/datadog/gradle/plugin/version/TracerVersionIntegrationTest.kt b/buildSrc/src/test/kotlin/datadog/gradle/plugin/version/TracerVersionIntegrationTest.kt index aa25c46c757..eb909d71862 100644 --- a/buildSrc/src/test/kotlin/datadog/gradle/plugin/version/TracerVersionIntegrationTest.kt +++ b/buildSrc/src/test/kotlin/datadog/gradle/plugin/version/TracerVersionIntegrationTest.kt @@ -144,6 +144,50 @@ class TracerVersionIntegrationTest : VersionPluginsFixture() { ) } + @Test + fun `should increment minor from merged main version tag on feature branch`() { + assertTracerVersion( + expectedVersion = "1.53.0-SNAPSHOT", + beforeGradle = { + initGitRepo() + exec("git", "tag", "v1.50.0", "-m", "") + exec("git", "switch", "-c", "feature") + writeFile("feature.txt", "feature") + exec("git", "add", "feature.txt") + exec("git", "commit", "-m", "Feature commit") + exec("git", "switch", "main") + writeFile("main.txt", "main") + exec("git", "add", "main.txt") + exec("git", "commit", "-m", "Main commit") + exec("git", "tag", "v1.52.0", "-m", "") + exec("git", "switch", "feature") + exec("git", "merge", "main", "--no-edit") + }, + ) + } + + @Test + fun `should increment patch from first parent on release branch after main merge`() { + assertTracerVersion( + expectedVersion = "1.52.1-SNAPSHOT", + beforeGradle = { + initGitRepo() + exec("git", "tag", "v1.52.0", "-m", "") + exec("git", "switch", "-c", "release/v1.52.x") + writeFile("release.txt", "release") + exec("git", "add", "release.txt") + exec("git", "commit", "-m", "Release commit") + exec("git", "switch", "main") + writeFile("main.txt", "main") + exec("git", "add", "main.txt") + exec("git", "commit", "-m", "Main commit") + exec("git", "tag", "v1.53.0", "-m", "") + exec("git", "switch", "release/v1.52.x") + exec("git", "merge", "main", "--no-edit") + }, + ) + } + private fun assertTracerVersion( expectedVersion: String, workingDirectory: File = projectDir,