Skip to content

Commit ba289f3

Browse files
authored
Use full Git hash in generated version files (#12001)
fix: Use full Git hash in generated version files Git chooses the minimum unique abbreviation when `--short` is used. That abbreviation can have a different length when runners have different Git objects, even when both jobs build the same commit. This produces different version files and prevents reproducible JARs across jobs. Emit the full commit hash in the version file instead, for example: 1.65.0-SNAPSHOT~846103dfeb02cb7e162b0404af6d87a4882e2e91 Also, capture the project name during configuration so the task action does not access the Gradle project. https://git-scm.com/docs/git-rev-parse#Documentation/git-rev-parse.txt---shortltlengthgt --- The validate build scan reported for `:dd-trace-api:writeVersionNumberFile`: ``` The task was not up-to-date because of the following reasons: Value of input property 'gitHash' has changed for task ':dd-trace-api:writeVersionNumberFile' ``` chore: Keep a 10 char hex abbreviation The tracer version string is propagated today by a few components: * tracer * telemetry * remote config poller * crash-tracking In all cases the tracer version is processed as an "opaque" string and ends up as a tag value for the tracer. The hash in the version string packaged in the agent jar shows the abbreviation has been growing over the years 7, 8, 9, 10, (and probaly soon 11) chars over the repo's history. Given the hash surfaces in UI, let's truncate it to 10 chars. https://git-scm.com/docs/hash-function-transition.html Co-authored-by: brice.dutheil <brice.dutheil@datadoghq.com>
1 parent 01b5e2c commit ba289f3

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

buildSrc/src/main/kotlin/datadog/gradle/plugin/version/WriteVersionFile.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package datadog.gradle.plugin.version
22

3+
import org.checkerframework.checker.units.qual.g
34
import org.gradle.api.DefaultTask
45
import org.gradle.api.file.DirectoryProperty
56
import org.gradle.api.file.ProjectLayout
67
import org.gradle.api.model.ObjectFactory
78
import org.gradle.api.provider.Property
89
import org.gradle.api.provider.ProviderFactory
910
import org.gradle.api.tasks.Input
11+
import org.gradle.api.tasks.Internal
12+
import org.gradle.api.tasks.Optional
1013
import org.gradle.api.tasks.OutputDirectory
1114
import org.gradle.api.tasks.TaskAction
1215
import org.gradle.kotlin.dsl.property
@@ -27,20 +30,28 @@ abstract class WriteVersionFile @Inject constructor(
2730
.convention(
2831
providerFactory.of(GitCommandValueSource::class.java) {
2932
parameters {
30-
gitCommand.addAll("git", "rev-parse", "--short", "HEAD")
33+
gitCommand.addAll("git", "rev-parse", "HEAD")
3134
workingDirectory.set(layout.projectDirectory)
3235
}
3336
}
3437
)
3538

39+
@get:Input
40+
val gitHashTruncation: Property<Int> = objects.property<Int>()
41+
.convention(10)
42+
43+
@get:Input
44+
val projectName: Property<String> = objects.property<String>()
45+
.convention(project.name)
46+
3647
@get:OutputDirectory
3748
val outputDirectory: DirectoryProperty = objects.directoryProperty()
3849
.convention(layout.buildDirectory.dir("generated/version"))
3950

4051
@TaskAction
4152
fun writeVersionFile() {
42-
val versionFile = outputDirectory.file("${project.name}.version").get().asFile
53+
val versionFile = outputDirectory.file("${projectName.get()}.version").get().asFile
4354
versionFile.parentFile.mkdirs()
44-
versionFile.writeText("${version.get()}~${gitHash.get()}")
55+
versionFile.writeText("${version.get()}~${gitHash.get().take(gitHashTruncation.get())}")
4556
}
4657
}

buildSrc/src/test/kotlin/datadog/gradle/plugin/version/WriteVersionFilePluginTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ class WriteVersionFilePluginTest : VersionPluginsFixture() {
99

1010
@Test
1111
fun `writes version file in version~hash format`() {
12+
// task truncation convention is 10
1213
assertVersionFile(
13-
expectedContentRegex = "1\\.2\\.3~[0-9a-f]+",
14+
expectedContentRegex = "1\\.2\\.3~[0-9a-f]{10}",
1415
beforeGradle = {
1516
initGitRepo()
1617
},

0 commit comments

Comments
 (0)